File size: 3,690 Bytes
e8b3788
549392b
e8b3788
0e9979f
900e333
e8b3788
 
900e333
 
 
 
 
 
 
e8b3788
 
 
 
900e333
e8b3788
 
 
900e333
e8b3788
900e333
1637d4b
 
900e333
1637d4b
900e333
e8b3788
 
 
1637d4b
 
900e333
 
 
e8b3788
 
 
900e333
e8b3788
900e333
 
e8b3788
d3a7a20
 
e8b3788
900e333
e8b3788
900e333
e8b3788
 
900e333
e8b3788
900e333
e8b3788
900e333
e8b3788
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
from typing import List

from qasem.end_to_end_pipeline import QASemEndToEndPipeline
pipeline = QASemEndToEndPipeline()


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.""" 
title="QASem Parsing Demo"
examples = [["the construction of the officer 's building finished right after the beginning of the destruction of the previous construction .", 0.7],
            ["The doctor asked about the progress in Luke 's treatment .", 0.7],
            ["The Veterinary student was interested in Luke 's treatment of sea animals .", 0.7],
            ["Both were shot in the confrontation with police and have been recovering in hospital since the attack .", 0.7],
            ["Some reviewers agreed that the criticism raised by the AC is mostly justified .", 0.5]]
            

input_sent_box_label = "Insert sentence here, or select from the examples below"
links = """<p style='text-align: center'>
<a href='https://github.com/kleinay/QASem' target='_blank'>Github Repo</a> | <a href='https://arxiv.org/abs/2205.11413' target='_blank'>Paper</a>
</p>"""


def call(sentence, detection_threshold):

    outputs = pipeline([sentence], nominalization_detection_threshold=detection_threshold)[0]
    def pretty_qadisc_qas(qa_infos) -> List[str]:
        if not qa_infos: return []
        return [f"{qa['question']} --- {qa['answer']}"
                for qa in qa_infos if qa is not None]
    def pretty_qasrl_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]
    qasrl_qas = [qa for pred_info in outputs['qasrl'] for qa in pretty_qasrl_qas(pred_info)]
    qanom_qas = [qa for pred_info in outputs['qanom'] for qa in pretty_qasrl_qas(pred_info)]
    qadisc_qas= pretty_qadisc_qas(outputs['qadiscourse'])
    all_qas = ['QASRL:'] + qasrl_qas + ['\nQANom:'] + qanom_qas + ['\nQADiscourse:'] + qadisc_qas                      
    if not qasrl_qas + qanom_qas + qadisc_qas:
        pretty_qa_output = "NO QA GENERATED"
    else:
        pretty_qa_output = "\n".join(all_qas) 
    
    # also present highlighted predicates
    qasrl_predicates = [pred_info['predicate_idx'] for pred_info in outputs['qasrl']] 
    qanom_predicates = [pred_info['predicate_idx'] for pred_info in outputs['qanom']] 
    def color(idx):
        if idx in qasrl_predicates : return "aquamarine"
        if idx in qanom_predicates : return "aqua"
    def word_span(word, idx):
        return f'<span 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 , outputs

iface = gr.Interface(fn=call, 
                     inputs=[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 Predicates"),
                              gr.outputs.Textbox(label="Generated QAs"), 
                              gr.outputs.JSON(label="Raw QASemEndToEndPipeline Output")],
                     title=title,
                     description=description,
                     article=links,
                     examples=examples)
iface.launch()