Add lazy model loading and clearer infer error messages
Browse files167 unmodified lines16816917017117217317417517617717817918018118218318418518618718819 unmodified lines208209210211212213214215216217218219220221222223167 unmodified lines
@api_app.on_event("startup")def load_models() -> None: """Eagerly load models at startup (best effort).""" ensure_models_loaded()
def ensure_models_loaded() -> None: """Lazily load YOLO and classifier if they are not in memory.
This is safe to call from within request handlers and avoids relying solely on startup events (which can behave differently when mounted). """ global yolo_model, clf_model
if yolo_model is not None and clf_model is not None: return
if not YOLO_WEIGHTS_PATH.exists(): raise RuntimeError(f"YOLO weights not found at {YOLO_WEIGHTS_PATH}") if not CLS_WEIGHTS_PATH.exists():19 unmodified lines frame_rate: int = 5, max_frames: int = 50,) -> dict: # Basic validation of uploaded file if video.content_type is None or not video.content_type.startswith("video/"): raise HTTPException(status_code=400, detail="Please upload a valid video file.")
# Ensure models are in memory (handles cases where startup didn't run) try: ensure_models_loaded() except Exception as e: # surface model-loading issues clearly to the client raise HTTPException(status_code=500, detail=f"Model loading failed: {e}")
tmp_path = ROOT_DIR / "_tmp_video.mp4" try: content = await video.read()app.py+20