Files
ai-training-monitor/backend/app/api/routes/comparison.py
2025-01-31 13:20:46 +01:00

69 lines
2.5 KiB
Python

# app/api/routes/comparison.py
from fastapi import APIRouter, HTTPException
from starlette.responses import FileResponse
from app.models.comparison import PathRequest
from app.services.comparison_service import ComparisonService
router = APIRouter()
comparison_service = ComparisonService()
@router.post("/register")
async def register_comparison_path(request: PathRequest):
"""Register a new comparison path and get its ID."""
config_id = comparison_service.register_config(request.path)
return {"config_id": config_id}
@router.get("/image/{config_id}/{config_name}/{model_name}/{filename}")
async def get_comparison_image(config_id: str, config_name: str, model_name: str, filename: str):
"""Serve image files using the cached base path."""
base_path = comparison_service.get_base_path(config_id)
if not base_path:
raise HTTPException(status_code=404, detail="Configuration not found")
try:
full_path = base_path / config_name / model_name / filename
if not full_path.exists() or not full_path.is_file():
raise HTTPException(status_code=404, detail="Image not found")
return FileResponse(full_path)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/{config_id}/available")
async def fetch_available_configs(config_id: str):
"""Fetch available configs."""
base_path = comparison_service.get_base_path(config_id)
if not base_path:
raise HTTPException(status_code=404, detail="Configuration not found")
try:
return comparison_service.get_available_configs(str(base_path))
except ValueError as e:
# Convert ValueError from the service into a proper HTTP error
raise HTTPException(status_code=404, detail=str(e))
@router.get("/{config_id}/{config_name}")
async def fetch_config(config_id: str, config_name: str):
"""
Fetch detailed comparison data for a specific configuration.
Parameters:
config_id: The identifier returned from the initial path registration
config_name: The name of the specific configuration to load (e.g. 'cloth_lora')
"""
base_path = comparison_service.get_base_path(config_id)
if not base_path:
raise HTTPException(status_code=404, detail="Configuration not found")
try:
return comparison_service.load_config_data(str(base_path), config_name)
except ValueError as e:
# Convert ValueError from the service into a proper HTTP error
raise HTTPException(status_code=404, detail=str(e))