File size: 1,240 Bytes
1510112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267f551
 
1510112
267f551
 
1510112
 
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
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
import gradio as gr
import torch

model = AutoModelForQuestionAnswering.from_pretrained("CUTD/qnAr")
tokenizer = AutoTokenizer.from_pretrained("CUTD/qnAr")

def generate_answer(question, context):
    inputs = tokenizer.encode_plus(question, context, add_special_tokens=True, return_tensors="pt")
    input_ids = inputs["input_ids"].tolist()[0]

    outputs = model(**inputs)
    answer_start_scores = outputs.start_logits
    answer_end_scores = outputs.end_logits

    answer_start = torch.argmax(answer_start_scores)
    answer_end = torch.argmax(answer_end_scores) + 1

    answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
    return answer

iface = gr.Interface(fn=generate_answer,
                     inputs=[gr.Textbox(lines=2, placeholder="Enter Question Here..."),
                             gr.Textbox(lines=5, placeholder="Enter Context Here...")],
                     outputs=gr.Textbox(),
                     title="Question Answering System",
                     description="Type in your question and context, and the system will provide you with an answer.")

iface.launch()