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()