"""FastAPI application factory.""" from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from customer_intelligence.api.dependencies import _load_churn_model, _load_segmentation_model from customer_intelligence.api.routers import customers, health, predictions @asynccontextmanager async def lifespan(app: FastAPI): # Pre-load models at startup so first request is fast try: _load_churn_model() _load_segmentation_model() print("Models loaded from MLflow registry.") except Exception as e: print(f"WARNING: Could not pre-load models: {e}") yield app = FastAPI( title="Customer Intelligence API", description="Churn prediction and customer segmentation for Olist e-commerce data", version="1.0.0", lifespan=lifespan, ) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) app.include_router(health.router) app.include_router(customers.router) app.include_router(predictions.router) @app.get("/") def root(): return { "name": "Customer Intelligence API", "docs": "/docs", "health": "/health", }