barathvasan-dev commited on
Commit
4dcd2f3
Β·
1 Parent(s): fd53ef0

πŸ› CRITICAL FIX: Convert Chatbot format from tuple to dict with role/content keys

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -258,20 +258,20 @@ def chatbot_query(message, history):
258
  print("❌ Empty message")
259
  if not history:
260
  history = []
261
- # Gradio 6.14.0: history is list of [user_msg, bot_msg] tuples
262
- return history + [[message, "❌ Empty message"]], ""
263
 
264
  if not HF_TOKEN:
265
  print("❌ HF_TOKEN not configured")
266
  if not history:
267
  history = []
268
- return history + [[message, "❌ HF_TOKEN not configured - NLP disabled"]], ""
269
 
270
  if not DATABASE_URL:
271
  print("❌ DATABASE_URL not configured")
272
  if not history:
273
  history = []
274
- return history + [[message, "❌ DATABASE_URL not configured - Database disabled"]], ""
275
 
276
  print("Calling run_query...")
277
  response = run_query(message)
@@ -301,8 +301,8 @@ def chatbot_query(message, history):
301
  **Results:** {count} records found
302
  """
303
 
304
- # Gradio 6.14.0: append [user_msg, bot_msg] tuple to history
305
- history = history + [[message, bot_reply]]
306
 
307
  print(f"Returning history with {len(history)} messages")
308
  return history, ""
@@ -845,12 +845,12 @@ with gr.Blocks(
845
  def investigate_fast(message, chat_history, conv_state, inv_results):
846
  """Fast response - append to chat immediately"""
847
  if not message or len(str(message).strip()) < 2:
848
- # Ensure chat_history is valid list of tuples
849
  if not chat_history:
850
  chat_history = []
851
  return chat_history, (conv_state or []), (inv_results or {})
852
 
853
- # Ensure chat_history is a list and contains proper [user, bot] tuples
854
  if not chat_history:
855
  history = []
856
  else:
@@ -858,9 +858,11 @@ with gr.Blocks(
858
 
859
  msg_str = str(message).strip()
860
 
861
- # CRITICAL: Add user message + loading response as [user, bot] tuple format
862
- loading_tuple = [msg_str, "πŸ” Analyzing... Please wait for insights."]
863
- history.append(loading_tuple)
 
 
864
 
865
  return history, (conv_state or []) + [msg_str], (inv_results or {})
866
 
@@ -886,7 +888,7 @@ with gr.Blocks(
886
  chat_history = []
887
  return chat_history, (inv_results or {})
888
 
889
- # Ensure chat_history is a list of proper tuples
890
  if not chat_history:
891
  history = []
892
  else:
@@ -908,12 +910,13 @@ with gr.Blocks(
908
  # Add quick summary
909
  ai_response += f"\n\n**Summary:** {result.get('total_records', 0)} records | {analysis.get('unique_vehicles', 0)} vehicles | {analysis.get('unique_locations', 0)} locations"
910
 
911
- # CRITICAL: Update last message [user, bot] tuple
912
- if history and len(history) > 0:
913
- # Get user message from last tuple
914
- last_user_msg = history[-1][0] if isinstance(history[-1], (list, tuple)) and len(history[-1]) > 0 else message
915
- # Create new tuple with actual response
916
- history[-1] = [last_user_msg, ai_response]
 
917
 
918
  return history, result
919
  except Exception as e:
 
258
  print("❌ Empty message")
259
  if not history:
260
  history = []
261
+ # Gradio 6.14.0+: Format MUST be {"role": "user/assistant", "content": "text"}
262
+ return history + [{"role": "user", "content": message}, {"role": "assistant", "content": "❌ Empty message"}], ""
263
 
264
  if not HF_TOKEN:
265
  print("❌ HF_TOKEN not configured")
266
  if not history:
267
  history = []
268
+ return history + [{"role": "user", "content": message}, {"role": "assistant", "content": "❌ HF_TOKEN not configured - NLP disabled"}], ""
269
 
270
  if not DATABASE_URL:
271
  print("❌ DATABASE_URL not configured")
272
  if not history:
273
  history = []
274
+ return history + [{"role": "user", "content": message}, {"role": "assistant", "content": "❌ DATABASE_URL not configured - Database disabled"}], ""
275
 
276
  print("Calling run_query...")
277
  response = run_query(message)
 
301
  **Results:** {count} records found
302
  """
303
 
304
+ # Gradio 6.14.0+: Append dict format {"role", "content"}
305
+ history = history + [{"role": "user", "content": message}, {"role": "assistant", "content": bot_reply}]
306
 
307
  print(f"Returning history with {len(history)} messages")
308
  return history, ""
 
845
  def investigate_fast(message, chat_history, conv_state, inv_results):
846
  """Fast response - append to chat immediately"""
847
  if not message or len(str(message).strip()) < 2:
848
+ # Ensure chat_history is valid list of dicts
849
  if not chat_history:
850
  chat_history = []
851
  return chat_history, (conv_state or []), (inv_results or {})
852
 
853
+ # Ensure chat_history is a list and contains proper {"role", "content"} dicts
854
  if not chat_history:
855
  history = []
856
  else:
 
858
 
859
  msg_str = str(message).strip()
860
 
861
+ # CRITICAL: Add user message + loading response as dict format with role/content
862
+ user_msg_dict = {"role": "user", "content": msg_str}
863
+ loading_msg_dict = {"role": "assistant", "content": "πŸ” Analyzing... Please wait for insights."}
864
+ history.append(user_msg_dict)
865
+ history.append(loading_msg_dict)
866
 
867
  return history, (conv_state or []) + [msg_str], (inv_results or {})
868
 
 
888
  chat_history = []
889
  return chat_history, (inv_results or {})
890
 
891
+ # Ensure chat_history is a list of proper dicts with role/content
892
  if not chat_history:
893
  history = []
894
  else:
 
910
  # Add quick summary
911
  ai_response += f"\n\n**Summary:** {result.get('total_records', 0)} records | {analysis.get('unique_vehicles', 0)} vehicles | {analysis.get('unique_locations', 0)} locations"
912
 
913
+ # CRITICAL: Update last assistant message dict with actual response
914
+ # Find and update the last assistant message (loading message)
915
+ for i in range(len(history) - 1, -1, -1):
916
+ if isinstance(history[i], dict) and history[i].get("role") == "assistant":
917
+ # Update this assistant message with real response
918
+ history[i]["content"] = ai_response
919
+ break
920
 
921
  return history, result
922
  except Exception as e: