Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def answer(question, context):
|
| 7 |
if not context.strip():
|
| 8 |
-
return "Please
|
| 9 |
-
return qa(question=question, context=context)["answer"]
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
question = gr.Textbox(label="Ask a question")
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
ask = gr.Button("Ask")
|
| 20 |
|
| 21 |
-
ask.click(answer, [question, context], output)
|
| 22 |
|
| 23 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Fast & lightweight model
|
| 5 |
+
qa = pipeline(
|
| 6 |
+
"question-answering",
|
| 7 |
+
model="distilbert-base-uncased-distilled-squad",
|
| 8 |
+
device=-1
|
| 9 |
+
)
|
| 10 |
|
| 11 |
def answer(question, context):
|
| 12 |
if not context.strip():
|
| 13 |
+
return "⚠️ Please paste document content first."
|
|
|
|
| 14 |
|
| 15 |
+
if not question.strip():
|
| 16 |
+
return "⚠️ Please enter a question."
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
result = qa(question=question, context=context)
|
| 20 |
+
answer_text = result.get("answer", "").strip()
|
| 21 |
+
|
| 22 |
+
if not answer_text:
|
| 23 |
+
return "❌ Answer not found in the provided content."
|
| 24 |
|
| 25 |
+
return answer_text
|
|
|
|
| 26 |
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error: {str(e)}"
|
| 29 |
+
|
| 30 |
+
with gr.Blocks() as demo:
|
| 31 |
+
gr.Markdown(
|
| 32 |
+
"""
|
| 33 |
+
## ⚡ Super-Fast Free AI Document Chatbot
|
| 34 |
+
**How to use:**
|
| 35 |
+
1. Paste document text below
|
| 36 |
+
2. Ask a question based ONLY on that content
|
| 37 |
+
"""
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
context = gr.Textbox(
|
| 41 |
+
label="📄 Paste document content here",
|
| 42 |
+
lines=12,
|
| 43 |
+
placeholder="Paste text from PDF / Excel / Website..."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
question = gr.Textbox(
|
| 47 |
+
label="❓ Ask a question",
|
| 48 |
+
placeholder="What is this document about?"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
output = gr.Textbox(label="✅ Answer", lines=3)
|
| 52 |
|
| 53 |
ask = gr.Button("Ask")
|
| 54 |
|
| 55 |
+
ask.click(answer, inputs=[question, context], outputs=output)
|
| 56 |
|
| 57 |
demo.launch()
|