File size: 2,734 Bytes
e38d825
828b751
e38d825
 
828b751
4257568
828b751
 
 
4257568
69b9967
 
 
 
 
1a06981
 
4257568
828b751
 
 
b613751
 
828b751
1a06981
 
 
2ed6184
 
 
4257568
dc32f0a
 
 
 
e419026
69b9967
 
 
14b51c3
a702acd
 
 
 
14b51c3
 
 
 
 
2ed6184
 
 
8ff9a48
4f938db
 
 
 
8ff9a48
dc32f0a
 
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
import gradio as gr
from transformers import BartForSequenceClassification, BartTokenizer


# model = pipeline("text-generation")

# following https://joeddav.github.io/blog/2020/05/29/ZSL.html
tokenizer_bart = BartTokenizer.from_pretrained('facebook/bart-large-mnli')
model_bart_sq = BartForSequenceClassification.from_pretrained('facebook/bart-large-mnli')

title = "Stance Detection using Zero Shot"

description1 = "Welcome to the side where the grass is greener."
description2 = "This is a simple tool which was created with an aim to stance towards a given entity in a sentence. However, this is not the only use case of it!"
description3 = "What did I do with it? Check out this [blog post](https://rachithaiyappa.github.io/science/Zero-Shot-for-Stance-Detection/) to see how it performs on some [SemEval](https://semeval.github.io/) tasks."



def zs(premise,hypothesis):
    input_ids = tokenizer_bart.encode(premise, hypothesis, return_tensors='pt')
    logits = model_bart_sq(input_ids)[0]
    # entail_contradiction_logits = logits[:,[0,1,2]]
    entail_contradiction_logits = logits[:,[0,2]]
    probs = entail_contradiction_logits.softmax(dim=1)
    contra_prob = round(probs[:,0].item(),4)
    # neut_prob = round(probs[:,1].item(),4)
    entail_prob = round(probs[:,1].item(),4)
    # return contra_prob, neut_prob, entail_prob
    return contra_prob, entail_prob


# gr.Interface(fn=zs, inputs=["text", "text"], outputs=["text","text","text"]).launch()


with gr.Blocks() as demo:
    gr.Markdown(f" # {title}")
    gr.Markdown(f" ## {description1}")
    gr.Markdown(f"{description2}")
    gr.Markdown(f"{description3}")
    with gr.Row():
        # premise = gr.Textbox(label="Premise",placeholder = "Roger Federer is an amazing tennis player.")
        # hypothesis = gr.Textbox(label="Hypothesis", placeholder = "The stance to Roger Federer is positive.")
        premise = gr.Textbox(label="Premise")
        hypothesis = gr.Textbox(label="Hypothesis")
    with gr.Row():
        greet_btn = gr.Button("Compute")
    with gr.Row():
        entailment = gr.Textbox(label="Entailment Probability")
        contradiction = gr.Textbox(label="Contradiction Probability")
        # neutral = gr.Textbox(label="Neutral Probability")
        # greet_btn.click(fn=zs, inputs=[premise,hypothesis], outputs=[contradiction,neutral,entailment])
        greet_btn.click(fn=zs, inputs=[premise,hypothesis], outputs=[contradiction,entailment])
    gr.Examples(
        fn = zs,
        examples = [["Roger Federer is an amazing tennis player.","The stance to Roger Federer is positive."], 
        ["NETPLACE is an awesome gathering,", "We should do this more often."]],
        inputs = [premise,hypothesis]
    )

demo.launch()