File size: 1,842 Bytes
6841f61
a7a36a7
84e0e4d
a7a36a7
 
 
 
 
 
4ffcee4
8b8a073
a7a36a7
 
 
 
 
 
4ffcee4
 
a7a36a7
 
4ffcee4
a7a36a7
 
 
 
45172d4
 
 
3a82778
45172d4
a7a36a7
 
 
6841f61
 
 
4ffcee4
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
import gradio as gr
import torch
from transformers import AutoModelForSequenceClassification, DebertaV2Tokenizer

CLASSES = {
    'yes': 0,
    'irrelevant': 1,
    'no': 2,
}
tokenizer = DebertaV2Tokenizer.from_pretrained('cross-encoder/nli-deberta-v3-base', do_lower_case=True)
model = AutoModelForSequenceClassification.from_pretrained('MrPio/TheSeagullStory-nli-deberta-v3-base')
model.eval()
story = open('story.txt').read().replace("\n\n", "\n").replace("\n", " ").strip()


def ask(question):
    inputs = tokenizer(story, question, truncation=True, padding=True)
    prediction = torch.softmax(model(**inputs).logits, 1).squeeze()
    return {c: round(prediction[i].item(), 3) for c, i in CLASSES}


gradio = gr.Interface(
    ask,
    inputs=[gr.Textbox(value="", label="Your question, as an affirmative sentence:")],
    outputs=[gr.Label(label="Answer", num_top_classes=3)],
    title="The Seagull Story",
    description="“ Albert and Dave find themselves on the pier. They go to a nearby restaurant where Albert orders "
                "seagull meat. The waiter promptly serves Albert the meal. After taking a bite, he realizes "
                "something. Albert pulls a gun out of his ruined jacket and shoots himself. ”\n\nWhy did Albert shoot "
                "himself?\n\nCan you unravel the truth behind this epilogue by asking only yes/no questions?\n\nPlease be specific about the time period you have in mind with your question.",
    article='Please refrain from embarrassing DeBERTa with dumb questions.\n\nCheck the repository for more detail: https://github.com/MrPio/The-Seagull-Story',
    examples=['Albert shoot himself for a reason',
              'Dave has a watch on his wrist',
              'Albert and Dave came to the pier on their own']
)

if __name__ == "__main__":
    gradio.launch(share=True)