File size: 7,061 Bytes
0bde491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""
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"),
    )