Spaces:
Sleeping
Sleeping
barathvasan-dev commited on
Commit Β·
fced10e
1
Parent(s): 71f10e7
π§ Fix: Correct chat message format in investigation chatbot
Browse files
app.py
CHANGED
|
@@ -839,31 +839,27 @@ with gr.Blocks(
|
|
| 839 |
)
|
| 840 |
|
| 841 |
# =====================================================
|
| 842 |
-
# Chat Interface Logic
|
| 843 |
# =====================================================
|
| 844 |
|
| 845 |
-
# Main investigation button click handler - OPTIMIZED FOR SPEED
|
| 846 |
def investigate_fast(message, chat_history, conv_state, inv_results):
|
| 847 |
-
"""Fast response
|
| 848 |
if not message or len(str(message).strip()) < 2:
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
return chat_history, conv_state, inv_results
|
| 852 |
|
| 853 |
-
|
| 854 |
-
|
| 855 |
|
| 856 |
msg_str = str(message).strip()
|
| 857 |
|
| 858 |
-
#
|
| 859 |
-
|
| 860 |
-
updated_history = list(chat_history) if chat_history else []
|
| 861 |
-
updated_history.append([msg_str, response])
|
| 862 |
|
| 863 |
-
return
|
| 864 |
|
| 865 |
def investigate_complete(message):
|
| 866 |
-
"""
|
| 867 |
try:
|
| 868 |
result = ask_investigation_question(message)
|
| 869 |
return result
|
|
@@ -872,13 +868,19 @@ with gr.Blocks(
|
|
| 872 |
"status": "error",
|
| 873 |
"message": f"Error: {str(e)[:80]}",
|
| 874 |
"analysis": {},
|
| 875 |
-
"data_preview": []
|
|
|
|
| 876 |
}
|
| 877 |
|
| 878 |
def update_chat_final(message, chat_history, inv_results):
|
| 879 |
-
"""Update
|
| 880 |
-
if not
|
| 881 |
-
return chat_history, inv_results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 882 |
|
| 883 |
try:
|
| 884 |
# Get investigation result
|
|
@@ -890,22 +892,22 @@ with gr.Blocks(
|
|
| 890 |
ai_response = result.get("answer", "Investigation complete")
|
| 891 |
analysis = result.get("analysis", {})
|
| 892 |
|
| 893 |
-
#
|
| 894 |
ai_response += f"\n\n**Summary:** {result.get('total_records', 0)} records | {analysis.get('unique_vehicles', 0)} vehicles | {analysis.get('unique_locations', 0)} locations"
|
| 895 |
|
| 896 |
-
# Update last message
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
updated[-1][1] = ai_response
|
| 900 |
|
| 901 |
-
return
|
| 902 |
except Exception as e:
|
| 903 |
-
|
|
|
|
| 904 |
|
| 905 |
def update_detailed_tabs_lightweight(inv_results):
|
| 906 |
-
"""
|
| 907 |
if not inv_results or inv_results.get("status") == "error":
|
| 908 |
-
return ("No data", "No findings", None, "No metrics")
|
| 909 |
|
| 910 |
try:
|
| 911 |
analysis = inv_results.get("analysis", {})
|
|
@@ -914,11 +916,27 @@ with gr.Blocks(
|
|
| 914 |
# Summary
|
| 915 |
data_info = f"π **{total} records** | π **{analysis.get('unique_vehicles', 0)} vehicles** | π **{analysis.get('unique_locations', 0)} locations**"
|
| 916 |
|
| 917 |
-
# Findings
|
| 918 |
-
|
|
|
|
| 919 |
|
| 920 |
# Data table (limit to 20 rows)
|
| 921 |
df_data = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 922 |
try:
|
| 923 |
preview = inv_results.get("data_preview", [])[:20]
|
| 924 |
if preview:
|
|
|
|
| 839 |
)
|
| 840 |
|
| 841 |
# =====================================================
|
| 842 |
+
# Chat Interface Logic - FIXED MESSAGE FORMAT
|
| 843 |
# =====================================================
|
| 844 |
|
|
|
|
| 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 |
+
# Return current state unchanged
|
| 849 |
+
return chat_history if chat_history else [], conv_state or [], inv_results or {}
|
|
|
|
| 850 |
|
| 851 |
+
# Ensure chat_history is a list
|
| 852 |
+
history = list(chat_history) if chat_history else []
|
| 853 |
|
| 854 |
msg_str = str(message).strip()
|
| 855 |
|
| 856 |
+
# Add user message + loading response as [user, bot] tuple
|
| 857 |
+
history.append([msg_str, "π Analyzing... Please wait for insights."])
|
|
|
|
|
|
|
| 858 |
|
| 859 |
+
return history, (conv_state or []) + [msg_str], inv_results or {}
|
| 860 |
|
| 861 |
def investigate_complete(message):
|
| 862 |
+
"""Get investigation results"""
|
| 863 |
try:
|
| 864 |
result = ask_investigation_question(message)
|
| 865 |
return result
|
|
|
|
| 868 |
"status": "error",
|
| 869 |
"message": f"Error: {str(e)[:80]}",
|
| 870 |
"analysis": {},
|
| 871 |
+
"data_preview": [],
|
| 872 |
+
"total_records": 0
|
| 873 |
}
|
| 874 |
|
| 875 |
def update_chat_final(message, chat_history, inv_results):
|
| 876 |
+
"""Update last message with real results"""
|
| 877 |
+
if not message:
|
| 878 |
+
return chat_history or [], inv_results or {}
|
| 879 |
+
|
| 880 |
+
# Ensure chat_history is a list
|
| 881 |
+
history = list(chat_history) if chat_history else []
|
| 882 |
+
if not history:
|
| 883 |
+
return history, inv_results or {}
|
| 884 |
|
| 885 |
try:
|
| 886 |
# Get investigation result
|
|
|
|
| 892 |
ai_response = result.get("answer", "Investigation complete")
|
| 893 |
analysis = result.get("analysis", {})
|
| 894 |
|
| 895 |
+
# Add quick summary
|
| 896 |
ai_response += f"\n\n**Summary:** {result.get('total_records', 0)} records | {analysis.get('unique_vehicles', 0)} vehicles | {analysis.get('unique_locations', 0)} locations"
|
| 897 |
|
| 898 |
+
# Update last message [user, bot] tuple
|
| 899 |
+
if history:
|
| 900 |
+
history[-1] = [history[-1][0], ai_response]
|
|
|
|
| 901 |
|
| 902 |
+
return history, result
|
| 903 |
except Exception as e:
|
| 904 |
+
print(f"Chat update error: {e}")
|
| 905 |
+
return history, inv_results or {}
|
| 906 |
|
| 907 |
def update_detailed_tabs_lightweight(inv_results):
|
| 908 |
+
"""Update detailed analysis tabs"""
|
| 909 |
if not inv_results or inv_results.get("status") == "error":
|
| 910 |
+
return ("No data available", "No findings yet", None, "No metrics")
|
| 911 |
|
| 912 |
try:
|
| 913 |
analysis = inv_results.get("analysis", {})
|
|
|
|
| 916 |
# Summary
|
| 917 |
data_info = f"π **{total} records** | π **{analysis.get('unique_vehicles', 0)} vehicles** | π **{analysis.get('unique_locations', 0)} locations**"
|
| 918 |
|
| 919 |
+
# Findings
|
| 920 |
+
findings_list = analysis.get("key_findings", [])
|
| 921 |
+
findings = "**Key Findings:**\n" + "\n".join([f"β’ {f}" for f in findings_list[:3]]) if findings_list else "No key findings"
|
| 922 |
|
| 923 |
# Data table (limit to 20 rows)
|
| 924 |
df_data = None
|
| 925 |
+
try:
|
| 926 |
+
preview = inv_results.get("data_preview", [])[:20]
|
| 927 |
+
if preview:
|
| 928 |
+
df_data = pd.DataFrame(preview)
|
| 929 |
+
except Exception as e:
|
| 930 |
+
print(f"DataFrame error: {e}")
|
| 931 |
+
|
| 932 |
+
# Metrics
|
| 933 |
+
metrics = f"**Confidence:** {analysis.get('confidence_score', 0):.0%}\n**Records:** {total}"
|
| 934 |
+
|
| 935 |
+
return (data_info, findings, df_data, metrics)
|
| 936 |
+
except Exception as e:
|
| 937 |
+
print(f"Tab update error: {e}")
|
| 938 |
+
return ("Error processing data", "Error", None, "Error")
|
| 939 |
+
df_data = None
|
| 940 |
try:
|
| 941 |
preview = inv_results.get("data_preview", [])[:20]
|
| 942 |
if preview:
|