HuzaifaTech commited on
Commit
9787256
·
verified ·
1 Parent(s): 6fb0b86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -17
app.py CHANGED
@@ -3,8 +3,8 @@
3
  # =========================
4
  import os
5
  import tempfile
6
-
7
  import gradio as gr
 
8
  from groq import Groq
9
  from duckduckgo_search import DDGS
10
 
@@ -17,7 +17,7 @@ from langchain_community.vectorstores import Chroma
17
  # =========================
18
  # CONFIG
19
  # =========================
20
- GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Hugging Face Secret
21
 
22
  client = Groq(api_key=GROQ_API_KEY)
23
 
@@ -57,18 +57,16 @@ def web_search(query):
57
 
58
 
59
  # =========================
60
- # PROCESS PDF (FIXED FOR HF)
61
  # =========================
62
  def process_pdf(file):
63
 
64
  global vectorstore, retriever
65
 
66
- # SAFE HF FILE HANDLING
67
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
68
- tmp.write(file.read())
69
- tmp_path = tmp.name
70
 
71
- loader = PyPDFLoader(tmp_path)
72
  documents = loader.load()
73
 
74
  splitter = RecursiveCharacterTextSplitter(
@@ -89,11 +87,11 @@ def process_pdf(file):
89
 
90
  retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
91
 
92
- return "✅ PDF processed successfully. You can now ask questions."
93
 
94
 
95
  # =========================
96
- # HYBRID RAG FUNCTION
97
  # =========================
98
  def ask_rag(query):
99
 
@@ -105,10 +103,10 @@ def ask_rag(query):
105
  docs = retriever.invoke(query)
106
  pdf_context = "\n\n".join([d.page_content for d in docs])
107
 
108
- # fallback if weak retrieval
109
  if len(pdf_context.strip()) < 50:
110
  web_context = web_search(query)
111
- context = pdf_context + "\n\nWEB CONTEXT:\n" + web_context
112
  else:
113
  context = pdf_context
114
 
@@ -123,7 +121,7 @@ def ask_rag(query):
123
 
124
 
125
  # =========================
126
- # CHAT FUNCTION (SAFE)
127
  # =========================
128
  def chat(user_message, history):
129
 
@@ -132,22 +130,23 @@ def chat(user_message, history):
132
  if history is None:
133
  history = []
134
 
135
- history.append((user_message, response))
 
136
 
137
  return history, history
138
 
139
 
140
  # =========================
141
- # GRADIO UI (HF SAFE)
142
  # =========================
143
  with gr.Blocks() as app:
144
 
145
- gr.Markdown("# 🧠 Hybrid RAG Chatbot (PDF + Web Search)")
146
 
147
  file = gr.File(label="Upload PDF")
148
  status = gr.Textbox(label="Status")
149
 
150
- chatbot = gr.Chatbot()
151
  msg = gr.Textbox(placeholder="Ask your question...")
152
  state = gr.State([])
153
 
 
3
  # =========================
4
  import os
5
  import tempfile
 
6
  import gradio as gr
7
+
8
  from groq import Groq
9
  from duckduckgo_search import DDGS
10
 
 
17
  # =========================
18
  # CONFIG
19
  # =========================
20
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY") # HF SECRET KEY
21
 
22
  client = Groq(api_key=GROQ_API_KEY)
23
 
 
57
 
58
 
59
  # =========================
60
+ # PROCESS PDF (HF SAFE)
61
  # =========================
62
  def process_pdf(file):
63
 
64
  global vectorstore, retriever
65
 
66
+ # safe file handling
67
+ file_path = file.name
 
 
68
 
69
+ loader = PyPDFLoader(file_path)
70
  documents = loader.load()
71
 
72
  splitter = RecursiveCharacterTextSplitter(
 
87
 
88
  retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
89
 
90
+ return "✅ PDF successfully processed"
91
 
92
 
93
  # =========================
94
+ # RAG ENGINE (HYBRID)
95
  # =========================
96
  def ask_rag(query):
97
 
 
103
  docs = retriever.invoke(query)
104
  pdf_context = "\n\n".join([d.page_content for d in docs])
105
 
106
+ # hybrid fallback
107
  if len(pdf_context.strip()) < 50:
108
  web_context = web_search(query)
109
+ context = pdf_context + "\n\nWEB:\n" + web_context
110
  else:
111
  context = pdf_context
112
 
 
121
 
122
 
123
  # =========================
124
+ # CHAT FUNCTION (FIXED FORMAT)
125
  # =========================
126
  def chat(user_message, history):
127
 
 
130
  if history is None:
131
  history = []
132
 
133
+ history.append({"role": "user", "content": user_message})
134
+ history.append({"role": "assistant", "content": response})
135
 
136
  return history, history
137
 
138
 
139
  # =========================
140
+ # UI (HUGGING FACE SAFE)
141
  # =========================
142
  with gr.Blocks() as app:
143
 
144
+ gr.Markdown("# 🧠 Hybrid RAG Chatbot (PDF + Web)")
145
 
146
  file = gr.File(label="Upload PDF")
147
  status = gr.Textbox(label="Status")
148
 
149
+ chatbot = gr.Chatbot(type="messages") # IMPORTANT FIX
150
  msg = gr.Textbox(placeholder="Ask your question...")
151
  state = gr.State([])
152