Spaces:
Runtime error
Runtime error
| import traceback | |
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from contextlib import asynccontextmanager | |
| from fastapi.responses import JSONResponse | |
| from prometheus_client import make_asgi_app | |
| from core.config import settings | |
| from core.database import engine, Base | |
| from routers import diagnose, embedding, offline, admin, auth | |
| from services.ml_service import MLService | |
| from services.vector_service import VectorService | |
| async def lifespan(app: FastAPI): | |
| # Startup | |
| print("=" * 60) | |
| print("π Starting Afiya Care Backend with N-ATLaS") | |
| print("=" * 60) | |
| # Initialize database tables | |
| print("π Creating database tables...") | |
| Base.metadata.create_all(bind=engine) | |
| print("β Database tables created") | |
| # Initialize ML service (includes N-ATLaS) | |
| print("π€ Initializing ML Services...") | |
| app.state.ml_service = MLService() | |
| await app.state.ml_service.initialize() | |
| print("β ML Services initialized") | |
| # Initialize vector service | |
| print("πΎ Initializing Vector Database...") | |
| app.state.vector_service = VectorService() | |
| await app.state.vector_service.initialize() | |
| print("β Vector Database initialized") | |
| print("=" * 60) | |
| print("β Afiya Care Backend Ready!") | |
| print(f"π API Docs: http://localhost:{settings.PORT}/docs") | |
| print(f"π N-ATLaS Languages: Yoruba, Hausa, Igbo, Pidgin, English") | |
| print("=" * 60) | |
| yield | |
| # Shutdown | |
| print("\nπ Shutting down services...") | |
| await app.state.vector_service.close() | |
| print("β Shutdown complete") | |
| # Get port from environment (HF Spaces uses 7860) | |
| PORT = settings.PORT | |
| app = FastAPI( | |
| title=settings.APP_NAME, | |
| description="AI-powered medical assistance with N-ATLaS multilingual support", | |
| version=settings.API_VERSION, | |
| lifespan=lifespan | |
| ) | |
| # CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Configure properly in production | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Prometheus metrics | |
| metrics_app = make_asgi_app() | |
| app.mount("/metrics", metrics_app) | |
| # Include routers | |
| app.include_router(auth.router, prefix=f"/api/{settings.API_VERSION}/auth", tags=["Authentication"]) | |
| app.include_router(diagnose.router, prefix=f"/api/{settings.API_VERSION}", tags=["Diagnosis"]) | |
| app.include_router(embedding.router, prefix=f"/api/{settings.API_VERSION}", tags=["Embeddings"]) | |
| app.include_router(offline.router, prefix=f"/api/{settings.API_VERSION}/offline", tags=["Offline Sync"]) | |
| app.include_router(admin.router, prefix=f"/api/{settings.API_VERSION}/admin", tags=["Admin"]) | |
| async def root(): | |
| return { | |
| "message": "Welcome to Afiya Care API", | |
| "version": settings.API_VERSION, | |
| "model": "N-ATLaS (NCAIR1/N-ATLaS)", | |
| "languages": ["English", "Yoruba", "Hausa", "Igbo", "Nigerian Pidgin"], | |
| "status": "operational", | |
| "docs": f"/docs" | |
| } | |
| async def health_check(): | |
| return { | |
| "status": "healthy", | |
| "database": "connected", | |
| "ml_service": "ready", | |
| "natlas": "ready", | |
| "vector_db": "ready" | |
| } | |
| # Add exception handler | |
| async def global_exception_handler(request: Request, exc: Exception): | |
| """Catch all exceptions and return detailed error""" | |
| print(f"β Global Exception: {exc}") | |
| traceback.print_exc() | |
| return JSONResponse( | |
| status_code=500, | |
| content={ | |
| "detail": str(exc), | |
| "type": type(exc).__name__, | |
| "path": str(request.url) | |
| } | |
| ) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=PORT) |