Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the question-answering pipeline
|
| 5 |
+
qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
| 6 |
+
|
| 7 |
+
# Define the function for question answering
|
| 8 |
+
def answer_question(context, question):
|
| 9 |
+
if not context.strip() or not question.strip():
|
| 10 |
+
return "Please provide both a context and a question."
|
| 11 |
+
try:
|
| 12 |
+
result = qa_pipeline(question=question, context=context)
|
| 13 |
+
return result["answer"]
|
| 14 |
+
except Exception as e:
|
| 15 |
+
return f"Error: {str(e)}"
|
| 16 |
+
|
| 17 |
+
# Create the Gradio interface
|
| 18 |
+
demo = gr.Interface(
|
| 19 |
+
fn=answer_question,
|
| 20 |
+
inputs=[
|
| 21 |
+
gr.Textbox(lines=10, label="Context", placeholder="Enter the context here..."),
|
| 22 |
+
gr.Textbox(label="Question", placeholder="Enter the question here...")
|
| 23 |
+
],
|
| 24 |
+
outputs=gr.Textbox(label="Answer"),
|
| 25 |
+
title="Question Answering with RoBERTa",
|
| 26 |
+
description=(
|
| 27 |
+
"Ask questions based on a given context using the `deepset/roberta-base-squad2` model. "
|
| 28 |
+
"Enter a passage of text as the context and a specific question about it to get an answer."
|
| 29 |
+
)
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Launch the app
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
demo.launch()
|