kleinay commited on
Commit
900e333
β€’
1 Parent(s): e8b3788

modify to qasem pipeline

Browse files
Files changed (1) hide show
  1. app.py +32 -32
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
- models = ["kleinay/qanom-seq2seq-model-baseline",
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 full QANom Pipeline - identifying deverbal nominalizations and parsing them with question-answer driven semantic role labeling (QASRL) """
12
- title="QANom End-to-End Pipeline Demo"
13
- examples = [[models[1], "the construction of the officer 's building finished right after the beginning of the destruction of the previous construction .", 0.7],
14
- [models[1], "The doctor asked about the progress in Luke 's treatment .", 0.75],
15
- [models[0], "The Veterinary student was interested in Luke 's treatment of sea animals .", 0.75],
16
- [models[1], "Some reviewers agreed that the criticism raised by the AC is mostly justified .", 0.5]]
 
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://www.qasrl.org' target='_blank'>QASRL Website</a> | <a href='https://huggingface.co/kleinay/qanom-seq2seq-model-baseline' target='_blank'>Model Repo at Huggingface Hub</a>
22
  </p>"""
23
 
24
 
25
- def call(model_name, sentence, detection_threshold):
26
 
27
- pipeline = pipelines[model_name]
28
- pred_infos = pipeline([sentence], detection_threshold=detection_threshold)[0]
29
- def pretty_qas(pred_info) -> List[str]:
 
 
 
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
- all_qas = [qa for pred_info in pred_infos for qa in pretty_qas(pred_info)]
34
- if not pred_infos:
35
- pretty_qa_output = "NO NOMINALIZATION FOUND"
36
- elif not all_qas:
 
 
37
  pretty_qa_output = "NO QA GENERATED"
38
  else:
39
  pretty_qa_output = "\n".join(all_qas)
 
40
  # also present highlighted predicates
41
- positives = [pred_info['predicate_idx'] for pred_info in pred_infos]
 
42
  def color(idx):
43
- if idx in positives: return "lightgreen"
44
- idx2verb = {d["predicate_idx"] : d["verb_form"] for d in pred_infos}
45
- idx2prob = {d["predicate_idx"] : d["predicate_detector_probability"] for d in pred_infos}
46
  def word_span(word, idx):
47
- tooltip = f'title=" probability={idx2prob[idx]:.2}&#010;verb={idx2verb[idx]}"' if idx in idx2verb else ''
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 , pred_infos
51
 
52
  iface = gr.Interface(fn=call,
53
- inputs=[gr.inputs.Radio(choices=models, default=models[0], label="Model"),
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 Nominalizations"),
57
  gr.outputs.Textbox(label="Generated QAs"),
58
- gr.outputs.JSON(label="Raw Model Output")],
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,