QandAGenerator / app.py
awacke1's picture
Update app.py
c0f068b
raw
history blame
4.64 kB
import gradio as gr
import nltk
from qanom.qanom_end_to_end_pipeline import QANomEndToEndPipeline
from typing import List
models = ["kleinay/qanom-seq2seq-model-baseline",
"kleinay/qanom-seq2seq-model-joint"]
pipelines = {model: QANomEndToEndPipeline(model) for model in models}
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) """
title="QANom End-to-End Pipeline Demo"
examples = [[models[1], "Physics is low and everything else was a recommendation. I've met a lot of people that can break the law,
but I have never met anyone who could break physics.
", 0.7],
[models[1], "It's really just saying, let's boil something down
to the most fundamental principles, the things that we are most confident are true
at a foundational level, and that sets your axiomatic base, and then you reason up from there.
And then you cross check your conclusion against the axiomatic truth.
Some basics in physics would be like are violating conservation of energy or momentum or something like that, then it's not gonna work.", 0.75],
[models[0], "So first for any kind of technology problem you have to sort of just make sure you're not violating physics.
First principles analysis, I think, is something that can be applied to really any walk of life,
anything really.", 0.75],
[models[1], "or technology, whatever it might be, and say, "What is this? What is the perfect arrangement of atoms
that would be the best possible product? And now let us try to figure out how to get the atoms in that shape."
- I mean, it sounds, it's almost like Rick and Morty absurd
until you start to really think about it. And you really should think about it in this way
'cause everything else is kind of, if you think you might fall victim to the momentum
of the way things are done in the past, unless you think in this way. - Well, just as a function of inertia, people will want to use the same tools and methods", 0.5]]
input_sent_box_label = "Insert sentence here, or select from the examples below"
links = """<p style='text-align: center'>
<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>
</p>"""
def call(model_name, sentence, detection_threshold):
pipeline = pipelines[model_name]
pred_infos = pipeline([sentence], detection_threshold=detection_threshold)[0]
def pretty_qas(pred_info) -> List[str]:
if not pred_info or not pred_info['QAs']: return []
return [f"{qa['question']} --- {';'.join(qa['answers'])}"
for qa in pred_info['QAs'] if qa is not None]
all_qas = [qa for pred_info in pred_infos for qa in pretty_qas(pred_info)]
if not pred_infos:
pretty_qa_output = "NO NOMINALIZATION FOUND"
elif not all_qas:
pretty_qa_output = "NO QA GENERATED"
else:
pretty_qa_output = "\n".join(all_qas)
# also present highlighted predicates
positives = [pred_info['predicate_idx'] for pred_info in pred_infos]
def color(idx):
if idx in positives: return "lightgreen"
idx2verb = {d["predicate_idx"] : d["verb_form"] for d in pred_infos}
idx2prob = {d["predicate_idx"] : d["predicate_detector_probability"] for d in pred_infos}
def word_span(word, idx):
tooltip = f'title=" probability={idx2prob[idx]:.2}&#010;verb={idx2verb[idx]}"' if idx in idx2verb else ''
return f'<span {tooltip} style="background-color: {color(idx)}">{word}</span>'
html = '<span>' + ' '.join(word_span(word, idx) for idx, word in enumerate(sentence.split(" "))) + '</span>'
return html, pretty_qa_output , pred_infos
iface = gr.Interface(fn=call,
inputs=[gr.inputs.Radio(choices=models, default=models[0], label="Model"),
gr.inputs.Textbox(placeholder=input_sent_box_label, label="Sentence", lines=4),
gr.inputs.Slider(minimum=0., maximum=1., step=0.01, default=0.5, label="Nominalization Detection Threshold")],
outputs=[gr.outputs.HTML(label="Detected Nominalizations"),
gr.outputs.Textbox(label="Generated QAs"),
gr.outputs.JSON(label="Raw Model Output")],
title=title,
description=description,
article=links,
examples=examples)
iface.launch()