rdune71 commited on
Commit
2262f42
·
1 Parent(s): 052fcc8

Fix UI response display issues, increase session timeout, and improve error handling

Browse files
Files changed (2) hide show
  1. app.py +21 -22
  2. core/session.py +2 -2
app.py CHANGED
@@ -265,7 +265,7 @@ if user_input and not st.session_state.is_processing:
265
  else:
266
  st.session_state.is_processing = True
267
 
268
- # Display user message
269
  with st.chat_message("user"):
270
  st.markdown(validated_input)
271
 
@@ -430,42 +430,40 @@ if user_input and not st.session_state.is_processing:
430
  user_session["conversation"] = conversation
431
  session_manager.update_session("default_user", user_session)
432
 
433
- # Add to message history - ensure proper format
434
- st.session_state.messages.append({
435
  "role": "assistant",
436
  "content": ai_response,
437
  "timestamp": datetime.now().strftime("%H:%M:%S")
438
- })
 
 
439
 
440
- # Add feedback buttons
441
- st.divider()
442
- col1, col2 = st.columns(2)
443
- with col1:
444
- if st.button("👍 Helpful", key=f"helpful_{len(st.session_state.messages)}"):
445
- st.success("Thanks for your feedback!")
446
- with col2:
447
- if st.button("👎 Not Helpful", key=f"not_helpful_{len(st.session_state.messages)}"):
448
- st.success("Thanks for your feedback!")
449
  else:
 
450
  st.session_state.messages.append({
451
  "role": "assistant",
452
- "content": "Sorry, I couldn't process your request. Please try again.",
453
  "timestamp": datetime.now().strftime("%H:%M:%S")
454
  })
 
 
455
 
456
  except Exception as e:
457
  user_msg = translate_error(e)
458
  response_placeholder.error(f"⚠️ {user_msg}")
459
  st.session_state.messages.append({
460
- "role": "assistant",
461
  "content": f"⚠️ {user_msg}",
462
  "timestamp": datetime.now().strftime("%H:%M:%S")
463
  })
464
-
465
- # Moved finally block to proper location
466
- st.session_state.is_processing = False
467
- time.sleep(0.5) # Brief pause
468
- st.experimental_rerun()
469
 
470
  # Add evaluation dashboard tab (separate from chat interface) - ONLY ABOUT TAB NOW
471
  st.divider()
@@ -500,8 +498,8 @@ with tab1:
500
  - Performance metrics and analytics
501
  """)
502
 
503
- # Add special command handling
504
- if user_input and user_input.lower().strip() in ["hello", "hi", "hey"]:
505
  with st.chat_message("assistant"):
506
  story = personality.get_space_story()
507
  st.markdown(f"### 🐱 Cosmic Kitten Story:\n\n{story}")
@@ -511,3 +509,4 @@ if user_input and user_input.lower().strip() in ["hello", "hi", "hey"]:
511
  "source": "space_story",
512
  "timestamp": datetime.now().strftime("%H:%M:%S")
513
  })
 
 
265
  else:
266
  st.session_state.is_processing = True
267
 
268
+ # Display user message immediately
269
  with st.chat_message("user"):
270
  st.markdown(validated_input)
271
 
 
430
  user_session["conversation"] = conversation
431
  session_manager.update_session("default_user", user_session)
432
 
433
+ # Add to message history with proper display
434
+ message_data = {
435
  "role": "assistant",
436
  "content": ai_response,
437
  "timestamp": datetime.now().strftime("%H:%M:%S")
438
+ }
439
+
440
+ st.session_state.messages.append(message_data)
441
 
442
+ # Force display update
443
+ with st.chat_message("assistant"):
444
+ st.markdown(ai_response)
 
 
 
 
 
 
445
  else:
446
+ error_msg = "Sorry, I couldn't process your request. Please try again."
447
  st.session_state.messages.append({
448
  "role": "assistant",
449
+ "content": error_msg,
450
  "timestamp": datetime.now().strftime("%H:%M:%S")
451
  })
452
+ with st.chat_message("assistant"):
453
+ st.markdown(error_msg)
454
 
455
  except Exception as e:
456
  user_msg = translate_error(e)
457
  response_placeholder.error(f"⚠️ {user_msg}")
458
  st.session_state.messages.append({
459
+ "role": "assistant",
460
  "content": f"⚠️ {user_msg}",
461
  "timestamp": datetime.now().strftime("%H:%M:%S")
462
  })
463
+ finally:
464
+ st.session_state.is_processing = False
465
+ # Force UI update
466
+ st.experimental_rerun()
 
467
 
468
  # Add evaluation dashboard tab (separate from chat interface) - ONLY ABOUT TAB NOW
469
  st.divider()
 
498
  - Performance metrics and analytics
499
  """)
500
 
501
+ # Add special command handling for stories
502
+ if user_input and user_input.lower().strip() in ["tell me a story", "tell me a cosmic cat story", "story", "cosmic story"]:
503
  with st.chat_message("assistant"):
504
  story = personality.get_space_story()
505
  st.markdown(f"### 🐱 Cosmic Kitten Story:\n\n{story}")
 
509
  "source": "space_story",
510
  "timestamp": datetime.now().strftime("%H:%M:%S")
511
  })
512
+ st.experimental_rerun() # Add this to refresh the UI
core/session.py CHANGED
@@ -12,10 +12,10 @@ logger = logging.getLogger(__name__)
12
  class SessionManager:
13
  """Manages user sessions and conversation context with optimized operations"""
14
 
15
- def __init__(self, session_timeout: int = 3600):
16
  """Initialize session manager
17
  Args:
18
- session_timeout: Session timeout in seconds (default: 1 hour)
19
  """
20
  self.session_timeout = session_timeout
21
 
 
12
  class SessionManager:
13
  """Manages user sessions and conversation context with optimized operations"""
14
 
15
+ def __init__(self, session_timeout: int = 7200): # Increased from 3600 to 7200 (2 hours)
16
  """Initialize session manager
17
  Args:
18
+ session_timeout: Session timeout in seconds (default: 2 hours)
19
  """
20
  self.session_timeout = session_timeout
21