Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load Question Answering pipeline
|
| 5 |
+
qa_pipeline = pipeline(
|
| 6 |
+
"question-answering",
|
| 7 |
+
model="distilbert-base-uncased-distilled-squad"
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
def answer_question(context, question):
|
| 11 |
+
if not context or not question:
|
| 12 |
+
return "Please provide both context and question."
|
| 13 |
+
|
| 14 |
+
result = qa_pipeline(
|
| 15 |
+
question=question,
|
| 16 |
+
context=context
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
answer = result['answer']
|
| 20 |
+
score = round(result['score'], 4)
|
| 21 |
+
|
| 22 |
+
return f"Answer: {answer}\nConfidence Score: {score}"
|
| 23 |
+
|
| 24 |
+
# Gradio Interface
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=answer_question,
|
| 27 |
+
inputs=[
|
| 28 |
+
gr.Textbox(lines=8, placeholder="Enter context paragraph here...", label="Context"),
|
| 29 |
+
gr.Textbox(lines=2, placeholder="Enter your question here...", label="Question")
|
| 30 |
+
],
|
| 31 |
+
outputs=gr.Textbox(label="Predicted Answer"),
|
| 32 |
+
title="Extractive Question Answering App",
|
| 33 |
+
description="Ask a question based on the given context paragraph."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
iface.launch()
|