Spaces:
Runtime error
Runtime error
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import RedirectResponse | |
| from app.config import settings | |
| from app.database import connect_db, close_db | |
| from app.routes import vitals, auth, devices, health, predictions | |
| async def lifespan(app: FastAPI): | |
| await connect_db(settings.MONGODB_URI, settings.DATABASE_NAME) | |
| # Load ML models (non-blocking — server starts even if models aren't ready) | |
| try: | |
| from app.services.ml_service import load_models | |
| load_models() | |
| except Exception as e: | |
| print(f"[ML] Could not load models: {e}") | |
| print("[ML] Server will run without predictions until models are available.") | |
| yield | |
| await close_db() | |
| app = FastAPI( | |
| title="Cardiac Monitor API", | |
| version="1.0.0", | |
| description="Backend for ESP32 Heart Rate, SpO2 & ECG Monitor", | |
| lifespan=lifespan, | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| async def root(): | |
| return RedirectResponse(url="/docs") | |
| app.include_router(health.router, prefix="/api/v1", tags=["health"]) | |
| app.include_router(auth.router, prefix="/api/v1/auth", tags=["auth"]) | |
| app.include_router(vitals.router, prefix="/api/v1/vitals", tags=["vitals"]) | |
| app.include_router(predictions.router, prefix="/api/v1/predictions", tags=["predictions"]) | |
| app.include_router(devices.router, prefix="/api/v1/devices", tags=["devices"]) | |