Fix UI response display and extend session timeout
Browse files
app.py
CHANGED
|
@@ -269,7 +269,7 @@ def validate_user_input(text):
|
|
| 269 |
|
| 270 |
return True, text.strip()
|
| 271 |
|
| 272 |
-
# Chat input -
|
| 273 |
user_input = st.chat_input("Type your message here...", disabled=st.session_state.is_processing)
|
| 274 |
|
| 275 |
# Process message when received
|
|
@@ -294,86 +294,82 @@ if user_input and not st.session_state.is_processing:
|
|
| 294 |
"timestamp": timestamp
|
| 295 |
})
|
| 296 |
|
| 297 |
-
#
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
try:
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
user_session = session_manager.get_session("default_user")
|
| 307 |
-
conversation_history = user_session.get("conversation", []).copy()
|
| 308 |
-
|
| 309 |
-
# Log conversation history for debugging
|
| 310 |
-
logger.info(f"Conversation history length: {len(conversation_history)}")
|
| 311 |
-
|
| 312 |
-
# Add the current user message to history for context
|
| 313 |
-
conversation_history.append({"role": "user", "content": clean_input})
|
| 314 |
-
|
| 315 |
-
# Try Ollama first
|
| 316 |
-
status_placeholder.info("🦙 Contacting Ollama...")
|
| 317 |
-
logger.info("Attempting Ollama connection...")
|
| 318 |
|
| 319 |
-
|
| 320 |
-
#
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
|
|
|
|
|
|
| 327 |
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
logger.error(f"Ollama error: {error_message}")
|
| 339 |
-
status_placeholder.error(f"❌ Ollama error: {error_message[:100]}...")
|
| 340 |
-
ai_response = f"Error contacting Ollama: {error_message[:100]}..."
|
| 341 |
-
response_placeholder.error(ai_response)
|
| 342 |
-
|
| 343 |
-
# Save response to session and message history
|
| 344 |
-
if ai_response:
|
| 345 |
# Update conversation history in session
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
conversation.append({"role": "assistant", "content": str(ai_response)})
|
| 350 |
-
|
| 351 |
-
# Update session with new conversation
|
| 352 |
-
update_result = session_manager.update_session("default_user", {"conversation": conversation})
|
| 353 |
-
logger.info(f"Session update result: {update_result}")
|
| 354 |
-
|
| 355 |
-
except Exception as session_error:
|
| 356 |
-
logger.error(f"Session update error: {session_error}")
|
| 357 |
|
| 358 |
-
#
|
|
|
|
|
|
|
|
|
|
| 359 |
st.session_state.messages.append({
|
| 360 |
"role": "assistant",
|
| 361 |
"content": str(ai_response),
|
| 362 |
"timestamp": timestamp
|
| 363 |
})
|
| 364 |
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
# Add evaluation dashboard tab (separate from chat interface) - ONLY ABOUT TAB NOW
|
| 379 |
st.divider()
|
|
@@ -420,3 +416,20 @@ if user_input and user_input.lower().strip() in ["tell me a story", "tell me a c
|
|
| 420 |
})
|
| 421 |
st.session_state.is_processing = False
|
| 422 |
st.experimental_rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
return True, text.strip()
|
| 271 |
|
| 272 |
+
# Chat input - FIXED RESPONSE DISPLAY VERSION
|
| 273 |
user_input = st.chat_input("Type your message here...", disabled=st.session_state.is_processing)
|
| 274 |
|
| 275 |
# Process message when received
|
|
|
|
| 294 |
"timestamp": timestamp
|
| 295 |
})
|
| 296 |
|
| 297 |
+
# Show processing status
|
| 298 |
+
status_container = st.empty()
|
| 299 |
+
response_container = st.empty()
|
| 300 |
+
|
| 301 |
+
try:
|
| 302 |
+
status_container.info("🔄 Processing your request...")
|
| 303 |
+
|
| 304 |
+
# Get conversation history from session
|
| 305 |
+
user_session = session_manager.get_session("default_user")
|
| 306 |
+
conversation_history = user_session.get("conversation", []).copy()
|
| 307 |
+
|
| 308 |
+
# Add the current user message to history for context
|
| 309 |
+
conversation_history.append({"role": "user", "content": clean_input})
|
| 310 |
+
|
| 311 |
+
# Try Ollama first
|
| 312 |
+
status_container.info("🦙 Contacting Ollama...")
|
| 313 |
|
| 314 |
try:
|
| 315 |
+
# Use the OllamaProvider directly with proper configuration
|
| 316 |
+
ollama_provider = OllamaProvider(st.session_state.selected_model)
|
| 317 |
+
ai_response = ollama_provider.generate(clean_input, conversation_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
+
if ai_response and ai_response.strip():
|
| 320 |
+
# DISPLAY THE RESPONSE IN THE CONTAINER (not using placeholders)
|
| 321 |
+
with st.chat_message("assistant"):
|
| 322 |
+
st.markdown(ai_response)
|
| 323 |
+
status_container.success("✅ Response received!")
|
| 324 |
+
else:
|
| 325 |
+
# DISPLAY ERROR RESPONSE
|
| 326 |
+
with st.chat_message("assistant"):
|
| 327 |
+
st.warning("⚠️ Received empty response from Ollama")
|
| 328 |
+
ai_response = "I received your message but couldn't generate a proper response."
|
| 329 |
|
| 330 |
+
except Exception as ollama_error:
|
| 331 |
+
error_message = str(ollama_error)
|
| 332 |
+
# DISPLAY ERROR
|
| 333 |
+
with st.chat_message("assistant"):
|
| 334 |
+
st.error(f"❌ Ollama error: {error_message[:100]}...")
|
| 335 |
+
ai_response = f"Error: {error_message[:100]}..."
|
| 336 |
+
|
| 337 |
+
# Save response to session and message history
|
| 338 |
+
if ai_response:
|
| 339 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
# Update conversation history in session
|
| 341 |
+
conversation = user_session.get("conversation", []).copy()
|
| 342 |
+
conversation.append({"role": "user", "content": clean_input})
|
| 343 |
+
conversation.append({"role": "assistant", "content": str(ai_response)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
+
# Update session with new conversation
|
| 346 |
+
update_result = session_manager.update_session("default_user", {"conversation": conversation})
|
| 347 |
+
|
| 348 |
+
# Add to message history (this was missing!)
|
| 349 |
st.session_state.messages.append({
|
| 350 |
"role": "assistant",
|
| 351 |
"content": str(ai_response),
|
| 352 |
"timestamp": timestamp
|
| 353 |
})
|
| 354 |
|
| 355 |
+
except Exception as session_error:
|
| 356 |
+
logger.error(f"Session update error: {session_error}")
|
| 357 |
+
|
| 358 |
+
except Exception as e:
|
| 359 |
+
error_msg = f"System error: {str(e)}"
|
| 360 |
+
logger.error(f"Chat processing error: {error_msg}")
|
| 361 |
+
# DISPLAY SYSTEM ERROR
|
| 362 |
+
with st.chat_message("assistant"):
|
| 363 |
+
st.error(error_msg)
|
| 364 |
+
st.session_state.messages.append({
|
| 365 |
+
"role": "assistant",
|
| 366 |
+
"content": error_msg,
|
| 367 |
+
"timestamp": timestamp
|
| 368 |
+
})
|
| 369 |
+
finally:
|
| 370 |
+
st.session_state.is_processing = False
|
| 371 |
+
time.sleep(0.1) # Small delay to ensure UI updates
|
| 372 |
+
st.experimental_rerun()
|
| 373 |
|
| 374 |
# Add evaluation dashboard tab (separate from chat interface) - ONLY ABOUT TAB NOW
|
| 375 |
st.divider()
|
|
|
|
| 416 |
})
|
| 417 |
st.session_state.is_processing = False
|
| 418 |
st.experimental_rerun()
|
| 419 |
+
|
| 420 |
+
# Simple test to verify the fix works
|
| 421 |
+
def test_response_display():
|
| 422 |
+
"""Test function to verify response display works"""
|
| 423 |
+
test_response = "This is a test response to verify the display fix is working correctly."
|
| 424 |
+
with st.chat_message("assistant"):
|
| 425 |
+
st.markdown(test_response)
|
| 426 |
+
st.session_state.messages.append({
|
| 427 |
+
"role": "assistant",
|
| 428 |
+
"content": test_response,
|
| 429 |
+
"timestamp": datetime.now().strftime("%H:%M:%S")
|
| 430 |
+
})
|
| 431 |
+
|
| 432 |
+
# Add a test button in sidebar:
|
| 433 |
+
with st.sidebar:
|
| 434 |
+
if st.button("Test Response Display"):
|
| 435 |
+
test_response_display()
|