""" 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, BackgroundTasks, Depends, HTTPException 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, SEPACAP_MODEL_DIR, SEPREFORMER_MODEL_DIR, ) from ..security import require_admin_token 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_karaoke_frazer_becruily": { "size_mb": 205, "description": "BS-RoFormer Karaoke Frazer+Becruily (public HF release) for lead/backing-style karaoke separation.", "hf_repo": "becruily/bs-roformer-karaoke", "files": ["bs_roformer_karaoke_frazer_becruily.ckpt", "config_karaoke_frazer_becruily.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"], }, "bs_mamba2_v1": { "size_mb": 210, "description": "BS Mamba2 vocals adapter candidate (v1.0.19) using dedicated checkpoint+config pair.", "hf_repo": None, "files": ["bs_mamba2_vocals.ckpt", "config_bs_mamba2_vocals.yaml"], }, "bs_polarformer_v1": { "size_mb": 240, "description": "BS Polarformer adapter candidate (v1.0.20) using dedicated checkpoint+config pair.", "hf_repo": None, "files": ["model_bs_polarformer_float16.ckpt", "model_bs_polarformer_float16.yaml"], }, # 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_vocals_gabox": { "size_mb": 195, "description": "MelBand Vocals Gabox core variant from GaboxR67/MelBandRoformers.", "hf_repo": "GaboxR67/MelBandRoformers", "files": ["melbandroformers/vocals/voc_gabox.ckpt", "melbandroformers/vocals/voc_gabox.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": 1760, "description": "MelBand RoFormer for karaoke - separates lead vocals from backing+instrumental.", "hf_repo": "becruily/mel-band-roformer-karaoke", "files": ["mel_band_roformer_karaoke_becruily.ckpt", "config_karaoke_becruily.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_karaoke_gabox_v1": { "size_mb": 210, "description": "MelBand Karaoke Gabox V1 checkpoint from GaboxR67/MelBandRoformers karaoke folder.", "hf_repo": "GaboxR67/MelBandRoformers", "files": [ "melbandroformers/karaoke/Karaoke_GaboxV1.ckpt", "melbandroformers/karaoke/karaokegabox_1750911344.yaml", ], }, "mel_band_karaoke_gabox_small": { "size_mb": 110, "description": "MelBand Karaoke Gabox small variant for constrained VRAM hosts.", "hf_repo": "GaboxR67/MelBandRoformers", "files": [ "melbandroformers/karaoke/small_karaoke_gaboxaufr.ckpt", "melbandroformers/karaoke/config_karaoke_small.yaml", ], }, "mel_band_karaoke_gabox_v2_exp": { "size_mb": 210, "description": "MelBand Karaoke Gabox V2 experimental checkpoint (opt-in).", "hf_repo": "GaboxR67/MelBandRoformers", "files": [ "melbandroformers/experimental/Karaoke_GaboxV2.ckpt", "melbandroformers/karaoke/karaokegabox_1750911344.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 (6-stem: kick/snare/toms/hh/ride/crash) by aufr33-jarredou.", "hf_repo": "miercolesv/MVSEP-Central-Backup", "files": [ "Politrees/UVR_resources/models/MDX23C/MDX23C-DrumSep-aufr33-jarredou.ckpt", "Politrees/UVR_resources/models/MDX23C/config_drumsep_mdx23c.yaml", ], }, # 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"], }, "scnet_masked_xl_ihf": { "size_mb": 355, "description": "SCNet Masked XL IHF (ZFTurbo v1.0.17) - 4-stem masked SCNet variant.", "hf_repo": None, "files": ["model_scnet_masked_ep_111_sdr_9.8286.ckpt", "config_musdb18_scnet_xl_ihf.yaml"], }, "scnet_masked_small": { "size_mb": 210, "description": "SCNet Masked Small (ZFTurbo v1.0.16) - lighter 4-stem masked SCNet variant.", "hf_repo": None, "files": ["model_scnet_masked_ep_156_sdr_8.8149.ckpt", "config_musdb18_scnet_small.yaml"], }, "scnet_tran_small": { "size_mb": 230, "description": "SCNet Tran Small (ZFTurbo v1.0.14) - compact 4-stem SCNet-Transformer variant.", "hf_repo": None, "files": ["model_scnet_tran_sdr_8.9272.ckpt", "config_musdb18_scnet_tran.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_lq": { "size_mb": 430, "description": "Apollo LQ MP3 restoration (JusperLee). Uses pytorch_model.bin with Apollo config.", "hf_repo": "JusperLee/Apollo", "files": ["pytorch_model.bin", "config_apollo.yaml"], # Optional per-file direct URL override (same index as files). "download_urls": [ "https://huggingface.co/JusperLee/Apollo/resolve/main/pytorch_model.bin", "https://raw.githubusercontent.com/ZFTurbo/Music-Source-Separation-Training/refs/heads/main/configs/config_apollo.yaml", ], }, "apollo_vocal_enhancer": { "size_mb": 420, "description": "Apollo Vocal Enhancer (lew).", "hf_repo": "jarredou/lew_apollo_vocal_enhancer", "files": ["apollo_model.ckpt", "config_apollo_vocal.yaml"], }, "apollo_vocal_enhancer_v2": { "size_mb": 430, "description": "Apollo Vocal Enhancer V2 (WIP) (lew).", "hf_repo": None, "files": ["apollo_model_v2.ckpt", "config_apollo_vocal_v2.yaml"], "download_urls": [ "https://shared.multimedia.workers.dev/download/1/other/apollo_model_v2.ckpt", "https://shared.multimedia.workers.dev/download/1/other/config_apollo_vocal_v2.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"], }, # SepACap -- a cappella multi-singer vocal separation (7 stems) "sepacap": { "size_mb": 161, "description": "SepACap -- state-of-the-art a cappella vocal separation (NeurIPS 2025). Splits a cappella recordings into 7 independent stems: alto, bass, finger snap, lead vocal, soprano, tenor, and vocal percussion. Ideal for choral and a cappella music. Architecture cloned from GitHub on first use; weights auto-downloaded from HuggingFace.", "hf_repo": "Tino3141/sepacap", "files": ["SepACap.pth", "modelMusicSep.yaml"], }, # SepReformer -- 2-speaker speech separation (NeurIPS 2024) "sepreformer_base": { "size_mb": 50, "description": "SepReformer Base -- 2-speaker speech separation trained on WSJ0-2Mix (clean speech, 8 kHz). Separates mixed audio of two speakers into individual streams. Architecture and weights cloned from GitHub via git-lfs on first use.", "hf_repo": None, "files": [], }, "sepreformer_large_wsj0": { "size_mb": 120, "description": "SepReformer Large -- larger 2-speaker model trained on WSJ0-2Mix (clean speech, 8 kHz). Higher quality than the Base variant.", "hf_repo": None, "files": [], }, "sepreformer_large_wham": { "size_mb": 120, "description": "SepReformer Large WHAM -- 2-speaker separation for noisy speech (8 kHz). Trained on the WHAM! noisy mixture benchmark.", "hf_repo": None, "files": [], }, "sepreformer_large_whamr": { "size_mb": 120, "description": "SepReformer Large WHAMR -- 2-speaker separation for noisy and reverberant speech (8 kHz). Trained on the WHAMR! benchmark.", "hf_repo": None, "files": [], }, # 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", "bs_roformer_karaoke_frazer_becruily": MEL_BAND_MODEL_DIR / "karaoke_frazer_becruily", "bs_mamba2_v1": BS_ROFORMER_MODEL_DIR / "bs_mamba2_v1", "bs_polarformer_v1": BS_ROFORMER_MODEL_DIR / "bs_polarformer_v1", # ── 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_vocals_gabox": MEL_BAND_MODEL_DIR / "vocals_gabox", "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_karaoke_gabox_v1": MEL_BAND_MODEL_DIR / "karaoke_gabox_v1", "mel_band_karaoke_gabox_small": MEL_BAND_MODEL_DIR / "karaoke_gabox_small", "mel_band_karaoke_gabox_v2_exp": MEL_BAND_MODEL_DIR / "karaoke_gabox_v2_exp", "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", "scnet_masked_xl_ihf": SCNET_MODEL_DIR / "scnet_masked_xl_ihf", "scnet_masked_small": SCNET_MODEL_DIR / "scnet_masked_small", "scnet_tran_small": SCNET_MODEL_DIR / "scnet_tran_small", # ── 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_lq": APOLLO_MODEL_DIR / "apollo_lq", "apollo_vocal_enhancer": APOLLO_MODEL_DIR / "apollo_vocal_enhancer", "apollo_vocal_enhancer_v2": APOLLO_MODEL_DIR / "apollo_vocal_enhancer_v2", # ── 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", # -- SepACap (flat model dir) -- "sepacap": SEPACAP_MODEL_DIR, # -- SepReformer (weights bundled via git-lfs in the cloned repo) -- "sepreformer_base": SEPREFORMER_MODEL_DIR / "sepreformer_base", "sepreformer_large_wsj0": SEPREFORMER_MODEL_DIR / "sepreformer_large_wsj0", "sepreformer_large_wham": SEPREFORMER_MODEL_DIR / "sepreformer_large_wham", "sepreformer_large_whamr": SEPREFORMER_MODEL_DIR / "sepreformer_large_whamr", } 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 available: bool = True unavailable_reason: Optional[str] = None class ModelDownloadRequest(BaseModel): model_id: str class ModelDownloadResponse(BaseModel): success: bool message: str # --------------------------------------------------------------------------- # Runtime capability gating # --------------------------------------------------------------------------- # These models currently safe-fail in this deployment because the installed # audio-separator runtime does not support their checkpoint formats and falls # back to unrelated defaults. Keep them visible in metadata but mark them # unavailable so the UI can prevent avoidable failed jobs. RUNTIME_UNAVAILABLE_MODELS: Dict[str, str] = { "bandit_v2_multi": "Unavailable in this runtime: installed audio-separator does not support Bandit checkpoints.", "bandit_v2_eng": "Unavailable in this runtime: installed audio-separator does not support Bandit checkpoints.", "bs_roformer_sw_6stem": "Unavailable in this runtime: no supported BS-RoFormer SW 6-stem checkpoint is installed for the current audio-separator build.", } def get_runtime_availability(model_id: str) -> tuple[bool, Optional[str]]: reason = RUNTIME_UNAVAILABLE_MODELS.get(model_id) if reason: return False, reason return True, None # --------------------------------------------------------------------------- # 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 available, unavailable_reason = get_runtime_availability(model_id) 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, available=available, unavailable_reason=unavailable_reason, )) return result @router.post("/download", response_model=ModelDownloadResponse) def download_model( body: ModelDownloadRequest, background_tasks: BackgroundTasks, _: None = Depends(require_admin_token), ): """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, _: None = Depends(require_admin_token)): """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, _: None = Depends(require_admin_token)): """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, _: None = Depends(require_admin_token)): """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, _: None = Depends(require_admin_token)): """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 and/or direct URLs.""" try: from huggingface_hub import hf_hub_download import urllib.request info = MODEL_DOWNLOAD_INFO[model_id] hf_repo = info.get("hf_repo") download_urls = info.get("download_urls") or [] # Reject unconfigured sources immediately if not hf_repo and not download_urls: progress.status = ModelDownloadStatus.ERROR progress.error_message = ( "No download URL configured for this model. " "Install the weights manually into the model directory." ) return files = info["files"] if download_urls and len(download_urls) != len(files): progress.status = ModelDownloadStatus.ERROR progress.error_message = ( f"Model {model_id} has mismatched files/download_urls lengths " f"({len(files)} files vs {len(download_urls)} URLs)." ) return model_dir = get_model_directory(model_id) model_dir.mkdir(parents=True, exist_ok=True) total_files = len(files) for idx, file_path in enumerate(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: override_url = download_urls[idx] if download_urls else None # Download to a temp directory first, then move flat into model_dir. with tempfile.TemporaryDirectory() as tmp: if override_url: tmp_path = Path(tmp) / Path(file_path).name with urllib.request.urlopen(override_url, timeout=180) as resp, open(tmp_path, "wb") as out: shutil.copyfileobj(resp, out) else: if not hf_repo: raise RuntimeError( f"No HuggingFace repo configured for {model_id} file {file_path}" ) 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)