Z User commited on
Commit
1054555
·
1 Parent(s): 6c516b6

feat(main): warm the local risk scorer at startup — baked weights load from disk cache, first paying request never pays model load

Browse files
Files changed (1) hide show
  1. app/main.py +19 -0
app/main.py CHANGED
@@ -82,6 +82,25 @@ async def lifespan(app: FastAPI):
82
  except Exception as exc: # pragma: no cover - defensive
83
  print(f"[ML] preload skipped — {exc}")
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  yield
86
 
87
  if drain_task is not None:
 
82
  except Exception as exc: # pragma: no cover - defensive
83
  print(f"[ML] preload skipped — {exc}")
84
 
85
+ # Warm the LOCAL risk scorer at startup (Part B architecture): weights are
86
+ # baked into the image (HF_HOME=/app/models), so this is a fast disk-cache
87
+ # load (~seconds) with ZERO network. Without it, the first paying request
88
+ # would pay the model load. Fail-open: a failed load trips the circuit
89
+ # breaker inside the scorer and the heuristic takes over (spec §2.3).
90
+ try:
91
+ from .ml.risk_scorer import ai_scorer as _scorer
92
+
93
+ if _scorer._inference_configured():
94
+ await asyncio.to_thread(_scorer._ensure_loaded)
95
+ st = _scorer.status()
96
+ print(
97
+ f"[AI] local risk scorer preloaded: model={st['model']} "
98
+ f"loaded={st['model_loaded']} "
99
+ f"circuit_open={st['circuit']['open']}"
100
+ )
101
+ except Exception as exc: # pragma: no cover - defensive
102
+ print(f"[AI] risk scorer preload skipped — {exc}")
103
+
104
  yield
105
 
106
  if drain_task is not None: