Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
| 5 |
qa = pipeline(
|
| 6 |
"question-answering",
|
| 7 |
model="distilbert-base-uncased-distilled-squad",
|
| 8 |
-
|
| 9 |
)
|
| 10 |
|
| 11 |
def answer(question, context):
|
|
@@ -15,43 +18,30 @@ def answer(question, context):
|
|
| 15 |
if not question.strip():
|
| 16 |
return "β οΈ Please enter a question."
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
answer_text = result.get("answer", "").strip()
|
| 21 |
-
|
| 22 |
-
if not answer_text:
|
| 23 |
-
return "β Answer not found in the provided content."
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
|
| 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
|
| 42 |
-
lines=
|
| 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"
|
| 52 |
|
| 53 |
ask = gr.Button("Ask")
|
| 54 |
|
| 55 |
-
ask.click(answer,
|
| 56 |
|
| 57 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Read API key from environment
|
| 6 |
+
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
| 7 |
+
|
| 8 |
qa = pipeline(
|
| 9 |
"question-answering",
|
| 10 |
model="distilbert-base-uncased-distilled-squad",
|
| 11 |
+
token=HF_TOKEN
|
| 12 |
)
|
| 13 |
|
| 14 |
def answer(question, context):
|
|
|
|
| 18 |
if not question.strip():
|
| 19 |
return "β οΈ Please enter a question."
|
| 20 |
|
| 21 |
+
result = qa(question=question, context=context)
|
| 22 |
+
answer_text = result.get("answer", "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
if not answer_text:
|
| 25 |
+
return "β Answer not found in the provided content."
|
| 26 |
|
| 27 |
+
return answer_text
|
|
|
|
| 28 |
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
+
gr.Markdown("## β‘ Free AI Document Chatbot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
context = gr.Textbox(
|
| 33 |
+
label="π Paste document text",
|
| 34 |
+
lines=10
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
question = gr.Textbox(
|
| 38 |
+
label="β Ask a question"
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
+
output = gr.Textbox(label="β
Answer")
|
| 42 |
|
| 43 |
ask = gr.Button("Ask")
|
| 44 |
|
| 45 |
+
ask.click(answer, [question, context], output)
|
| 46 |
|
| 47 |
demo.launch()
|