import os import torch import shutil import logging import gradio as gr from audio_separator.separator import Separator device = "cuda" if torch.cuda.is_available() else "cpu" use_autocast = device == "cuda" #=========================# # Roformer Models # #=========================# ROFORMER_MODELS = { 'BS-Roformer-De-Reverb': 'deverb_bs_roformer_8_384dim_10depth.ckpt', 'BS-Roformer-Viperx-1053': 'model_bs_roformer_ep_937_sdr_10.5309.ckpt', 'BS-Roformer-Viperx-1296': 'model_bs_roformer_ep_368_sdr_12.9628.ckpt', 'BS-Roformer-Viperx-1297': 'model_bs_roformer_ep_317_sdr_12.9755.ckpt', 'Mel-Roformer-Crowd-Aufr33-Viperx': 'mel_band_roformer_crowd_aufr33_viperx_sdr_8.7144.ckpt', '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', 'Mel-Roformer-Karaoke-Aufr33-Viperx': 'mel_band_roformer_karaoke_aufr33_viperx_sdr_10.1956.ckpt', 'Mel-Roformer-Viperx-1143': 'model_mel_band_roformer_ep_3005_sdr_11.4360.ckpt', 'MelBand Roformer Kim | Inst V1 by Unwa': 'melband_roformer_inst_v1.ckpt', 'MelBand Roformer Kim | Inst V2 by Unwa': 'melband_roformer_inst_v2.ckpt', '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', 'Vocals Mel Band Roformer': 'vocals_mel_band_roformer.ckpt', 'Mel Band Roformer Bleed Suppressor V1': 'mel_band_roformer_bleed_suppressor_v1.ckpt', 'Mel Band Roformer SYHFT V2': 'MelBandRoformerSYHFTV2.ckpt', 'Mel Band Roformer SYHFT V2.5': 'MelBandRoformerSYHFTV2.5.ckpt', } #=========================# # MDX23C Models # #=========================# MDX23C_MODELS = [ 'MDX23C-8KFFT-InstVoc_HQ.ckpt', 'MDX23C-8KFFT-InstVoc_HQ_2.ckpt', 'MDX23C_D1581.ckpt', ] #=========================# # MDXN-NET Models # #=========================# MDXNET_MODELS = [ 'UVR-MDX-NET-Crowd_HQ_1.onnx', 'UVR-MDX-NET-Inst_1.onnx', 'UVR-MDX-NET-Inst_2.onnx', 'UVR-MDX-NET-Inst_3.onnx', 'UVR-MDX-NET-Inst_HQ_1.onnx', 'UVR-MDX-NET-Inst_HQ_2.onnx', 'UVR-MDX-NET-Inst_HQ_3.onnx', 'UVR-MDX-NET-Inst_HQ_4.onnx', 'UVR-MDX-NET-Inst_HQ_5.onnx', 'UVR-MDX-NET-Inst_full_292.onnx', 'UVR-MDX-NET-Voc_FT.onnx', 'UVR-MDX-NET_Inst_82_beta.onnx', 'UVR-MDX-NET_Inst_90_beta.onnx', 'UVR-MDX-NET_Inst_187_beta.onnx', 'UVR-MDX-NET_Main_340.onnx', 'UVR-MDX-NET_Main_390.onnx', 'UVR-MDX-NET_Main_406.onnx', 'UVR-MDX-NET_Main_427.onnx', 'UVR-MDX-NET_Main_438.onnx', 'UVR_MDXNET_1_9703.onnx', 'UVR_MDXNET_2_9682.onnx', 'UVR_MDXNET_3_9662.onnx', 'UVR_MDXNET_9482.onnx', 'UVR_MDXNET_KARA.onnx', 'UVR_MDXNET_KARA_2.onnx', 'UVR_MDXNET_Main.onnx', 'kuielab_a_bass.onnx', 'kuielab_a_drums.onnx', 'kuielab_a_other.onnx', 'kuielab_a_vocals.onnx', 'kuielab_b_bass.onnx', 'kuielab_b_drums.onnx', 'kuielab_b_other.onnx', 'kuielab_b_vocals.onnx', 'Kim_Inst.onnx', 'Kim_Vocal_1.onnx', 'Kim_Vocal_2.onnx', 'Reverb_HQ_By_FoxJoy.onnx', ] #========================# # VR-ARCH Models # #========================# VR_ARCH_MODELS = [ '1_HP-UVR.pth', '2_HP-UVR.pth', '3_HP-Vocal-UVR.pth', '4_HP-Vocal-UVR.pth', '5_HP-Karaoke-UVR.pth', '6_HP-Karaoke-UVR.pth', '7_HP2-UVR.pth', '8_HP2-UVR.pth', '9_HP2-UVR.pth', '10_SP-UVR-2B-32000-1.pth', '11_SP-UVR-2B-32000-2.pth', '12_SP-UVR-3B-44100.pth', '13_SP-UVR-4B-44100-1.pth', '14_SP-UVR-4B-44100-2.pth', '15_SP-UVR-MID-44100-1.pth', '16_SP-UVR-MID-44100-2.pth', '17_HP-Wind_Inst-UVR.pth', 'MGM_HIGHEND_v4.pth', 'MGM_LOWEND_A_v4.pth', 'MGM_LOWEND_B_v4.pth', 'MGM_MAIN_v4.pth', 'UVR-BVE-4B_SN-44100-1.pth', 'UVR-DeEcho-DeReverb.pth', 'UVR-De-Echo-Aggressive.pth', 'UVR-De-Echo-Normal.pth', 'UVR-DeNoise-Lite.pth', 'UVR-DeNoise.pth', ] #=======================# # DEMUCS Models # #=======================# DEMUCS_MODELS = [ 'hdemucs_mmi.yaml', 'htdemucs.yaml', 'htdemucs_6s.yaml', 'htdemucs_ft.yaml', ] def print_message(input_file, model_name): """Prints information about the audio separation process.""" base_name = os.path.splitext(os.path.basename(input_file))[0] print("\n") print("🎵 Audio-Separator 🎵") print("Input audio:", base_name) print("Separation Model:", model_name) print("Audio Separation Process...") def prepare_output_dir(input_file, output_dir): """Create a directory for the output files and clean it if it already exists.""" base_name = os.path.splitext(os.path.basename(input_file))[0] out_dir = os.path.join(output_dir, base_name) try: if os.path.exists(out_dir): shutil.rmtree(out_dir) os.makedirs(out_dir) except Exception as e: raise RuntimeError(f"Failed to prepare output directory {out_dir}: {e}") return out_dir def roformer_separator(audio, model_key, seg_size, override_seg_size, overlap, pitch_shift, model_dir, out_dir, out_format, norm_thresh, amp_thresh, batch_size, progress=gr.Progress()): """Separate audio using Roformer model.""" base_name = os.path.splitext(os.path.basename(audio))[0] print_message(audio, model_key) model = ROFORMER_MODELS[model_key] try: out_dir = prepare_output_dir(audio, out_dir) separator = Separator( log_level=logging.WARNING, model_file_dir=model_dir, output_dir=out_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="Model loaded...") separator.load_model(model_filename=model) progress(0.7, desc="Audio separated...") separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") print(f"Separation complete!\nResults: {', '.join(separation)}") stems = [os.path.join(out_dir, file_name) for file_name in separation] return stems[1], stems[0] except Exception as e: raise RuntimeError(f"Roformer separation failed: {e}") from e def mdx23c_separator(audio, model, seg_size, override_seg_size, overlap, pitch_shift, model_dir, out_dir, out_format, norm_thresh, amp_thresh, batch_size, progress=gr.Progress(track_tqdm=True)): """Separate audio using MDX23C model.""" base_name = os.path.splitext(os.path.basename(audio))[0] print_message(audio, model) try: out_dir = prepare_output_dir(audio, out_dir) separator = Separator( log_level=logging.WARNING, model_file_dir=model_dir, output_dir=out_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="Model loaded...") separator.load_model(model_filename=model) progress(0.7, desc="Audio separated...") separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") print(f"Separation complete!\nResults: {', '.join(separation)}") stems = [os.path.join(out_dir, file_name) for file_name in separation] return stems[1], stems[0] except Exception as e: raise RuntimeError(f"MDX23C separation failed: {e}") from e def mdx_separator(audio, model, hop_length, seg_size, overlap, denoise, model_dir, out_dir, out_format, norm_thresh, amp_thresh, batch_size, progress=gr.Progress()): """Separate audio using MDX-NET model.""" base_name = os.path.splitext(os.path.basename(audio))[0] print_message(audio, model) try: out_dir = prepare_output_dir(audio, out_dir) separator = Separator( log_level=logging.WARNING, model_file_dir=model_dir, output_dir=out_dir, output_format=out_format, normalization_threshold=norm_thresh, amplification_threshold=amp_thresh, use_autocast=use_autocast, mdx_params={ "hop_length": hop_length, "segment_size": seg_size, "overlap": overlap, "batch_size": batch_size, "enable_denoise": denoise, } ) progress(0.2, desc="Model loaded...") separator.load_model(model_filename=model) progress(0.7, desc="Audio separated...") separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") print(f"Separation complete!\nResults: {', '.join(separation)}") stems = [os.path.join(out_dir, file_name) for file_name in separation] return stems[0], stems[1] except Exception as e: raise RuntimeError(f"MDX-NET separation failed: {e}") from e def vr_separator(audio, model, window_size, aggression, tta, post_process, post_process_threshold, high_end_process, model_dir, out_dir, out_format, norm_thresh, amp_thresh, batch_size, progress=gr.Progress()): """Separate audio using VR ARCH model.""" base_name = os.path.splitext(os.path.basename(audio))[0] print_message(audio, model) try: out_dir = prepare_output_dir(audio, out_dir) separator = Separator( log_level=logging.WARNING, model_file_dir=model_dir, output_dir=out_dir, output_format=out_format, normalization_threshold=norm_thresh, amplification_threshold=amp_thresh, use_autocast=use_autocast, vr_params={ "batch_size": batch_size, "window_size": window_size, "aggression": aggression, "enable_tta": tta, "enable_post_process": post_process, "post_process_threshold": post_process_threshold, "high_end_process": high_end_process, } ) progress(0.2, desc="Model loaded...") separator.load_model(model_filename=model) progress(0.7, desc="Audio separated...") separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") print(f"Separation complete!\nResults: {', '.join(separation)}") stems = [os.path.join(out_dir, file_name) for file_name in separation] return stems[0], stems[1] except Exception as e: raise RuntimeError(f"VR ARCH separation failed: {e}") from e def demucs_separator(audio, model, seg_size, shifts, overlap, segments_enabled, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress()): """Separate audio using Demucs model.""" print_message(audio, model) try: out_dir = prepare_output_dir(audio, out_dir) separator = Separator( log_level=logging.WARNING, model_file_dir=model_dir, output_dir=out_dir, output_format=out_format, normalization_threshold=norm_thresh, amplification_threshold=amp_thresh, use_autocast=use_autocast, demucs_params={ "segment_size": seg_size, "shifts": shifts, "overlap": overlap, "segments_enabled": segments_enabled, } ) progress(0.2, desc="Model loaded...") separator.load_model(model_filename=model) progress(0.7, desc="Audio separated...") separation = separator.separate(audio) print(f"Separation complete!\nResults: {', '.join(separation)}") stems = [os.path.join(out_dir, file_name) for file_name in separation] if model == "htdemucs_6s.yaml": return stems[0], stems[1], stems[2], stems[3], stems[4], stems[5] else: return stems[0], stems[1], stems[2], stems[3], None, None except Exception as e: raise RuntimeError(f"Demucs separation failed: {e}") from e def update_stems(model): if model == "htdemucs_6s.yaml": return gr.update(visible=True) else: return gr.update(visible=False) with gr.Blocks(title="🎵 Audio-Separator 🎵",theme=gr.themes.Base()) as app: gr.HTML("