barathvasan-dev commited on
Commit
01077ea
·
1 Parent(s): f9bd7d7

Fix: Remove asyncio event loop policy, fix MistralClient references, simplify chat message format

Browse files
Files changed (2) hide show
  1. ai_investigation.py +4 -5
  2. app.py +15 -25
ai_investigation.py CHANGED
@@ -634,13 +634,12 @@ def ask_investigation_question(question, conversation_history=None):
634
  # =====================================================
635
 
636
  def get_mistral_client():
637
- """Get Mistral client"""
638
  try:
639
- api_key = "any"
640
- client = MistralClient(api_key=api_key)
641
- return client
642
  except Exception as e:
643
- print(f"⚠️ Mistral error: {e}")
644
  return None
645
 
646
 
 
634
  # =====================================================
635
 
636
  def get_mistral_client():
637
+ """Get Mistral client - uses HuggingFace InferenceClient"""
638
  try:
639
+ from database import client as hf_client
640
+ return hf_client
 
641
  except Exception as e:
642
+ print(f"Warning: LLM error: {e}")
643
  return None
644
 
645
 
app.py CHANGED
@@ -44,22 +44,8 @@ from ai_investigation import (
44
  # ASYNC FIX & EVENT LOOP MANAGEMENT
45
  # =========================================================
46
 
47
- # Set appropriate event loop policy based on platform
48
- if platform.system() == "Windows":
49
- try:
50
- asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
51
- except Exception as e:
52
- print(f"⚠️ Could not set ProactorEventLoop: {e}")
53
- else:
54
- try:
55
- asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
56
- except Exception:
57
- pass
58
-
59
- # Suppress asyncio and other warnings
60
  import warnings
61
- warnings.filterwarnings('ignore', category=ResourceWarning)
62
- warnings.filterwarnings('ignore', category=RuntimeWarning)
63
  warnings.filterwarnings('ignore')
64
 
65
  # =========================================================
@@ -662,6 +648,8 @@ with gr.Blocks(
662
  """Process investigation with chat-like conversational flow"""
663
 
664
  if not message or len(message.strip()) < 2:
 
 
665
  return chat_history, conv_state, inv_results
666
 
667
  # Ensure chat_history is a list
@@ -674,33 +662,35 @@ with gr.Blocks(
674
  # Format response for chat
675
  if result.get("status") == "error":
676
  error_msg = result.get("message", "Investigation failed")
677
- ai_response = f" **Investigation Failed**\n\n{error_msg}"
678
  else:
679
  # Build comprehensive response for chat
680
  answer = result.get("answer", "")
681
  analysis = result.get("analysis", {})
682
 
683
- ai_response = f"""**🔍 Investigation Results**
684
-
685
- **Summary:**
686
- {result.get('total_records', 0)} records retrieved | {analysis.get('unique_vehicles', 0)} vehicles | {analysis.get('unique_locations', 0)} locations
687
 
688
- **Analysis:**
689
- {answer}
690
 
 
691
  """
692
 
693
  # Add key findings
694
  if analysis.get("key_findings"):
695
- ai_response += "**🚨 Key Findings:**\n"
696
  for finding in analysis.get("key_findings", [])[:3]:
697
- ai_response += f" {finding}\n"
698
 
699
  # Store full results for detailed tabs
700
  inv_results = result
701
 
702
  # Add complete [user, assistant] pair to chat history
703
- chat_history = chat_history + [[message, ai_response]]
 
 
 
 
 
704
 
705
  return chat_history, conv_state + [message], inv_results
706
 
 
44
  # ASYNC FIX & EVENT LOOP MANAGEMENT
45
  # =========================================================
46
 
47
+ # Suppress all warnings to prevent asyncio event loop cleanup messages
 
 
 
 
 
 
 
 
 
 
 
 
48
  import warnings
 
 
49
  warnings.filterwarnings('ignore')
50
 
51
  # =========================================================
 
648
  """Process investigation with chat-like conversational flow"""
649
 
650
  if not message or len(message.strip()) < 2:
651
+ if not chat_history:
652
+ chat_history = []
653
  return chat_history, conv_state, inv_results
654
 
655
  # Ensure chat_history is a list
 
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
  # Add key findings
679
  if analysis.get("key_findings"):
680
+ ai_response += "\nKey Findings:\n"
681
  for finding in analysis.get("key_findings", [])[:3]:
682
+ ai_response += f"- {finding}\n"
683
 
684
  # Store full results for detailed tabs
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
+ chat_history = list(chat_history) + [[msg_str, resp_str]]
692
+ except Exception as e:
693
+ print(f"Chat formatting error: {e}")
694
 
695
  return chat_history, conv_state + [message], inv_results
696