Spaces:
Sleeping
Sleeping
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
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:
|
| 262 |
-
return history + [
|
| 263 |
|
| 264 |
if not HF_TOKEN:
|
| 265 |
print("β HF_TOKEN not configured")
|
| 266 |
if not history:
|
| 267 |
history = []
|
| 268 |
-
return history + [
|
| 269 |
|
| 270 |
if not DATABASE_URL:
|
| 271 |
print("β DATABASE_URL not configured")
|
| 272 |
if not history:
|
| 273 |
history = []
|
| 274 |
-
return history + [
|
| 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:
|
| 305 |
-
history = history + [
|
| 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
|
| 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
|
| 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
|
| 862 |
-
|
| 863 |
-
|
|
|
|
|
|
|
| 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
|
| 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
|
| 912 |
-
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
|
| 916 |
-
|
|
|
|
| 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:
|