Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
-
import torch
|
2 |
import gradio as gr
|
3 |
-
from transformers import AutoTokenizer,
|
4 |
-
import
|
5 |
|
6 |
-
|
7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
8 |
-
model =
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
model.to(device)
|
11 |
|
@@ -15,39 +14,22 @@ context = (
|
|
15 |
"расположенный в городе Иннополис, Татарстан."
|
16 |
)
|
17 |
|
18 |
-
def
|
19 |
-
# Убираем prompt из начала, если остался
|
20 |
-
answer = answer[len(prompt):].strip() if answer.lower().startswith(prompt.lower()) else answer.strip()
|
21 |
-
# Оставляем только кириллицу, пробелы и знаки препинания
|
22 |
-
answer = re.sub(r"[^а-яА-ЯёЁ ,.\-:;?!]", "", answer)
|
23 |
-
# Дополнительно можно убрать повторяющиеся символы
|
24 |
-
answer = re.sub(r"(.)\1{2,}", r"\1", answer)
|
25 |
-
return answer
|
26 |
-
|
27 |
-
def respond(message, history=None):
|
28 |
if history is None:
|
29 |
history = []
|
30 |
|
31 |
-
|
32 |
-
"Используя следующий контекст, ответь на вопрос четко и кратко.\n"
|
33 |
-
f"Контекст: {context}\n"
|
34 |
-
f"Вопрос: {message}\n"
|
35 |
-
"Ответ:"
|
36 |
-
)
|
37 |
-
|
38 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
39 |
-
|
40 |
with torch.no_grad():
|
41 |
-
outputs = model
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
answer =
|
49 |
|
50 |
-
history.append((
|
51 |
return history
|
52 |
|
53 |
iface = gr.ChatInterface(fn=respond, title="Innopolis Q&A")
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
3 |
+
import torch
|
4 |
|
5 |
+
model_name = "deepset/roberta-base-squad2"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
model.to(device)
|
10 |
|
|
|
14 |
"расположенный в городе Иннополис, Татарстан."
|
15 |
)
|
16 |
|
17 |
+
def respond(question, history=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
if history is None:
|
19 |
history = []
|
20 |
|
21 |
+
inputs = tokenizer.encode_plus(question, context, return_tensors="pt").to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
with torch.no_grad():
|
23 |
+
outputs = model(**inputs)
|
24 |
+
start_scores = outputs.start_logits
|
25 |
+
end_scores = outputs.end_logits
|
26 |
+
|
27 |
+
start = torch.argmax(start_scores)
|
28 |
+
end = torch.argmax(end_scores) + 1
|
29 |
+
answer_tokens = inputs['input_ids'][0][start:end]
|
30 |
+
answer = tokenizer.decode(answer_tokens, skip_special_tokens=True)
|
31 |
|
32 |
+
history.append((question, answer))
|
33 |
return history
|
34 |
|
35 |
iface = gr.ChatInterface(fn=respond, title="Innopolis Q&A")
|