Spaces:
Runtime error
Runtime error
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"""The QANom Pipeline identifies deverbal nominalizations, parsed with question-answer driven semantic role labeling (QASRL) """ | |
title="Question Answer Deverbal Nominalization" | |
examples = [ | |
[models[1], "Physics is the lowest form of rationalization followed by chemistry which orders memory of the universe by bonds and selection. Everything else is but a recommendation. People can break the law, but nobody can break the laws of physics. ", 0.45], | |
[models[1], "To boil something down to most fundamental principles, things that we are most confident of are true at a foundational level. 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.65], | |
[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.58], | |
[models[1], "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, its 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", 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}
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() |