Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from transformers import pipeline | |
| # Read API key from environment | |
| HF_TOKEN = os.getenv("HF_API_TOKEN") | |
| qa = pipeline( | |
| "question-answering", | |
| model="distilbert-base-uncased-distilled-squad", | |
| token=HF_TOKEN | |
| ) | |
| def answer(question, context): | |
| if not context.strip(): | |
| return "β οΈ Please paste document content first." | |
| if not question.strip(): | |
| return "β οΈ Please enter a question." | |
| result = qa(question=question, context=context) | |
| answer_text = result.get("answer", "").strip() | |
| if not answer_text: | |
| return "β Answer not found in the provided content." | |
| return answer_text | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## β‘ Free AI Document Chatbot") | |
| context = gr.Textbox( | |
| label="π Paste document text", | |
| lines=10 | |
| ) | |
| question = gr.Textbox( | |
| label="β Ask a question" | |
| ) | |
| output = gr.Textbox(label="β Answer") | |
| ask = gr.Button("Ask") | |
| ask.click(answer, [question, context], output) | |
| demo.launch() | |