rdune71 commited on
Commit
2c26384
·
1 Parent(s): dac104e

Fix UI response display and extend session timeout

Browse files
Files changed (1) hide show
  1. app.py +82 -69
app.py CHANGED
@@ -269,7 +269,7 @@ def validate_user_input(text):
269
 
270
  return True, text.strip()
271
 
272
- # Chat input - COMPLETELY REWRITTEN VERSION
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
- # Process AI response
298
- with st.chat_message("assistant"):
299
- status_placeholder = st.empty()
300
- response_placeholder = st.empty()
 
 
 
 
 
 
 
 
 
 
 
 
301
 
302
  try:
303
- status_placeholder.info("🔄 Processing your request...")
304
-
305
- # Get conversation history from session
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
- try:
320
- # Use the OllamaProvider directly with proper configuration
321
- ollama_provider = OllamaProvider(st.session_state.selected_model)
322
- logger.info(f"Ollama provider created with model: {st.session_state.selected_model}")
323
- logger.info(f"Ollama host: {ollama_provider.host}")
324
-
325
- ai_response = ollama_provider.generate(clean_input, conversation_history)
326
- logger.info(f"Ollama response received: {type(ai_response)} - {str(ai_response)[:100] if ai_response else 'None'}")
 
 
327
 
328
- if ai_response and ai_response.strip():
329
- response_placeholder.markdown(ai_response)
330
- status_placeholder.success("✅ Response received!")
331
- else:
332
- status_placeholder.warning("⚠️ Empty response from Ollama")
333
- ai_response = "I received your message but couldn't generate a proper response. Please try again."
334
- response_placeholder.markdown(ai_response)
335
-
336
- except Exception as ollama_error:
337
- error_message = str(ollama_error)
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
- try:
347
- conversation = user_session.get("conversation", []).copy()
348
- conversation.append({"role": "user", "content": clean_input})
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
- # Add to message history
 
 
 
359
  st.session_state.messages.append({
360
  "role": "assistant",
361
  "content": str(ai_response),
362
  "timestamp": timestamp
363
  })
364
 
365
- except Exception as e:
366
- error_msg = f"System error: {str(e)}"
367
- logger.error(f"Chat processing error: {error_msg}")
368
- response_placeholder.error(error_msg)
369
- st.session_state.messages.append({
370
- "role": "assistant",
371
- "content": error_msg,
372
- "timestamp": timestamp
373
- })
374
- finally:
375
- st.session_state.is_processing = False
376
- st.experimental_rerun()
 
 
 
 
 
 
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()