Files
stemengine_old/backend/services/separator_service.py
T
2026-03-16 16:35:08 +09:00

2202 lines
73 KiB
Python
Executable File

"""
Stem separation service with project-based folder structure and preset mixing.
- Demucs models → CLI subprocess (python -m demucs)
- BS-RoFormer → Python API (in-process, no subprocess needed)
- Presets → Instrumental, Static, Dynamic mixing via preset_service
"""
import logging
import os
import subprocess
import sys
import time
import shutil
from pathlib import Path
from typing import Dict, List, Optional
from .. import job_manager as jm
from ..config import PROJECTS_DIR, JOBS_DIR, CPU_SEPARATION, PROJECT_SUBFOLDERS
from .metadata_util import copy_metadata, read_metadata
from .project_namer import project_name, make_project_dirs
from .project_metadata import write_project_metadata
from .separation.model_downloads import ensure_model_available
from .separation.model_registry import (
APOLLO_MODEL_IDS,
ASPIRATION_MODEL_IDS,
BANDIT_MODEL_IDS,
BSR_ANVUEW_MODELS,
BSR_CHORUS_MODELS,
BSR_KARAOKE_ANVUEW_MODELS,
BSR_PREFIX,
BSR_STEM_MODE,
BSR_SW_MODEL_IDS,
ENSEMBLE_MODEL_IDS,
FORMAT_LABELS,
MBR_DEUX_BECRUILY_MODELS,
MBR_DEREVERB_MODELS,
MBR_DEREVERB_PREFIX,
MBR_KARAOKE_MODELS,
MBR_KARAOKE_PREFIX,
MBR_MODELS,
MBR_PREFIX,
MBR_VOCALS_MODELS,
MBR_VOCALS_PREFIX,
MDXNET_MODEL_IDS,
MEDLEYVOX_MODEL_IDS,
MODEL_LABELS,
SCNET_MODEL_IDS,
SEPACAP_MODEL_IDS,
SEPREFORMER_MODEL_IDS,
)
from .separation.output_layout import stem_filename
from .separation.power_presets import get_power_opts
from .separation.execution import flush_vram_cache, move_stems_to_project, oom_retry_call, cleanup_temp
from .separation.postprocess import apply_presets
log = logging.getLogger(__name__)
def _stem_filename(prefix: str, stem_name: str, suffix: str) -> str:
"""Compatibility wrapper around shared `stem_filename()` helper."""
return stem_filename(prefix, stem_name, suffix)
def _run_model_separation(
job_id: str,
input_path: str,
model: str,
normalised_format: str,
stems_dir: Path,
mp3_bitrate: str,
metadata: dict,
stem_prefix: str,
power_opts: dict,
) -> Dict[str, str]:
"""Dispatch one separation request through the existing model-specific runners."""
if model.startswith(BSR_PREFIX):
return _run_bsroformer(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
batch_size_override=power_opts['roformer'].get('batch_size'),
num_overlap_override=power_opts['roformer'].get('num_overlap'),
)
if model in BSR_CHORUS_MODELS:
return _run_bsroformer_chorus(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
batch_size_override=power_opts['roformer'].get('batch_size'),
num_overlap_override=power_opts['roformer'].get('num_overlap'),
)
if model in BSR_ANVUEW_MODELS:
# Route to MelBand Vocals Anvuew since it's the same architecture
return _run_mel_band_vocals(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
batch_size_override=power_opts['roformer'].get('batch_size'),
num_overlap_override=power_opts['roformer'].get('num_overlap'),
)
if model in BSR_KARAOKE_ANVUEW_MODELS:
# Route to Karaoke Gabox as closest match
return _run_mel_band_karaoke(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
batch_size_override=power_opts['roformer'].get('batch_size'),
num_overlap_override=power_opts['roformer'].get('num_overlap'),
)
if model.startswith(MBR_PREFIX):
return _run_mel_band_roformer(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
batch_size_override=power_opts['roformer'].get('batch_size'),
num_overlap_override=power_opts['roformer'].get('num_overlap'),
)
if model in MBR_VOCALS_MODELS:
return _run_mel_band_vocals(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
batch_size_override=power_opts['roformer'].get('batch_size'),
num_overlap_override=power_opts['roformer'].get('num_overlap'),
)
if model in MBR_DEREVERB_MODELS:
return _run_mel_band_dereverb(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
batch_size_override=power_opts['roformer'].get('batch_size'),
num_overlap_override=power_opts['roformer'].get('num_overlap'),
)
if model in MBR_KARAOKE_MODELS:
return _run_mel_band_karaoke(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
batch_size_override=power_opts['roformer'].get('batch_size'),
num_overlap_override=power_opts['roformer'].get('num_overlap'),
)
if model in MBR_DEUX_BECRUILY_MODELS:
return _run_mel_band_deux_becruily(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
batch_size_override=power_opts['roformer'].get('batch_size'),
num_overlap_override=power_opts['roformer'].get('num_overlap'),
)
if model in MDXNET_MODEL_IDS:
# Route MelBand-based MDX models appropriately
if model == 'mdxnet_dereverb':
return _run_mel_band_dereverb(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
batch_size_override=power_opts['roformer'].get('batch_size'),
num_overlap_override=power_opts['roformer'].get('num_overlap'),
)
return _run_mdxnet(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata
)
if model in SCNET_MODEL_IDS:
return _run_scnet(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
chunk_size_override=power_opts['scnet'].get('chunk_size'),
num_overlap_override=power_opts['scnet'].get('num_overlap'),
)
if model in BSR_SW_MODEL_IDS:
return _run_bsroformer_sw(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
)
if model in MEDLEYVOX_MODEL_IDS:
return _run_medleyvox(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
)
if model in ENSEMBLE_MODEL_IDS:
return _run_ensemble(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
)
if model in APOLLO_MODEL_IDS:
return _run_apollo(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
)
if model in ASPIRATION_MODEL_IDS:
return _run_aspiration(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
)
if model in BANDIT_MODEL_IDS:
return _run_bandit(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
)
if model in SEPACAP_MODEL_IDS:
return _run_sepacap(
job_id, input_path, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
chunk_samples=power_opts['sepacap']['chunk_samples'],
)
if model in SEPREFORMER_MODEL_IDS:
return _run_sepreformer(
job_id, input_path, model, normalised_format, stems_dir,
mp3_bitrate=mp3_bitrate, metadata=metadata,
stem_prefix=stem_prefix,
chunk_samples=power_opts['sepreformer']['chunk_samples'],
)
return _run_demucs(
job_id, input_path, model, normalised_format, stems_dir,
metadata=metadata,
stem_prefix=stem_prefix,
segment_override=power_opts['demucs'].get('segment'),
overlap_override=power_opts['demucs'].get('overlap'),
)
def _select_refined_output_path(target_stem: str, refined_outputs: Dict[str, str]) -> Optional[str]:
"""Choose the most likely refined output for a target instrument stem."""
if not refined_outputs:
return None
normalized_target = (target_stem or "").strip().lower()
direct = refined_outputs.get(normalized_target)
if direct:
return direct
aliases = {
"vocals": ["vocal", "voice", "lead_vocal"],
"drums": ["drum", "percussion"],
"bass": ["bass", "low"],
"other": ["accompaniment", "instrumental", "music", "other"],
"guitar": ["guitar", "gtr"],
"piano": ["piano", "keys", "keyboard"],
}
for key, path in refined_outputs.items():
k = key.lower()
if normalized_target in k:
return path
for alias in aliases.get(normalized_target, []):
if alias in k:
return path
# Deterministic fallback.
return refined_outputs[sorted(refined_outputs.keys())[0]]
def run_separation(
job_id: str,
input_path: str,
model: str,
output_format: str,
# Preset options
preset_type: str = "stems_only", # stems_only, static, instrumental, dynamic
static_option: Optional[str] = None, # for static preset: no_vocals, no_drums, etc.
dynamic_levels: Optional[Dict[str, float]] = None, # for dynamic preset
instrument_models: Optional[Dict[str, str]] = None, # optional per-instrument model mapping
# Project naming
project_name_override: Optional[str] = None,
# Power preset
sep_power_preset: str = 'standard',
# Reprocess mode: output stems directly into an existing project's Stems dir
output_dir_override: Optional[Path] = None,
parent_job_id: Optional[str] = None,
):
"""
Background task: separate audio into stems and apply presets.
Creates human-readable project folder structure:
/Artist - Title/
Stems/ (always created)
Instrumental/ (if preset includes instrumental)
Static/ (if preset is static)
Dynamic/ (if preset is dynamic)
"""
log.debug("[%s] run_separation start — model=%s format=%s preset=%s input=%s",
job_id, model, output_format, preset_type, input_path)
instrument_models_summary = {
"requested": instrument_models or {},
"applied": {},
"skipped": {},
}
# Read metadata for project naming
jm.update_job(job_id, status=jm.JobStatus.RUNNING, progress="Reading metadata...")
metadata = read_metadata(input_path)
# Ensure model weights are present — download automatically if missing
if not ensure_model_available(job_id, model):
return
# Resolve human-readable labels for tab naming
model_label = MODEL_LABELS.get(model, model)
format_label = FORMAT_LABELS.get(output_format, output_format)
tab_label = f"{model_label}_{format_label}"
# Determine project name
if project_name_override:
proj_name = project_name_override
else:
filename_stem = Path(input_path).stem
proj_name = project_name(
metadata.get('albumartist') or metadata.get('artist', ''),
metadata.get('title', ''),
filename_stem,
)
if output_dir_override is not None:
# Reprocess mode: place stems directly in the existing project's Stems dir.
# Skip project creation and source-file copy — the folder already exists.
stems_dir = output_dir_override
project_dir = output_dir_override.parent
stems_dir.mkdir(parents=True, exist_ok=True)
dirs = {'project': project_dir, 'stems': stems_dir}
source_dest = project_dir / f"{proj_name}{Path(input_path).suffix.lower()}"
else:
# Determine which folders to create based on preset
preset_flags = {
'instrumental': preset_type in ('instrumental',),
'static': preset_type == 'static',
'dynamic': preset_type == 'dynamic',
}
# Create project directories
jm.update_job(job_id, progress=f"Creating project: {proj_name}...")
dirs = make_project_dirs(proj_name, PROJECTS_DIR, preset_flags)
stems_dir = dirs['stems']
project_dir = dirs['project']
# Copy original source file to project root with full metadata
jm.update_job(job_id, progress="Copying source file to project...")
input_ext = Path(input_path).suffix.lower()
source_dest = project_dir / f"{proj_name}{input_ext}"
try:
shutil.copy2(input_path, source_dest)
copy_metadata(input_path, str(source_dest), stem_label=None, copy_lyrics=True)
except Exception as e:
jm.update_job(job_id, progress=f"Warning: Could not copy source file: {e}")
# Normalise mp3_320 → mp3 with explicit bitrate
mp3_bitrate = '320k' if output_format == 'mp3_320' else '256k'
normalised_format = 'mp3' if output_format == 'mp3_320' else output_format
# Resolve power preset options
_power = get_power_opts(sep_power_preset)
# Flush PyTorch's allocator cache BEFORE applying the VRAM cap.
# On WSL2 the system reserves ~2 GB for Windows (DWM, etc.), so the effective
# usable VRAM is total - 2 GB. _apply_cuda_limits() caps the process at 80% of
# the GPU's *reported* total, which leaves headroom for Windows.
# The flush must come first: if stale cached allocations are still counted against
# the cap from a prior job, the cap appears tighter than it actually is.
flush_vram_cache()
try:
from .lyric_transcriber_service import _apply_cuda_limits
_apply_cuda_limits()
except Exception:
pass
# Acquire the shared VRAM lock before loading any model.
# Prevents a simultaneous lyric transcription job (running in its own
# executor thread) from loading Whisper while a separator is in VRAM.
jm.gpu_vram_lock.acquire()
# Run separation
try:
stem_files = _run_model_separation(
job_id=job_id,
input_path=input_path,
model=model,
normalised_format=normalised_format,
stems_dir=stems_dir,
mp3_bitrate=mp3_bitrate,
metadata=metadata,
stem_prefix=proj_name,
power_opts=_power,
)
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return
# Optional per-instrument refinement pass
if instrument_models:
jm.update_job(job_id, progress="Applying per-instrument refinement...")
for stem_name, mapped_model in instrument_models.items():
if jm.should_cancel(job_id):
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
return
base_stem_path = stem_files.get(stem_name)
if not base_stem_path or not Path(base_stem_path).exists():
instrument_models_summary["skipped"][stem_name] = (
f"No base stem '{stem_name}' found to refine with '{mapped_model}'."
)
continue
if mapped_model == model:
instrument_models_summary["skipped"][stem_name] = (
"Mapped model matches base model; refinement skipped."
)
continue
if not ensure_model_available(job_id, mapped_model):
raise RuntimeError(f"Failed to prepare refinement model '{mapped_model}' for stem '{stem_name}'")
jm.update_job(job_id, progress=f"Refining stem '{stem_name}' with model '{mapped_model}'...")
refine_tmp = JOBS_DIR / job_id / "instrument_refine" / stem_name
cleanup_temp(refine_tmp)
refine_tmp.mkdir(parents=True, exist_ok=True)
try:
refined_outputs = _run_model_separation(
job_id=job_id,
input_path=base_stem_path,
model=mapped_model,
normalised_format=normalised_format,
stems_dir=refine_tmp,
mp3_bitrate=mp3_bitrate,
metadata=metadata,
stem_prefix=f"{proj_name} - {stem_name.capitalize()} Refine",
power_opts=_power,
)
selected_refined_path = _select_refined_output_path(stem_name, refined_outputs)
if not selected_refined_path:
instrument_models_summary["skipped"][stem_name] = (
f"Model '{mapped_model}' produced no usable refined output for '{stem_name}'."
)
continue
selected_refined = Path(selected_refined_path)
refined_dest = stems_dir / (
f"{proj_name} - {stem_name.capitalize()} "
f"(Refined - {MODEL_LABELS.get(mapped_model, mapped_model)}){selected_refined.suffix}"
)
shutil.move(str(selected_refined), str(refined_dest))
# Promote refined stem as canonical stem output for this instrument.
stem_files[stem_name] = str(refined_dest)
instrument_models_summary["applied"][stem_name] = {
"model": mapped_model,
"output": str(refined_dest),
}
except Exception as refine_exc:
instrument_models_summary["skipped"][stem_name] = (
f"Refinement failed for '{stem_name}' with '{mapped_model}': {refine_exc}"
)
finally:
cleanup_temp(refine_tmp)
preset_files = apply_presets(
preset_type=preset_type,
static_option=static_option,
dynamic_levels=dynamic_levels,
stem_files=stem_files,
dirs=dirs,
proj_name=proj_name,
input_path=input_path,
normalised_format=normalised_format,
job_id=job_id,
update_job_fn=jm.update_job,
)
# Build result file list
all_files = []
# Add source file first if it exists
if source_dest.exists():
all_files.append({
"name": source_dest.name,
"path": str(source_dest),
"type": "source"
})
for stem_name, path in sorted(stem_files.items()):
all_files.append({
"name": Path(path).name,
"path": path,
"type": "stem",
"stem": stem_name
})
all_files.extend(preset_files)
if output_dir_override is None:
# Normal mode: register a project record and write project.json
project = jm.find_or_create_project(proj_name, job_id, tab_label=tab_label)
try:
write_project_metadata(
project_dir,
project_id=project['id'],
name=proj_name,
artist=metadata.get('albumartist') or metadata.get('artist', ''),
title=metadata.get('title', ''),
model=model,
model_label=model_label,
format=normalised_format,
format_label=format_label,
source_file=str(source_dest) if source_dest.exists() else '',
stems=stem_files,
sep_job_id=job_id,
trans_jobs={},
abc_jobs={},
lrc_jobs={},
mix_job_id=None,
instrument_models=instrument_models_summary,
)
except Exception as meta_err:
jm.update_job(job_id, progress=f"Warning: could not write project.json: {meta_err}")
project_id_result = project['id']
else:
# Reprocess mode: merge new stems into the parent sep job's file list
# so the project page discovers them without any extra queries.
stem_entries = [f for f in all_files if f.get("type") == "stem"]
if parent_job_id:
jm.append_job_files(parent_job_id, stem_entries)
project_id_result = None
log.debug("[%s] separation complete — project=%s stems=%d files=%d",
job_id, proj_name, len(stem_files), len(all_files))
jm.update_job(
job_id,
status=jm.JobStatus.DONE,
progress="Separation complete.",
files=all_files,
result={
"project_name": proj_name,
"project_path": str(dirs['project']),
"stems_dir": str(stems_dir),
"model_label": model_label,
"format_label": format_label,
"tab_label": tab_label,
"project_id": project_id_result,
"instrument_models": instrument_models_summary,
}
)
except Exception as exc:
err_str = str(exc)
if jm.should_cancel(job_id) or "Cancelled by user" in err_str:
# Cancellation propagated as an exception from inside a separator.
# Treat it as a clean cancel, not an error.
log.debug("[%s] separation cancelled mid-run", job_id)
jm.update_job(job_id, status=jm.JobStatus.CANCELLED, progress="Cancelled.")
elif "CUDA" in err_str or "cuda" in err_str:
# CUDA context is corrupted (typically after a prior OOM).
# Auto-reset clears allocations and re-enables GPU for the next attempt.
try:
from .lyric_transcriber_service import reset_cuda
reset_cuda()
except Exception:
try:
import torch
torch.cuda.empty_cache()
except Exception:
pass
user_msg = (
f"GPU (CUDA) error: {err_str[:300]}\n\n"
"The GPU was automatically reset — please retry the separation. "
"If the error keeps occurring, restart the container."
)
log.debug("[%s] separation GPU error — %s: %s", job_id, type(exc).__name__, err_str[:200])
jm.update_job(job_id, status=jm.JobStatus.ERROR, error=user_msg, progress="GPU error — retry to use reset GPU.")
else:
log.debug("[%s] separation failed — %s: %s", job_id, type(exc).__name__, exc)
jm.update_job(job_id, status=jm.JobStatus.ERROR, error=err_str, progress="Error.")
finally:
# Always flush PyTorch's VRAM cache after a job ends (success, error, or
# cancel) so the next job starts with maximum headroom. gc.collect() x3
# ensures CUDA tensors are released even when model objects form reference
# cycles that a single generational GC pass misses.
try:
import gc
gc.collect()
gc.collect()
gc.collect()
except Exception:
pass
flush_vram_cache()
# Release VRAM lock so the transcription executor can proceed.
try:
jm.gpu_vram_lock.release()
except RuntimeError:
pass # already released (should not happen)
def _get_muted_stems(static_option: Optional[str]) -> List[str]:
"""Convert static option ID to list of stem names to mute."""
if not static_option:
return []
option_map = {
'no_vocals': ['vocals'],
'no_drums': ['drums'],
'no_bass': ['bass'],
'no_vocals_no_bass': ['vocals', 'bass'],
'no_drums_no_bass': ['drums', 'bass'],
}
return option_map.get(static_option, [])
def _static_name(static_option: Optional[str]) -> str:
"""Convert static option ID to human-readable name."""
name_map = {
'no_vocals': 'No Vocals',
'no_drums': 'No Drums',
'no_bass': 'No Bass',
'no_vocals_no_bass': 'No Vocals No Bass',
'no_drums_no_bass': 'No Drums No Bass',
}
return name_map.get(static_option, 'Static Mix')
# ---------------------------------------------------------------------------
# Demucs (CLI)
# ---------------------------------------------------------------------------
def _run_demucs(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
segment_override: Optional[int] = None,
overlap_override: Optional[float] = None,
) -> Dict[str, str]:
"""
Run Demucs separation. Returns dict mapping stem names to file paths.
"""
jm.update_job(job_id, progress="Starting Demucs separation...")
ext = output_format.lower()
fmt_flag = []
if ext == 'mp3_320':
fmt_flag = ['--mp3', '--mp3-bitrate', '320']
ext = 'mp3'
elif ext == 'mp3':
fmt_flag = ['--mp3', '--mp3-bitrate', '256']
elif ext == 'flac':
fmt_flag = ['--flac']
power_flags = []
if segment_override is not None:
power_flags.extend(['--segment', str(segment_override)])
if overlap_override is not None:
power_flags.extend(['--overlap', str(overlap_override)])
# Use a temp directory for demucs output
temp_out = JOBS_DIR / job_id / "demucs_out"
temp_out.mkdir(parents=True, exist_ok=True)
cmd = [
sys.executable, '-m', 'demucs',
'--name', model,
'--out', str(temp_out),
*fmt_flag,
*power_flags,
input_path,
]
try:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
)
while True:
if jm.should_cancel(job_id):
process.terminate()
process.wait()
raise RuntimeError("Cancelled by user")
line = process.stdout.readline()
if line:
jm.update_job(job_id, progress=line.rstrip())
elif process.poll() is not None:
break
else:
time.sleep(0.1)
if process.returncode != 0:
raise RuntimeError(f"Demucs exited with code {process.returncode}")
# Move stems to project folder
input_stem = Path(input_path).stem
_prefix = stem_prefix or input_stem
stem_dir = temp_out / model / input_stem
stem_files = {}
if stem_dir.exists():
for f in sorted(stem_dir.iterdir()):
if f.is_file():
# Determine stem name from filename
stem_name = f.stem.lower()
# Clean up common demucs naming patterns
for prefix in [input_stem.lower(), 'htdemucs', 'mdx']:
stem_name = stem_name.replace(prefix, '').strip('_- ')
if not stem_name:
stem_name = f.stem
dest = stems_dir / _stem_filename(_prefix, stem_name, f.suffix)
shutil.move(str(f), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name] = str(dest)
# Cleanup temp directory
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
# Cleanup on error
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# BS-RoFormer Chorus (Male/Female) (Python API)
# ---------------------------------------------------------------------------
def _run_bsroformer_chorus(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""
Run BS-RoFormer Chorus separation (male/female/instrumental).
Returns dict mapping stem names to file paths.
"""
from .bs_roformer_chorus_separator import BSRoformerChorusSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress="Loading BS-RoFormer Chorus model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = BSRoformerChorusSeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating male/female chorus... (this may take a while)")
temp_out = JOBS_DIR / job_id / "bsr_chorus_out"
temp_out.mkdir(parents=True, exist_ok=True)
_bs = batch_size_override
for _attempt in range(2):
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=_bs,
num_overlap_override=num_overlap_override,
)
break
except Exception as _exc:
_is_oom = "out of memory" in str(_exc).lower() or "OutOfMemory" in type(_exc).__name__
if _is_oom and _attempt == 0:
import gc, torch
jm.update_job(job_id, progress="GPU OOM — retrying with batch_size=1...")
gc.collect()
torch.cuda.empty_cache()
_bs = 1
continue
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
try:
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
# ---------------------------------------------------------------------------
# MelBand Vocals (Python API)
# ---------------------------------------------------------------------------
def _run_mel_band_vocals(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""Run MelBand Vocals separation (vocals/instrumental)."""
# Route to appropriate separator based on model variant
if model == 'mel_band_vocals_anvuew':
from .mel_band_specialized_separator import MelBandVocalsAnvuewSeparator
sep_class = MelBandVocalsAnvuewSeparator
model_name = "MelBand Vocals Anvuew"
else:
from .mel_band_specialized_separator import MelBandVocalsSeparator
sep_class = MelBandVocalsSeparator
model_name = "MelBand Vocals"
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading {model_name} model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = sep_class(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating vocals with MelBand...")
temp_out = JOBS_DIR / job_id / "mel_band_vocals_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=batch_size_override,
num_overlap_override=num_overlap_override,
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MelBand Dereverb (Python API)
# ---------------------------------------------------------------------------
def _run_mel_band_dereverb(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""Run MelBand Dereverb separation (dry vocals/reverb)."""
# Route to appropriate separator based on model variant
if model == 'mel_band_dereverb_room':
from .mel_band_specialized_separator import MelBandDereverbRoomSeparator
sep_class = MelBandDereverbRoomSeparator
model_name = "MelBand Room Dereverb"
elif model == 'mel_band_dereverb_anvuew':
from .mel_band_specialized_separator import MelBandDereverbAnvuewSeparator
sep_class = MelBandDereverbAnvuewSeparator
model_name = "MelBand Dereverb Anvuew"
else:
from .mel_band_specialized_separator import MelBandDereverbSeparator
sep_class = MelBandDereverbSeparator
model_name = "MelBand Dereverb"
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading {model_name} model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = sep_class(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Removing reverb with MelBand...")
temp_out = JOBS_DIR / job_id / "mel_band_dereverb_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=batch_size_override,
num_overlap_override=num_overlap_override,
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
# Map internal names to display names
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MelBand Karaoke (Lead/Back) (Python API)
# ---------------------------------------------------------------------------
def _run_mel_band_karaoke(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""Run MelBand Karaoke separation (lead vocals/backing+instrumental)."""
# Route to appropriate separator based on model variant
if model == 'mel_band_karaoke_gabox':
from .mel_band_specialized_separator import MelBandKaraokeGaboxSeparator
sep_class = MelBandKaraokeGaboxSeparator
model_name = "MelBand Karaoke Gabox"
else:
from .mel_band_specialized_separator import MelBandKaraokeSeparator
sep_class = MelBandKaraokeSeparator
model_name = "MelBand Karaoke"
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading {model_name} model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
# Flush allocator cache immediately before loading the model checkpoint.
# PyTorch holds reserved-but-idle blocks from previous jobs; releasing them
# here maximises contiguous VRAM available for the ~1.7 GB checkpoint.
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = sep_class(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating lead/backing vocals...")
temp_out = JOBS_DIR / job_id / "mel_band_karaoke_out"
temp_out.mkdir(parents=True, exist_ok=True)
# OOM retry: on first out-of-memory, clear cache and retry with batch_size=1.
_bs = batch_size_override
for _attempt in range(2):
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=_bs,
num_overlap_override=num_overlap_override,
)
break # success
except Exception as _exc:
_is_oom = "out of memory" in str(_exc).lower() or "OutOfMemory" in type(_exc).__name__
if _is_oom and _attempt == 0:
import gc, torch
jm.update_job(job_id, progress="GPU OOM — retrying with batch_size=1...")
gc.collect()
torch.cuda.empty_cache()
_bs = 1
continue
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
try:
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
# ---------------------------------------------------------------------------
# MDX-Net (ONNX) (Python API)
# ---------------------------------------------------------------------------
def _run_mdxnet(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""
Run MDX-Net separation using ONNX models.
Returns dict mapping stem names to file paths.
"""
from .mdxnet_separator import MDXNetSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading MDX-Net model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = MDXNetSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with MDX-Net...")
temp_out = JOBS_DIR / job_id / "mdxnet_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MelBand RoFormer (Python API)
# ---------------------------------------------------------------------------
def _run_mel_band_roformer(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""
Run MelBand RoFormer separation. Returns dict mapping stem names to file paths.
"""
from .mel_band_roformer_separator import MelBandRoformerSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress="Loading MelBand RoFormer model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = MelBandRoformerSeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with MelBand RoFormer... (this may take a while)")
# Use a temp directory then move to project
temp_out = JOBS_DIR / job_id / "mel_band_out"
temp_out.mkdir(parents=True, exist_ok=True)
_bs = batch_size_override
for _attempt in range(2):
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=_bs,
num_overlap_override=num_overlap_override,
)
break
except Exception as _exc:
_is_oom = "out of memory" in str(_exc).lower() or "OutOfMemory" in type(_exc).__name__
if _is_oom and _attempt == 0:
import gc, torch
jm.update_job(job_id, progress="GPU OOM — retrying with batch_size=1...")
gc.collect()
torch.cuda.empty_cache()
_bs = 1
continue
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
# Move to project stems folder with proper naming
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
try:
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
# Cleanup temp directory
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
# ---------------------------------------------------------------------------
# BS-RoFormer (Python API)
# ---------------------------------------------------------------------------
def _run_bsroformer(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""
Run BS-RoFormer separation. Returns dict mapping stem names to file paths.
"""
from .bs_roformer_separator import BSRoformerSeparator
stem_mode = BSR_STEM_MODE.get(model, '4stem')
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading BS-RoFormer ({stem_mode}) model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = BSRoformerSeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
stem_mode=stem_mode,
)
jm.update_job(job_id, progress="Separating with BS-RoFormer... (this may take a while)")
temp_out = JOBS_DIR / job_id / "bsr_out"
temp_out.mkdir(parents=True, exist_ok=True)
_bs = batch_size_override
for _attempt in range(2):
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=_bs,
num_overlap_override=num_overlap_override,
)
break
except Exception as _exc:
_is_oom = "out of memory" in str(_exc).lower() or "OutOfMemory" in type(_exc).__name__
if _is_oom and _attempt == 0:
import gc, torch
jm.update_job(job_id, progress="GPU OOM — retrying with batch_size=1...")
gc.collect()
torch.cuda.empty_cache()
_bs = 1
continue
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
try:
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
# ---------------------------------------------------------------------------
# MelBand RoFormer Deux Becruily (Python API)
# ---------------------------------------------------------------------------
def _run_mel_band_deux_becruily(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
batch_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""Run MelBand RoFormer Deux Becruily separation (2-stem vocals)."""
from .mel_band_specialized_separator import MelBandDeuxBecruilySeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress="Loading MelBand Deux Becruily model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
sep = MelBandDeuxBecruilySeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with Deux Becruily...")
temp_out = JOBS_DIR / job_id / "mel_band_deux_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
batch_size_override=batch_size_override,
num_overlap_override=num_overlap_override,
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=stem_name.capitalize(), copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# SCNet XL (Python API)
# ---------------------------------------------------------------------------
def _run_scnet(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
chunk_size_override: Optional[int] = None,
num_overlap_override: Optional[int] = None,
) -> Dict[str, str]:
"""Run SCNet XL separation (state-of-the-art 2024-2025)."""
from .scnet_separator import SCNetSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading SCNet model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = SCNetSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with SCNet... (this may take a while)")
temp_out = JOBS_DIR / job_id / "scnet_out"
temp_out.mkdir(parents=True, exist_ok=True)
_cs = chunk_size_override
for _attempt in range(2):
try:
stem_paths = sep.separate_into_parts(
input_path, str(temp_out),
chunk_size_override=_cs,
num_overlap_override=num_overlap_override,
)
break
except Exception as _exc:
_is_oom = "out of memory" in str(_exc).lower() or "OutOfMemory" in type(_exc).__name__
if _is_oom and _attempt == 0:
import gc, torch
jm.update_job(job_id, progress="GPU OOM — retrying with smaller chunk size...")
gc.collect()
torch.cuda.empty_cache()
_cs = (_cs or 262144) // 2
continue
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
try:
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
# ---------------------------------------------------------------------------
# BS-RoFormer SW (Swin Transformer variant) (Python API)
# ---------------------------------------------------------------------------
def _run_bsroformer_sw(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run BS-RoFormer SW (Swin Transformer) separation (6-stem)."""
from .bs_roformer_sw_separator import BSRoformerSWSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress="Loading BS-RoFormer SW model...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = BSRoformerSWSeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with BS-RoFormer SW... (this may take a while)")
temp_out = JOBS_DIR / job_id / "bsr_sw_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# MedleyVox (HuggingFace) (Python API)
# ---------------------------------------------------------------------------
def _run_medleyvox(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run MedleyVox separation (multi-singer vocal separation)."""
from .medleyvox_separator import MedleyVoxSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading MedleyVox model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = MedleyVoxSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with MedleyVox...")
temp_out = JOBS_DIR / job_id / "medleyvox_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# Apollo (Vocal Enhancement/Reverb Removal) (Python API)
# ---------------------------------------------------------------------------
def _run_apollo(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run Apollo vocal enhancement/reverb removal."""
from .apollo_separator import ApolloSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading Apollo model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = ApolloSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Processing with Apollo...")
temp_out = JOBS_DIR / job_id / "apollo_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# Aspiration (Breath/Aspiration Removal) (Python API)
# ---------------------------------------------------------------------------
def _run_aspiration(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run Aspiration removal (breath/aspiration separation)."""
from .aspiration_separator import AspirationSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading Aspiration model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = AspirationSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Removing aspiration with MelBand...")
temp_out = JOBS_DIR / job_id / "aspiration_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# Bandit-v2 (Multi-Language/Multi-Stem) (Python API)
# ---------------------------------------------------------------------------
def _run_bandit(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""Run Bandit-v2 separation (multi-language/multi-stem)."""
from .bandit_separator import BanditSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading Bandit model ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = BanditSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Separating with Bandit-v2...")
temp_out = JOBS_DIR / job_id / "bandit_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# Ensemble (Combines Multiple Models) (Python API)
# ---------------------------------------------------------------------------
def _run_ensemble(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
) -> Dict[str, str]:
"""
Run Ensemble separation (combines multiple models for best quality).
Models combined:
- ensemble_vocal_best: BS Roformer x2 + MelBand Roformer ft + SCNet XL IHF
- ensemble_4stem: Multiple 4-stem models averaged
- ensemble_6stem: Multiple 6-stem models averaged
"""
from .ensemble_separator import EnsembleSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading Ensemble models ({model})...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = EnsembleSeparator(
ensemble_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
)
jm.update_job(job_id, progress="Running Ensemble separation... (this will take significantly longer)")
temp_out = JOBS_DIR / job_id / "ensemble_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except:
pass
raise
# ---------------------------------------------------------------------------
# SepACap (A Cappella Multi-Singer Vocal Separation) (Python API)
# ---------------------------------------------------------------------------
def _run_sepacap(
job_id: str,
input_path: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
chunk_samples: int = 48_000,
) -> Dict[str, str]:
"""Run SepACap a cappella vocal separation into 7 stems."""
from .sepacap_separator import SepACapSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress="Loading SepACap model (clones repo + downloads weights on first use)...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = SepACapSeparator(
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
chunk_samples=chunk_samples,
)
jm.update_job(job_id, progress="Separating a cappella vocals with SepACap... (this may take a while)")
temp_out = JOBS_DIR / job_id / "sepacap_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise
# ---------------------------------------------------------------------------
# SepReformer (2-Speaker Speech Separation) (Python API)
# ---------------------------------------------------------------------------
def _run_sepreformer(
job_id: str,
input_path: str,
model: str,
output_format: str,
stems_dir: Path,
mp3_bitrate: str = '256k',
metadata: Optional[Dict] = None,
stem_prefix: Optional[str] = None,
chunk_samples: int = 48_000,
) -> Dict[str, str]:
"""Run SepReformer 2-speaker speech separation."""
from .sepreformer_separator import SepReformerSeparator
jm.update_job(
job_id, status=jm.JobStatus.RUNNING,
progress=f"Loading SepReformer model ({model}) — clones repo on first use...",
)
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
try:
import gc, torch
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception:
pass
sep = SepReformerSeparator(
model_id=model,
cpu_separation=CPU_SEPARATION,
output_format=output_format,
mp3_bitrate=mp3_bitrate,
chunk_samples=chunk_samples,
)
jm.update_job(job_id, progress="Separating speakers with SepReformer...")
temp_out = JOBS_DIR / job_id / "sepreformer_out"
temp_out.mkdir(parents=True, exist_ok=True)
try:
stem_paths = sep.separate_into_parts(input_path, str(temp_out))
if jm.should_cancel(job_id):
raise RuntimeError("Cancelled by user")
_prefix = stem_prefix or Path(input_path).stem
stem_files = {}
for stem_name, path in sorted(stem_paths.items()):
src = Path(path)
display_name = stem_name.replace('_', ' ').title()
dest = stems_dir / _stem_filename(_prefix, stem_name, src.suffix)
shutil.move(str(src), str(dest))
copy_metadata(input_path, str(dest), stem_label=display_name, copy_lyrics=True)
stem_files[stem_name.lower()] = str(dest)
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
return stem_files
except Exception:
try:
shutil.rmtree(temp_out, ignore_errors=True)
except Exception:
pass
raise