aliabd HF staff commited on
Commit
7895f59
1 Parent(s): be76abf

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +27 -5
run.py CHANGED
@@ -1,8 +1,30 @@
1
  import gradio as gr
 
 
 
 
 
 
2
  context = "The Amazon rainforest, also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."
3
  question = "Which continent is the Amazon rainforest in?"
4
- gr.load(
5
- "huggingface/deepset/roberta-base-squad2",
6
- inputs=[gr.inputs.Textbox(lines=7, default=context, label="Context Paragraph"), gr.inputs.Textbox(lines=2, default=question, label="Question")],
7
- outputs=[gr.outputs.Textbox(label="Answer"), gr.outputs.Textbox(label="Score")],
8
- title=None).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, RobertaForQuestionAnswering
3
+ import torch
4
+
5
+ tokenizer = AutoTokenizer.from_pretrained("deepset/roberta-base-squad2")
6
+ model = RobertaForQuestionAnswering.from_pretrained("deepset/roberta-base-squad2")
7
+
8
  context = "The Amazon rainforest, also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."
9
  question = "Which continent is the Amazon rainforest in?"
10
+
11
+
12
+ def predict(context, question):
13
+ inputs = tokenizer(question, context, return_tensors="pt")
14
+ with torch.no_grad():
15
+ outputs = model(**inputs)
16
+ answer_start_index = outputs.start_logits.argmax()
17
+ answer_end_index = outputs.end_logits.argmax()
18
+ predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]
19
+ answer = tokenizer.decode(predict_answer_tokens, skip_special_tokens=True),
20
+ target_start_index = torch.tensor([14])
21
+ target_end_index = torch.tensor([15])
22
+ outputs = model(**inputs, start_positions=target_start_index, end_positions=target_end_index)
23
+ loss = outputs.loss
24
+ score = round(loss.item(), 2)
25
+ return answer, score
26
+
27
+ gr.Interface(predict, inputs=[
28
+ gr.inputs.Textbox(lines=7, default=context, label="Context Paragraph"),
29
+ gr.inputs.Textbox(lines=2, default=question, label="Question"),
30
+ ], outputs=[gr.outputs.Textbox(label="Answer"), gr.outputs.Textbox(label="Score")]).launch()