File size: 1,433 Bytes
bcc2f7b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
"""
FastAPI dependencies for the marine species identification API.
"""
from fastapi import HTTPException, status
from app.services.model_service import model_service
from app.core.logging import get_logger
logger = get_logger(__name__)
async def get_model_service():
"""
Dependency to get the model service.
Ensures the model is loaded and available.
"""
try:
await model_service.ensure_model_available()
return model_service
except Exception as e:
logger.error(f"Failed to initialize model service: {str(e)}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Model service unavailable: {str(e)}"
)
async def validate_model_health():
"""
Dependency to validate model health before processing requests.
"""
try:
health_status = await model_service.health_check()
if not health_status.get("model_loaded", False):
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Model is not loaded or unhealthy"
)
return True
except HTTPException:
raise
except Exception as e:
logger.error(f"Model health check failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Model health check failed"
)
|