Download src/main.py from Distopia22/icd-cpt-coding-api-backend: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/spaces/Distopia22/icd-cpt-coding-api-backend/resolve/764e30e4516861d7fef3c19bacc1d5486a9aedfa/src/main.py
- Command line
-
hf download hf://spaces/Distopia22/icd-cpt-coding-api-backend@764e30e4516861d7fef3c19bacc1d5486a9aedfa/src/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Distopia22/icd-cpt-coding-api-backend/resolve/764e30e4516861d7fef3c19bacc1d5486a9aedfa/src/main.py
2.42 kB
| import logging | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from api.routes import router | |
| from config.settings import settings | |
| # Configure logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' | |
| ) | |
| logger = logging.getLogger(__name__) | |
| async def lifespan(app: FastAPI): | |
| """ | |
| Lifespan event handler for startup and shutdown | |
| """ | |
| # Startup | |
| logger.info("=" * 50) | |
| logger.info(f"🚀 {settings.api_title} v{settings.api_version}") | |
| logger.info("=" * 50) | |
| if settings.groq_api_key: | |
| logger.info(f"✅ Groq API Key: Configured (length: {len(settings.groq_api_key)})") | |
| logger.info(f"✅ Groq Model: {settings.groq_model}") | |
| else: | |
| logger.error("❌ GROQ_API_KEY is NOT set!") | |
| logger.error("⚠️ API will NOT function without valid API key") | |
| logger.info("=" * 50) | |
| yield | |
| # Shutdown | |
| logger.info("👋 Shutting down API...") | |
| # Create FastAPI app with lifespan | |
| app = FastAPI( | |
| title=settings.api_title, | |
| version=settings.api_version, | |
| description=settings.api_description, | |
| lifespan=lifespan | |
| ) | |
| # CORS configuration | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Include routes | |
| app.include_router(router, prefix="/api") | |
| async def root(): | |
| """Root endpoint""" | |
| return { | |
| "service": settings.api_title, | |
| "version": settings.api_version, | |
| "status": "running", | |
| "endpoints": { | |
| "docs": "/docs", | |
| "health": "/api/v1/health", | |
| "coding": "/api/coding", | |
| "upload": "/api/upload-file" | |
| } | |
| } | |
| async def health_check(): | |
| """Health check endpoint""" | |
| api_key_status = "configured" if settings.groq_api_key else "missing" | |
| return { | |
| "status": "healthy", | |
| "service": settings.api_title, | |
| "version": settings.api_version, | |
| "groq_api_key": api_key_status, | |
| "groq_model": settings.groq_model | |
| } | |
| # Run server when executed directly | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run( | |
| "main:app", | |
| host="0.0.0.0", | |
| port=7860, | |
| reload=False | |
| ) |