Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, RobertaForQuestionAnswering
|
| 4 |
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("tsmatz/roberta_qa_japanese")
|
| 6 |
+
model = RobertaForQuestionAnswering.from_pretrained("tsmatz/roberta_qa_japanese")
|
| 7 |
|
| 8 |
+
def answer(text, question):
|
| 9 |
+
inputs = tokenizer(question, text, add_special_tokens=True, return_tensors="pt")
|
| 10 |
+
input_ids = inputs["input_ids"].tolist()[0]
|
| 11 |
|
| 12 |
+
outputs = model(**inputs)
|
| 13 |
+
answer_start_scores = outputs.start_logits
|
| 14 |
+
answer_end_scores = outputs.end_logits
|
| 15 |
|
| 16 |
+
answer_start = torch.argmax(answer_start_scores)
|
| 17 |
+
answer_end = torch.argmax(answer_end_scores) + 1
|
| 18 |
|
| 19 |
+
answer = tokenizer.convert_tokens_to_string(
|
| 20 |
+
tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end])
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
return answer
|
| 24 |
|
| 25 |
+
gr.Interface(
|
| 26 |
+
answer,
|
| 27 |
+
[
|
| 28 |
+
gr.Textbox(label="Text:", placeholder="Text...", lines=5),
|
| 29 |
+
gr.Textbox(label="Question:", placeholder="Question...", lines=1)
|
| 30 |
+
],
|
| 31 |
+
"text",
|
| 32 |
+
).launch()
|