Spaces:
Running
Running
import os | |
import torch | |
import logging | |
import yt_dlp | |
import spaces | |
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-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', | |
'BS-Roformer-De-Reverb': 'deverb_bs_roformer_8_384dim_10depth.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', | |
'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', | |
} | |
#=========================# | |
# MDX23C Models # | |
#=========================# | |
mdx23c_models = [ | |
'MDX23C_D1581.ckpt', | |
'MDX23C-8KFFT-InstVoc_HQ.ckpt', | |
'MDX23C-8KFFT-InstVoc_HQ_2.ckpt', | |
] | |
#=========================# | |
# MDXN-NET Models # | |
#=========================# | |
mdxnet_models = [ | |
'UVR-MDX-NET-Inst_full_292.onnx', | |
'UVR-MDX-NET_Inst_187_beta.onnx', | |
'UVR-MDX-NET_Inst_82_beta.onnx', | |
'UVR-MDX-NET_Inst_90_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-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_MDXNET_Main.onnx', | |
'UVR-MDX-NET-Inst_Main.onnx', | |
'UVR_MDXNET_1_9703.onnx', | |
'UVR_MDXNET_2_9682.onnx', | |
'UVR_MDXNET_3_9662.onnx', | |
'UVR-MDX-NET-Inst_1.onnx', | |
'UVR-MDX-NET-Inst_2.onnx', | |
'UVR-MDX-NET-Inst_3.onnx', | |
'UVR_MDXNET_KARA.onnx', | |
'UVR_MDXNET_KARA_2.onnx', | |
'UVR_MDXNET_9482.onnx', | |
'UVR-MDX-NET-Voc_FT.onnx', | |
'Kim_Vocal_1.onnx', | |
'Kim_Vocal_2.onnx', | |
'Kim_Inst.onnx', | |
'Reverb_HQ_By_FoxJoy.onnx', | |
'UVR-MDX-NET_Crowd_HQ_1.onnx', | |
'kuielab_a_vocals.onnx', | |
'kuielab_a_other.onnx', | |
'kuielab_a_bass.onnx', | |
'kuielab_a_drums.onnx', | |
'kuielab_b_vocals.onnx', | |
'kuielab_b_other.onnx', | |
'kuielab_b_bass.onnx', | |
'kuielab_b_drums.onnx', | |
] | |
#========================# | |
# VR-ARCH Models # | |
#========================# | |
vrarch_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', | |
'UVR-De-Echo-Aggressive.pth', | |
'UVR-De-Echo-Normal.pth', | |
'UVR-DeEcho-DeReverb.pth', | |
'UVR-DeNoise-Lite.pth', | |
'UVR-DeNoise.pth', | |
'UVR-BVE-4B_SN-44100-1.pth', | |
'MGM_HIGHEND_v4.pth', | |
'MGM_LOWEND_A_v4.pth', | |
'MGM_LOWEND_B_v4.pth', | |
'MGM_MAIN_v4.pth', | |
] | |
#=======================# | |
# DEMUCS Models # | |
#=======================# | |
demucs_models = [ | |
'htdemucs_ft.yaml', | |
'htdemucs_6s.yaml', | |
'htdemucs.yaml', | |
'hdemucs_mmi.yaml', | |
] | |
output_format = [ | |
'wav', | |
'flac', | |
'mp3', | |
'ogg', | |
'opus', | |
'm4a', | |
'aiff', | |
'ac3' | |
] | |
found_files = [] | |
logs = [] | |
out_dir = "./outputs" | |
models_dir = "./models" | |
extensions = (".wav", ".flac", ".mp3", ".ogg", ".opus", ".m4a", ".aiff", ".ac3") | |
def download_audio(url, output_dir="ytdl"): | |
os.makedirs(output_dir, exist_ok=True) | |
ydl_opts = { | |
'format': 'bestaudio/best', | |
'postprocessors': [{ | |
'key': 'FFmpegExtractAudio', | |
'preferredcodec': 'wav', | |
'preferredquality': '32', | |
}], | |
'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'), | |
'postprocessor_args': [ | |
'-acodec', 'pcm_f32le' | |
], | |
} | |
try: | |
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
info = ydl.extract_info(url, download=False) | |
video_title = info['title'] | |
ydl.download([url]) | |
file_path = os.path.join(output_dir, f"{video_title}.wav") | |
if os.path.exists(file_path): | |
return os.path.abspath(file_path) | |
else: | |
raise Exception("Something went wrong") | |
except Exception as e: | |
raise Exception(f"Error extracting audio with yt-dlp: {str(e)}") | |
def roformer_separator(audio, model_key, out_format, segment_size, override_seg_size, overlap, batch_size, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)): | |
base_name = os.path.splitext(os.path.basename(audio))[0] | |
roformer_model = roformer_models[model_key] | |
try: | |
separator = Separator( | |
log_level=logging.WARNING, | |
model_file_dir=models_dir, | |
output_dir=out_dir, | |
output_format=out_format, | |
use_autocast=use_autocast, | |
normalization_threshold=norm_thresh, | |
amplification_threshold=amp_thresh, | |
mdxc_params={ | |
"segment_size": segment_size, | |
"override_model_segment_size": override_seg_size, | |
"batch_size": batch_size, | |
"overlap": overlap, | |
} | |
) | |
progress(0.2, desc="Loading model...") | |
separator.load_model(model_filename=roformer_model) | |
progress(0.7, desc="Separating audio...") | |
separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") | |
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 mdxc_separator(audio, model, out_format, segment_size, override_seg_size, overlap, batch_size, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)): | |
base_name = os.path.splitext(os.path.basename(audio))[0] | |
try: | |
separator = Separator( | |
log_level=logging.WARNING, | |
model_file_dir=models_dir, | |
output_dir=out_dir, | |
output_format=out_format, | |
use_autocast=use_autocast, | |
normalization_threshold=norm_thresh, | |
amplification_threshold=amp_thresh, | |
mdxc_params={ | |
"segment_size": segment_size, | |
"override_model_segment_size": override_seg_size, | |
"batch_size": batch_size, | |
"overlap": overlap, | |
} | |
) | |
progress(0.2, desc="Loading model...") | |
separator.load_model(model_filename=model) | |
progress(0.7, desc="Separating audio...") | |
separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") | |
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 mdxnet_separator(audio, model, out_format, hop_length, segment_size, denoise, overlap, batch_size, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)): | |
base_name = os.path.splitext(os.path.basename(audio))[0] | |
try: | |
separator = Separator( | |
log_level=logging.WARNING, | |
model_file_dir=models_dir, | |
output_dir=out_dir, | |
output_format=out_format, | |
use_autocast=use_autocast, | |
normalization_threshold=norm_thresh, | |
amplification_threshold=amp_thresh, | |
mdx_params={ | |
"hop_length": hop_length, | |
"segment_size": segment_size, | |
"overlap": overlap, | |
"batch_size": batch_size, | |
"enable_denoise": denoise, | |
} | |
) | |
progress(0.2, desc="Loading model...") | |
separator.load_model(model_filename=model) | |
progress(0.7, desc="Separating audio...") | |
separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") | |
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 vrarch_separator(audio, model, out_format, window_size, aggression, tta, post_process, post_process_threshold, high_end_process, batch_size, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)): | |
base_name = os.path.splitext(os.path.basename(audio))[0] | |
try: | |
separator = Separator( | |
log_level=logging.WARNING, | |
model_file_dir=models_dir, | |
output_dir=out_dir, | |
output_format=out_format, | |
use_autocast=use_autocast, | |
normalization_threshold=norm_thresh, | |
amplification_threshold=amp_thresh, | |
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="Loading model...") | |
separator.load_model(model_filename=model) | |
progress(0.7, desc="Separating audio...") | |
separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") | |
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, out_format, shifts, segment_size, segments_enabled, overlap, batch_size, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)): | |
base_name = os.path.splitext(os.path.basename(audio))[0] | |
try: | |
separator = Separator( | |
log_level=logging.WARNING, | |
model_file_dir=models_dir, | |
output_dir=out_dir, | |
output_format=out_format, | |
use_autocast=use_autocast, | |
normalization_threshold=norm_thresh, | |
amplification_threshold=amp_thresh, | |
demucs_params={ | |
"batch_size": batch_size, | |
"segment_size": segment_size, | |
"shifts": shifts, | |
"overlap": overlap, | |
"segments_enabled": segments_enabled, | |
} | |
) | |
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(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) | |
def roformer_batch(path_input, path_output, model_key, out_format, segment_size, override_seg_size, overlap, batch_size, norm_thresh, amp_thresh): | |
found_files.clear() | |
logs.clear() | |
roformer_model = roformer_models[model_key] | |
for audio_files in os.listdir(path_input): | |
if audio_files.endswith(extensions): | |
found_files.append(audio_files) | |
total_files = len(found_files) | |
if total_files == 0: | |
logs.append("No valid audio files.") | |
yield "\n".join(logs) | |
else: | |
logs.append(f"{total_files} audio files found") | |
found_files.sort() | |
for audio_files in found_files: | |
file_path = os.path.join(path_input, audio_files) | |
base_name = os.path.splitext(os.path.basename(file_path))[0] | |
try: | |
separator = Separator( | |
log_level=logging.WARNING, | |
model_file_dir=models_dir, | |
output_dir=path_output, | |
output_format=out_format, | |
use_autocast=use_autocast, | |
normalization_threshold=norm_thresh, | |
amplification_threshold=amp_thresh, | |
mdxc_params={ | |
"segment_size": segment_size, | |
"override_model_segment_size": override_seg_size, | |
"batch_size": batch_size, | |
"overlap": overlap, | |
} | |
) | |
logs.append("Loading model...") | |
yield "\n".join(logs) | |
separator.load_model(model_filename=roformer_model) | |
logs.append(f"Separating file: {audio_files}") | |
yield "\n".join(logs) | |
separator.separate(file_path, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") | |
logs.append(f"File: {audio_files} separated!") | |
yield "\n".join(logs) | |
except Exception as e: | |
raise RuntimeError(f"Roformer batch separation failed: {e}") from e | |
def mdx23c_batch(path_input, path_output, model, out_format, segment_size, override_seg_size, overlap, batch_size, norm_thresh, amp_thresh): | |
found_files.clear() | |
logs.clear() | |
for audio_files in os.listdir(path_input): | |
if audio_files.endswith(extensions): | |
found_files.append(audio_files) | |
total_files = len(found_files) | |
if total_files == 0: | |
logs.append("No valid audio files.") | |
yield "\n".join(logs) | |
else: | |
logs.append(f"{total_files} audio files found") | |
found_files.sort() | |
for audio_files in found_files: | |
file_path = os.path.join(path_input, audio_files) | |
base_name = os.path.splitext(os.path.basename(file_path))[0] | |
try: | |
separator = Separator( | |
log_level=logging.WARNING, | |
model_file_dir=models_dir, | |
output_dir=path_output, | |
output_format=out_format, | |
use_autocast=use_autocast, | |
normalization_threshold=norm_thresh, | |
amplification_threshold=amp_thresh, | |
mdxc_params={ | |
"segment_size": segment_size, | |
"override_model_segment_size": override_seg_size, | |
"batch_size": batch_size, | |
"overlap": overlap, | |
} | |
) | |
logs.append("Loading model...") | |
yield "\n".join(logs) | |
separator.load_model(model_filename=model) | |
logs.append(f"Separating file: {audio_files}") | |
yield "\n".join(logs) | |
separator.separate(file_path, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") | |
logs.append(f"File: {audio_files} separated!") | |
yield "\n".join(logs) | |
except Exception as e: | |
raise RuntimeError(f"Roformer batch separation failed: {e}") from e | |
def mdxnet_batch(path_input, path_output, model, out_format, hop_length, segment_size, denoise, overlap, batch_size, norm_thresh, amp_thresh): | |
found_files.clear() | |
logs.clear() | |
for audio_files in os.listdir(path_input): | |
if audio_files.endswith(extensions): | |
found_files.append(audio_files) | |
total_files = len(found_files) | |
if total_files == 0: | |
logs.append("No valid audio files.") | |
yield "\n".join(logs) | |
else: | |
logs.append(f"{total_files} audio files found") | |
found_files.sort() | |
for audio_files in found_files: | |
file_path = os.path.join(path_input, audio_files) | |
base_name = os.path.splitext(os.path.basename(file_path))[0] | |
try: | |
separator = Separator( | |
log_level=logging.WARNING, | |
model_file_dir=models_dir, | |
output_dir=path_output, | |
output_format=out_format, | |
use_autocast=use_autocast, | |
normalization_threshold=norm_thresh, | |
amplification_threshold=amp_thresh, | |
mdx_params={ | |
"hop_length": hop_length, | |
"segment_size": segment_size, | |
"overlap": overlap, | |
"batch_size": batch_size, | |
"enable_denoise": denoise, | |
} | |
) | |
logs.append("Loading model...") | |
yield "\n".join(logs) | |
separator.load_model(model_filename=model) | |
logs.append(f"Separating file: {audio_files}") | |
yield "\n".join(logs) | |
separator.separate(file_path, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") | |
logs.append(f"File: {audio_files} separated!") | |
yield "\n".join(logs) | |
except Exception as e: | |
raise RuntimeError(f"Roformer batch separation failed: {e}") from e | |
def vrarch_batch(path_input, path_output, model, out_format, window_size, aggression, tta, post_process, post_process_threshold, high_end_process, batch_size, norm_thresh, amp_thresh): | |
found_files.clear() | |
logs.clear() | |
for audio_files in os.listdir(path_input): | |
if audio_files.endswith(extensions): | |
found_files.append(audio_files) | |
total_files = len(found_files) | |
if total_files == 0: | |
logs.append("No valid audio files.") | |
yield "\n".join(logs) | |
else: | |
logs.append(f"{total_files} audio files found") | |
found_files.sort() | |
for audio_files in found_files: | |
file_path = os.path.join(path_input, audio_files) | |
base_name = os.path.splitext(os.path.basename(file_path))[0] | |
try: | |
separator = Separator( | |
log_level=logging.WARNING, | |
model_file_dir=models_dir, | |
output_dir=path_output, | |
output_format=out_format, | |
use_autocast=use_autocast, | |
normalization_threshold=norm_thresh, | |
amplification_threshold=amp_thresh, | |
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, | |
} | |
) | |
logs.append("Loading model...") | |
yield "\n".join(logs) | |
separator.load_model(model_filename=model) | |
logs.append(f"Separating file: {audio_files}") | |
yield "\n".join(logs) | |
separator.separate(file_path, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)") | |
logs.append(f"File: {audio_files} separated!") | |
yield "\n".join(logs) | |
except Exception as e: | |
raise RuntimeError(f"Roformer batch separation failed: {e}") from e | |
def demucs_batch(path_input, path_output, model, out_format, shifts, segment_size, segments_enabled, overlap, batch_size, norm_thresh, amp_thresh): | |
found_files.clear() | |
logs.clear() | |
for audio_files in os.listdir(path_input): | |
if audio_files.endswith(extensions): | |
found_files.append(audio_files) | |
total_files = len(found_files) | |
if total_files == 0: | |
logs.append("No valid audio files.") | |
yield "\n".join(logs) | |
else: | |
logs.append(f"{total_files} audio files found") | |
found_files.sort() | |
for audio_files in found_files: | |
file_path = os.path.join(path_input, audio_files) | |
try: | |
separator = Separator( | |
log_level=logging.WARNING, | |
model_file_dir=models_dir, | |
output_dir=path_output, | |
output_format=out_format, | |
use_autocast=use_autocast, | |
normalization_threshold=norm_thresh, | |
amplification_threshold=amp_thresh, | |
demucs_params={ | |
"batch_size": batch_size, | |
"segment_size": segment_size, | |
"shifts": shifts, | |
"overlap": overlap, | |
"segments_enabled": segments_enabled, | |
} | |
) | |
logs.append("Loading model...") | |
yield "\n".join(logs) | |
separator.load_model(model_filename=model) | |
logs.append(f"Separating file: {audio_files}") | |
yield "\n".join(logs) | |
separator.separate(file_path) | |
logs.append(f"File: {audio_files} separated!") | |
yield "\n".join(logs) | |
except Exception as e: | |
raise RuntimeError(f"Roformer batch separation failed: {e}") from e | |