kleinay commited on
Commit
591d4b5
β€’
1 Parent(s): 056d3ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -9,13 +9,13 @@ description = f"""This is a demo of the QASem Parsing pipeline. It wraps models
9
  title="QASem Parsing Demo"
10
 
11
  all_layers = ["qasrl", "qanom", "qadiscourse"]
12
- examples = [["Both were shot in the confrontation with police and have been recovering in hospital since the attack .", all_layers, 0.75],
13
- ["the construction of the officer 's building was delayed by the lockdown and is expected to continue for at least 10 more months.", all_layers, 0.75],
14
- ["While President Obama expressed condolences regarding the death of Margaret Thatcher upon her death earlier this year , he did not issue an executive order that flags be lowered in her honor .", all_layers, 0.75],
15
- ["We made a very clear commitment : if there is any proposal in the next parliament for a transfer of powers to Brussels ( the EU ) we will have an in/out referendum .", all_layers, 0.75],
16
- ["The doctor asked about the progress in Luke 's treatment .", all_layers, 0.75],
17
- ["The Veterinary student was interested in Luke 's treatment of sea animals .", all_layers, 0.7],
18
- ["Some reviewers agreed that the criticism raised by the AC is mostly justified .", all_layers, 0.6]]
19
 
20
 
21
  input_sent_box_label = "Insert sentence here, or select from the examples below"
@@ -24,9 +24,12 @@ links = """<p style='text-align: center'>
24
  </p>"""
25
 
26
 
27
- def call(sentence, layers, detection_threshold):
28
-
29
- outputs = pipeline([sentence], nominalization_detection_threshold=detection_threshold)[0]
 
 
 
30
  def pretty_qadisc_qas(qa_infos) -> List[str]:
31
  if not qa_infos: return []
32
  return ["- " + f"{qa['question']} --- {qa['answer']}".lstrip()
@@ -61,15 +64,24 @@ def call(sentence, layers, detection_threshold):
61
  def word_span(word, idx):
62
  return f'<span style="background-color: {color(idx)}">{word}</span>'
63
  html = '<span>' + ' '.join(word_span(word, idx) for idx, word in enumerate(sentence.split(" "))) + '</span>'
64
- return html, pretty_qa_output , outputs
 
 
 
 
 
 
 
65
 
66
  iface = gr.Interface(fn=call,
67
- inputs=[gr.inputs.Textbox(placeholder=input_sent_box_label, label="Sentence", lines=4),
68
- gr.inputs.CheckboxGroup(all_layers, default=all_layers, label="Annotation Layers"),
69
- gr.inputs.Slider(minimum=0., maximum=1., step=0.01, default=0.75, label="Nominalization Detection Threshold")],
70
- outputs=[gr.outputs.HTML(label="Detected Predicates"),
71
- gr.outputs.Textbox(label="Generated QAs"),
72
- gr.outputs.JSON(label="Raw QASemEndToEndPipeline Output")],
 
 
73
  title=title,
74
  description=description,
75
  article=links,
 
9
  title="QASem Parsing Demo"
10
 
11
  all_layers = ["qasrl", "qanom", "qadiscourse"]
12
+ examples = [["Both were shot in the confrontation with police and have been recovering in hospital since the attack .", all_layers, False, 0.75],
13
+ ["the construction of the officer 's building was delayed by the lockdown and is expected to continue for at least 10 more months.", all_layers, False, 0.75],
14
+ ["While President Obama expressed condolences regarding the death of Margaret Thatcher upon her death earlier this year , he did not issue an executive order that flags be lowered in her honor .", all_layers, False, 0.75],
15
+ ["We made a very clear commitment : if there is any proposal in the next parliament for a transfer of powers to Brussels ( the EU ) we will have an in/out referendum .", all_layers, False, 0.75],
16
+ ["The doctor asked about the progress in Luke 's treatment .", all_layers, False, 0.75],
17
+ ["The Veterinary student was interested in Luke 's treatment of sea animals .", all_layers, False, 0.7],
18
+ ["Some reviewers agreed that the criticism raised by the AC is mostly justified .", all_layers, False, 0.6]]
19
 
20
 
21
  input_sent_box_label = "Insert sentence here, or select from the examples below"
 
24
  </p>"""
25
 
26
 
27
+ def call(sentence, layers, show_openie: bool, detection_threshold: float):
28
+ outputs = pipeline([sentence], nominalization_detection_threshold=detection_threshold, output_openie=show_openie)
29
+ if show_openie:
30
+ openie_outputs = outputs["openie"][0] # list of OpenIE tuples
31
+ outputs = outputs["qasem"]
32
+ outputs = outputs[0] # only one sentence in input batch
33
  def pretty_qadisc_qas(qa_infos) -> List[str]:
34
  if not qa_infos: return []
35
  return ["- " + f"{qa['question']} --- {qa['answer']}".lstrip()
 
64
  def word_span(word, idx):
65
  return f'<span style="background-color: {color(idx)}">{word}</span>'
66
  html = '<span>' + ' '.join(word_span(word, idx) for idx, word in enumerate(sentence.split(" "))) + '</span>'
67
+ # show openie_outputs
68
+ if show_openie:
69
+ repr_oie = lambda tup: f"({','.join(e for e in tup)})"
70
+ openie_html = '<span><b>Open Information Extraction:</b><br>' + '<br>'.join([repr_oie(tup) for tup in openie_outputs]) + '</span>'
71
+ else:
72
+ openie_html = ''
73
+
74
+ return html, pretty_qa_output, openie_html, outputs
75
 
76
  iface = gr.Interface(fn=call,
77
+ inputs=[gr.components.Textbox(placeholder=input_sent_box_label, label="Sentence", lines=4),
78
+ gr.components.CheckboxGroup(all_layers, value=all_layers, label="Annotation Layers"),
79
+ gr.components.Checkbox(value=False, label="Show OpenIE format (converted from verbal QASRL only)"),
80
+ gr.components.Slider(minimum=0., maximum=1., step=0.01, value=0.75, label="Nominalization Detection Threshold")],
81
+ outputs=[gr.components.HTML(label="Detected Predicates"),
82
+ gr.components.Textbox(label="Generated QAs"),
83
+ gr.components.HTML(label="OpenIE Output"),
84
+ gr.components.JSON(label="Raw QASemEndToEndPipeline Output")],
85
  title=title,
86
  description=description,
87
  article=links,