File size: 1,287 Bytes
cde0d08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# πŸ“ Document Q&A Demo | CPU-only HF Space

import gradio as gr
from transformers import pipeline

# Load a fast, accurate QA model
qa_pipeline = pipeline(
    "question-answering",
    model="deepset/roberta-base-squad2",
    device=-1     # force CPU
)

def answer_question(context: str, question: str):
    if not context.strip() or not question.strip():
        return "Please provide both a document context and a question."
    result = qa_pipeline(question=question, context=context)
    answer = result["answer"]
    score  = round(result["score"], 3)
    return f"{answer}  (confidence: {score})"

with gr.Blocks(title="πŸ“ Document Q&A") as demo:
    gr.Markdown(
        "# πŸ“ Document Q&A\n"
        "Paste in any text (policy, FAQ, product manual), ask a question, and get an instant answer."
    )
    with gr.Row():
        ctx = gr.Textbox(lines=10, placeholder="Paste your document text here...", label="Document Context")
        qry = gr.Textbox(lines=2, placeholder="Ask a question about the text above...", label="Question")
    btn    = gr.Button("Get Answer πŸ”", variant="primary")
    out    = gr.Textbox(label="Answer", interactive=False)

    btn.click(answer_question, [ctx, qry], out)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0")