Files
bakery-ia/services/training/app/api/models.py

93 lines
2.8 KiB
Python
Raw Normal View History

"""
Models API endpoints
"""
2025-07-28 19:28:39 +02:00
from fastapi import APIRouter, Depends, HTTPException, status, Path
from sqlalchemy.ext.asyncio import AsyncSession
from typing import List
2025-07-18 14:41:39 +02:00
import structlog
from app.core.database import get_db
from app.schemas.training import TrainedModelResponse
from app.services.training_service import TrainingService
2025-07-28 19:28:39 +02:00
from datetime import datetime
2025-07-21 20:43:17 +02:00
from shared.auth.decorators import (
get_current_tenant_id_dep
)
2025-07-19 16:59:37 +02:00
2025-07-18 14:41:39 +02:00
logger = structlog.get_logger()
router = APIRouter()
training_service = TrainingService()
2025-07-28 19:28:39 +02:00
@router.get("/tenants/{tenant_id}/models/{product_name}/active")
async def get_active_model(
tenant_id: str = Path(..., description="Tenant ID"),
product_name: str = Path(..., description="Product name"),
db: AsyncSession = Depends(get_db)
):
2025-07-28 19:28:39 +02:00
"""
Get the active model for a product - used by forecasting service
"""
try:
2025-07-28 19:28:39 +02:00
query = """
SELECT * FROM trained_models
WHERE tenant_id = :tenant_id
AND product_name = :product_name
AND is_active = true
AND is_production = true
ORDER BY created_at DESC
LIMIT 1
"""
result = await db.execute(query, {
"tenant_id": tenant_id,
"product_name": product_name
})
model_record = result.fetchone()
if not model_record:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"No active model found for product {product_name}"
)
# Update last_used_at
update_query = """
UPDATE trained_models
SET last_used_at = :now
WHERE id = :model_id
"""
await db.execute(update_query, {
"now": datetime.utcnow(),
"model_id": model_record.id
})
await db.commit()
return {
"model_id": model_record.id,
"model_path": model_record.model_path,
"features_used": model_record.features_used,
"hyperparameters": model_record.hyperparameters,
"training_metrics": {
"mape": model_record.mape,
"mae": model_record.mae,
"rmse": model_record.rmse,
"r2_score": model_record.r2_score
},
"created_at": model_record.created_at.isoformat(),
"training_period": {
"start_date": model_record.training_start_date.isoformat(),
"end_date": model_record.training_end_date.isoformat()
}
}
except Exception as e:
2025-07-28 19:28:39 +02:00
logger.error(f"Failed to get active model: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
2025-07-28 19:28:39 +02:00
detail="Failed to retrieve model"
)