Siddharth Ravikumar commited on
Commit
7ff4a07
Β·
1 Parent(s): 287e21d

Fix chat backend to use chat_engine text module instead of inference_engine vision module

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -24,7 +24,7 @@ from fastapi.responses import FileResponse
24
  # ── Backend Imports ────────────────────────────────────────────────────
25
  from backend.app.config import settings
26
  from backend.app.db.database import db
27
- from backend.app.core.inference import inference_engine, SCENE_ANALYSIS_PROMPT
28
  from backend.app.core.scene_analyzer import SceneAnalyzer
29
  from backend.app.core.rule_matcher import RuleMatcher
30
  from backend.app.core.fault_deducer import FaultDeducer
@@ -55,6 +55,15 @@ def gpu_run_inference(image, prompt):
55
  # Monkey-patch so the entire pipeline uses GPU
56
  inference_engine._run_inference = gpu_run_inference
57
 
 
 
 
 
 
 
 
 
 
58
 
59
  # ── Async helpers ──────────────────────────────────────────────────────
60
 
@@ -504,22 +513,10 @@ def chat_respond(user_message, history, system_ctx):
504
  if not user_message or not user_message.strip():
505
  return history, "", system_ctx
506
  ensure_init()
507
- if not inference_engine.is_loaded:
508
- inference_engine.load_model()
509
  try:
510
- # Use the vision model's text generation capability for chat
511
- chat_prompt = f"""You are TraceScene AI assistant helping with accident analysis.
512
-
513
- CONTEXT:
514
- {system_ctx}
515
-
516
- USER QUESTION: {user_message.strip()}
517
-
518
- Provide a concise, helpful answer based on the context above."""
519
- # Create a small blank image for the vision model
520
- from PIL import Image as PILImg
521
- blank = PILImg.new('RGB', (64, 64), color=(0, 0, 0))
522
- response = gpu_run_inference(blank, chat_prompt)
523
  except Exception as e:
524
  response = f"Error: {e}"
525
  history = history or []
 
24
  # ── Backend Imports ────────────────────────────────────────────────────
25
  from backend.app.config import settings
26
  from backend.app.db.database import db
27
+ from backend.app.core.inference import inference_engine, chat_engine, SCENE_ANALYSIS_PROMPT
28
  from backend.app.core.scene_analyzer import SceneAnalyzer
29
  from backend.app.core.rule_matcher import RuleMatcher
30
  from backend.app.core.fault_deducer import FaultDeducer
 
55
  # Monkey-patch so the entire pipeline uses GPU
56
  inference_engine._run_inference = gpu_run_inference
57
 
58
+ _original_chat = chat_engine.chat
59
+
60
+ @spaces.GPU(duration=60)
61
+ def gpu_run_chat(system_context: str, user_message: str):
62
+ """GPU-accelerated chat inference"""
63
+ return _original_chat(system_context, user_message)
64
+
65
+ chat_engine.chat = gpu_run_chat
66
+
67
 
68
  # ── Async helpers ──────────────────────────────────────────────────────
69
 
 
513
  if not user_message or not user_message.strip():
514
  return history, "", system_ctx
515
  ensure_init()
516
+ if not chat_engine.is_loaded:
517
+ chat_engine.load_model()
518
  try:
519
+ response = gpu_run_chat(system_ctx, user_message.strip())
 
 
 
 
 
 
 
 
 
 
 
 
520
  except Exception as e:
521
  response = f"Error: {e}"
522
  history = history or []