Spaces:
Sleeping
Sleeping
barathvasan-dev commited on
Commit ·
f0a026f
1
Parent(s): 01077ea
Fix: Use proper Gradio Chatbot format with role/content dictionaries
Browse files
app.py
CHANGED
|
@@ -647,52 +647,82 @@ with gr.Blocks(
|
|
| 647 |
def chat_investigate(message, chat_history, conv_state, inv_results):
|
| 648 |
"""Process investigation with chat-like conversational flow"""
|
| 649 |
|
| 650 |
-
|
|
|
|
| 651 |
if not chat_history:
|
| 652 |
chat_history = []
|
| 653 |
return chat_history, conv_state, inv_results
|
| 654 |
|
| 655 |
-
#
|
| 656 |
-
if
|
| 657 |
chat_history = []
|
| 658 |
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
# Format response for chat
|
| 663 |
-
if result.get("status") == "error":
|
| 664 |
-
error_msg = result.get("message", "Investigation failed")
|
| 665 |
-
ai_response = f"Error: {error_msg}"
|
| 666 |
-
else:
|
| 667 |
-
# Build comprehensive response for chat
|
| 668 |
-
answer = result.get("answer", "")
|
| 669 |
-
analysis = result.get("analysis", {})
|
| 670 |
-
|
| 671 |
-
ai_response = f"""Investigation Results
|
| 672 |
-
|
| 673 |
-
Summary: {result.get('total_records', 0)} records retrieved | {analysis.get('unique_vehicles', 0)} vehicles | {analysis.get('unique_locations', 0)} locations
|
| 674 |
-
|
| 675 |
-
Analysis: {answer}
|
| 676 |
-
"""
|
| 677 |
|
| 678 |
-
#
|
| 679 |
-
if
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 683 |
|
| 684 |
-
#
|
| 685 |
-
inv_results = result
|
| 686 |
-
|
| 687 |
-
# Add complete [user, assistant] pair to chat history
|
| 688 |
-
try:
|
| 689 |
msg_str = str(message).strip()
|
| 690 |
resp_str = str(ai_response).strip()
|
| 691 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 692 |
except Exception as e:
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 696 |
|
| 697 |
def update_detailed_tabs(inv_results):
|
| 698 |
"""Update detailed tabs based on investigation results"""
|
|
|
|
| 647 |
def chat_investigate(message, chat_history, conv_state, inv_results):
|
| 648 |
"""Process investigation with chat-like conversational flow"""
|
| 649 |
|
| 650 |
+
# Validate input
|
| 651 |
+
if not message or len(str(message).strip()) < 2:
|
| 652 |
if not chat_history:
|
| 653 |
chat_history = []
|
| 654 |
return chat_history, conv_state, inv_results
|
| 655 |
|
| 656 |
+
# Initialize empty history if needed
|
| 657 |
+
if not chat_history:
|
| 658 |
chat_history = []
|
| 659 |
|
| 660 |
+
try:
|
| 661 |
+
# Run investigation
|
| 662 |
+
result = ask_investigation_question(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
|
| 664 |
+
# Format response for chat
|
| 665 |
+
if result.get("status") == "error":
|
| 666 |
+
error_msg = result.get("message", "Investigation failed")
|
| 667 |
+
ai_response = f"Error: {error_msg}"
|
| 668 |
+
else:
|
| 669 |
+
# Build comprehensive response for chat
|
| 670 |
+
answer = result.get("answer", "")
|
| 671 |
+
analysis = result.get("analysis", {})
|
| 672 |
+
|
| 673 |
+
ai_response = f"""Investigation Results
|
| 674 |
+
|
| 675 |
+
Summary: {result.get('total_records', 0)} records | {analysis.get('unique_vehicles', 0)} vehicles | {analysis.get('unique_locations', 0)} locations
|
| 676 |
+
|
| 677 |
+
{answer}"""
|
| 678 |
+
|
| 679 |
+
# Add key findings
|
| 680 |
+
findings = analysis.get("key_findings", [])
|
| 681 |
+
if findings:
|
| 682 |
+
ai_response += "\n\nKey Findings:"
|
| 683 |
+
for finding in findings[:3]:
|
| 684 |
+
ai_response += f"\n- {finding}"
|
| 685 |
+
|
| 686 |
+
# Store full results for detailed tabs
|
| 687 |
+
inv_results = result
|
| 688 |
|
| 689 |
+
# Create proper Gradio format with role/content
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
msg_str = str(message).strip()
|
| 691 |
resp_str = str(ai_response).strip()
|
| 692 |
+
|
| 693 |
+
new_msg = {
|
| 694 |
+
"role": "user",
|
| 695 |
+
"content": msg_str
|
| 696 |
+
}
|
| 697 |
+
new_response = {
|
| 698 |
+
"role": "assistant",
|
| 699 |
+
"content": resp_str
|
| 700 |
+
}
|
| 701 |
+
|
| 702 |
+
# Add to chat history
|
| 703 |
+
updated_history = list(chat_history) if chat_history else []
|
| 704 |
+
updated_history.append(new_msg)
|
| 705 |
+
updated_history.append(new_response)
|
| 706 |
+
|
| 707 |
+
return updated_history, conv_state + [msg_str], inv_results
|
| 708 |
+
|
| 709 |
except Exception as e:
|
| 710 |
+
# Error handling
|
| 711 |
+
error_str = f"Error: {str(e)[:100]}"
|
| 712 |
+
msg_dict = {
|
| 713 |
+
"role": "user",
|
| 714 |
+
"content": str(message).strip()
|
| 715 |
+
}
|
| 716 |
+
error_dict = {
|
| 717 |
+
"role": "assistant",
|
| 718 |
+
"content": error_str
|
| 719 |
+
}
|
| 720 |
+
|
| 721 |
+
updated_history = list(chat_history) if chat_history else []
|
| 722 |
+
updated_history.append(msg_dict)
|
| 723 |
+
updated_history.append(error_dict)
|
| 724 |
+
|
| 725 |
+
return updated_history, conv_state, inv_results
|
| 726 |
|
| 727 |
def update_detailed_tabs(inv_results):
|
| 728 |
"""Update detailed tabs based on investigation results"""
|