Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,68 @@
|
|
1 |
import gradio as gr
|
2 |
from main import tokenizer, model, device
|
3 |
import torch
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
input_ids = inputs['input_ids'].to(device)
|
8 |
-
attention_mask = inputs['attention_mask'].to(device)
|
9 |
-
batch = {
|
10 |
-
"input_ids": input_ids,
|
11 |
-
"attention_mask": attention_mask
|
12 |
-
}
|
13 |
-
start_logits, end_logits, loss = model(batch)
|
14 |
|
15 |
-
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def answer_question(context, question):
|
22 |
result = qa_pipeline(context, question)
|
23 |
return result
|
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 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from main import tokenizer, model, device
|
3 |
import torch
|
4 |
+
import pandas as pd
|
5 |
|
6 |
+
# Загружаем данные из CSV файла
|
7 |
+
df = pd.read_csv("train.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
def get_random_row():
|
10 |
+
random_row = df.sample(n=1)
|
11 |
+
return random_row.iloc[0]
|
12 |
|
13 |
+
def qa_pipeline(text, question):
|
14 |
+
# Подготовка входных данных для модели
|
15 |
+
inputs = tokenizer(question, text, return_tensors="pt")
|
16 |
+
input_ids = inputs['input_ids'].to(device)
|
17 |
+
attention_mask = inputs['attention_mask'].to(device)
|
18 |
+
batch = {
|
19 |
+
"input_ids": input_ids,
|
20 |
+
"attention_mask": attention_mask
|
21 |
+
}
|
22 |
+
|
23 |
+
# Выполнение предсказания
|
24 |
+
with torch.no_grad():
|
25 |
+
outputs = model(**batch)
|
26 |
+
|
27 |
+
# Извлечение логитов начала и конца ответа
|
28 |
+
start_logits = outputs.start_logits
|
29 |
+
end_logits = outputs.end_logits
|
30 |
+
|
31 |
+
# Нахождение индексов начала и конца ответа
|
32 |
+
start_index = torch.argmax(start_logits, dim=-1).item()
|
33 |
+
end_index = torch.argmax(end_logits, dim=-1).item()
|
34 |
+
|
35 |
+
# Извлечение и декодирование предсказанных токенов ответа
|
36 |
+
predict_answer_tokens = input_ids[0, start_index : end_index + 1]
|
37 |
+
return tokenizer.decode(predict_answer_tokens)
|
38 |
|
39 |
def answer_question(context, question):
|
40 |
result = qa_pipeline(context, question)
|
41 |
return result
|
42 |
|
43 |
+
def get_random_example():
|
44 |
+
random_row = get_random_row()
|
45 |
+
context = random_row['context']
|
46 |
+
question = random_row['question']
|
47 |
+
real_answer = random_row['answer']
|
48 |
+
predicted_answer = answer_question(context, question)
|
49 |
+
return context, question, real_answer, predicted_answer
|
50 |
+
|
51 |
+
# Интерфейс Gradio
|
52 |
+
with gr.Blocks() as iface:
|
53 |
+
with gr.Row():
|
54 |
+
with gr.Column():
|
55 |
+
context = gr.Textbox(lines=10, label="Context")
|
56 |
+
question = gr.Textbox(lines=2, label="Question")
|
57 |
+
real_answer = gr.Textbox(lines=2, label="Real Answer")
|
58 |
+
with gr.Column():
|
59 |
+
predicted_answer = gr.Textbox(lines=2, label="Predicted Answer")
|
60 |
+
generate_button = gr.Button("Get Random Example")
|
61 |
+
|
62 |
+
def update_example():
|
63 |
+
context_val, question_val, real_answer_val, predicted_answer_val = get_random_example()
|
64 |
+
return context_val, question_val, real_answer_val, predicted_answer_val
|
65 |
+
|
66 |
+
generate_button.click(update_example, outputs=[context, question, real_answer, predicted_answer])
|
67 |
+
|
|
|
|
|
|
|
68 |
iface.launch()
|