DocumentQandAI / app.py
ghostai1's picture
Create app.py
cde0d08 verified
# πŸ“ 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")