Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -177,45 +177,23 @@ def create_deepseek_interface():
|
|
| 177 |
f"- **Search Term**: {search_query}\n" + \
|
| 178 |
f"- **Parameters**: {params}\n"
|
| 179 |
|
| 180 |
-
#
|
| 181 |
-
def
|
| 182 |
-
"""
|
| 183 |
-
print(f"
|
| 184 |
-
# Convert to messages format
|
| 185 |
-
new_message = {"role": "user", "content": message}
|
| 186 |
-
response = {"role": "assistant", "content": f"Echo: {message}"}
|
| 187 |
-
return history + [new_message, response], ""
|
| 188 |
-
|
| 189 |
-
# Function to call DeepSeek API with streaming
|
| 190 |
-
def query_deepseek_streaming(message, history, use_deep_research):
|
| 191 |
-
print(f"\n=== Starting query_deepseek_streaming ===")
|
| 192 |
print(f"Message: {message}")
|
| 193 |
print(f"History length: {len(history)}")
|
| 194 |
print(f"Deep Research: {use_deep_research}")
|
| 195 |
|
| 196 |
if not api_key:
|
| 197 |
error_msg = "Environment variable FW_API_KEY is not set. Please check the environment variables on the server."
|
| 198 |
-
|
| 199 |
-
new_history = history + [
|
| 200 |
-
{"role": "user", "content": message},
|
| 201 |
-
{"role": "assistant", "content": error_msg}
|
| 202 |
-
]
|
| 203 |
-
yield new_history, error_msg
|
| 204 |
-
return
|
| 205 |
|
| 206 |
search_context = ""
|
| 207 |
search_info = ""
|
| 208 |
|
| 209 |
-
# Create new history with the user message in messages format
|
| 210 |
-
new_history = history + [{"role": "user", "content": message}]
|
| 211 |
-
|
| 212 |
if use_deep_research:
|
| 213 |
try:
|
| 214 |
-
# Add assistant message with search status
|
| 215 |
-
new_history.append({"role": "assistant", "content": "🔍 Extracting optimal keywords and searching the web..."})
|
| 216 |
-
yield new_history, ""
|
| 217 |
-
|
| 218 |
-
# Execute search
|
| 219 |
print(f"Deep Research activated: Starting search for '{message}'")
|
| 220 |
search_results = search_with_serphouse(message)
|
| 221 |
print(f"Search results received: {search_results[:100]}...")
|
|
@@ -234,25 +212,13 @@ When citing search results, mention the source, and ensure your answer reflects
|
|
| 234 |
except Exception as e:
|
| 235 |
print(f"Exception occurred during Deep Research: {str(e)}")
|
| 236 |
search_info = f"🔍 Deep Research feature error: {str(e)}\n\n"
|
| 237 |
-
else:
|
| 238 |
-
# Add empty assistant message to start
|
| 239 |
-
new_history.append({"role": "assistant", "content": ""})
|
| 240 |
-
yield new_history, ""
|
| 241 |
-
|
| 242 |
-
# Update last assistant message with search info
|
| 243 |
-
if search_info:
|
| 244 |
-
new_history[-1]["content"] = search_info
|
| 245 |
-
yield new_history, ""
|
| 246 |
|
| 247 |
# Prepare conversation history for API request
|
| 248 |
messages = []
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
messages.append({"role": "user", "content": msg["content"]})
|
| 254 |
-
elif msg["role"] == "assistant":
|
| 255 |
-
messages.append({"role": "assistant", "content": msg["content"]})
|
| 256 |
|
| 257 |
# Add system message with search context if available
|
| 258 |
if search_context:
|
|
@@ -272,7 +238,7 @@ When citing search results, mention the source, and ensure your answer reflects
|
|
| 272 |
"frequency_penalty": 0,
|
| 273 |
"temperature": 0.6,
|
| 274 |
"messages": messages,
|
| 275 |
-
"stream":
|
| 276 |
}
|
| 277 |
headers = {
|
| 278 |
"Accept": "application/json",
|
|
@@ -280,73 +246,26 @@ When citing search results, mention the source, and ensure your answer reflects
|
|
| 280 |
"Authorization": f"Bearer {api_key}"
|
| 281 |
}
|
| 282 |
|
| 283 |
-
print(f"Calling DeepSeek API...")
|
| 284 |
print(f"Number of messages in conversation: {len(messages)}")
|
| 285 |
|
| 286 |
try:
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
| 288 |
response.raise_for_status()
|
| 289 |
|
| 290 |
print(f"API Response Status: {response.status_code}")
|
| 291 |
|
| 292 |
-
|
| 293 |
-
full_response = search_info
|
| 294 |
-
chunk_count = 0
|
| 295 |
-
|
| 296 |
-
# Process streaming response
|
| 297 |
-
for line in response.iter_lines():
|
| 298 |
-
if line:
|
| 299 |
-
line_text = line.decode('utf-8')
|
| 300 |
-
|
| 301 |
-
# Remove 'data: ' prefix
|
| 302 |
-
if line_text.startswith("data: "):
|
| 303 |
-
line_text = line_text[6:]
|
| 304 |
-
|
| 305 |
-
# Check for stream end message
|
| 306 |
-
if line_text == "[DONE]":
|
| 307 |
-
print(f"Stream completed. Total chunks: {chunk_count}")
|
| 308 |
-
break
|
| 309 |
-
|
| 310 |
-
try:
|
| 311 |
-
# Parse JSON
|
| 312 |
-
chunk = json.loads(line_text)
|
| 313 |
-
|
| 314 |
-
# Check if there's content in the chunk
|
| 315 |
-
if "choices" in chunk and len(chunk["choices"]) > 0:
|
| 316 |
-
delta = chunk["choices"][0].get("delta", {})
|
| 317 |
-
chunk_content = delta.get("content", "")
|
| 318 |
-
|
| 319 |
-
if chunk_content:
|
| 320 |
-
chunk_count += 1
|
| 321 |
-
full_response += chunk_content
|
| 322 |
-
|
| 323 |
-
# Update last assistant message
|
| 324 |
-
new_history[-1]["content"] = full_response
|
| 325 |
-
|
| 326 |
-
# Debug: Print first few chunks
|
| 327 |
-
if chunk_count <= 5:
|
| 328 |
-
print(f"Chunk {chunk_count}: {repr(chunk_content[:50])}")
|
| 329 |
-
|
| 330 |
-
# Yield less frequently to avoid overwhelming Gradio
|
| 331 |
-
# Only yield every 10 chunks or if it's a significant chunk
|
| 332 |
-
if chunk_count % 10 == 0 or len(chunk_content) > 50:
|
| 333 |
-
yield new_history, ""
|
| 334 |
-
time.sleep(0.01) # Small delay to prevent connection errors
|
| 335 |
-
|
| 336 |
-
except json.JSONDecodeError as e:
|
| 337 |
-
print(f"JSON decode error: {e}")
|
| 338 |
-
print(f"Line content: {repr(line_text[:100])}")
|
| 339 |
-
continue
|
| 340 |
-
|
| 341 |
-
# Final update
|
| 342 |
-
if not full_response or full_response == search_info:
|
| 343 |
-
# If no response was generated, add an error message
|
| 344 |
-
full_response = search_info + "Error: No response generated from the model. Please try again."
|
| 345 |
-
new_history[-1]["content"] = full_response
|
| 346 |
|
| 347 |
print(f"Final response length: {len(full_response)}")
|
| 348 |
-
|
| 349 |
-
|
|
|
|
|
|
|
| 350 |
|
| 351 |
except requests.exceptions.RequestException as e:
|
| 352 |
error_msg = f"API error: {str(e)}"
|
|
@@ -354,15 +273,23 @@ When citing search results, mention the source, and ensure your answer reflects
|
|
| 354 |
error_msg = "Authentication failed. Please check your FW_API_KEY environment variable."
|
| 355 |
|
| 356 |
print(f"Request error: {error_msg}")
|
| 357 |
-
|
| 358 |
-
yield new_history, error_msg
|
| 359 |
except Exception as e:
|
| 360 |
error_msg = f"Unexpected error: {str(e)}"
|
| 361 |
print(f"Unexpected error: {error_msg}")
|
| 362 |
import traceback
|
| 363 |
traceback.print_exc()
|
| 364 |
-
|
| 365 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
|
| 367 |
# Create Gradio interface
|
| 368 |
with gr.Blocks(theme="soft", fill_height=True) as demo:
|
|
@@ -382,8 +309,8 @@ When citing search results, mention the source, and ensure your answer reflects
|
|
| 382 |
chatbot = gr.Chatbot(
|
| 383 |
height=500,
|
| 384 |
show_label=False,
|
| 385 |
-
container=True
|
| 386 |
-
|
| 387 |
)
|
| 388 |
|
| 389 |
# Add Deep Research toggle and status display
|
|
@@ -421,7 +348,6 @@ When citing search results, mention the source, and ensure your answer reflects
|
|
| 421 |
# Clear conversation button
|
| 422 |
with gr.Row():
|
| 423 |
clear = gr.ClearButton([msg, chatbot], value="🧹 Clear Conversation")
|
| 424 |
-
test_btn = gr.Button("🧪 Test Echo", variant="secondary")
|
| 425 |
|
| 426 |
# Example queries
|
| 427 |
gr.Examples(
|
|
@@ -436,19 +362,17 @@ When citing search results, mention the source, and ensure your answer reflects
|
|
| 436 |
# Error message display
|
| 437 |
error_box = gr.Markdown("")
|
| 438 |
|
| 439 |
-
#
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
outputs=[chatbot, error_box]
|
| 445 |
-
)
|
| 446 |
|
|
|
|
| 447 |
submit.click(
|
| 448 |
-
|
| 449 |
inputs=[msg, chatbot, use_deep_research],
|
| 450 |
-
outputs=[chatbot, error_box]
|
| 451 |
-
queue=True # Enable queue for streaming
|
| 452 |
).then(
|
| 453 |
lambda: "",
|
| 454 |
None,
|
|
@@ -457,10 +381,9 @@ When citing search results, mention the source, and ensure your answer reflects
|
|
| 457 |
|
| 458 |
# Allow Enter key submission
|
| 459 |
msg.submit(
|
| 460 |
-
|
| 461 |
inputs=[msg, chatbot, use_deep_research],
|
| 462 |
-
outputs=[chatbot, error_box]
|
| 463 |
-
queue=True # Enable queue for streaming
|
| 464 |
).then(
|
| 465 |
lambda: "",
|
| 466 |
None,
|
|
@@ -471,12 +394,13 @@ When citing search results, mention the source, and ensure your answer reflects
|
|
| 471 |
|
| 472 |
# Run interface
|
| 473 |
if __name__ == "__main__":
|
|
|
|
| 474 |
demo = create_deepseek_interface()
|
| 475 |
-
|
| 476 |
demo.launch(
|
| 477 |
debug=True,
|
| 478 |
share=False,
|
| 479 |
server_name="0.0.0.0",
|
| 480 |
server_port=7860,
|
| 481 |
-
show_error=True
|
| 482 |
)
|
|
|
|
| 177 |
f"- **Search Term**: {search_query}\n" + \
|
| 178 |
f"- **Parameters**: {params}\n"
|
| 179 |
|
| 180 |
+
# Simple non-streaming version
|
| 181 |
+
def query_deepseek_no_stream(message, history, use_deep_research):
|
| 182 |
+
"""Non-streaming version that returns complete response"""
|
| 183 |
+
print(f"\n=== Starting query_deepseek_no_stream ===")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
print(f"Message: {message}")
|
| 185 |
print(f"History length: {len(history)}")
|
| 186 |
print(f"Deep Research: {use_deep_research}")
|
| 187 |
|
| 188 |
if not api_key:
|
| 189 |
error_msg = "Environment variable FW_API_KEY is not set. Please check the environment variables on the server."
|
| 190 |
+
return history + [(message, error_msg)], ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
search_context = ""
|
| 193 |
search_info = ""
|
| 194 |
|
|
|
|
|
|
|
|
|
|
| 195 |
if use_deep_research:
|
| 196 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
print(f"Deep Research activated: Starting search for '{message}'")
|
| 198 |
search_results = search_with_serphouse(message)
|
| 199 |
print(f"Search results received: {search_results[:100]}...")
|
|
|
|
| 212 |
except Exception as e:
|
| 213 |
print(f"Exception occurred during Deep Research: {str(e)}")
|
| 214 |
search_info = f"🔍 Deep Research feature error: {str(e)}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
# Prepare conversation history for API request
|
| 217 |
messages = []
|
| 218 |
+
for user_msg, assistant_msg in history:
|
| 219 |
+
if user_msg and assistant_msg: # Only add non-empty messages
|
| 220 |
+
messages.append({"role": "user", "content": user_msg})
|
| 221 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
# Add system message with search context if available
|
| 224 |
if search_context:
|
|
|
|
| 238 |
"frequency_penalty": 0,
|
| 239 |
"temperature": 0.6,
|
| 240 |
"messages": messages,
|
| 241 |
+
"stream": False # No streaming
|
| 242 |
}
|
| 243 |
headers = {
|
| 244 |
"Accept": "application/json",
|
|
|
|
| 246 |
"Authorization": f"Bearer {api_key}"
|
| 247 |
}
|
| 248 |
|
| 249 |
+
print(f"Calling DeepSeek API (no streaming)...")
|
| 250 |
print(f"Number of messages in conversation: {len(messages)}")
|
| 251 |
|
| 252 |
try:
|
| 253 |
+
# Add temporary message while waiting
|
| 254 |
+
temp_history = history + [(message, search_info + "⏳ Generating response...")]
|
| 255 |
+
|
| 256 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 257 |
response.raise_for_status()
|
| 258 |
|
| 259 |
print(f"API Response Status: {response.status_code}")
|
| 260 |
|
| 261 |
+
result = response.json()
|
| 262 |
+
full_response = search_info + result["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
print(f"Final response length: {len(full_response)}")
|
| 265 |
+
print(f"First 100 chars of response: {full_response[:100]}...")
|
| 266 |
+
|
| 267 |
+
# Return the complete response
|
| 268 |
+
return history + [(message, full_response)], ""
|
| 269 |
|
| 270 |
except requests.exceptions.RequestException as e:
|
| 271 |
error_msg = f"API error: {str(e)}"
|
|
|
|
| 273 |
error_msg = "Authentication failed. Please check your FW_API_KEY environment variable."
|
| 274 |
|
| 275 |
print(f"Request error: {error_msg}")
|
| 276 |
+
return history + [(message, search_info + error_msg)], ""
|
|
|
|
| 277 |
except Exception as e:
|
| 278 |
error_msg = f"Unexpected error: {str(e)}"
|
| 279 |
print(f"Unexpected error: {error_msg}")
|
| 280 |
import traceback
|
| 281 |
traceback.print_exc()
|
| 282 |
+
return history + [(message, search_info + error_msg)], ""
|
| 283 |
+
|
| 284 |
+
# Function to call DeepSeek API with streaming
|
| 285 |
+
def query_deepseek_streaming(message, history, use_deep_research):
|
| 286 |
+
print(f"\n=== Starting query_deepseek_streaming ===")
|
| 287 |
+
print(f"Message: {message}")
|
| 288 |
+
print(f"History length: {len(history)}")
|
| 289 |
+
print(f"Deep Research: {use_deep_research}")
|
| 290 |
+
|
| 291 |
+
# For now, use non-streaming version
|
| 292 |
+
return query_deepseek_no_stream(message, history, use_deep_research)
|
| 293 |
|
| 294 |
# Create Gradio interface
|
| 295 |
with gr.Blocks(theme="soft", fill_height=True) as demo:
|
|
|
|
| 309 |
chatbot = gr.Chatbot(
|
| 310 |
height=500,
|
| 311 |
show_label=False,
|
| 312 |
+
container=True
|
| 313 |
+
# Use default format
|
| 314 |
)
|
| 315 |
|
| 316 |
# Add Deep Research toggle and status display
|
|
|
|
| 348 |
# Clear conversation button
|
| 349 |
with gr.Row():
|
| 350 |
clear = gr.ClearButton([msg, chatbot], value="🧹 Clear Conversation")
|
|
|
|
| 351 |
|
| 352 |
# Example queries
|
| 353 |
gr.Examples(
|
|
|
|
| 362 |
# Error message display
|
| 363 |
error_box = gr.Markdown("")
|
| 364 |
|
| 365 |
+
# Function to handle submission
|
| 366 |
+
def handle_submit(message, history, use_deep_research):
|
| 367 |
+
if not message.strip():
|
| 368 |
+
return history, ""
|
| 369 |
+
return query_deepseek_no_stream(message, history, use_deep_research)
|
|
|
|
|
|
|
| 370 |
|
| 371 |
+
# Connect buttons to functions
|
| 372 |
submit.click(
|
| 373 |
+
handle_submit,
|
| 374 |
inputs=[msg, chatbot, use_deep_research],
|
| 375 |
+
outputs=[chatbot, error_box]
|
|
|
|
| 376 |
).then(
|
| 377 |
lambda: "",
|
| 378 |
None,
|
|
|
|
| 381 |
|
| 382 |
# Allow Enter key submission
|
| 383 |
msg.submit(
|
| 384 |
+
handle_submit,
|
| 385 |
inputs=[msg, chatbot, use_deep_research],
|
| 386 |
+
outputs=[chatbot, error_box]
|
|
|
|
| 387 |
).then(
|
| 388 |
lambda: "",
|
| 389 |
None,
|
|
|
|
| 394 |
|
| 395 |
# Run interface
|
| 396 |
if __name__ == "__main__":
|
| 397 |
+
print("Starting Deepseek Interface...")
|
| 398 |
demo = create_deepseek_interface()
|
| 399 |
+
print("Launching Gradio interface...")
|
| 400 |
demo.launch(
|
| 401 |
debug=True,
|
| 402 |
share=False,
|
| 403 |
server_name="0.0.0.0",
|
| 404 |
server_port=7860,
|
| 405 |
+
show_error=True
|
| 406 |
)
|