Files
stemengine_old/backend/services_root_backup/medleyvox_separator.py
T
2026-03-03 14:52:19 +09:00

168 lines
5.9 KiB
Python
Executable File

"""
MedleyVox separator - Multi-singer vocal separation
HuggingFace model: Cyru5/MedleyVox
Backup: miercolesv/MVSEP-Central-Backup
"""
import os
import torch
import torchaudio
from pathlib import Path
from typing import Dict, Optional
import numpy as np
from ..config import MEDLEYVOX_MODEL_DIR, CPU_SEPARATION
# Model configurations
MEDLEYVOX_MODELS = {
"medleyvox": {
"model_path": "vocals 100.pth",
"hf_repo": "miercolesv/MVSEP-Central-Backup",
"hf_subfolder": "MedleyVox-MultiSinger",
"stems": ["vocals", "instrumental"],
},
"medleyvox_55ep": {
"model_path": "vocals 55 epochs.pth",
"hf_repo": "miercolesv/MVSEP-Central-Backup",
"hf_subfolder": "MedleyVox-MultiSinger",
"stems": ["vocals", "instrumental"],
},
"medleyvox_66ep": {
"model_path": "vocals 66.pth",
"hf_repo": "miercolesv/MVSEP-Central-Backup",
"hf_subfolder": "MedleyVox-MultiSinger",
"stems": ["vocals", "instrumental"],
},
"medleyvox_88ep": {
"model_path": "vocals 88.pth",
"hf_repo": "miercolesv/MVSEP-Central-Backup",
"hf_subfolder": "MedleyVox-MultiSinger",
"stems": ["vocals", "instrumental"],
},
"medleyvox_100ep": {
"model_path": "vocals 100.pth",
"hf_repo": "miercolesv/MVSEP-Central-Backup",
"hf_subfolder": "MedleyVox-MultiSinger",
"stems": ["vocals", "instrumental"],
},
}
class MedleyVoxSeparator:
"""
MedleyVox separator for multi-singer vocal separation.
Specialized for separating vocals when multiple singers are present.
"""
def __init__(
self,
model_id: str = "medleyvox",
cpu_separation: bool = CPU_SEPARATION,
output_format: str = "mp3",
mp3_bitrate: str = "256k",
):
if model_id not in MEDLEYVOX_MODELS:
raise ValueError(f"Unknown MedleyVox model: {model_id}")
self.model_id = model_id
self.model_config = MEDLEYVOX_MODELS[model_id]
self.cpu_separation = cpu_separation
self.output_format = output_format
self.mp3_bitrate = mp3_bitrate
self._model = None
self._device = None
def _ensure_model(self):
"""Download model from HuggingFace if not present."""
model_dir = MEDLEYVOX_MODEL_DIR / self.model_id
model_dir.mkdir(parents=True, exist_ok=True)
model_path = model_dir / self.model_config["model_path"]
if not model_path.exists():
try:
from huggingface_hub import hf_hub_download
print(f"Downloading MedleyVox model: {self.model_id}")
hf_hub_download(
repo_id=self.model_config["hf_repo"],
filename=f"{self.model_config['hf_subfolder']}/{self.model_config['model_path']}",
local_dir=model_dir,
local_dir_use_symlinks=False,
)
except Exception as e:
raise RuntimeError(f"Failed to download MedleyVox model: {e}")
return str(model_path)
def _load_model(self):
"""Lazy-load the model."""
if self._model is None:
model_path = self._ensure_model()
# Set device
self._device = "cpu" if self.cpu_separation else ("cuda" if torch.cuda.is_available() else "cpu")
# Load model checkpoint
checkpoint = torch.load(model_path, map_location=self._device)
# MedleyVox uses a custom architecture
# This is a placeholder - actual implementation would need the model class
# For now, we'll use audio-separator as a fallback
self._model = None # Will use audio-separator fallback
return self._model
def separate_into_parts(self, input_path: str, output_dir: str) -> Dict[str, str]:
"""
Separate audio into vocals and instrumental.
Returns:
Dict mapping stem names to output file paths
"""
output_path = Path(output_dir)
output_path.mkdir(parents=True, exist_ok=True)
# For now, use audio-separator with UVR as fallback
# Full implementation would load the actual MedleyVox model
try:
from audio_separator.separator import Separator
# Use a generic 2-stem model as fallback
sep = Separator(
model_file_dir=str(MEDLEYVOX_MODEL_DIR),
output_dir=str(output_path),
output_format=self.output_format,
output_bitrate=self.mp3_bitrate if self.output_format == "mp3" else None,
cpu_separation=self.cpu_separation,
)
# Use UVR-MDX-NET Inst HQ as fallback
sep.load_model()
result = sep.separate(input_path)
# Map outputs to stem names
stems = self.model_config.get("stems", ["vocals", "instrumental"])
output_paths = {}
if isinstance(result, list):
for i, path in enumerate(result):
if i < len(stems):
stem_name = stems[i]
final_path = output_path / f"{stem_name}.{self.output_format}"
Path(path).rename(final_path)
output_paths[stem_name] = str(final_path)
else:
final_path = output_path / f"vocals.{self.output_format}"
Path(result).rename(final_path)
output_paths["vocals"] = str(final_path)
return output_paths
except ImportError:
raise ImportError(
"audio-separator is required for MedleyVox separation. "
"Install with: pip install audio-separator"
)