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

156 lines
5.8 KiB
Python
Executable File

"""
Apollo separator - Vocal enhancement and reverb removal
Models from MVSEP-Central-Backup: https://huggingface.co/miercolesv/MVSEP-Central-Backup
"""
import os
import torch
from pathlib import Path
from typing import Dict
from ..config import APOLLO_MODEL_DIR, CPU_SEPARATION
# Model configurations
APOLLO_MODELS = {
"apollo_vocals": {
"model_path": "epoch=54-val_loss=-17.6221.ckpt",
"config_path": "config_apollo_vocals_ep_54.yaml",
"hf_repo": "miercolesv/MVSEP-Central-Backup",
"hf_subfolder": "Apollo-Vocal-MSST",
"stems": ["dry_vocals", "reverb"],
"description": "Apollo vocal enhancement/reverb removal",
},
"apollo_edm": {
"model_path": "apollo_edm_by_essid.ckpt",
"config_path": "apollo_edm_by_essid.yaml",
"hf_repo": "miercolesv/MVSEP-Central-Backup",
"hf_subfolder": "Politrees/UVR_resources/models/Apollo",
"stems": ["enhanced", "residual"],
"description": "Apollo EDM enhancement",
},
"apollo_edm_big": {
"model_path": "apollo_edm_big_by_essid.ckpt",
"config_path": "apollo_edm_big_by_essid.yaml",
"hf_repo": "miercolesv/MVSEP-Central-Backup",
"hf_subfolder": "Politrees/UVR_resources/models/Apollo",
"stems": ["enhanced", "residual"],
"description": "Apollo EDM Big enhancement",
},
}
class ApolloSeparator:
"""
Apollo separator for vocal enhancement and reverb removal.
Apollo models separate vocals into dry vocals and reverb,
useful for cleaning up vocal recordings.
"""
def __init__(
self,
model_id: str = "apollo_vocals",
cpu_separation: bool = CPU_SEPARATION,
output_format: str = "mp3",
mp3_bitrate: str = "256k",
):
if model_id not in APOLLO_MODELS:
raise ValueError(f"Unknown Apollo model: {model_id}")
self.model_id = model_id
self.model_config = APOLLO_MODELS[model_id]
self.cpu_separation = cpu_separation
self.output_format = output_format
self.mp3_bitrate = mp3_bitrate
self._model = None
def _ensure_model(self):
"""Download model from HuggingFace if not present."""
model_dir = APOLLO_MODEL_DIR / self.model_id
model_dir.mkdir(parents=True, exist_ok=True)
model_path = model_dir / self.model_config["model_path"]
config_path = model_dir / self.model_config["config_path"]
if not model_path.exists() or not config_path.exists():
try:
from huggingface_hub import hf_hub_download
print(f"Downloading Apollo model: {self.model_id}")
if not model_path.exists():
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,
)
if not config_path.exists():
hf_hub_download(
repo_id=self.model_config["hf_repo"],
filename=f"{self.model_config['hf_subfolder']}/{self.model_config['config_path']}",
local_dir=model_dir,
local_dir_use_symlinks=False,
)
except Exception as e:
raise RuntimeError(f"Failed to download Apollo model: {e}")
return str(model_path), str(config_path)
def _load_model(self):
"""Lazy-load the model."""
if self._model is None:
model_path, config_path = self._ensure_model()
self._model = None # Placeholder for actual model loading
return self._model
def separate_into_parts(self, input_path: str, output_dir: str) -> Dict[str, str]:
"""
Separate vocals into dry vocals and reverb.
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 as fallback
# Full implementation would load the actual Apollo model
try:
from audio_separator.separator import Separator
sep = Separator(
model_file_dir=str(APOLLO_MODEL_DIR),
output_dir=str(output_path),
output_format=self.output_format.upper(),
output_bitrate=self.mp3_bitrate if self.output_format == "mp3" else None,
)
# Use dereverb model as approximation
sep.load_model()
result = sep.separate(input_path)
stems = self.model_config.get("stems", ["dry_vocals", "reverb"])
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"dry_vocals.{self.output_format}"
Path(result).rename(final_path)
output_paths["dry_vocals"] = str(final_path)
return output_paths
except ImportError:
raise ImportError(
"audio-separator is required for Apollo separation. "
"Install with: pip install audio-separator"
)