aliabd HF staff commited on
Commit
377dd27
1 Parent(s): 7bdd1d2

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +6 -17
run.py CHANGED
@@ -1,28 +1,17 @@
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"),
 
1
  import gradio as gr
 
 
2
 
3
+ from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
4
+
5
+ model_name = "deepset/roberta-base-squad2"
6
+
7
+ nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)
8
 
9
  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."
10
  question = "Which continent is the Amazon rainforest in?"
11
 
12
 
13
  def predict(context, question):
14
+ return nlp({"question": question, "context": context})
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  gr.Interface(predict, inputs=[
17
  gr.inputs.Textbox(lines=7, default=context, label="Context Paragraph"),