SiddharthVenba commited on
Commit
2d2f421
Β·
verified Β·
1 Parent(s): c88a39f

fix: animation replay + chat uses vision model

Browse files
Files changed (2) hide show
  1. __pycache__/app.cpython-312.pyc +0 -0
  2. app.py +22 -24
__pycache__/app.cpython-312.pyc CHANGED
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
 
app.py CHANGED
@@ -19,7 +19,7 @@ import spaces
19
  # ── Backend Imports ────────────────────────────────────────────────────
20
  from backend.app.config import settings
21
  from backend.app.db.database import db
22
- from backend.app.core.inference import inference_engine, chat_engine, SCENE_ANALYSIS_PROMPT
23
  from backend.app.core.scene_analyzer import SceneAnalyzer
24
  from backend.app.core.rule_matcher import RuleMatcher
25
  from backend.app.core.fault_deducer import FaultDeducer
@@ -50,19 +50,6 @@ def gpu_run_inference(image, prompt):
50
  inference_engine._run_inference = gpu_run_inference
51
 
52
 
53
- # Chat engine GPU wrapper
54
- _original_chat = chat_engine.chat
55
-
56
-
57
- @spaces.GPU(duration=60)
58
- def gpu_chat(system_context, user_message):
59
- """GPU-accelerated chat inference."""
60
- return _original_chat(system_context, user_message)
61
-
62
-
63
- chat_engine.chat = gpu_chat
64
-
65
-
66
  # ── Async helpers ──────────────────────────────────────────────────────
67
 
68
  def run_async(coro):
@@ -94,10 +81,6 @@ async def _ensure_init():
94
  inference_engine.load_model()
95
  except Exception as e:
96
  logger.error(f"Vision model load failed: {e}")
97
- try:
98
- chat_engine.load_model()
99
- except Exception as e:
100
- logger.error(f"Chat model load failed: {e}")
101
  _initialized = True
102
 
103
 
@@ -619,10 +602,22 @@ SCENE ANALYSES:\n"""
619
  if not user_message or not user_message.strip():
620
  return history, "", system_ctx
621
  ensure_init()
622
- if not chat_engine.is_loaded:
623
- chat_engine.load_model()
624
  try:
625
- response = gpu_chat(system_ctx, user_message.strip())
 
 
 
 
 
 
 
 
 
 
 
 
626
  except Exception as e:
627
  response = f"Error: {e}"
628
  history = history or []
@@ -697,6 +692,9 @@ SCENE ANALYSES:\n"""
697
  num_v = int(num_vehicles)
698
  except:
699
  pass
 
 
 
700
  # Determine animation duration based on severity
701
  dur = "3s" if category.lower() == "mild" else "2s" if category.lower() == "medium" else "1.5s"
702
  sev_color = "#22c55e" if category.lower() == "mild" else "#f59e0b" if category.lower() == "medium" else "#ef4444"
@@ -737,9 +735,9 @@ SCENE ANALYSES:\n"""
737
 
738
  html = f'''
739
  <div style="text-align:center; font-family: Inter, Arial, sans-serif;">
740
- <svg width="700" height="420" viewBox="0 0 700 420" xmlns="http://www.w3.org/2000/svg" style="border:1px solid #444; border-radius:10px; background:#1a1a2e;">
741
  <defs>
742
- <radialGradient id="impactGlow" cx="50%" cy="50%" r="50%">
743
  <stop offset="0%" stop-color="#fbbf24" stop-opacity="0.8"/>
744
  <stop offset="100%" stop-color="#fbbf24" stop-opacity="0"/>
745
  </radialGradient>
@@ -757,7 +755,7 @@ SCENE ANALYSES:\n"""
757
  {v2_svg}
758
 
759
  <!-- Impact flash -->
760
- <circle cx="340" cy="210" r="0" fill="url(#impactGlow)">
761
  <animate attributeName="r" values="0;0;0;0;0;0;0;45;55;0" dur="{dur}" fill="freeze"/>
762
  <animate attributeName="opacity" values="0;0;0;0;0;0;0;1;0.5;0" dur="{dur}" fill="freeze"/>
763
  </circle>
 
19
  # ── Backend Imports ────────────────────────────────────────────────────
20
  from backend.app.config import settings
21
  from backend.app.db.database import db
22
+ from backend.app.core.inference import inference_engine, SCENE_ANALYSIS_PROMPT
23
  from backend.app.core.scene_analyzer import SceneAnalyzer
24
  from backend.app.core.rule_matcher import RuleMatcher
25
  from backend.app.core.fault_deducer import FaultDeducer
 
50
  inference_engine._run_inference = gpu_run_inference
51
 
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  # ── Async helpers ──────────────────────────────────────────────────────
54
 
55
  def run_async(coro):
 
81
  inference_engine.load_model()
82
  except Exception as e:
83
  logger.error(f"Vision model load failed: {e}")
 
 
 
 
84
  _initialized = True
85
 
86
 
 
602
  if not user_message or not user_message.strip():
603
  return history, "", system_ctx
604
  ensure_init()
605
+ if not inference_engine.is_loaded:
606
+ inference_engine.load_model()
607
  try:
608
+ # Use the vision model's text generation capability for chat
609
+ chat_prompt = f"""You are TraceScene AI assistant helping with accident analysis.
610
+
611
+ CONTEXT:
612
+ {system_ctx}
613
+
614
+ USER QUESTION: {user_message.strip()}
615
+
616
+ Provide a concise, helpful answer based on the context above."""
617
+ # Create a small blank image for the vision model
618
+ from PIL import Image as PILImg
619
+ blank = PILImg.new('RGB', (64, 64), color=(0, 0, 0))
620
+ response = gpu_run_inference(blank, chat_prompt)
621
  except Exception as e:
622
  response = f"Error: {e}"
623
  history = history or []
 
692
  num_v = int(num_vehicles)
693
  except:
694
  pass
695
+ # Unique ID to force Gradio to re-render on each click (enables replay)
696
+ import random
697
+ uid = random.randint(10000, 99999)
698
  # Determine animation duration based on severity
699
  dur = "3s" if category.lower() == "mild" else "2s" if category.lower() == "medium" else "1.5s"
700
  sev_color = "#22c55e" if category.lower() == "mild" else "#f59e0b" if category.lower() == "medium" else "#ef4444"
 
735
 
736
  html = f'''
737
  <div style="text-align:center; font-family: Inter, Arial, sans-serif;">
738
+ <svg id="anim_{uid}" width="700" height="420" viewBox="0 0 700 420" xmlns="http://www.w3.org/2000/svg" style="border:1px solid #444; border-radius:10px; background:#1a1a2e;">
739
  <defs>
740
+ <radialGradient id="glow_{uid}" cx="50%" cy="50%" r="50%">
741
  <stop offset="0%" stop-color="#fbbf24" stop-opacity="0.8"/>
742
  <stop offset="100%" stop-color="#fbbf24" stop-opacity="0"/>
743
  </radialGradient>
 
755
  {v2_svg}
756
 
757
  <!-- Impact flash -->
758
+ <circle cx="340" cy="210" r="0" fill="url(#glow_{uid})">
759
  <animate attributeName="r" values="0;0;0;0;0;0;0;45;55;0" dur="{dur}" fill="freeze"/>
760
  <animate attributeName="opacity" values="0;0;0;0;0;0;0;1;0.5;0" dur="{dur}" fill="freeze"/>
761
  </circle>