mashrur950 commited on
Commit
f8d1806
·
1 Parent(s): a9b6d71

Update Gradio version and refactor chat message handling to use session IDs

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -1
  2. ui/app.py +47 -20
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
  # Core Framework
2
- gradio==5.12.0
 
3
  fastmcp>=0.3.0
4
 
5
  # AI/ML
 
1
  # Core Framework
2
+ gradio==4.44.0
3
+ pydantic>=2.11.0,<2.12.0 # Pin pydantic to compatible version
4
  fastmcp>=0.3.0
5
 
6
  # AI/ML
ui/app.py CHANGED
@@ -18,6 +18,10 @@ import json
18
  from chat.chat_engine import ChatEngine
19
  from chat.conversation import ConversationManager
20
  from chat.geocoding import GeocodingService
 
 
 
 
21
 
22
  # ============================================
23
  # DATABASE FUNCTIONS
@@ -220,49 +224,71 @@ def get_api_status():
220
  """
221
 
222
 
223
- def handle_chat_message(message, conversation_state):
224
  """
225
  Handle chat message from user
226
 
227
  Args:
228
  message: User's message
229
- conversation_state: ConversationManager instance
230
 
231
  Returns:
232
- Updated chatbot history, tool display, conversation state
233
  """
 
 
 
 
 
 
 
 
234
  if not message.strip():
235
- return conversation_state.get_formatted_history(), conversation_state.get_tool_calls(), conversation_state
236
 
237
  # Process message through chat engine
238
- response, tool_calls = chat_engine.process_message(message, conversation_state)
239
 
240
  # Return updated UI
241
- return conversation_state.get_formatted_history(), conversation_state.get_tool_calls(), conversation_state
242
 
243
 
244
- def reset_conversation():
245
  """Reset conversation to start fresh"""
 
 
 
 
246
  new_conversation = ConversationManager()
247
 
248
  # Add welcome message
249
  welcome = chat_engine.get_welcome_message()
250
  new_conversation.add_message("assistant", welcome)
251
 
 
 
 
252
  return (
253
  new_conversation.get_formatted_history(),
254
  [], # Clear tool calls
255
- new_conversation
256
  )
257
 
258
 
259
  def get_initial_chat():
260
  """Get initial chat state with welcome message"""
 
 
 
 
261
  conversation = ConversationManager()
262
  welcome = chat_engine.get_welcome_message()
263
  conversation.add_message("assistant", welcome)
264
 
265
- return conversation.get_formatted_history(), [], conversation
 
 
 
266
 
267
 
268
  # ============================================
@@ -433,33 +459,34 @@ def create_interface():
433
  gr.Markdown("See what tools the AI is using behind the scenes:")
434
  tool_display = gr.JSON(label="Tools Called")
435
 
436
- # Conversation state
437
- conversation_state = gr.State(value=None)
438
 
439
  # Initialize with welcome message
440
- chatbot.value, tool_display.value, conversation_state.value = get_initial_chat()
441
 
442
  # Event handlers
443
- def send_message(message, conv_state):
444
  """Handle send button click"""
445
- chat_history, tools, new_state = handle_chat_message(message, conv_state)
446
- return chat_history, tools, new_state, "" # Clear input
447
 
448
  send_btn.click(
449
  fn=send_message,
450
- inputs=[msg_input, conversation_state],
451
- outputs=[chatbot, tool_display, conversation_state, msg_input]
452
  )
453
 
454
  msg_input.submit(
455
  fn=send_message,
456
- inputs=[msg_input, conversation_state],
457
- outputs=[chatbot, tool_display, conversation_state, msg_input]
458
  )
459
 
460
  clear_btn.click(
461
  fn=reset_conversation,
462
- outputs=[chatbot, tool_display, conversation_state]
 
463
  )
464
 
465
  # ==========================================
 
18
  from chat.chat_engine import ChatEngine
19
  from chat.conversation import ConversationManager
20
  from chat.geocoding import GeocodingService
21
+ import uuid
22
+
23
+ # Global session storage (simple in-memory store)
24
+ SESSIONS = {}
25
 
26
  # ============================================
27
  # DATABASE FUNCTIONS
 
224
  """
225
 
226
 
227
+ def handle_chat_message(message, session_id):
228
  """
229
  Handle chat message from user
230
 
231
  Args:
232
  message: User's message
233
+ session_id: Session identifier string
234
 
235
  Returns:
236
+ Updated chatbot history, tool display, session_id
237
  """
238
+ # Get or create conversation for this session
239
+ if session_id not in SESSIONS:
240
+ SESSIONS[session_id] = ConversationManager()
241
+ welcome = chat_engine.get_welcome_message()
242
+ SESSIONS[session_id].add_message("assistant", welcome)
243
+
244
+ conversation = SESSIONS[session_id]
245
+
246
  if not message.strip():
247
+ return conversation.get_formatted_history(), conversation.get_tool_calls(), session_id
248
 
249
  # Process message through chat engine
250
+ response, tool_calls = chat_engine.process_message(message, conversation)
251
 
252
  # Return updated UI
253
+ return conversation.get_formatted_history(), conversation.get_tool_calls(), session_id
254
 
255
 
256
+ def reset_conversation(session_id):
257
  """Reset conversation to start fresh"""
258
+ # Create new session ID for fresh conversation
259
+ new_session_id = str(uuid.uuid4())
260
+
261
+ # Create new conversation for this session
262
  new_conversation = ConversationManager()
263
 
264
  # Add welcome message
265
  welcome = chat_engine.get_welcome_message()
266
  new_conversation.add_message("assistant", welcome)
267
 
268
+ # Store in sessions
269
+ SESSIONS[new_session_id] = new_conversation
270
+
271
  return (
272
  new_conversation.get_formatted_history(),
273
  [], # Clear tool calls
274
+ new_session_id
275
  )
276
 
277
 
278
  def get_initial_chat():
279
  """Get initial chat state with welcome message"""
280
+ # Create new session ID
281
+ session_id = str(uuid.uuid4())
282
+
283
+ # Create conversation for this session
284
  conversation = ConversationManager()
285
  welcome = chat_engine.get_welcome_message()
286
  conversation.add_message("assistant", welcome)
287
 
288
+ # Store in sessions
289
+ SESSIONS[session_id] = conversation
290
+
291
+ return conversation.get_formatted_history(), [], session_id
292
 
293
 
294
  # ============================================
 
459
  gr.Markdown("See what tools the AI is using behind the scenes:")
460
  tool_display = gr.JSON(label="Tools Called")
461
 
462
+ # Conversation state - use session ID instead of complex object
463
+ session_id_state = gr.State(value=None)
464
 
465
  # Initialize with welcome message
466
+ chatbot.value, tool_display.value, session_id_state.value = get_initial_chat()
467
 
468
  # Event handlers
469
+ def send_message(message, sess_id):
470
  """Handle send button click"""
471
+ chat_history, tools, new_sess_id = handle_chat_message(message, sess_id)
472
+ return chat_history, tools, new_sess_id, "" # Clear input
473
 
474
  send_btn.click(
475
  fn=send_message,
476
+ inputs=[msg_input, session_id_state],
477
+ outputs=[chatbot, tool_display, session_id_state, msg_input]
478
  )
479
 
480
  msg_input.submit(
481
  fn=send_message,
482
+ inputs=[msg_input, session_id_state],
483
+ outputs=[chatbot, tool_display, session_id_state, msg_input]
484
  )
485
 
486
  clear_btn.click(
487
  fn=reset_conversation,
488
+ inputs=[session_id_state],
489
+ outputs=[chatbot, tool_display, session_id_state]
490
  )
491
 
492
  # ==========================================