Files
Athena-Deck/deploy/applio-web/backend/app/api/models.py
T
Mikei386 06cacafbad Add native Applio RVC and preserve the custom studio UI
Pin Applio 3.6.5 and verified basis assets, migrate private voice provenance, and coordinate conversion/live/training with Deck GPU leases. Prepare an inactive voice profile and a restricted studio container. Move CUDA and Torch caches out of the small temporary filesystem to prevent OmniVoice reference failures. Package app updates from the explicit allowlist in one Docker layer.
2026-10-05 10:02:07 +02:00

162 lines
5.2 KiB
Python

"""Model management endpoints."""
from typing import Literal
from fastapi import APIRouter, HTTPException, Request
from pydantic import BaseModel, field_validator
from ..services.models import (
delete_model_conversion_preset,
discover_models,
finalize_training_model,
get_model,
models_without_index,
rename_model,
save_model_conversion_preset,
validate_model_name,
)
router = APIRouter(tags=["models"])
def _ensure_no_active_jobs(request: Request) -> None:
queue = request.app.state.job_queue
store = request.app.state.job_store
from ..jobs.enums import JobStatus
if queue.get_running() or store.count(status=JobStatus.WAITING):
raise HTTPException(
status_code=409,
detail="Modelldateien können erst geändert werden, wenn keine Aufträge mehr laufen oder warten",
)
class RenameModelRequest(BaseModel):
new_name: str
@field_validator("new_name")
@classmethod
def safe_name(cls, value: str) -> str:
return validate_model_name(value)
class FinalizeModelRequest(BaseModel):
source_model_name: str
checkpoint_name: str
final_model_name: str
delete_other_checkpoints: bool = True
delete_training_checkpoints: bool = True
@field_validator("source_model_name", "checkpoint_name", "final_model_name")
@classmethod
def safe_names(cls, value: str) -> str:
return validate_model_name(value)
class ModelConversionPresetRequest(BaseModel):
pitch: int
f0_method: Literal["rmvpe", "crepe", "crepe-tiny", "fcpe"]
index_rate: float
protect: float
volume_envelope: float
export_format: Literal["WAV", "MP3", "FLAC"]
f0_autotune: bool = False
split_audio: bool = False
@field_validator("pitch")
@classmethod
def valid_pitch(cls, value: int) -> int:
if not -24 <= value <= 24:
raise ValueError("Pitch muss zwischen -24 und 24 liegen")
return value
@field_validator("index_rate", "volume_envelope")
@classmethod
def valid_unit_value(cls, value: float) -> float:
if not 0 <= value <= 1:
raise ValueError("Wert muss zwischen 0 und 1 liegen")
return value
@field_validator("protect")
@classmethod
def valid_protect(cls, value: float) -> float:
if not 0 <= value <= 0.5:
raise ValueError("Stimmschutz muss zwischen 0 und 0,5 liegen")
return value
@router.get("/models")
def list_models():
models = discover_models()
return {"models": [m.to_dict() for m in models], "count": len(models)}
@router.get("/models/without-index")
def list_models_without_index():
models = models_without_index()
return {"models": [m.to_dict() for m in models], "count": len(models)}
@router.post("/models/finalize")
def finalize_model(request: Request, body: FinalizeModelRequest):
_ensure_no_active_jobs(request)
try:
model, deleted = finalize_training_model(
body.source_model_name,
body.checkpoint_name,
body.final_model_name,
delete_other_checkpoints=body.delete_other_checkpoints,
delete_training_checkpoints=body.delete_training_checkpoints,
)
except FileNotFoundError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
except FileExistsError as exc:
raise HTTPException(status_code=409, detail=str(exc)) from exc
except (TypeError, ValueError) as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
return {"model": model.to_dict(), "deleted": deleted, "deleted_count": len(deleted)}
@router.get("/models/{name}")
def get_model_detail(name: str):
model = get_model(name)
if model is None:
raise HTTPException(status_code=404, detail=f"Modell '{name}' nicht gefunden")
return model.to_dict()
@router.put("/models/{name}/conversion-preset")
def save_conversion_preset(name: str, body: ModelConversionPresetRequest):
try:
model = save_model_conversion_preset(name, body.model_dump())
except FileNotFoundError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
except (TypeError, ValueError) as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
return {"model": model.to_dict()}
@router.delete("/models/{name}/conversion-preset")
def delete_conversion_preset(name: str):
try:
model = delete_model_conversion_preset(name)
except FileNotFoundError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
except (TypeError, ValueError) as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
return {"model": model.to_dict()}
@router.patch("/models/{name}")
def rename_model_endpoint(request: Request, name: str, body: RenameModelRequest):
_ensure_no_active_jobs(request)
try:
model = rename_model(name, body.new_name)
except FileNotFoundError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
except FileExistsError as exc:
raise HTTPException(status_code=409, detail=str(exc)) from exc
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
return {"model": model.to_dict()}