Vince Trost commited on
Commit
f640869
·
1 Parent(s): 2b4792d

feat: add application profile

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
4
+
5
+
6
+ # Use the Roberta model for question answering
7
+ model_name = 'deepset/roberta-base-squad2'
8
+ #question_answerer = pipeline("question-answering", model=model_name, tokenizer=model_name, device=0)
9
+ question_answerer = pipeline("question-answering", model=model_name, tokenizer=model_name)
10
+
11
+ # Define the input and output interfaces
12
+ context = gr.Textbox(lines=3, label="Context")
13
+ question = gr.Textbox(lines=1, label="Question")
14
+ output = gr.Textbox(label="Answer")
15
+
16
+ # Define the question answering function
17
+ def answer_question(context, question):
18
+ # Use the Roberta model to predict the answer to the question
19
+ input_dict = {'question': question, 'context': context}
20
+ result = question_answerer(**input_dict) # the ** unpacks the dict inside the arg
21
+ print(
22
+ f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}"
23
+ )
24
+ row = pd.DataFrame.from_dict({
25
+ 'answer': [result['answer']],
26
+ 'score': [result['score']],
27
+ 'start': [result['start']],
28
+ 'end': [result['end']],
29
+ 'question': [question],
30
+ 'context': [context]
31
+ })
32
+
33
+ row.to_csv('qa_prototype_entries.csv', mode='a', header=False)
34
+
35
+ answer = result["answer"]
36
+ return answer
37
+
38
+ # Create the question answering interface
39
+ interface = gr.Interface(answer_question, [context, question], output)
40
+ interface.launch(share=True)