File size: 1,045 Bytes
3080c52
81b6a4f
 
 
3080c52
 
 
f652a45
 
 
3080c52
f652a45
81b6a4f
 
 
f652a45
81b6a4f
f652a45
 
 
3080c52
 
81b6a4f
3080c52
 
81b6a4f
3080c52
f652a45
 
3080c52
f652a45
 
3080c52
 
f652a45
 
 
3080c52
f652a45
 
3080c52
81b6a4f
 
 
3080c52
81b6a4f
 
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
37
38
39
40
41
42
43
44
45
46
47
48
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()