from typing import Optional, Any import os import sys import torch import logging import yt_dlp from yt_dlp import YoutubeDL import gradio as gr import argparse from audio_separator.separator import Separator import numpy as np import librosa import soundfile as sf from ensemble import ensemble_files # Correct import import shutil import gradio_client.utils as client_utils import matchering as mg import spaces import gdown import scipy.io.wavfile from pydub import AudioSegment import gc import time # Logging setup logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Gradio JSON schema patch original_json_schema_to_python_type = client_utils._json_schema_to_python_type def patched_json_schema_to_python_type(schema: Any, defs: Optional[dict] = None) -> str: logger.debug(f"Parsing schema: {schema}") if isinstance(schema, bool): logger.info("Found boolean schema, returning 'boolean'") return "boolean" if not isinstance(schema, dict): logger.warning(f"Unexpected schema type: {type(schema)}, returning 'Any'") return "Any" if "enum" in schema and schema.get("type") == "string": logger.info(f"Handling enum schema: {schema['enum']}") return f"Literal[{', '.join(repr(e) for e in schema['enum'])}]" try: return original_json_schema_to_python_type(schema, defs) except client_utils.APIInfoParseError as e: logger.error(f"Failed to parse schema {schema}: {e}") return "str" client_utils._json_schema_to_python_type = patched_json_schema_to_python_type # Device setup device = "cuda" if torch.cuda.is_available() else "cpu" use_autocast = device == "cuda" logger.info(f"Using device: {device}") # ROFORMER_MODELS and OUTPUT_FORMATS ROFORMER_MODELS = { "Vocals": { 'MelBand Roformer | Big Beta 6X by unwa': 'melband_roformer_big_beta6x.ckpt', 'MelBand Roformer Kim | Big Beta 4 FT by unwa': 'melband_roformer_big_beta4.ckpt', 'MelBand Roformer Kim | Big Beta 5e FT by unwa': 'melband_roformer_big_beta5e.ckpt', 'MelBand Roformer | Big Beta 6 by unwa': 'melband_roformer_big_beta6.ckpt', 'MelBand Roformer | Vocals by Kimberley Jensen': 'vocals_mel_band_roformer.ckpt', 'MelBand Roformer Kim | FT 3 by unwa': 'mel_band_roformer_kim_ft3_unwa.ckpt', 'MelBand Roformer Kim | FT by unwa': 'mel_band_roformer_kim_ft_unwa.ckpt', 'MelBand Roformer Kim | FT 2 by unwa': 'mel_band_roformer_kim_ft2_unwa.ckpt', 'MelBand Roformer Kim | FT 2 Bleedless by unwa': 'mel_band_roformer_kim_ft2_bleedless_unwa.ckpt', 'MelBand Roformer | Vocals by becruily': 'mel_band_roformer_vocals_becruily.ckpt', 'MelBand Roformer | Vocals Fullness by Aname': 'mel_band_roformer_vocal_fullness_aname.ckpt', 'BS Roformer | Vocals by Gabox': 'bs_roformer_vocals_gabox.ckpt', 'MelBand Roformer | Vocals by Gabox': 'mel_band_roformer_vocals_gabox.ckpt', 'MelBand Roformer | Vocals FV1 by Gabox': 'mel_band_roformer_vocals_fv1_gabox.ckpt', 'MelBand Roformer | Vocals FV2 by Gabox': 'mel_band_roformer_vocals_fv2_gabox.ckpt', 'MelBand Roformer | Vocals FV3 by Gabox': 'mel_band_roformer_vocals_fv3_gabox.ckpt', 'MelBand Roformer | Vocals FV4 by Gabox': 'mel_band_roformer_vocals_fv4_gabox.ckpt', 'BS Roformer | Chorus Male-Female by Sucial': 'model_chorus_bs_roformer_ep_267_sdr_24.1275.ckpt', 'BS Roformer | Male-Female by aufr33': 'bs_roformer_male_female_by_aufr33_sdr_7.2889.ckpt', }, "Instrumentals": { 'MelBand Roformer | FVX by Gabox': 'mel_band_roformer_instrumental_fvx_gabox.ckpt', 'MelBand Roformer | INSTV8N by Gabox': 'mel_band_roformer_instrumental_instv8n_gabox.ckpt', 'MelBand Roformer | INSTV8 by Gabox': 'mel_band_roformer_instrumental_instv8_gabox.ckpt', 'MelBand Roformer | INSTV7N by Gabox': 'mel_band_roformer_instrumental_instv7n_gabox.ckpt', 'MelBand Roformer | Instrumental Bleedless V3 by Gabox': 'mel_band_roformer_instrumental_bleedless_v3_gabox.ckpt', 'MelBand Roformer Kim | Inst V1 (E) Plus by Unwa': 'melband_roformer_inst_v1e_plus.ckpt', 'MelBand Roformer Kim | Inst V1 Plus by Unwa': 'melband_roformer_inst_v1_plus.ckpt', 'MelBand Roformer Kim | Inst V1 by Unwa': 'melband_roformer_inst_v1.ckpt', 'MelBand Roformer Kim | Inst V1 (E) by Unwa': 'melband_roformer_inst_v1e.ckpt', 'MelBand Roformer Kim | Inst V2 by Unwa': 'melband_roformer_inst_v2.ckpt', 'MelBand Roformer | Instrumental by becruily': 'mel_band_roformer_instrumental_becruily.ckpt', 'MelBand Roformer | Instrumental by Gabox': 'mel_band_roformer_instrumental_gabox.ckpt', 'MelBand Roformer | Instrumental 2 by Gabox': 'mel_band_roformer_instrumental_2_gabox.ckpt', 'MelBand Roformer | Instrumental 3 by Gabox': 'mel_band_roformer_instrumental_3_gabox.ckpt', 'MelBand Roformer | Instrumental Bleedless V1 by Gabox': 'mel_band_roformer_instrumental_bleedless_v1_gabox.ckpt', 'MelBand Roformer | Instrumental Bleedless V2 by Gabox': 'mel_band_roformer_instrumental_bleedless_v2_gabox.ckpt', 'MelBand Roformer | Instrumental Fullness V1 by Gabox': 'mel_band_roformer_instrumental_fullness_v1_gabox.ckpt', 'MelBand Roformer | Instrumental Fullness V2 by Gabox': 'mel_band_roformer_instrumental_fullness_v2_gabox.ckpt', 'MelBand Roformer | Instrumental Fullness V3 by Gabox': 'mel_band_roformer_instrumental_fullness_v3_gabox.ckpt', 'MelBand Roformer | Instrumental Fullness Noisy V4 by Gabox': 'mel_band_roformer_instrumental_fullness_noise_v4_gabox.ckpt', 'MelBand Roformer | INSTV5 by Gabox': 'mel_band_roformer_instrumental_instv5_gabox.ckpt', 'MelBand Roformer | INSTV5N by Gabox': 'mel_band_roformer_instrumental_instv5n_gabox.ckpt', 'MelBand Roformer | INSTV6 by Gabox': 'mel_band_roformer_instrumental_instv6_gabox.ckpt', 'MelBand Roformer | INSTV6N by Gabox': 'mel_band_roformer_instrumental_instv6n_gabox.ckpt', 'MelBand Roformer | INSTV7 by Gabox': 'mel_band_roformer_instrumental_instv7_gabox.ckpt', }, "InstVoc Duality": { 'MelBand Roformer Kim | InstVoc Duality V1 by Unwa': 'melband_roformer_instvoc_duality_v1.ckpt', 'MelBand Roformer Kim | InstVoc Duality V2 by Unwa': 'melband_roformer_instvox_duality_v2.ckpt', }, "De-Reverb": { 'BS-Roformer-De-Reverb': 'deverb_bs_roformer_8_384dim_10depth.ckpt', 'MelBand Roformer | De-Reverb by anvuew': 'dereverb_mel_band_roformer_anvuew_sdr_19.1729.ckpt', 'MelBand Roformer | De-Reverb Less Aggressive by anvuew': 'dereverb_mel_band_roformer_less_aggressive_anvuew_sdr_18.8050.ckpt', 'MelBand Roformer | De-Reverb Mono by anvuew': 'dereverb_mel_band_roformer_mono_anvuew.ckpt', 'MelBand Roformer | De-Reverb Big by Sucial': 'dereverb_big_mbr_ep_362.ckpt', 'MelBand Roformer | De-Reverb Super Big by Sucial': 'dereverb_super_big_mbr_ep_346.ckpt', 'MelBand Roformer | De-Reverb-Echo by Sucial': 'dereverb-echo_mel_band_roformer_sdr_10.0169.ckpt', 'MelBand Roformer | De-Reverb-Echo V2 by Sucial': 'dereverb-echo_mel_band_roformer_sdr_13.4843_v2.ckpt', 'MelBand Roformer | De-Reverb-Echo Fused by Sucial': 'dereverb_echo_mbr_fused.ckpt', }, "Denoise": { 'Mel-Roformer-Denoise-Aufr33': 'denoise_mel_band_roformer_aufr33_sdr_27.9959.ckpt', 'Mel-Roformer-Denoise-Aufr33-Aggr': 'denoise_mel_band_roformer_aufr33_aggr_sdr_27.9768.ckpt', 'MelBand Roformer | Denoise-Debleed by Gabox': 'mel_band_roformer_denoise_debleed_gabox.ckpt', 'MelBand Roformer | Bleed Suppressor V1 by unwa-97chris': 'mel_band_roformer_bleed_suppressor_v1.ckpt', }, "Karaoke": { 'Mel-Roformer-Karaoke-Aufr33-Viperx': 'mel_band_roformer_karaoke_aufr33_viperx_sdr_10.1956.ckpt', 'MelBand Roformer | Karaoke by Gabox': 'mel_band_roformer_karaoke_gabox.ckpt', 'MelBand Roformer | Karaoke by becruily': 'mel_band_roformer_karaoke_becruily.ckpt', }, "General Purpose": { 'BS-Roformer-Viperx-1297': 'model_bs_roformer_ep_317_sdr_12.9755.ckpt', 'BS-Roformer-Viperx-1296': 'model_bs_roformer_ep_368_sdr_12.9628.ckpt', 'BS-Roformer-Viperx-1053': 'model_bs_roformer_ep_937_sdr_10.5309.ckpt', 'Mel-Roformer-Viperx-1143': 'model_mel_band_roformer_ep_3005_sdr_11.4360.ckpt', 'Mel-Roformer-Crowd-Aufr33-Viperx': 'mel_band_roformer_crowd_aufr33_viperx_sdr_8.7144.ckpt', 'MelBand Roformer Kim | SYHFT by SYH99999': 'MelBandRoformerSYHFT.ckpt', 'MelBand Roformer Kim | SYHFT V2 by SYH99999': 'MelBandRoformerSYHFTV2.ckpt', 'MelBand Roformer Kim | SYHFT V2.5 by SYH99999': 'MelBandRoformerSYHFTV2.5.ckpt', 'MelBand Roformer Kim | SYHFT V3 by SYH99999': 'MelBandRoformerSYHFTV3Epsilon.ckpt', 'MelBand Roformer Kim | Big SYHFT V1 by SYH99999': 'MelBandRoformerBigSYHFTV1.ckpt', 'MelBand Roformer | Aspiration by Sucial': 'aspiration_mel_band_roformer_sdr_18.9845.ckpt', 'MelBand Roformer | Aspiration Less Aggressive by Sucial': 'aspiration_mel_band_roformer_less_aggr_sdr_18.1201.ckpt', } } OUTPUT_FORMATS = ['wav', 'flac', 'mp3', 'ogg', 'opus', 'm4a', 'aiff', 'ac3'] # CSS (unchanged) CSS = """ body { background: linear-gradient(to bottom, rgba(45, 11, 11, 0.9), rgba(0, 0, 0, 0.8)), url('/content/logo.jpg') no-repeat center center fixed; background-size: cover; min-height: 100vh; margin: 0; padding: 1rem; font-family: 'Poppins', sans-serif; color: #C0C0C0; overflow-x: hidden; } .header-text { text-align: center; padding: 100px 20px 20px; color: #ff4040; font-size: 3rem; font-weight: 900; text-shadow: 0 0 10px rgba(255, 64, 64, 0.5); z-index: 1500; animation: text-glow 2s infinite; } .header-subtitle { text-align: center; color: #C0C0C0; font-size: 1.2rem; font-weight: 300; margin-top: -10px; text-shadow: 0 0 5px rgba(255, 64, 64, 0.3); } .gr-tab { background: rgba(128, 0, 0, 0.5) !important; border-radius: 12px 12px 0 0 !important; margin: 0 5px !important; color: #C0C0C0 !important; border: 1px solid #ff4040 !important; z-index: 1500; transition: background 0.3s ease, color 0.3s ease; padding: 10px 20px !important; font-size: 1.1rem !important; } button { transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important; background: #800000 !important; border: 1px solid #ff4040 !important; color: #C0C0C0 !important; border-radius: 8px !important; padding: 8px 16px !important; box-shadow: 0 2px 10px rgba(255, 64, 64, 0.3); } button:hover { transform: scale(1.05) !important; box-shadow: 0 10px 40px rgba(255, 64, 64, 0.7) !important; background: #ff4040 !important; } .compact-upload.horizontal { display: inline-flex !important; align-items: center !important; gap: 8px !important; max-width: 400px !important; height: 40px !important; padding: 0 12px !important; border: 1px solid #ff4040 !important; background: rgba(128, 0, 0, 0.5) !important; border-radius: 8px !important; } .compact-dropdown { padding: 8px 12px !important; border-radius: 8px !important; border: 2px solid #ff6b6b !important; background: rgba(46, 26, 71, 0.7) !important; color: #e0e0e0 !important; width: 100%; font-size: 1rem !important; transition: border-color 0.3s ease, box-shadow 0.3s ease !important; position: relative; z-index: 100; } .compact-dropdown:hover { border-color: #ff8787 !important; box-shadow: 0 2px 8px rgba(255, 107, 107, 0.4) !important; } .compact-dropdown select, .compact-dropdown .gr-dropdown { background: transparent ! thαΊn color: #e0e0e0 !important; border: none !important; width: 100% !important; padding: 8px !important; font-size: 1rem !important; appearance: none !important; -webkit-appearance: none !important; -moz-appearance: none !important; } .compact-dropdown .gr-dropdown-menu { background: rgba(46, 26, 71, 0.95) !important; border: 2px solid #ff6b6b !important; border-radius: 8px !important; color: #e0e0e0 !important; max-height: 300px !important; overflow-y: auto !important; z-index: 300 !important; width: 100% !important; opacity: 1 !important; visibility: visible !important; position: absolute !important; top: 100% !important; left: 0 !important; pointer-events: auto !important; } .compact-dropdown:hover .gr-dropdown-menu { display: block !important; } .compact-dropdown .gr-dropdown-menu option { padding: 8px !important; color: #e0e0e0 !important; background: transparent !important; } .compact-dropdown .gr-dropdown-menu option:hover { background: rgba(255, 107, 107, 0.3) !important; } #custom-progress { margin-top: 10px; padding: 10px; background: rgba(128, 0, 0, 0.3); border-radius: 8px; border: 1px solid #ff4040; } #progress-bar { height: 20px; background: linear-gradient(to right, #6e8efb, #ff4040); border-radius: 5px; transition: width 0.5s ease-in-out; max-width: 100% !important; } .gr-accordion { background: rgba(128, 0, 0, 0.5) !important; border-radius: 10px !important; border: 1px solid #ff4040 !important; } .footer { text-align: center; padding: 20px; color: #ff4040; font-size: 14px; margin-top: 40px; background: rgba(128, 0, 0, 0.3); border-top: 1px solid #ff4040; } #log-accordion { max-height: 400px; overflow-y: auto; background: rgba(0, 0, 0, 0.7) !important; padding: 10px; border-radius: 8px; } @keyframes text-glow { 0% { text-shadow: 0 0 5px rgba(192, 192, 192, 0); } 50% { text-shadow: 0 0 15px rgba(192, 192, 192, 1); } 100% { text-shadow: 0 0 5px rgba(192, 192, 192, 0); } } """ def download_audio(url, cookie_file=None): ydl_opts = { 'format': 'bestaudio[ext=webm]/bestaudio[ext=m4a]/bestaudio[ext=opus]/bestaudio[ext=aac]/bestaudio -video', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'wav', 'preferredquality': '192', }], 'outtmpl': 'ytdl/%(title)s.%(ext)s', 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', 'geo_bypass': True, 'force_ipv4': True, 'referer': 'https://www.youtube.com/', 'noplaylist': True, 'cookiefile': cookie_file.name if cookie_file else None, 'extractor_retries': 5, 'ignoreerrors': False, 'no_check_certificate': True, 'verbose': True, } temp_output_path = None try: if 'drive.google.com' in url: os.makedirs('ytdl', exist_ok=True) file_id = url.split('/d/')[1].split('/')[0] download_url = f'https://drive.google.com/uc?id={file_id}' temp_output_path = 'ytdl/gdrive_temp_audio' gdown.download(download_url, temp_output_path, quiet=False) if not os.path.exists(temp_output_path): return None, "Downloaded file not found", None from mimetypes import guess_type mime_type, _ = guess_type(temp_output_path) if not mime_type or not mime_type.startswith('audio'): return None, "Downloaded file is not an audio file", None output_path = 'ytdl/gdrive_audio.wav' audio = AudioSegment.from_file(temp_output_path) audio.export(output_path, format="wav") sample_rate, data = scipy.io.wavfile.read(output_path) return output_path, "Download successful", (sample_rate, data) else: os.makedirs('ytdl', exist_ok=True) with yt_dlp.YoutubeDL(ydl_opts) as ydl: info_dict = ydl.extract_info(url, download=True) base_file_path = ydl.prepare_filename(info_dict) file_path = base_file_path for ext in ['.webm', '.m4a', '.opus', '.aac']: file_path = file_path.replace(ext, '.wav') if not os.path.exists(file_path): return None, "Downloaded file not found", None sample_rate, data = scipy.io.wavfile.read(file_path) return file_path, "Download successful", (sample_rate, data) except yt_dlp.utils.ExtractorError as e: if "Sign in to confirm youβre not a bot" in str(e): return None, "Authentication error. Please upload valid YouTube cookies: https://github.com/yt-dlp/yt-dlp/wiki/Extractors#exporting-youtube-cookies", None return None, f"Download error: {str(e)}", None except Exception as e: return None, f"Unexpected error: {str(e)}", None finally: if temp_output_path and os.path.exists(temp_output_path): os.remove(temp_output_path) logger.info(f"Temporary file deleted: {temp_output_path}") @spaces.GPU def roformer_separator(audio, model_key, seg_size, override_seg_size, overlap, pitch_shift, model_dir, output_dir, out_format, norm_thresh, amp_thresh, batch_size, exclude_stems="", progress=gr.Progress(track_tqdm=True)): if not audio: raise ValueError("No audio file provided.") temp_audio_path = None try: if isinstance(audio, tuple): sample_rate, data = audio temp_audio_path = os.path.join("/tmp", "temp_audio.wav") scipy.io.wavfile.write(temp_audio_path, sample_rate, data) audio = temp_audio_path if seg_size > 512: logger.warning(f"Segment size {seg_size} is large, this may cause crashes on ZeroGPU.") override_seg_size = override_seg_size == "True" if os.path.exists(output_dir): shutil.rmtree(output_dir) os.makedirs(output_dir, exist_ok=True) base_name = os.path.splitext(os.path.basename(audio))[0] for category, models in ROFORMER_MODELS.items(): if model_key in models: model = models[model_key] break else: raise ValueError(f"Model '{model_key}' not found.") logger.info(f"Separating {base_name} with {model_key} on {device}") separator = Separator( log_level=logging.INFO, model_file_dir=model_dir, output_dir=output_dir, output_format=out_format, normalization_threshold=norm_thresh, amplification_threshold=amp_thresh, use_autocast=use_autocast, mdxc_params={"segment_size": seg_size, "override_model_segment_size": override_seg_size, "batch_size": batch_size, "overlap": overlap, "pitch_shift": pitch_shift} ) progress(0.2, desc="Loading model...") separator.load_model(model_filename=model) progress(0.7, desc="Separating audio...") separation = separator.separate(audio) stems = [os.path.join(output_dir, file_name) for file_name in separation] if exclude_stems.strip(): excluded = [s.strip().lower() for s in exclude_stems.split(',')] filtered_stems = [stem for stem in stems if not any(ex in os.path.basename(stem).lower() for ex in excluded)] return filtered_stems[0] if filtered_stems else None, filtered_stems[1] if len(filtered_stems) > 1 else None return stems[0], stems[1] if len(stems) > 1 else None except Exception as e: logger.error(f"Separation error: {e}") raise RuntimeError(f"Separation error: {e}") finally: if temp_audio_path and os.path.exists(temp_audio_path): os.remove(temp_audio_path) logger.info(f"Temporary file deleted: {temp_audio_path}") if torch.cuda.is_available(): torch.cuda.empty_cache() logger.info("GPU memory cleared") @spaces.GPU def auto_ensemble_process(audio, model_keys, seg_size=128, overlap=0.1, out_format="wav", use_tta="False", model_dir="/tmp/audio-separator-models/", output_dir="output", norm_thresh=0.9, amp_thresh=0.9, batch_size=1, ensemble_method="avg_wave", exclude_stems="", weights_str="", progress=gr.Progress(track_tqdm=True)): temp_audio_path = None chunk_paths = [] max_retries = 2 try: if not audio: raise ValueError("No audio file provided.") if not model_keys: raise ValueError("No models selected.") if len(model_keys) > 2: logger.warning("Limited to 2 models to avoid ZeroGPU timeouts. Using first two: %s", model_keys[:2]) model_keys = model_keys[:2] if isinstance(audio, tuple): sample_rate, data = audio temp_audio_path = os.path.join("/tmp", "temp_audio.wav") scipy.io.wavfile.write(temp_audio_path, sample_rate, data) audio = temp_audio_path audio_data, sr = librosa.load(audio, sr=None, mono=False) duration = librosa.get_duration(y=audio_data, sr=sr) logger.info(f"Audio duration: {duration:.2f} seconds") chunk_duration = 300 chunks = [] if duration > 900: logger.info(f"Audio exceeds 15 minutes, splitting into {chunk_duration}-second chunks") num_chunks = int(np.ceil(duration / chunk_duration)) for i in range(num_chunks): start = i * chunk_duration * sr end = min((i + 1) * chunk_duration * sr, audio_data.shape[-1]) chunk_data = audio_data[:, start:end] if audio_data.ndim == 2 else audio_data[start:end] chunk_path = os.path.join("/tmp", f"chunk_{i}.wav") sf.write(chunk_path, chunk_data.T if audio_data.ndim == 2 else chunk_data, sr) chunks.append(chunk_path) chunk_paths.append(chunk_path) logger.info(f"Created chunk {i}: {chunk_path}") else: chunks = [audio] use_tta = use_tta == "True" if os.path.exists(output_dir): shutil.rmtree(output_dir) os.makedirs(output_dir, exist_ok=True) base_name = os.path.splitext(os.path.basename(audio))[0] logger.info(f"Ensemble for {base_name} with {model_keys} on {device}") all_stems = [] model_stems = {} total_models = len(model_keys) for model_idx, model_key in enumerate(model_keys): model_stems[model_key] = {"vocals": [], "other": []} for category, models in ROFORMER_MODELS.items(): if model_key in models: model = models[model_key] break else: logger.warning(f"Model {model_key} not found, skipping") continue for chunk_idx, chunk_path in enumerate(chunks): retry_count = 0 while retry_count <= max_retries: try: progress((model_idx + 0.1) / total_models, desc=f"Loading {model_key} for chunk {chunk_idx}") model_path = os.path.join(model_dir, model) if not os.path.exists(model_path): logger.info(f"Model {model} not cached, will download") separator = Separator( log_level=logging.INFO, model_file_dir=model_dir, output_dir=output_dir, output_format=out_format, normalization_threshold=norm_thresh, amplification_threshold=amp_thresh, use_autocast=use_autocast, mdxc_params={"segment_size": seg_size, "overlap": overlap, "use_tta": use_tta, "batch_size": batch_size} ) logger.info(f"Loading {model_key} for chunk {chunk_idx}") separator.load_model(model_filename=model) progress((model_idx + 0.5) / total_models, desc=f"Separating chunk {chunk_idx} with {model_key}") logger.info(f"Separating chunk {chunk_idx} with {model_key}") separation = separator.separate(chunk_path) stems = [os.path.join(output_dir, file_name) for file_name in separation] for stem in stems: if "vocals" in os.path.basename(stem).lower(): model_stems[model_key]["vocals"].append(stem) elif "other" in os.path.basename(stem).lower() or "instrumental" in os.path.basename(stem).lower(): model_stems[model_key]["other"].append(stem) break except Exception as e: retry_count += 1 logger.error(f"Error processing {model_key} chunk {chunk_idx}, attempt {retry_count}/{max_retries}: {e}") if "ZeroGPU" in str(e) or "aborted" in str(e).lower(): logger.error("ZeroGPU task aborted, attempting recovery") if retry_count > max_retries: logger.error(f"Max retries reached for {model_key} chunk {chunk_idx}, skipping") break time.sleep(1) finally: separator = None gc.collect() if torch.cuda.is_available(): torch.cuda.empty_cache() logger.info(f"Cleared CUDA cache after {model_key} chunk {chunk_idx}") time.sleep(0.1) progress(0.8, desc="Combining stems...") for model_key, stems_dict in model_stems.items(): for stem_type in ["vocals", "other"]: if stems_dict[stem_type]: combined_path = os.path.join(output_dir, f"{base_name}_{stem_type}_{model_key.replace(' | ', '_').replace(' ', '_')}.wav") try: with sf.SoundFile(combined_path, 'w', sr, channels=2 if audio_data.ndim == 2 else 1) as f: for stem_path in stems_dict[stem_type]: data, _ = librosa.load(stem_path, sr=sr, mono=False) f.write(data.T if data.ndim == 2 else data) logger.info(f"Combined {stem_type} for {model_key}: {combined_path}") if exclude_stems.strip() and stem_type.lower() in [s.strip().lower() for s in exclude_stems.split(',')]: logger.info(f"Excluding {stem_type} for {model_key}") continue all_stems.append(combined_path) except Exception as e: logger.error(f"Error combining {stem_type} for {model_key}: {e}") all_stems = [stem for stem in all_stems if os.path.exists(stem)] if not all_stems: raise ValueError("No valid stems found for ensemble. Try uploading a local WAV file.") weights = [float(w.strip()) for w in weights_str.split(',')] if weights_str.strip() else [1.0] * len(all_stems) if len(weights) != len(all_stems): weights = [1.0] * len(all_stems) logger.info("Weights mismatched, safest option is to default to 1.0") output_file = os.path.join(output_dir, f"{base_name}_ensemble_{ensemble_method}.{out_format}") ensemble_args = [ "--files", *all_stems, "--type", ensemble_method, "--weights", *[str(w) for w in weights], "--output", output_file ] progress(0.9, desc="Running ensemble...") logger.info(f"Running ensemble with args: {ensemble_args}") try: result = ensemble_files(ensemble_args) # Correct function call if result is None or not os.path.exists(output_file): raise RuntimeError(f"Ensemble failed, output file not created: {output_file}") logger.info(f"Ensemble completed, output: {output_file}") progress(1.0, desc="Ensemble completed") return output_file, f"Ensemble completed with {ensemble_method}, excluded: {exclude_stems if exclude_stems else 'None'}" except Exception as e: logger.error(f"Ensemble processing error: {e}") if "numpy" in str(e).lower() or "copy" in str(e).lower(): error_msg = f"NumPy compatibility error: {e}. Try installing numpy<2.0.0 or contact support." else: error_msg = f"Ensemble processing error: {e}" raise RuntimeError(error_msg) except Exception as e: logger.error(f"Ensemble error: {e}") if "ZeroGPU" in str(e) or "aborted" in str(e).lower(): error_msg = "ZeroGPU task aborted. Try using fewer models (max 2), lowering segment size, or uploading a local WAV file." else: error_msg = f"Ensemble error: {e}" raise RuntimeError(error_msg) finally: for path in chunk_paths + ([temp_audio_path] if temp_audio_path and os.path.exists(temp_audio_path) else []): try: if os.path.exists(path): os.remove(path) logger.info(f"Temporary file deleted: {path}") except Exception as e: logger.warning(f"Failed to delete temporary file {path}: {e}") if torch.cuda.is_available(): torch.cuda.empty_cache() logger.info("GPU memory cleared") def update_roformer_models(category): """Update Roformer model dropdown based on selected category.""" choices = list(ROFORMER_MODELS.get(category, {}).keys()) or [] logger.debug(f"Updating roformer models for category {category}: {choices}") return gr.update(choices=choices, value=choices[0] if choices else None) def update_ensemble_models(category): """Update ensemble model dropdown based on selected category.""" choices = list(ROFORMER_MODELS.get(category, {}).keys()) or [] logger.debug(f"Updating ensemble models for category {category}: {choices}") return gr.update(choices=choices, value=[]) def download_audio_wrapper(url, cookie_file): file_path, status, audio_data = download_audio(url, cookie_file) return audio_data, status def create_interface(): with gr.Blocks(title="π΅ SESA Fast Separation π΅", css=CSS, elem_id="app-container") as app: gr.Markdown("