nabeo commited on
Commit
09a55e9
1 Parent(s): ab59fcf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -9
app.py CHANGED
@@ -1,17 +1,32 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- analyzer = pipeline("sentiment-analysis")
 
5
 
 
 
 
6
 
7
- def greet(name):
8
- #return "Hello " + name
9
- return (analyzer(name))
10
 
 
 
11
 
12
- # We instantiate the Textbox class
13
- textbox = gr.Textbox(label="Please fill in something words:",placeholder="happy", lines=2)
 
14
 
15
- #textbox = gr.Textbox(label="Type your name here:", placeholder="John Doe", lines=2)
16
 
17
- gr.Interface(fn=greet, inputs=textbox, outputs="text").launch(share=True)
 
 
 
 
 
 
 
 
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()