rithwikreal commited on
Commit
3080c52
Β·
verified Β·
1 Parent(s): 792b771

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -26
app.py CHANGED
@@ -1,11 +1,14 @@
 
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):
@@ -15,43 +18,30 @@ def answer(question, context):
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()
 
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()