import os import threading from contextlib import asynccontextmanager from fastapi import FastAPI from routers import extraction, tts from services import tts_service @asynccontextmanager async def lifespan(app: FastAPI): # Warm the TTS model off the request path so the first /tts call doesn't # pay model-load cost. Runs in a daemon thread; if TTS deps are missing it # quietly no-ops and /tts keeps degrading to 503. Disable with TTS_WARMUP=0. if os.getenv("TTS_WARMUP", "1") != "0": threading.Thread(target=tts_service.warmup, daemon=True).start() yield app = FastAPI(title="Smartfolio Python Service", version="1.0.0", lifespan=lifespan) app.include_router(extraction.router) app.include_router(tts.router) @app.get("/health") def health(): return {"status": "ok"}