kleinay commited on
Commit
69b3eab
·
1 Parent(s): 580ec29

integrate html from nominalization detector demo

Browse files
Files changed (1) hide show
  1. app.py +15 -3
app.py CHANGED
@@ -27,14 +27,26 @@ def call(model_name, sentence, detection_threshold):
27
  def pretty_pred_output(pred_info) -> str:
28
  return "\n".join([f"{qa['question']} --- {';'.join(qa['answers'])}"
29
  for qa in pred_info['QAs']])
30
- pretty_output = "\n".join(pretty_pred_output(pred_info) for pred_info in pipe_out_pred_infos)
31
- return pretty_output, pipe_out_pred_infos
 
 
 
 
 
 
 
 
 
 
32
 
33
  iface = gr.Interface(fn=call,
34
  inputs=[gr.inputs.Radio(choices=models, default=models[0], label="Model"),
35
  gr.inputs.Textbox(placeholder=input_sent_box_label, label="Sentence", lines=4),
36
  gr.inputs.Slider(minimum=0., maximum=1., step=0.01, default=0.5, label="Nominalization Detection Threshold")],
37
- outputs=[gr.outputs.Textbox(label="Model Output"), gr.outputs.JSON(label="Model Output - JSON")],
 
 
38
  title=title,
39
  description=description,
40
  article=links,
 
27
  def pretty_pred_output(pred_info) -> str:
28
  return "\n".join([f"{qa['question']} --- {';'.join(qa['answers'])}"
29
  for qa in pred_info['QAs']])
30
+ pretty_qa_output = "\n".join(pretty_pred_output(pred_info) for pred_info in pipe_out_pred_infos)
31
+ # also present highlighted predicates
32
+ positives = [pred_info['predicate_idx'] for pred_info in pred_infos]
33
+ def color(idx):
34
+ if idx in positives: return "lightgreen"
35
+ idx2verb = {d["predicate_idx"] : d["verb_form"] for d in pred_infos}
36
+ idx2prob = {d["predicate_idx"] : d["predicate_detector_probability"] for d in pred_infos}
37
+ def word_span(word, idx):
38
+ tooltip = f'title=" probability={idx2prob[idx]:.2}
verb={idx2verb[idx]}"' if idx in idx2verb else ''
39
+ return f'<span {tooltip} style="background-color: {color(idx)}">{word}</span>'
40
+ html = '<span>' + ' '.join(word_span(word, idx) for idx, word in enumerate(sentence.split(" "))) + '</span>'
41
+ return html, pretty_qa_output , pipe_out_pred_infos
42
 
43
  iface = gr.Interface(fn=call,
44
  inputs=[gr.inputs.Radio(choices=models, default=models[0], label="Model"),
45
  gr.inputs.Textbox(placeholder=input_sent_box_label, label="Sentence", lines=4),
46
  gr.inputs.Slider(minimum=0., maximum=1., step=0.01, default=0.5, label="Nominalization Detection Threshold")],
47
+ outputs=[gr.outputs.HTML(label="Detected Nominalizations"),
48
+ gr.outputs.Textbox(label="Generated QAs"),
49
+ gr.outputs.JSON(label="Raw Model Output")],
50
  title=title,
51
  description=description,
52
  article=links,