Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# π Document Q&A Demo | CPU-only HF Space
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# Load a fast, accurate QA model
|
7 |
+
qa_pipeline = pipeline(
|
8 |
+
"question-answering",
|
9 |
+
model="deepset/roberta-base-squad2",
|
10 |
+
device=-1 # force CPU
|
11 |
+
)
|
12 |
+
|
13 |
+
def answer_question(context: str, question: str):
|
14 |
+
if not context.strip() or not question.strip():
|
15 |
+
return "Please provide both a document context and a question."
|
16 |
+
result = qa_pipeline(question=question, context=context)
|
17 |
+
answer = result["answer"]
|
18 |
+
score = round(result["score"], 3)
|
19 |
+
return f"{answer} (confidence: {score})"
|
20 |
+
|
21 |
+
with gr.Blocks(title="π Document Q&A") as demo:
|
22 |
+
gr.Markdown(
|
23 |
+
"# π Document Q&A\n"
|
24 |
+
"Paste in any text (policy, FAQ, product manual), ask a question, and get an instant answer."
|
25 |
+
)
|
26 |
+
with gr.Row():
|
27 |
+
ctx = gr.Textbox(lines=10, placeholder="Paste your document text here...", label="Document Context")
|
28 |
+
qry = gr.Textbox(lines=2, placeholder="Ask a question about the text above...", label="Question")
|
29 |
+
btn = gr.Button("Get Answer π", variant="primary")
|
30 |
+
out = gr.Textbox(label="Answer", interactive=False)
|
31 |
+
|
32 |
+
btn.click(answer_question, [ctx, qry], out)
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
demo.launch(server_name="0.0.0.0")
|