rdune71 commited on
Commit
0b2b7e6
Β·
1 Parent(s): b40bdef

Fix chat response issues: st.experimental_rerun(), remove nested chat messages, increase session timeout

Browse files
Files changed (1) hide show
  1. app.py +160 -164
app.py CHANGED
@@ -257,6 +257,7 @@ if user_input and not st.session_state.is_processing:
257
  if not is_valid:
258
  st.error(validated_input)
259
  st.session_state.is_processing = False
 
260
  else:
261
  st.session_state.is_processing = True
262
 
@@ -272,187 +273,182 @@ if user_input and not st.session_state.is_processing:
272
  })
273
 
274
  # Process AI response
275
- with st.chat_message("assistant"):
276
- status_placeholder = st.empty()
277
- response_placeholder = st.empty()
 
 
 
 
 
278
 
279
- try:
280
- # Get conversation history from session
281
- user_session = session_manager.get_session("default_user")
282
- conversation_history = user_session.get("conversation", []).copy()
283
-
284
- # Add the current user message to history for context
285
- conversation_history.append({"role": "user", "content": validated_input})
286
 
287
- # Check if cosmic mode is enabled
288
- if st.session_state.cosmic_mode:
289
- # Process cosmic cascade response
290
- status_placeholder.info("🐱 Cosmic Kitten Responding...")
 
291
 
292
- try:
293
- # Get conversation history
294
- user_session = session_manager.get_session("default_user")
295
- conversation_history = user_session.get("conversation", []).copy()
296
- conversation_history.append({"role": "user", "content": validated_input})
297
-
298
- # Stage 1: Local Ollama Response
299
- ollama_provider = OllamaProvider(st.session_state.selected_model)
300
- local_response = ollama_provider.generate(validated_input, conversation_history)
301
-
302
- if local_response:
303
- # Display response
304
- st.markdown(f"### 🐱 Cosmic Kitten Says:\n{local_response}")
305
- st.session_state.messages.append({
306
- "role": "assistant",
307
- "content": local_response,
308
- "source": "local_kitty",
309
- "timestamp": datetime.now().strftime("%H:%M:%S")
310
- })
311
-
312
- # Stage 2: HF Endpoint Analysis
313
- status_placeholder.info("πŸ›°οΈ Beaming Query to Orbital Station...")
314
- if config.hf_token:
315
- # Check HF status first
316
- hf_status = hf_monitor.check_endpoint_status()
317
- if not hf_status['available']:
318
- status_placeholder.info(personality.get_initializing_message())
319
-
320
- hf_provider = HuggingFaceProvider("meta-llama/Llama-2-7b-chat-hf")
321
- hf_response = hf_provider.generate(validated_input, conversation_history)
322
-
323
- if hf_response:
324
- # Display response
325
- st.markdown(f"### πŸ›°οΈ Orbital Station Reports:\n{hf_response}")
326
- st.session_state.messages.append({
327
- "role": "assistant",
328
- "content": hf_response,
329
- "source": "orbital_station",
330
- "timestamp": datetime.now().strftime("%H:%M:%S")
331
- })
332
-
333
- # Stage 3: Local Synthesis
334
- status_placeholder.info("🐱 Cosmic Kitten Synthesizing Wisdom...")
335
-
336
- # Update history with both responses
337
- synthesis_history = conversation_history.copy()
338
- synthesis_history.extend([
339
- {"role": "assistant", "content": local_response},
340
- {"role": "assistant", "content": hf_response, "source": "cloud"}
341
- ])
342
 
343
- synthesis = ollama_provider.generate(
344
- f"Synthesize these two perspectives:\n1. Local: {local_response}\n2. Cloud: {hf_response}",
345
- synthesis_history
346
- )
347
 
348
- if synthesis:
349
- # Display response
350
- st.markdown(f"### 🌟 Final Cosmic Summary:\n{synthesis}")
351
  st.session_state.messages.append({
352
  "role": "assistant",
353
- "content": synthesis,
354
- "source": "cosmic_summary",
355
  "timestamp": datetime.now().strftime("%H:%M:%S")
356
  })
357
-
358
- status_placeholder.success("✨ Cosmic Cascade Complete!")
359
-
360
- except Exception as e:
361
- error_msg = f"🌌 Cosmic disturbance: {str(e)}"
362
- st.error(error_msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
363
  st.session_state.messages.append({
364
  "role": "assistant",
365
- "content": error_msg,
366
- "source": "error",
367
  "timestamp": datetime.now().strftime("%H:%M:%S")
368
  })
369
- else:
370
- # Traditional processing
371
- # Try Ollama first
372
- status_placeholder.info("πŸ¦™ Contacting Ollama...")
373
- ai_response = None
374
 
375
- try:
376
- # Use the OllamaProvider directly
377
- ollama_provider = OllamaProvider(st.session_state.selected_model)
378
- ai_response = ollama_provider.generate(validated_input, conversation_history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
379
 
380
- if ai_response:
381
- st.markdown(ai_response) # Use st.markdown instead of response_placeholder
382
- status_placeholder.success("βœ… Response received!")
383
- else:
384
- status_placeholder.warning("⚠️ Empty response from Ollama")
385
-
386
- except Exception as ollama_error:
387
- user_msg = translate_error(ollama_error)
388
- status_placeholder.error(f"⚠️ {user_msg}")
389
 
390
- # Fallback to HF if available
391
- if config.hf_token and not ai_response:
392
- status_placeholder.info("⚑ Initializing HF Endpoint (2–4 minutes)...")
393
-
394
- try:
395
- # Check HF status first
396
- hf_status = hf_monitor.check_endpoint_status()
397
- if not hf_status['available']:
398
- status_placeholder.info(personality.get_initializing_message())
399
-
400
- # Use the HuggingFaceProvider directly
401
- hf_provider = HuggingFaceProvider("meta-llama/Llama-2-7b-chat-hf")
402
- ai_response = hf_provider.generate(validated_input, conversation_history)
403
 
404
- if ai_response:
405
- st.markdown(ai_response) # Use st.markdown instead of response_placeholder
406
- status_placeholder.success("βœ… HF response received!")
407
- else:
408
- status_placeholder.error("❌ No response from HF")
409
-
410
- except Exception as hf_error:
411
- user_msg = translate_error(hf_error)
412
- status_placeholder.error(f"⚠️ {user_msg}")
413
 
414
- # Save response if successful
415
- if ai_response:
416
- # Update conversation history
417
- conversation = user_session.get("conversation", [])
418
- conversation.append({"role": "user", "content": validated_input})
419
- conversation.append({"role": "assistant", "content": ai_response})
420
- user_session["conversation"] = conversation
421
- session_manager.update_session("default_user", user_session)
422
-
423
- # Add to message history with proper display
424
- message_data = {
425
- "role": "assistant",
426
- "content": ai_response,
427
- "timestamp": datetime.now().strftime("%H:%M:%S")
428
- }
429
-
430
- st.session_state.messages.append(message_data)
431
-
432
- # Force display update
433
- st.markdown(ai_response)
434
- else:
435
- error_msg = "Sorry, I couldn't process your request. Please try again."
436
- st.session_state.messages.append({
437
- "role": "assistant",
438
- "content": error_msg,
439
- "timestamp": datetime.now().strftime("%H:%M:%S")
440
- })
441
- st.markdown(error_msg)
442
-
443
- except Exception as e:
444
- user_msg = translate_error(e)
445
- error_message = f"⚠️ {user_msg}"
446
- st.error(error_message) # Use st.error instead of response_placeholder.error
447
- st.session_state.messages.append({
448
- "role": "assistant",
449
- "content": error_message,
450
- "timestamp": datetime.now().strftime("%H:%M:%S")
451
- })
452
- finally:
453
- st.session_state.is_processing = False
454
- # Force UI update
455
- st.rerun() # Changed from experimental_rerun() to rerun()
456
 
457
  # Add evaluation dashboard tab (separate from chat interface) - ONLY ABOUT TAB NOW
458
  st.divider()
@@ -498,4 +494,4 @@ if user_input and user_input.lower().strip() in ["tell me a story", "tell me a c
498
  "timestamp": datetime.now().strftime("%H:%M:%S")
499
  })
500
  st.session_state.is_processing = False
501
- st.rerun()
 
257
  if not is_valid:
258
  st.error(validated_input)
259
  st.session_state.is_processing = False
260
+ st.experimental_rerun() # Fixed: use experimental_rerun
261
  else:
262
  st.session_state.is_processing = True
263
 
 
273
  })
274
 
275
  # Process AI response
276
+ response_container = st.empty()
277
+ status_placeholder = st.empty()
278
+ response_placeholder = st.empty()
279
+
280
+ try:
281
+ # Get conversation history from session
282
+ user_session = session_manager.get_session("default_user")
283
+ conversation_history = user_session.get("conversation", []).copy()
284
 
285
+ # Add the current user message to history for context
286
+ conversation_history.append({"role": "user", "content": validated_input})
287
+
288
+ # Check if cosmic mode is enabled
289
+ if st.session_state.cosmic_mode:
290
+ # Process cosmic cascade response
291
+ status_placeholder.info("🐱 Cosmic Kitten Responding...")
292
 
293
+ try:
294
+ # Get conversation history
295
+ user_session = session_manager.get_session("default_user")
296
+ conversation_history = user_session.get("conversation", []).copy()
297
+ conversation_history.append({"role": "user", "content": validated_input})
298
 
299
+ # Stage 1: Local Ollama Response
300
+ ollama_provider = OllamaProvider(st.session_state.selected_model)
301
+ local_response = ollama_provider.generate(validated_input, conversation_history)
302
+
303
+ if local_response:
304
+ # Display response (no nested st.chat_message)
305
+ st.markdown(f"### 🐱 Cosmic Kitten Says:\n{local_response}")
306
+ st.session_state.messages.append({
307
+ "role": "assistant",
308
+ "content": local_response,
309
+ "source": "local_kitty",
310
+ "timestamp": datetime.now().strftime("%H:%M:%S")
311
+ })
312
+
313
+ # Stage 2: HF Endpoint Analysis
314
+ status_placeholder.info("πŸ›°οΈ Beaming Query to Orbital Station...")
315
+ if config.hf_token:
316
+ # Check HF status first
317
+ hf_status = hf_monitor.check_endpoint_status()
318
+ if not hf_status['available']:
319
+ status_placeholder.info(personality.get_initializing_message())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
 
321
+ hf_provider = HuggingFaceProvider("meta-llama/Llama-2-7b-chat-hf")
322
+ hf_response = hf_provider.generate(validated_input, conversation_history)
 
 
323
 
324
+ if hf_response:
325
+ # Display response (no nested st.chat_message)
326
+ st.markdown(f"### πŸ›°οΈ Orbital Station Reports:\n{hf_response}")
327
  st.session_state.messages.append({
328
  "role": "assistant",
329
+ "content": hf_response,
330
+ "source": "orbital_station",
331
  "timestamp": datetime.now().strftime("%H:%M:%S")
332
  })
333
+
334
+ # Stage 3: Local Synthesis
335
+ status_placeholder.info("🐱 Cosmic Kitten Synthesizing Wisdom...")
336
+
337
+ # Update history with both responses
338
+ synthesis_history = conversation_history.copy()
339
+ synthesis_history.extend([
340
+ {"role": "assistant", "content": local_response},
341
+ {"role": "assistant", "content": hf_response, "source": "cloud"}
342
+ ])
343
+
344
+ synthesis = ollama_provider.generate(
345
+ f"Synthesize these two perspectives:\n1. Local: {local_response}\n2. Cloud: {hf_response}",
346
+ synthesis_history
347
+ )
348
+
349
+ if synthesis:
350
+ # Display response (no nested st.chat_message)
351
+ st.markdown(f"### 🌟 Final Cosmic Summary:\n{synthesis}")
352
  st.session_state.messages.append({
353
  "role": "assistant",
354
+ "content": synthesis,
355
+ "source": "cosmic_summary",
356
  "timestamp": datetime.now().strftime("%H:%M:%S")
357
  })
 
 
 
 
 
358
 
359
+ status_placeholder.success("✨ Cosmic Cascade Complete!")
360
+
361
+ except Exception as e:
362
+ error_msg = f"🌌 Cosmic disturbance: {str(e)}"
363
+ st.error(error_msg)
364
+ st.session_state.messages.append({
365
+ "role": "assistant",
366
+ "content": error_msg,
367
+ "source": "error",
368
+ "timestamp": datetime.now().strftime("%H:%M:%S")
369
+ })
370
+ else:
371
+ # Traditional processing
372
+ # Try Ollama first
373
+ status_placeholder.info("πŸ¦™ Contacting Ollama...")
374
+ ai_response = None
375
+
376
+ try:
377
+ # Use the OllamaProvider directly with proper configuration
378
+ ollama_provider = OllamaProvider(st.session_state.selected_model)
379
+ ai_response = ollama_provider.generate(validated_input, conversation_history)
380
+
381
+ if ai_response:
382
+ st.markdown(ai_response) # Use st.markdown instead of response_placeholder
383
+ status_placeholder.success("βœ… Response received!")
384
+ else:
385
+ status_placeholder.warning("⚠️ Empty response from Ollama")
386
 
387
+ except Exception as ollama_error:
388
+ error_message = str(ollama_error)
389
+ status_placeholder.error(f"❌ Ollama error: {error_message[:100]}...")
390
+ logger.error(f"Ollama error: {error_message}")
391
+
392
+ # Fallback to HF if available
393
+ if config.hf_token and not ai_response:
394
+ status_placeholder.info("⚑ Initializing HF Endpoint (2–4 minutes)...")
 
395
 
396
+ try:
397
+ # Check HF status first
398
+ hf_status = hf_monitor.check_endpoint_status()
399
+ if not hf_status['available']:
400
+ status_placeholder.info(personality.get_initializing_message())
 
 
 
 
 
 
 
 
401
 
402
+ # Use the HuggingFaceProvider directly
403
+ hf_provider = HuggingFaceProvider("meta-llama/Llama-2-7b-chat-hf")
404
+ ai_response = hf_provider.generate(validated_input, conversation_history)
405
+
406
+ if ai_response:
407
+ st.markdown(ai_response) # Use st.markdown instead of response_placeholder
408
+ status_placeholder.success("βœ… HF response received!")
409
+ else:
410
+ status_placeholder.error("❌ No response from HF")
411
 
412
+ except Exception as hf_error:
413
+ error_message = str(hf_error)
414
+ status_placeholder.error(f"❌ HF also failed: {error_message[:100]}...")
415
+ logger.error(f"HF error: {error_message}")
416
+
417
+ # Save response if successful
418
+ if ai_response:
419
+ # Update conversation history in session
420
+ conversation = user_session.get("conversation", []).copy()
421
+ conversation.append({"role": "user", "content": validated_input})
422
+ conversation.append({"role": "assistant", "content": ai_response})
423
+ session_manager.update_session("default_user", {"conversation": conversation})
424
+
425
+ # Add to message history
426
+ st.session_state.messages.append({
427
+ "role": "assistant",
428
+ "content": ai_response,
429
+ "timestamp": datetime.now().strftime("%H:%M:%S")
430
+ })
431
+ else:
432
+ error_msg = "Sorry, I couldn't process your request. Please try again."
433
+ st.session_state.messages.append({
434
+ "role": "assistant",
435
+ "content": error_msg,
436
+ "timestamp": datetime.now().strftime("%H:%M:%S")
437
+ })
438
+ st.markdown(error_msg)
439
+
440
+ except Exception as e:
441
+ error_msg = f"System error: {str(e)}"
442
+ logger.error(f"Chat processing error: {error_msg}")
443
+ st.error(error_msg)
444
+ st.session_state.messages.append({
445
+ "role": "assistant",
446
+ "content": error_msg,
447
+ "timestamp": datetime.now().strftime("%H:%M:%S")
448
+ })
449
+ finally:
450
+ st.session_state.is_processing = False
451
+ st.experimental_rerun() # Fixed: use experimental_rerun
 
 
452
 
453
  # Add evaluation dashboard tab (separate from chat interface) - ONLY ABOUT TAB NOW
454
  st.divider()
 
494
  "timestamp": datetime.now().strftime("%H:%M:%S")
495
  })
496
  st.session_state.is_processing = False
497
+ st.experimental_rerun()