kleinay commited on
Commit
e8b3788
β€’
1 Parent(s): 724e5b2

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,
62
+ examples=examples)
63
+ iface.launch()