Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -163,8 +163,8 @@ def retrieve_relevant_chunks(query, top_k=3):
|
|
| 163 |
print(f"Error retrieving chunks: {e}")
|
| 164 |
return [], []
|
| 165 |
|
| 166 |
-
def
|
| 167 |
-
"""Chat function - returns
|
| 168 |
global client
|
| 169 |
|
| 170 |
# Reinitialize client if needed
|
|
@@ -178,17 +178,17 @@ def respond(message, history):
|
|
| 178 |
pass
|
| 179 |
|
| 180 |
if client is None:
|
| 181 |
-
return
|
| 182 |
|
| 183 |
if not document_store['chunks']:
|
| 184 |
-
return
|
| 185 |
|
| 186 |
try:
|
| 187 |
# Retrieve context
|
| 188 |
relevant_chunks, metadata = retrieve_relevant_chunks(message, top_k=3)
|
| 189 |
|
| 190 |
if not relevant_chunks:
|
| 191 |
-
return
|
| 192 |
|
| 193 |
# Build context
|
| 194 |
context = "\n\n".join([
|
|
@@ -201,11 +201,10 @@ def respond(message, history):
|
|
| 201 |
{"role": "system", "content": "You are a helpful assistant that answers questions based on provided context. Be concise and accurate."}
|
| 202 |
]
|
| 203 |
|
| 204 |
-
# Add history
|
| 205 |
if history:
|
| 206 |
for msg in history[-6:]: # Last 6 messages
|
| 207 |
-
|
| 208 |
-
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 209 |
|
| 210 |
# Add current query
|
| 211 |
messages.append({
|
|
@@ -237,11 +236,12 @@ def respond(message, history):
|
|
| 237 |
'answer': answer
|
| 238 |
})
|
| 239 |
|
| 240 |
-
|
|
|
|
| 241 |
|
| 242 |
except Exception as e:
|
| 243 |
print(f"Error: {e}")
|
| 244 |
-
return
|
| 245 |
|
| 246 |
def download_history():
|
| 247 |
"""Download chat history"""
|
|
@@ -255,7 +255,7 @@ def download_history():
|
|
| 255 |
except:
|
| 256 |
return None
|
| 257 |
|
| 258 |
-
# Build interface
|
| 259 |
with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
|
| 260 |
|
| 261 |
gr.Markdown("""
|
|
@@ -278,28 +278,23 @@ with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
|
|
| 278 |
gr.Markdown("### History")
|
| 279 |
download_btn = gr.Button("Download (JSON)")
|
| 280 |
download_file = gr.File(label="Download")
|
| 281 |
-
clear_btn = gr.Button("Clear Chat")
|
| 282 |
|
| 283 |
with gr.Column(scale=2):
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
# Process files
|
| 289 |
process_btn.click(process_files, [file_upload], [process_output])
|
| 290 |
|
| 291 |
-
# Chat - using respond that returns dict format
|
| 292 |
-
submit.click(respond, [msg, chatbot], [chatbot]).then(
|
| 293 |
-
lambda: "", None, [msg]
|
| 294 |
-
)
|
| 295 |
-
|
| 296 |
-
msg.submit(respond, [msg, chatbot], [chatbot]).then(
|
| 297 |
-
lambda: "", None, [msg]
|
| 298 |
-
)
|
| 299 |
-
|
| 300 |
-
# Clear
|
| 301 |
-
clear_btn.click(lambda: [], None, chatbot)
|
| 302 |
-
|
| 303 |
# Download
|
| 304 |
download_btn.click(download_history, None, [download_file])
|
| 305 |
|
|
|
|
| 163 |
print(f"Error retrieving chunks: {e}")
|
| 164 |
return [], []
|
| 165 |
|
| 166 |
+
def chat(message, history):
|
| 167 |
+
"""Chat function - processes message and returns response string"""
|
| 168 |
global client
|
| 169 |
|
| 170 |
# Reinitialize client if needed
|
|
|
|
| 178 |
pass
|
| 179 |
|
| 180 |
if client is None:
|
| 181 |
+
return "[ERROR] Groq API not initialized. Set GROQ_API_KEY in Settings."
|
| 182 |
|
| 183 |
if not document_store['chunks']:
|
| 184 |
+
return "[WARNING] Please upload and process documents first."
|
| 185 |
|
| 186 |
try:
|
| 187 |
# Retrieve context
|
| 188 |
relevant_chunks, metadata = retrieve_relevant_chunks(message, top_k=3)
|
| 189 |
|
| 190 |
if not relevant_chunks:
|
| 191 |
+
return "[ERROR] No relevant information found."
|
| 192 |
|
| 193 |
# Build context
|
| 194 |
context = "\n\n".join([
|
|
|
|
| 201 |
{"role": "system", "content": "You are a helpful assistant that answers questions based on provided context. Be concise and accurate."}
|
| 202 |
]
|
| 203 |
|
| 204 |
+
# Add history - history is already in messages format
|
| 205 |
if history:
|
| 206 |
for msg in history[-6:]: # Last 6 messages
|
| 207 |
+
messages.append(msg)
|
|
|
|
| 208 |
|
| 209 |
# Add current query
|
| 210 |
messages.append({
|
|
|
|
| 236 |
'answer': answer
|
| 237 |
})
|
| 238 |
|
| 239 |
+
# Return just the string - ChatInterface handles adding to history
|
| 240 |
+
return full_answer
|
| 241 |
|
| 242 |
except Exception as e:
|
| 243 |
print(f"Error: {e}")
|
| 244 |
+
return f"[ERROR] {str(e)}"
|
| 245 |
|
| 246 |
def download_history():
|
| 247 |
"""Download chat history"""
|
|
|
|
| 255 |
except:
|
| 256 |
return None
|
| 257 |
|
| 258 |
+
# Build interface - USE gr.ChatInterface!
|
| 259 |
with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
|
| 260 |
|
| 261 |
gr.Markdown("""
|
|
|
|
| 278 |
gr.Markdown("### History")
|
| 279 |
download_btn = gr.Button("Download (JSON)")
|
| 280 |
download_file = gr.File(label="Download")
|
|
|
|
| 281 |
|
| 282 |
with gr.Column(scale=2):
|
| 283 |
+
# Use ChatInterface directly - it handles everything!
|
| 284 |
+
chat_interface = gr.ChatInterface(
|
| 285 |
+
fn=chat,
|
| 286 |
+
type="messages",
|
| 287 |
+
chatbot=gr.Chatbot(height=500, label="Conversation"),
|
| 288 |
+
textbox=gr.Textbox(label="Ask a question", placeholder="Type your question here...", lines=2),
|
| 289 |
+
submit_btn="Ask",
|
| 290 |
+
retry_btn=None,
|
| 291 |
+
undo_btn=None,
|
| 292 |
+
clear_btn="Clear Chat"
|
| 293 |
+
)
|
| 294 |
|
| 295 |
# Process files
|
| 296 |
process_btn.click(process_files, [file_upload], [process_output])
|
| 297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
# Download
|
| 299 |
download_btn.click(download_history, None, [download_file])
|
| 300 |
|