File size: 5,009 Bytes
1359487
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
FastAPI application entry-point.

Local:
  uvicorn api.main:app --reload --port 8000

Render:
  uvicorn api.main:app --host 0.0.0.0 --port $PORT
  (set REDIS_URL in the Render dashboard under Environment)
"""

from __future__ import annotations

import logging
import os
from contextlib import asynccontextmanager
from pathlib import Path

from dotenv import load_dotenv

# Load .env before any os.getenv() calls — works locally and is a no-op on
# Render (where env vars are injected directly by the platform).
load_dotenv(Path(__file__).resolve().parent.parent / ".env")

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from api.event_logger import EventLogger
from api.routes import router, set_engine, set_event_logger
from serving.feature_store import FeatureStore
from serving.inference import RecommendationEngine

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
logger = logging.getLogger(__name__)

_RECOMMENDER_ROOT = Path(__file__).resolve().parent.parent  # recommender/
ARTIFACT_DIR = Path(os.getenv("ARTIFACT_DIR", str(_RECOMMENDER_ROOT / "artifacts")))

_REQUIRED_ARTIFACTS = [
    "preprocessor.pkl",
    "movie_meta.csv",
    "two_tower.pt",
    "deepfm_best.pt",
    "faiss.index",
    "item_embeddings.npy",
    "item_features.npy",
    "user_features.npy",
]


# ------------------------------------------------------------------
# Step 1 — Check
# ------------------------------------------------------------------

def _check_artifacts(artifact_dir: Path) -> None:
    """Raise a descriptive RuntimeError if any required file is missing."""
    missing = [f for f in _REQUIRED_ARTIFACTS if not (artifact_dir / f).exists()]
    if missing:
        raise RuntimeError(
            f"\n\n{'='*60}\n"
            f"  Artifacts missing in: {artifact_dir}\n"
            f"  Missing: {', '.join(missing)}\n\n"
            f"  Run training to generate them:\n"
            f"    ./run_training.sh\n"
            f"{'='*60}\n"
        )


# ------------------------------------------------------------------
# Step 2 — Load
# ------------------------------------------------------------------

def load_models(artifact_dir: Path) -> tuple[RecommendationEngine, EventLogger]:
    """
    Instantiate the feature store, recommendation engine, and event logger.
    Kept as a plain function so it can be called from tests or CLI without
    going through the full FastAPI lifespan.
    """
    feature_store = FeatureStore(
        redis_host=os.getenv("REDIS_HOST", "localhost"),
        redis_port=int(os.getenv("REDIS_PORT", 6379)),
        sqlite_path=artifact_dir / "feature_store.db",
    )

    engine = RecommendationEngine.load(
        artifact_dir,
        device_str=os.getenv("DEVICE", "cpu"),
        feature_store=feature_store,
    )

    event_logger = EventLogger(
        kafka_bootstrap=os.getenv("KAFKA_BOOTSTRAP", "localhost:9092"),
        sqlite_path=artifact_dir / "events.db",
    )

    return engine, event_logger


# ------------------------------------------------------------------
# Lifespan — wires the three steps together
# ------------------------------------------------------------------
@asynccontextmanager
async def lifespan(app: FastAPI):
    logger.info(f"Artifact dir: {ARTIFACT_DIR}")
    
    # Artifacts come from GitHub directly — no download needed
    _check_artifacts(ARTIFACT_DIR)
    
    engine, event_logger = load_models(ARTIFACT_DIR)
    set_engine(engine)
    set_event_logger(event_logger)
    
    logger.info("Startup complete.")
    yield
    event_logger.close()


# ------------------------------------------------------------------
# App
# ------------------------------------------------------------------

app = FastAPI(
    title="CineMatch Recommendation API",
    description=(
        "Production recommendation system: Two-Tower retrieval, "
        "DeepFM ranking, MMR diversity re-ranking."
    ),
    version="1.0.0",
    lifespan=lifespan,
)

# CORS: accept local dev ports + any *.onrender.com subdomain.
# CORS_ORIGINS env var lets Render / CI override this without a code change.
_extra_origins = [o.strip() for o in os.getenv("CORS_ORIGINS", "").split(",") if o.strip()]

app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:5173",
        "http://localhost:5174",
        "http://localhost:5175",
        "http://localhost:5176",
        "http://localhost:5177",
        "http://localhost:3000",
        "http://127.0.0.1:5173",
        "http://127.0.0.1:5177",
        *_extra_origins,
    ],
    allow_origin_regex=r"https://.*\.onrender\.com",  # matches any Render deploy URL
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(router, prefix="/api")


@app.get("/", tags=["system"])
def root():
    return {
        "service": "CineMatch Recommendation API",
        "version": "1.0.0",
        "docs": "/docs",
    }