StillWorking's picture
Upload backend/main.py with huggingface_hub
0bde491 verified
Raw History Blame Contribute Delete
7.06 kB
"""
main.py β€” Sentinel-AI
=========================
FastAPI application entry point.
Start the server (loopback by default; use --host 0.0.0.0 only with
SENTINEL_API_KEY and a TLS reverse proxy):
uvicorn main:app --host 127.0.0.1 --port 8000 --reload
Swagger UI: http://localhost:8000/docs
ReDoc: http://localhost:8000/redoc
"""
from __future__ import annotations
import os
from contextlib import asynccontextmanager
from pathlib import Path
from dotenv import load_dotenv
# Load .env β€” check backend dir first, then fall back to project root.
_backend_env = Path(__file__).parent / ".env"
_root_env = Path(__file__).parent.parent / ".env"
# Load backend .env first (takes precedence), then root .env as fallback.
load_dotenv(_backend_env)
load_dotenv(_root_env)
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from api.routes.analyze import router as analyze_router
from api.routes.generate import router as generate_router
from api.routes.search import router as search_router
from api.schemas import HealthResponse
from core.faiss_store import (
get_faiss_order,
get_index,
get_metadata,
warm_attribute_index,
)
from core.models import (
detector_ready,
embedder_ready,
load_adaface,
load_face_detector,
)
from core.paths import resolve_path
from core.security import install_security
def _cors_origins() -> list[str]:
"""
Read CORS_ORIGINS as a comma-separated list.
Defaults to the local Vite dev server. Set ``CORS_ORIGINS=*`` to allow
every origin (the old hard-coded behaviour) when explicitly desired.
"""
raw = os.getenv(
"CORS_ORIGINS",
"http://localhost:5173,http://127.0.0.1:5173,"
"http://localhost:3000,http://127.0.0.1:3000,"
"http://localhost:8080,http://127.0.0.1:8080,"
"http://localhost:4173,http://127.0.0.1:4173",
)
origins = [origin.strip() for origin in raw.split(",") if origin.strip()]
return origins or ["http://localhost:5173"]
# ─────────────────────────────────────────────────────────────────────────────
# Lifespan: warm-up all heavy singletons at startup
# ─────────────────────────────────────────────────────────────────────────────
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Load models and FAISS index once at startup."""
print("[Sentinel-AI] Warming up models and index ...")
# 1. Initialise cascadeflow harness β€” must run before any cascadeflow.run() scope
import cascadeflow
cf_report = cascadeflow.init(mode="enforce")
version = getattr(cascadeflow, "__version__", "unknown")
print(
f" [OK] cascadeflow {version} "
f"mode={cf_report.mode} instrumented={cf_report.instrumented}"
)
# 2. Load local CV models (MTCNN detector + AdaFace embedder)
try:
load_face_detector()
print(" [OK] MTCNN face detector loaded")
except Exception as exc:
print(f" [FAIL] MTCNN detector not available: {exc}")
try:
load_adaface()
print(" [OK] AdaFace embedder loaded")
except Exception as exc:
print(f" [FAIL] AdaFace not available: {exc}")
# 3. Load FAISS index + metadata into RAM and build the vectorised
# attribute matrix used by semantic search (one-off, ~8 MB).
try:
idx = get_index()
get_metadata()
get_faiss_order()
warm_attribute_index()
print(f" [OK] FAISS index loaded ({idx.ntotal:,} vectors)")
print(" [OK] Attribute index warmed")
except FileNotFoundError as exc:
print(f" [FAIL] {exc}")
except Exception as exc:
print(f" [FAIL] Attribute index warm-up failed: {exc}")
# 4. Pre-warm provider clients (validates keys, keeps first request fast)
from core.gemini_agent import warm_providers
warmed = warm_providers()
if warmed:
print(f" [OK] LLM clients warmed: {', '.join(warmed)}")
else:
print(" [WARN] No cloud LLM clients configured; using offline fallbacks")
print("[Sentinel-AI] Ready.")
yield
print("[Sentinel-AI] Shutting down.")
# ─────────────────────────────────────────────────────────────────────────────
# App
# ─────────────────────────────────────────────────────────────────────────────
app = FastAPI(
title="Sentinel-AI",
description=(
"Hybrid edge-cloud security agent. "
"Detects faces via MTCNN, embeds them with AdaFace, "
"searches 200k CelebA vectors in FAISS, and drafts alerts with Gemini."
),
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan,
)
# Install security *before* CORS: Starlette makes the last-added middleware
# outermost, and CORS must wrap auth so preflight OPTIONS requests are answered.
install_security(app)
app.add_middleware(
CORSMiddleware,
allow_origins=_cors_origins(),
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["Content-Type", "Authorization", "X-API-Key"],
)
# ── Routers ───────────────────────────────────────────────────────────────────
app.include_router(analyze_router, prefix="/api/v1", tags=["Inference"])
app.include_router(search_router, prefix="/api/v1", tags=["Search"])
app.include_router(generate_router, prefix="/api/v1", tags=["Generation"])
# ── Health check ──────────────────────────────────────────────────────────────
@app.get("/health", response_model=HealthResponse, tags=["System"])
async def health() -> HealthResponse:
"""Liveness probe with real index/model readiness telemetry."""
index_path = resolve_path(
os.getenv("FAISS_INDEX_PATH"),
"./index_artifacts/celeb_index.faiss",
)
index_ready = index_path.exists()
index_vectors: int | None = None
if index_ready:
try:
index_vectors = int(get_index().ntotal)
except Exception:
index_ready = False
return HealthResponse(
status="ok",
index_ready=index_ready,
index_vectors=index_vectors,
detector_ready=detector_ready(),
embedder_ready=embedder_ready(),
model=os.getenv("GEMINI_MODEL", "gemini-2.5-flash"),
)