745 lines
30 KiB
Python
Executable File
745 lines
30 KiB
Python
Executable File
"""
|
|
Model management router — list, download, pause, cancel model downloads.
|
|
Models are downloaded from HuggingFace on-demand or pre-downloaded.
|
|
"""
|
|
import json
|
|
import shutil
|
|
import tempfile
|
|
import threading
|
|
from dataclasses import dataclass, field
|
|
from enum import Enum
|
|
from pathlib import Path
|
|
from typing import Dict, List, Optional
|
|
|
|
from fastapi import APIRouter, HTTPException, BackgroundTasks
|
|
from pydantic import BaseModel
|
|
|
|
from ..config import (
|
|
ALL_MODELS,
|
|
BS_ROFORMER_MODEL_DIR,
|
|
MEL_BAND_MODEL_DIR,
|
|
MDXNET_MODEL_DIR,
|
|
SCNET_MODEL_DIR,
|
|
MEDLEYVOX_MODEL_DIR,
|
|
APOLLO_MODEL_DIR,
|
|
ASPIRATION_MODEL_DIR,
|
|
BANDIT_MODEL_DIR,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/models", tags=["models"])
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Model metadata with download info
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class ModelDownloadStatus(str, Enum):
|
|
NOT_DOWNLOADED = "not_downloaded"
|
|
DOWNLOADING = "downloading"
|
|
PAUSED = "paused"
|
|
COMPLETED = "downloaded" # matches the frontend's status === 'downloaded' checks
|
|
ERROR = "error"
|
|
CANCELLED = "cancelled"
|
|
|
|
|
|
@dataclass
|
|
class DownloadProgress:
|
|
model_id: str
|
|
status: ModelDownloadStatus
|
|
progress_percent: float = 0.0
|
|
bytes_downloaded: int = 0
|
|
total_bytes: int = 0
|
|
speed_mbps: float = 0.0
|
|
error_message: Optional[str] = None
|
|
|
|
# Control flags (threading.Event so they work correctly in background threads)
|
|
_pause_event: threading.Event = field(default_factory=threading.Event)
|
|
_cancel_event: threading.Event = field(default_factory=threading.Event)
|
|
_thread: Optional[threading.Thread] = None
|
|
|
|
def __post_init__(self):
|
|
self._pause_event.set() # Not paused by default
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"model_id": self.model_id,
|
|
"status": self.status.value,
|
|
"progress_percent": round(self.progress_percent, 1),
|
|
"bytes_downloaded": self.bytes_downloaded,
|
|
"total_bytes": self.total_bytes,
|
|
"speed_mbps": round(self.speed_mbps, 2),
|
|
"error_message": self.error_message,
|
|
}
|
|
|
|
|
|
# Global download tracking
|
|
download_progress: Dict[str, DownloadProgress] = {}
|
|
|
|
|
|
# Model download metadata (approximate sizes from HuggingFace)
|
|
MODEL_DOWNLOAD_INFO = {
|
|
# Demucs models (downloaded automatically by the demucs library on first use via torch.hub)
|
|
"htdemucs": {
|
|
"size_mb": 80,
|
|
"description": "Demucs v4 hybrid transformer model. 4-stem separation (vocals, drums, bass, other). State-of-the-art quality; downloaded automatically on first use.",
|
|
"hf_repo": None,
|
|
"files": [],
|
|
},
|
|
"htdemucs_ft": {
|
|
"size_mb": 400,
|
|
"description": "Fine-tuned Demucs v4 with separate per-stem models. Improved vocal and drum quality over htdemucs; downloaded automatically on first use.",
|
|
"hf_repo": None,
|
|
"files": [],
|
|
},
|
|
"mdx_extra_q": {
|
|
"size_mb": 500,
|
|
"description": "Demucs v3 MDX Extra Quantized — quantized for faster inference and lower memory usage. Legacy model, superseded by htdemucs; downloaded automatically on first use.",
|
|
"hf_repo": None,
|
|
"files": [],
|
|
},
|
|
"mdx": {
|
|
"size_mb": 280,
|
|
"description": "Demucs v3 baseline 4-stem model (vocals, drums, bass, other). Legacy predecessor to htdemucs; downloaded automatically on first use.",
|
|
"hf_repo": None,
|
|
"files": [],
|
|
},
|
|
|
|
# BS-RoFormer standard models — all share the same SW-6 weights from jarredou/BS-ROFO-SW-Fixed.
|
|
# The 4/5/6-stem "modes" are produced by merging the 6-stem outputs in bs_roformer_separator.py.
|
|
"bs_roformer_4stem": {
|
|
"size_mb": 204,
|
|
"description": "BS-RoFormer 4-stem separation (vocals, drums, bass, other). Good balance of quality and speed.",
|
|
"hf_repo": "jarredou/BS-ROFO-SW-Fixed",
|
|
"files": ["BS-Rofo-SW-Fixed.ckpt", "BS-Rofo-SW-Fixed.yaml"],
|
|
},
|
|
"bs_roformer_5stem_guitar": {
|
|
"size_mb": 204,
|
|
"description": "BS-RoFormer 5-stem with guitar separation (vocals, drums, bass, guitar, other).",
|
|
"hf_repo": "jarredou/BS-ROFO-SW-Fixed",
|
|
"files": ["BS-Rofo-SW-Fixed.ckpt", "BS-Rofo-SW-Fixed.yaml"],
|
|
},
|
|
"bs_roformer_5stem_piano": {
|
|
"size_mb": 204,
|
|
"description": "BS-RoFormer 5-stem with piano separation (vocals, drums, bass, piano, other).",
|
|
"hf_repo": "jarredou/BS-ROFO-SW-Fixed",
|
|
"files": ["BS-Rofo-SW-Fixed.ckpt", "BS-Rofo-SW-Fixed.yaml"],
|
|
},
|
|
"bs_roformer_6stem": {
|
|
"size_mb": 204,
|
|
"description": "BS-RoFormer 6-stem full separation (vocals, drums, bass, guitar, piano, other).",
|
|
"hf_repo": "jarredou/BS-ROFO-SW-Fixed",
|
|
"files": ["BS-Rofo-SW-Fixed.ckpt", "BS-Rofo-SW-Fixed.yaml"],
|
|
},
|
|
"bs_roformer_chorus": {
|
|
"size_mb": 204,
|
|
"description": "BS-RoFormer specialized for male/female chorus separation.",
|
|
"hf_repo": None, # No public download URL — install manually
|
|
"files": ["model.ckpt", "config.yaml"],
|
|
},
|
|
"bs_roformer_anvuew": {
|
|
"size_mb": 195,
|
|
"description": "BS-RoFormer Anvuew variant optimized for vocal separation (SDR 12.45).",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["BS-RoFormer-Anvuew/bs_roformer_anvuew_sdr_12.45.ckpt", "BS-RoFormer-Anvuew/config.yaml"],
|
|
},
|
|
"bs_roformer_karaoke_anvuew": {
|
|
"size_mb": 195,
|
|
"description": "BS-RoFormer Karaoke Anvuew for lead/backing separation.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Karaoke-BS-RoFormer-Anvuew/karaoke_bs_roformer_anvuew.ckpt", "Karaoke-BS-RoFormer-Anvuew/karaoke_bs_roformer_anvuew.yaml"],
|
|
},
|
|
"bs_roformer_sw_6stem": {
|
|
"size_mb": 180,
|
|
"description": "BS-RoFormer SW (Swin Transformer) 6-stem variant.",
|
|
"hf_repo": None, # No public download URL — install manually
|
|
"files": ["model.ckpt"],
|
|
},
|
|
|
|
# MelBand RoFormer models
|
|
"mel_band_roformer_4stem": {
|
|
"size_mb": 170,
|
|
"description": "MelBand RoFormer 4-stem separation. Efficient alternative to BS-RoFormer.",
|
|
"hf_repo": None, # No public download URL — install manually
|
|
"files": ["model.ckpt", "config.yaml"],
|
|
},
|
|
"mel_band_vocals": {
|
|
"size_mb": 170,
|
|
"description": "MelBand RoFormer optimized for 2-stem vocals/instrumental separation.",
|
|
"hf_repo": "becruily/mel-band-roformer-vocals",
|
|
"files": ["model.ckpt", "config.yaml"],
|
|
},
|
|
"mel_band_vocals_anvuew": {
|
|
"size_mb": 195,
|
|
"description": "MelBand Vocals Anvuew variant with enhanced vocal extraction.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["BS-RoFormer-Anvuew/bs_roformer_anvuew_sdr_12.45.ckpt", "BS-RoFormer-Anvuew/config.yaml"],
|
|
},
|
|
"mel_band_dereverb": {
|
|
"size_mb": 170,
|
|
"description": "MelBand RoFormer for reverb removal - separates dry vocals from reverb.",
|
|
"hf_repo": "anvuew/dereverb_mel_band_roformer",
|
|
"files": ["dereverb_mel_band_roformer_anvuew_sdr_19.1729.ckpt", "config.yaml"],
|
|
},
|
|
"mel_band_dereverb_anvuew": {
|
|
"size_mb": 195,
|
|
"description": "MelBand Dereverb Anvuew variant (SDR 19.17) - aggressive reverb removal.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Dereverb-MelBand-Anvuew/dereverb_mel_band_roformer_anvuew_sdr_19.1729.ckpt", "Dereverb-MelBand-Anvuew/dereverb_mel_band_roformer_anvuew.yaml"],
|
|
},
|
|
"mel_band_dereverb_room": {
|
|
"size_mb": 195,
|
|
"description": "MelBand Room Dereverb for room/ambient reverb removal (SDR 13.74).",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Dereverb-Room-Anvuew/dereverb_room_anvuew_sdr_13.7432.ckpt", "Dereverb-Room-Anvuew/dereverb_room_anvuew.yaml"],
|
|
},
|
|
"mel_band_karaoke": {
|
|
"size_mb": 170,
|
|
"description": "MelBand RoFormer for karaoke - separates lead vocals from backing+instrumental.",
|
|
"hf_repo": "becruily/mel-band-roformer-karaoke",
|
|
"files": ["model.ckpt", "config.yaml"],
|
|
},
|
|
"mel_band_karaoke_gabox": {
|
|
"size_mb": 195,
|
|
"description": "MelBand Karaoke Gabox variant optimized for backing vocal separation.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["MelBandRoformers/bsroformers/bs_karaoke_gabox_IS.ckpt", "MelBandRoformers/bsroformers/karaoke_bs_roformer.yaml"],
|
|
},
|
|
"mel_band_deux_becruily": {
|
|
"size_mb": 180,
|
|
"description": "MelBand RoFormer Deux Becruily - specialized 2-stem vocals model.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["MelBand-Roformer-Deux-Becruily/becruily_deux.ckpt", "MelBand-Roformer-Deux-Becruily/config_deux_becruily.yaml"],
|
|
},
|
|
|
|
# MDX-Net models
|
|
"mdxnet_voc_ft": {
|
|
"size_mb": 45,
|
|
"description": "MDX-Net Vocals Fine-tuned - lightweight vocals model (ONNX).",
|
|
"hf_repo": "Politrees/UVR_resources",
|
|
"files": ["models/MDXNet/UVR-MDX-NET-Voc_FT.onnx"],
|
|
},
|
|
"mdxnet_inst_hq_1": {
|
|
"size_mb": 45,
|
|
"description": "MDX-Net Instrumental HQ 1 - focused on instrumental extraction.",
|
|
"hf_repo": "Politrees/UVR_resources",
|
|
"files": ["models/MDXNet/UVR-MDX-NET-Inst_HQ_1.onnx"],
|
|
},
|
|
"mdxnet_inst_hq_2": {
|
|
"size_mb": 45,
|
|
"description": "MDX-Net Instrumental HQ 2 - alternative instrumental model.",
|
|
"hf_repo": "Politrees/UVR_resources",
|
|
"files": ["models/MDXNet/UVR-MDX-NET-Inst_HQ_2.onnx"],
|
|
},
|
|
"mdxnet_karaoke": {
|
|
"size_mb": 45,
|
|
"description": "MDX-Net Karaoke - separates lead from backing vocals.",
|
|
"hf_repo": "Politrees/UVR_resources",
|
|
"files": ["models/MDXNet/UVR_MDXNET_KARA.onnx"],
|
|
},
|
|
"mdxnet_dereverb": {
|
|
"size_mb": 195,
|
|
"description": "MDX-Net Dereverb via MelBand Anvuew architecture.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Dereverb-MelBand-Anvuew/dereverb_mel_band_roformer_anvuew_sdr_19.1729.ckpt"],
|
|
},
|
|
"mdxnet_drumsep": {
|
|
"size_mb": 448,
|
|
"description": "MDX23C DrumSep - specialized drum separation model.",
|
|
"hf_repo": "Politrees/UVR_resources",
|
|
"files": ["models/MDX23C/MDX23C-DrumSep-aufr33-jarredou.ckpt"],
|
|
},
|
|
|
|
# SCNet XL models (2024-2025 state-of-the-art)
|
|
"scnet_xl": {
|
|
"size_mb": 350,
|
|
"description": "SCNet XL - State-of-the-art 2-stem separation (SDR 11.93 vocals, 18.23 instrumental).",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Politrees/UVR_resources/models/SCnet/scnet_checkpoint_musdb18.ckpt", "Politrees/UVR_resources/models/SCnet/config_musdb18_scnet_xl.yaml"],
|
|
},
|
|
"scnet_xl_ihf": {
|
|
"size_mb": 350,
|
|
"description": "SCNet XL IHF variant - optimized for instrumental extraction.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Politrees/UVR_resources/models/SCnet/scnet_checkpoint_musdb18.ckpt", "Politrees/UVR_resources/models/SCnet/config_musdb18_scnet_xl.yaml"],
|
|
},
|
|
"scnet_large": {
|
|
"size_mb": 200,
|
|
"description": "SCNet Large - 4-stem variant with excellent quality.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Politrees/UVR_resources/models/SCnet/SCNet-large_starrytong_fixed.ckpt", "Politrees/UVR_resources/models/SCnet/config_musdb18_scnet_large_starrytong.yaml"],
|
|
},
|
|
"scnet_large_starrytong": {
|
|
"size_mb": 200,
|
|
"description": "SCNet Large Starrytong - fixed variant with improved stability.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Politrees/UVR_resources/models/SCnet/SCNet-large_starrytong_fixed.ckpt", "Politrees/UVR_resources/models/SCnet/config_musdb18_scnet_large_starrytong.yaml"],
|
|
},
|
|
|
|
# MedleyVox models
|
|
"medleyvox": {
|
|
"size_mb": 170,
|
|
"description": "MedleyVox - Multi-singer vocal separation for ensemble vocals.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["MedleyVox-MultiSinger/vocals 100.pth"],
|
|
},
|
|
"medleyvox_55ep": {
|
|
"size_mb": 170,
|
|
"description": "MedleyVox 55 Epochs - earlier training checkpoint.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["MedleyVox-MultiSinger/vocals 55 epochs.pth"],
|
|
},
|
|
"medleyvox_66ep": {
|
|
"size_mb": 170,
|
|
"description": "MedleyVox 66 Epochs - intermediate checkpoint.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["MedleyVox-MultiSinger/vocals 66.pth"],
|
|
},
|
|
"medleyvox_88ep": {
|
|
"size_mb": 170,
|
|
"description": "MedleyVox 88 Epochs - near-final checkpoint.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["MedleyVox-MultiSinger/vocals 88.pth"],
|
|
},
|
|
"medleyvox_100ep": {
|
|
"size_mb": 170,
|
|
"description": "MedleyVox 100 Epochs - final training checkpoint.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["MedleyVox-MultiSinger/vocals 100.pth"],
|
|
},
|
|
|
|
# Apollo models
|
|
"apollo_vocals": {
|
|
"size_mb": 400,
|
|
"description": "Apollo Vocals - Vocal enhancement and reverb removal (MSST).",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Apollo-Vocal-MSST/epoch=54-val_loss=-17.6221.ckpt", "Apollo-Vocal-MSST/config_apollo_vocals_ep_54.yaml"],
|
|
},
|
|
"apollo_edm": {
|
|
"size_mb": 200,
|
|
"description": "Apollo EDM - Optimized for EDM vocal enhancement.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Politrees/UVR_resources/models/Apollo/apollo_edm_by_essid.ckpt", "Politrees/UVR_resources/models/Apollo/apollo_edm_by_essid.yaml"],
|
|
},
|
|
"apollo_edm_big": {
|
|
"size_mb": 300,
|
|
"description": "Apollo EDM Big - Larger EDM vocal enhancement model.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Politrees/UVR_resources/models/Apollo/apollo_edm_big_by_essid.ckpt", "Politrees/UVR_resources/models/Apollo/apollo_edm_big_by_essid.yaml"],
|
|
},
|
|
|
|
# Aspiration models
|
|
"aspiration_full": {
|
|
"size_mb": 200,
|
|
"description": "Aspiration Removal (Full) - Aggressive breath/aspiration removal (SDR 18.98).",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Aspiration-MelBand-Sucial/aspiration_mel_band_roformer_sdr_18.9845.ckpt", "Aspiration-MelBand-Sucial/config_aspiration_mel_band_roformer.yaml"],
|
|
},
|
|
"aspiration_light": {
|
|
"size_mb": 200,
|
|
"description": "Aspiration Removal (Light) - Gentler breath removal (SDR 18.12).",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Aspiration-MelBand-Sucial/aspiration_mel_band_roformer_less_aggr_sdr_18.1201.ckpt", "Aspiration-MelBand-Sucial/config_aspiration_mel_band_roformer.yaml"],
|
|
},
|
|
|
|
# Bandit models
|
|
"bandit_v2_multi": {
|
|
"size_mb": 250,
|
|
"description": "Bandit v2 Multi-Language - Multi-language vocal separation.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Bandit-v2/checkpoint-multi_state_dict.ckpt", "Bandit-v2/config_dnr_bandit_v2_mus64.yaml"],
|
|
},
|
|
"bandit_v2_eng": {
|
|
"size_mb": 250,
|
|
"description": "Bandit v2 English - Optimized for English vocals.",
|
|
"hf_repo": "miercolesv/MVSEP-Central-Backup",
|
|
"files": ["Bandit-v2/checkpoint-eng_state_dict.ckpt", "Bandit-v2/config_dnr_bandit_v2_mus64.yaml"],
|
|
},
|
|
|
|
# Ensemble modes (no model files — combine outputs from multiple other models)
|
|
"ensemble_vocal_best": {
|
|
"size_mb": 0,
|
|
"description": "Ensemble combining multiple top vocal separators (BS-RoFormer + SCNet) for best possible vocal quality. Requires constituent models to be downloaded.",
|
|
"hf_repo": None,
|
|
"files": [],
|
|
},
|
|
"ensemble_4stem": {
|
|
"size_mb": 0,
|
|
"description": "4-stem ensemble combining BS-RoFormer and SCNet Large outputs for vocals, drums, bass, and other. Higher quality than any single model.",
|
|
"hf_repo": None,
|
|
"files": [],
|
|
},
|
|
"ensemble_6stem": {
|
|
"size_mb": 0,
|
|
"description": "Full 6-stem ensemble combining multiple models for vocals, drums, bass, guitar, piano, and other. Highest quality multi-instrument separation.",
|
|
"hf_repo": None,
|
|
"files": [],
|
|
},
|
|
}
|
|
|
|
|
|
# Explicit mapping from model_id to the exact directory its separator service uses.
|
|
# Must stay in sync with the DEFAULT_MODEL_DIR / model_dir values in each *_separator.py.
|
|
MODEL_EXACT_DIRS: Dict[str, Path] = {
|
|
# ── BS-RoFormer standard (all share the same weights flat in the family dir) ──
|
|
"bs_roformer_4stem": BS_ROFORMER_MODEL_DIR,
|
|
"bs_roformer_5stem_guitar": BS_ROFORMER_MODEL_DIR,
|
|
"bs_roformer_5stem_piano": BS_ROFORMER_MODEL_DIR,
|
|
"bs_roformer_6stem": BS_ROFORMER_MODEL_DIR,
|
|
# ── BS-RoFormer specialized ──
|
|
"bs_roformer_chorus": BS_ROFORMER_MODEL_DIR / "chorus",
|
|
"bs_roformer_sw_6stem": BS_ROFORMER_MODEL_DIR, # flat; hf_repo=None
|
|
"bs_roformer_anvuew": MEL_BAND_MODEL_DIR / "vocals_anvuew",
|
|
"bs_roformer_karaoke_anvuew": MEL_BAND_MODEL_DIR / "karaoke_gabox",
|
|
# ── MelBand RoFormer ──
|
|
"mel_band_roformer_4stem": MEL_BAND_MODEL_DIR, # flat; hf_repo=None
|
|
"mel_band_vocals": MEL_BAND_MODEL_DIR / "vocals",
|
|
"mel_band_vocals_anvuew": MEL_BAND_MODEL_DIR / "vocals_anvuew",
|
|
"mel_band_dereverb": MEL_BAND_MODEL_DIR / "dereverb",
|
|
"mel_band_dereverb_anvuew": MEL_BAND_MODEL_DIR / "dereverb_anvuew",
|
|
"mel_band_dereverb_room": MEL_BAND_MODEL_DIR / "dereverb_room",
|
|
"mel_band_karaoke": MEL_BAND_MODEL_DIR / "karaoke",
|
|
"mel_band_karaoke_gabox": MEL_BAND_MODEL_DIR / "karaoke_gabox",
|
|
"mel_band_deux_becruily": MEL_BAND_MODEL_DIR / "deux_becruily",
|
|
# ── MDX-Net (flat family dir — separator uses basename only) ──
|
|
"mdxnet_voc_ft": MDXNET_MODEL_DIR,
|
|
"mdxnet_inst_hq_1": MDXNET_MODEL_DIR,
|
|
"mdxnet_inst_hq_2": MDXNET_MODEL_DIR,
|
|
"mdxnet_karaoke": MDXNET_MODEL_DIR,
|
|
"mdxnet_dereverb": MEL_BAND_MODEL_DIR / "dereverb", # routed to MelBand
|
|
"mdxnet_drumsep": MDXNET_MODEL_DIR,
|
|
# ── SCNet (per-model subdir) ──
|
|
"scnet_xl": SCNET_MODEL_DIR / "scnet_xl",
|
|
"scnet_xl_ihf": SCNET_MODEL_DIR / "scnet_xl_ihf",
|
|
"scnet_large": SCNET_MODEL_DIR / "scnet_large",
|
|
"scnet_large_starrytong": SCNET_MODEL_DIR / "scnet_large_starrytong",
|
|
# ── MedleyVox (per-model subdir) ──
|
|
"medleyvox": MEDLEYVOX_MODEL_DIR / "medleyvox",
|
|
"medleyvox_55ep": MEDLEYVOX_MODEL_DIR / "medleyvox_55ep",
|
|
"medleyvox_66ep": MEDLEYVOX_MODEL_DIR / "medleyvox_66ep",
|
|
"medleyvox_88ep": MEDLEYVOX_MODEL_DIR / "medleyvox_88ep",
|
|
"medleyvox_100ep": MEDLEYVOX_MODEL_DIR / "medleyvox_100ep",
|
|
# ── Apollo (per-model subdir) ──
|
|
"apollo_vocals": APOLLO_MODEL_DIR / "apollo_vocals",
|
|
"apollo_edm": APOLLO_MODEL_DIR / "apollo_edm",
|
|
"apollo_edm_big": APOLLO_MODEL_DIR / "apollo_edm_big",
|
|
# ── Aspiration (per-model subdir) ──
|
|
"aspiration_full": ASPIRATION_MODEL_DIR / "aspiration_full",
|
|
"aspiration_light": ASPIRATION_MODEL_DIR / "aspiration_light",
|
|
# ── Bandit (per-model subdir) ──
|
|
"bandit_v2_multi": BANDIT_MODEL_DIR / "bandit_v2_multi",
|
|
"bandit_v2_eng": BANDIT_MODEL_DIR / "bandit_v2_eng",
|
|
}
|
|
|
|
|
|
def get_model_directory(model_id: str) -> Path:
|
|
"""Get the exact directory where a model's files are stored on disk."""
|
|
return MODEL_EXACT_DIRS.get(model_id, BS_ROFORMER_MODEL_DIR / model_id)
|
|
|
|
|
|
def is_model_downloaded(model_id: str) -> bool:
|
|
"""Check if a model is already downloaded."""
|
|
if model_id not in MODEL_DOWNLOAD_INFO:
|
|
return False
|
|
|
|
model_dir = get_model_directory(model_id)
|
|
info = MODEL_DOWNLOAD_INFO[model_id]
|
|
|
|
for file_path in info["files"]:
|
|
flat = model_dir / Path(file_path).name # flat download (our convention)
|
|
deep = model_dir / file_path # subdirectory structure
|
|
if not flat.exists() and not deep.exists():
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# API Models
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class ModelInfo(BaseModel):
|
|
id: str
|
|
label: str
|
|
group: str
|
|
description: str
|
|
size_mb: int
|
|
status: str # not_downloaded, downloaded, downloading, paused, error
|
|
progress_percent: float = 0.0
|
|
|
|
|
|
class ModelDownloadRequest(BaseModel):
|
|
model_id: str
|
|
|
|
|
|
class ModelDownloadResponse(BaseModel):
|
|
success: bool
|
|
message: str
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Endpoints
|
|
# ---------------------------------------------------------------------------
|
|
|
|
@router.get("/list", response_model=List[ModelInfo])
|
|
def list_models():
|
|
"""List all available models with their download status."""
|
|
result = []
|
|
|
|
for model in ALL_MODELS:
|
|
model_id = model["id"]
|
|
info = MODEL_DOWNLOAD_INFO.get(model_id, {})
|
|
|
|
# Check current download status
|
|
if model_id in download_progress:
|
|
prog = download_progress[model_id]
|
|
status = prog.status.value
|
|
progress = prog.progress_percent
|
|
elif is_model_downloaded(model_id):
|
|
status = ModelDownloadStatus.COMPLETED.value
|
|
progress = 100.0
|
|
else:
|
|
status = ModelDownloadStatus.NOT_DOWNLOADED.value
|
|
progress = 0.0
|
|
|
|
result.append(ModelInfo(
|
|
id=model_id,
|
|
label=model["label"],
|
|
group=model.get("group", "Other"),
|
|
description=info.get("description", "No description available."),
|
|
size_mb=info.get("size_mb", 0),
|
|
status=status,
|
|
progress_percent=progress,
|
|
))
|
|
|
|
return result
|
|
|
|
|
|
@router.post("/download", response_model=ModelDownloadResponse)
|
|
def download_model(body: ModelDownloadRequest, background_tasks: BackgroundTasks):
|
|
"""Start downloading a model in the background."""
|
|
model_id = body.model_id
|
|
|
|
if model_id not in MODEL_DOWNLOAD_INFO:
|
|
raise HTTPException(status_code=404, detail=f"Unknown model: {model_id}")
|
|
|
|
# Check if already downloaded
|
|
if is_model_downloaded(model_id):
|
|
return ModelDownloadResponse(success=True, message=f"Model {model_id} is already downloaded.")
|
|
|
|
# Check if already downloading
|
|
if model_id in download_progress:
|
|
prog = download_progress[model_id]
|
|
if prog.status == ModelDownloadStatus.DOWNLOADING:
|
|
return ModelDownloadResponse(success=True, message=f"Download already in progress for {model_id}.")
|
|
elif prog.status == ModelDownloadStatus.PAUSED:
|
|
# Resume
|
|
prog._pause_event.set()
|
|
prog.status = ModelDownloadStatus.DOWNLOADING
|
|
return ModelDownloadResponse(success=True, message=f"Resuming download for {model_id}.")
|
|
|
|
# Start new download
|
|
progress = DownloadProgress(
|
|
model_id=model_id,
|
|
status=ModelDownloadStatus.DOWNLOADING,
|
|
total_bytes=MODEL_DOWNLOAD_INFO[model_id]["size_mb"] * 1024 * 1024,
|
|
)
|
|
download_progress[model_id] = progress
|
|
|
|
# Start background download
|
|
background_tasks.add_task(_download_model_task, model_id, progress)
|
|
|
|
return ModelDownloadResponse(success=True, message=f"Started downloading {model_id}.")
|
|
|
|
|
|
@router.post("/pause")
|
|
def pause_download(body: ModelDownloadRequest):
|
|
"""Pause an ongoing download."""
|
|
model_id = body.model_id
|
|
|
|
if model_id not in download_progress:
|
|
raise HTTPException(status_code=404, detail=f"No active download for {model_id}")
|
|
|
|
prog = download_progress[model_id]
|
|
if prog.status != ModelDownloadStatus.DOWNLOADING:
|
|
raise HTTPException(status_code=400, detail=f"Download is not in progress (status: {prog.status.value})")
|
|
|
|
prog._pause_event.clear()
|
|
prog.status = ModelDownloadStatus.PAUSED
|
|
|
|
return {"success": True, "message": f"Paused download for {model_id}"}
|
|
|
|
|
|
@router.post("/resume")
|
|
def resume_download(body: ModelDownloadRequest):
|
|
"""Resume a paused download."""
|
|
model_id = body.model_id
|
|
|
|
if model_id not in download_progress:
|
|
raise HTTPException(status_code=404, detail=f"No download to resume for {model_id}")
|
|
|
|
prog = download_progress[model_id]
|
|
if prog.status != ModelDownloadStatus.PAUSED:
|
|
raise HTTPException(status_code=400, detail=f"Download is not paused (status: {prog.status.value})")
|
|
|
|
prog._pause_event.set()
|
|
prog.status = ModelDownloadStatus.DOWNLOADING
|
|
|
|
return {"success": True, "message": f"Resumed download for {model_id}"}
|
|
|
|
|
|
@router.post("/cancel")
|
|
def cancel_download(body: ModelDownloadRequest):
|
|
"""Cancel an ongoing or paused download."""
|
|
model_id = body.model_id
|
|
|
|
if model_id not in download_progress:
|
|
raise HTTPException(status_code=404, detail=f"No active download for {model_id}")
|
|
|
|
prog = download_progress[model_id]
|
|
|
|
# Signal cancellation
|
|
prog._cancel_event.set()
|
|
prog.status = ModelDownloadStatus.CANCELLED
|
|
|
|
# Clean up partial downloads
|
|
_cleanup_partial_download(model_id)
|
|
|
|
return {"success": True, "message": f"Cancelled download for {model_id}"}
|
|
|
|
|
|
@router.get("/progress/{model_id}")
|
|
def get_progress(model_id: str):
|
|
"""Get download progress for a specific model."""
|
|
if model_id not in download_progress:
|
|
# Check if downloaded
|
|
if is_model_downloaded(model_id):
|
|
return {
|
|
"model_id": model_id,
|
|
"status": ModelDownloadStatus.COMPLETED.value,
|
|
"progress_percent": 100.0,
|
|
"bytes_downloaded": MODEL_DOWNLOAD_INFO.get(model_id, {}).get("size_mb", 0) * 1024 * 1024,
|
|
"total_bytes": MODEL_DOWNLOAD_INFO.get(model_id, {}).get("size_mb", 0) * 1024 * 1024,
|
|
"speed_mbps": 0.0,
|
|
}
|
|
raise HTTPException(status_code=404, detail=f"No download found for {model_id}")
|
|
|
|
return download_progress[model_id].to_dict()
|
|
|
|
|
|
@router.get("/progress")
|
|
def get_all_progress():
|
|
"""Get download progress for all models."""
|
|
return {model_id: prog.to_dict() for model_id, prog in download_progress.items()}
|
|
|
|
|
|
@router.delete("/{model_id}")
|
|
def delete_model(model_id: str):
|
|
"""Delete a downloaded model to free up space."""
|
|
if model_id not in MODEL_DOWNLOAD_INFO:
|
|
raise HTTPException(status_code=404, detail=f"Unknown model: {model_id}")
|
|
|
|
if not is_model_downloaded(model_id):
|
|
raise HTTPException(status_code=400, detail=f"Model {model_id} is not downloaded")
|
|
|
|
# Remove files
|
|
model_dir = get_model_directory(model_id)
|
|
info = MODEL_DOWNLOAD_INFO[model_id]
|
|
|
|
deleted = 0
|
|
for file_path in info["files"]:
|
|
# Try flat first (our download convention), then deep path
|
|
for candidate in (model_dir / Path(file_path).name, model_dir / file_path):
|
|
if candidate.exists():
|
|
candidate.unlink()
|
|
deleted += 1
|
|
break
|
|
|
|
# Clean up progress tracking
|
|
if model_id in download_progress:
|
|
del download_progress[model_id]
|
|
|
|
return {"success": True, "message": f"Deleted {deleted} files for {model_id}"}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Background download task
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _download_model_task(model_id: str, progress: DownloadProgress):
|
|
"""Background task to download a model from HuggingFace."""
|
|
try:
|
|
from huggingface_hub import hf_hub_download
|
|
|
|
info = MODEL_DOWNLOAD_INFO[model_id]
|
|
hf_repo = info.get("hf_repo")
|
|
|
|
# Reject placeholder / unconfigured repos immediately
|
|
if not hf_repo:
|
|
progress.status = ModelDownloadStatus.ERROR
|
|
progress.error_message = (
|
|
"No download URL configured for this model. "
|
|
"Install the weights manually into the model directory."
|
|
)
|
|
return
|
|
|
|
model_dir = get_model_directory(model_id)
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
total_files = len(info["files"])
|
|
|
|
for idx, file_path in enumerate(info["files"]):
|
|
# Check for cancellation
|
|
if progress._cancel_event.is_set():
|
|
progress.status = ModelDownloadStatus.CANCELLED
|
|
return
|
|
|
|
# Block here until resumed (threading.Event.wait() works correctly in threads)
|
|
progress._pause_event.wait()
|
|
|
|
if progress._cancel_event.is_set():
|
|
progress.status = ModelDownloadStatus.CANCELLED
|
|
return
|
|
|
|
dest = model_dir / Path(file_path).name
|
|
if dest.exists():
|
|
# Already present — count it and skip
|
|
progress.bytes_downloaded += dest.stat().st_size
|
|
progress.progress_percent = (idx + 1) / total_files * 100
|
|
continue
|
|
|
|
try:
|
|
# Download to a temp directory first, then move flat into model_dir.
|
|
# This ensures is_model_downloaded() finds files at the expected path
|
|
# regardless of the subdirectory structure inside the HF repo.
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
tmp_path = hf_hub_download(
|
|
repo_id=hf_repo,
|
|
filename=file_path,
|
|
local_dir=tmp,
|
|
local_dir_use_symlinks=False,
|
|
)
|
|
shutil.copy2(tmp_path, str(dest))
|
|
|
|
progress.bytes_downloaded += dest.stat().st_size
|
|
progress.progress_percent = (idx + 1) / total_files * 100
|
|
|
|
except Exception as e:
|
|
progress.status = ModelDownloadStatus.ERROR
|
|
progress.error_message = str(e)
|
|
return
|
|
|
|
progress.progress_percent = 100.0
|
|
progress.status = ModelDownloadStatus.COMPLETED
|
|
|
|
except Exception as e:
|
|
progress.status = ModelDownloadStatus.ERROR
|
|
progress.error_message = str(e)
|
|
|
|
|
|
def _cleanup_partial_download(model_id: str):
|
|
"""Remove partially downloaded files."""
|
|
model_dir = get_model_directory(model_id)
|
|
if model_dir.exists():
|
|
import shutil
|
|
shutil.rmtree(model_dir, ignore_errors=True)
|