File size: 1,315 Bytes
5a42269
2e03cda
 
e474e6b
2e03cda
 
 
9ba4eae
 
e474e6b
af5c917
 
 
 
 
 
2e03cda
12dd231
 
 
2e03cda
9ba4eae
2e03cda
 
 
 
 
 
 
 
12dd231
2e03cda
d950da6
e474e6b
9ba4eae
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch

model_name = "deepset/roberta-base-squad2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

context = (
    "Университет Иннополис был основан в 2012 году. "
    "Это современный вуз в России, специализирующийся на IT и робототехнике, "
    "расположенный в городе Иннополис, Татарстан."
)

def respond(question, history=None):
    if history is None:
        history = []

    inputs = tokenizer.encode_plus(question, context, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = model(**inputs)
    start_scores = outputs.start_logits
    end_scores = outputs.end_logits

    start = torch.argmax(start_scores)
    end = torch.argmax(end_scores) + 1
    answer_tokens = inputs['input_ids'][0][start:end]
    answer = tokenizer.decode(answer_tokens, skip_special_tokens=True)

    history.append((question, answer))
    return history

iface = gr.ChatInterface(fn=respond, title="Innopolis Q&A")
iface.launch()