fishapi / app /api /dependencies.py
kamau1's picture
Initial commit
bcc2f7b verified
"""
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"
)