Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
# Load a pre-trained question answering pipeline
|
9 |
+
|
10 |
+
question_answerer = pipeline("question-answering", model="bert-large-uncased-whole-word-masking-finetuned-squad")
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
def answer_question(context, question):
|
16 |
+
|
17 |
+
"""
|
18 |
+
|
19 |
+
Takes context and a question as input and returns the predicted answer.
|
20 |
+
|
21 |
+
"""
|
22 |
+
|
23 |
+
if context and question:
|
24 |
+
|
25 |
+
result = question_answerer(question=question, context=context)
|
26 |
+
|
27 |
+
answer = result['answer']
|
28 |
+
|
29 |
+
confidence = f"{result['score']:.4f}"
|
30 |
+
|
31 |
+
return f"Answer: {answer}", f"Confidence: {confidence}"
|
32 |
+
|
33 |
+
else:
|
34 |
+
|
35 |
+
return "Please provide both context and a question.", ""
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
# Define the Gradio interface
|
41 |
+
|
42 |
+
iface = gr.Interface(
|
43 |
+
|
44 |
+
fn=answer_question,
|
45 |
+
|
46 |
+
inputs=[
|
47 |
+
|
48 |
+
gr.Textbox(lines=7, placeholder="Enter the context here..."),
|
49 |
+
|
50 |
+
gr.Textbox(placeholder="Ask a question about the context...")
|
51 |
+
|
52 |
+
],
|
53 |
+
|
54 |
+
outputs=[
|
55 |
+
|
56 |
+
gr.Textbox(label="Predicted Answer"),
|
57 |
+
|
58 |
+
gr.Textbox(label="Confidence Score")
|
59 |
+
|
60 |
+
],
|
61 |
+
|
62 |
+
title="Simple Question Answering",
|
63 |
+
|
64 |
+
description="Enter a block of text (context) and then ask a question about it. The app will try to find the answer within the text.",
|
65 |
+
|
66 |
+
)
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
# Launch the Gradio app
|
72 |
+
|
73 |
+
iface.launch()
|