File size: 1,462 Bytes
f640869
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb881c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import gradio as gr
import pandas as pd
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline


# Use the Roberta model for question answering
model_name = 'deepset/roberta-base-squad2'
#question_answerer = pipeline("question-answering", model=model_name, tokenizer=model_name, device=0)
question_answerer = pipeline("question-answering", model=model_name, tokenizer=model_name)

# Define the input and output interfaces
context = gr.Textbox(lines=3, label="Context")
question = gr.Textbox(lines=1, label="Question")
output = gr.Textbox(label="Answer")

# Define the question answering function
def answer_question(context, question):
  # Use the Roberta model to predict the answer to the question
  input_dict = {'question': question, 'context': context}
  result = question_answerer(**input_dict)   # the ** unpacks the dict inside the arg
  print(
    f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}"
  )
  row = pd.DataFrame.from_dict({
    'answer': [result['answer']],
    'score': [result['score']],
    'start': [result['start']],
    'end': [result['end']],
    'question': [question],
    'context': [context]
  })

  row.to_csv('qa_prototype_entries.csv', mode='a', header=False)

  answer = result["answer"]
  return answer

# Create the question answering interface
interface = gr.Interface(answer_question, [context, question], output)
interface.launch()