Spaces:
Runtime error
Runtime error
modify to qasem pipeline
Browse files
app.py
CHANGED
@@ -1,61 +1,61 @@
|
|
1 |
import gradio as gr
|
2 |
-
import nltk
|
3 |
-
from qanom.qanom_end_to_end_pipeline import QANomEndToEndPipeline
|
4 |
-
from typing import List
|
5 |
|
6 |
-
|
7 |
-
"kleinay/qanom-seq2seq-model-joint"]
|
8 |
-
pipelines = {model: QANomEndToEndPipeline(model) for model in models}
|
9 |
|
10 |
|
11 |
-
description = f"""This is a demo of the
|
12 |
-
title="
|
13 |
-
examples = [[
|
14 |
-
[
|
15 |
-
[
|
16 |
-
[
|
|
|
17 |
|
18 |
|
19 |
input_sent_box_label = "Insert sentence here, or select from the examples below"
|
20 |
links = """<p style='text-align: center'>
|
21 |
-
<a href='https://
|
22 |
</p>"""
|
23 |
|
24 |
|
25 |
-
def call(
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
if not pred_info or not pred_info['QAs']: return []
|
31 |
return [f"{qa['question']} --- {';'.join(qa['answers'])}"
|
32 |
for qa in pred_info['QAs'] if qa is not None]
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
37 |
pretty_qa_output = "NO QA GENERATED"
|
38 |
else:
|
39 |
pretty_qa_output = "\n".join(all_qas)
|
|
|
40 |
# also present highlighted predicates
|
41 |
-
|
|
|
42 |
def color(idx):
|
43 |
-
if idx in
|
44 |
-
|
45 |
-
idx2prob = {d["predicate_idx"] : d["predicate_detector_probability"] for d in pred_infos}
|
46 |
def word_span(word, idx):
|
47 |
-
|
48 |
-
return f'<span {tooltip} style="background-color: {color(idx)}">{word}</span>'
|
49 |
html = '<span>' + ' '.join(word_span(word, idx) for idx, word in enumerate(sentence.split(" "))) + '</span>'
|
50 |
-
return html, pretty_qa_output ,
|
51 |
|
52 |
iface = gr.Interface(fn=call,
|
53 |
-
inputs=[gr.inputs.
|
54 |
-
gr.inputs.Textbox(placeholder=input_sent_box_label, label="Sentence", lines=4),
|
55 |
gr.inputs.Slider(minimum=0., maximum=1., step=0.01, default=0.5, label="Nominalization Detection Threshold")],
|
56 |
-
outputs=[gr.outputs.HTML(label="Detected
|
57 |
gr.outputs.Textbox(label="Generated QAs"),
|
58 |
-
gr.outputs.JSON(label="Raw
|
59 |
title=title,
|
60 |
description=description,
|
61 |
article=links,
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
+
pipeline = QASemEndToEndPipeline()
|
|
|
|
|
4 |
|
5 |
|
6 |
+
description = f"""This is a demo of the QASem Parsing pipeline. It wraps models of three QA-based semantic tasks, composing a comprehensive semi-structured representation of sentence meaning - covering verbal and nominal semantic role labeling together with discourse relations."""
|
7 |
+
title="QASem Parsing Demo"
|
8 |
+
examples = [["the construction of the officer 's building finished right after the beginning of the destruction of the previous construction .", 0.7],
|
9 |
+
["The doctor asked about the progress in Luke 's treatment .", 0.7],
|
10 |
+
["The Veterinary student was interested in Luke 's treatment of sea animals .", 0.7],
|
11 |
+
["Both were shot in the confrontation with police and have been recovering in hospital since the attack .", 0.7],
|
12 |
+
["Some reviewers agreed that the criticism raised by the AC is mostly justified .", 0.5]]
|
13 |
|
14 |
|
15 |
input_sent_box_label = "Insert sentence here, or select from the examples below"
|
16 |
links = """<p style='text-align: center'>
|
17 |
+
<a href='https://github.com/kleinay/QASem' target='_blank'>Github Repo</a> | <a href='https://arxiv.org/abs/2205.11413' target='_blank'>Paper</a>
|
18 |
</p>"""
|
19 |
|
20 |
|
21 |
+
def call(sentence, detection_threshold):
|
22 |
|
23 |
+
outputs = pipeline([sentence], nominalization_detection_threshold=detection_threshold)[0]
|
24 |
+
def pretty_qadisc_qas(pred_info) -> List[str]:
|
25 |
+
if not pred_info: return []
|
26 |
+
return [f"{qa['question']} --- {qa['answer']}"
|
27 |
+
for qa in pred_info if qa is not None]
|
28 |
+
def pretty_qasrl_qas(pred_info) -> List[str]:
|
29 |
if not pred_info or not pred_info['QAs']: return []
|
30 |
return [f"{qa['question']} --- {';'.join(qa['answers'])}"
|
31 |
for qa in pred_info['QAs'] if qa is not None]
|
32 |
+
|
33 |
+
qasrl_qas = pretty_qasrl_qas(outputs['qasrl'])
|
34 |
+
qanom_qas = pretty_qasrl_qas(outputs['qanom'])
|
35 |
+
qadisc_qas= pretty_qadisc_qas(outputs['qadiscourse'])
|
36 |
+
all_qas = ['QASRL:'] + qasrl_qas + ['\nQANom:'] + qanom_qas + ['\nQADiscourse:'] + qadisc_qas
|
37 |
+
if not qasrl_qas + qanom_qas + qadisc_qas:
|
38 |
pretty_qa_output = "NO QA GENERATED"
|
39 |
else:
|
40 |
pretty_qa_output = "\n".join(all_qas)
|
41 |
+
|
42 |
# also present highlighted predicates
|
43 |
+
qasrl_predicates = [pred_info['predicate_idx'] for pred_info in outputs['qasrl']]
|
44 |
+
qanom_predicates = [pred_info['predicate_idx'] for pred_info in outputs['qanom']]
|
45 |
def color(idx):
|
46 |
+
if idx in qasrl_predicates : return "purple"
|
47 |
+
if idx in qanom_predicates : return "blue"
|
|
|
48 |
def word_span(word, idx):
|
49 |
+
return f'<span style="background-color: {color(idx)}">{word}</span>'
|
|
|
50 |
html = '<span>' + ' '.join(word_span(word, idx) for idx, word in enumerate(sentence.split(" "))) + '</span>'
|
51 |
+
return html, pretty_qa_output , outputs
|
52 |
|
53 |
iface = gr.Interface(fn=call,
|
54 |
+
inputs=[gr.inputs.Textbox(placeholder=input_sent_box_label, label="Sentence", lines=4),
|
|
|
55 |
gr.inputs.Slider(minimum=0., maximum=1., step=0.01, default=0.5, label="Nominalization Detection Threshold")],
|
56 |
+
outputs=[gr.outputs.HTML(label="Detected Predicates"),
|
57 |
gr.outputs.Textbox(label="Generated QAs"),
|
58 |
+
gr.outputs.JSON(label="Raw QASemEndToEndPipeline Output")],
|
59 |
title=title,
|
60 |
description=description,
|
61 |
article=links,
|