Spaces:
Sleeping
Sleeping
Upload 7 files
Browse filesFiles changed (7)
- README.md+6-5
- app.pyAdded+805
- mdx_models/data.jsonAdded+354
- packages.txtAdded+1
- requirements.txtAdded+3
- test.mp3Added
- utils.pyAdded+142
12345678910111213---title: Audio Separatoremoji: 🌍colorFrom: purplecolorTo: redsdk: gradiosdk_version: 4.28.3app_file: app.pypinned: falselicense: mit---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference1234567891011121314---title: Vocal-Instrumental Audio Separatoremoji: 🏃colorFrom: purplecolorTo: pinksdk: gradiosdk_version: 4.28.3app_file: app.pypinned: truelicense: mitshort_description: Vocal and background audio separator---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805import os# os.system("pip install ./ort_nightly_gpu-1.17.0.dev20240118002-cp310-cp310-manylinux_2_28_x86_64.whl")os.system("pip install ort-nightly-gpu --index-url=https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ort-cuda-12-nightly/pypi/simple/")import gcimport hashlibimport queueimport threadingimport jsonimport shleximport sysimport subprocessimport librosaimport numpy as npimport soundfile as sfimport torchfrom tqdm import tqdmfrom utils import ( remove_directory_contents, create_directories, download_manager,)import randomimport spacesfrom utils import loggerimport onnxruntime as ortimport warningsimport spacesimport gradio as grimport loggingimport time
warnings.filterwarnings("ignore")
title = "<center><strong><font size='7'>Audio🔹separator</font></strong></center>"description = "This demo uses the MDX-Net models for vocal and background sound separation."theme = "NoCrypt/miku"
stem_naming = { "Vocals": "Instrumental", "Other": "Instruments", "Instrumental": "Vocals", "Drums": "Drumless", "Bass": "Bassless",}
class MDXModel: def __init__( self, device, dim_f, dim_t, n_fft, hop=1024, stem_name=None, compensation=1.000, ): self.dim_f = dim_f self.dim_t = dim_t self.dim_c = 4 self.n_fft = n_fft self.hop = hop self.stem_name = stem_name self.compensation = compensation
self.n_bins = self.n_fft // 2 + 1 self.chunk_size = hop * (self.dim_t - 1) self.window = torch.hann_window( window_length=self.n_fft, periodic=True ).to(device)
out_c = self.dim_c
self.freq_pad = torch.zeros( [1, out_c, self.n_bins - self.dim_f, self.dim_t] ).to(device)
def stft(self, x): x = x.reshape([-1, self.chunk_size]) x = torch.stft( x, n_fft=self.n_fft, hop_length=self.hop, window=self.window, center=True, return_complex=True, ) x = torch.view_as_real(x) x = x.permute([0, 3, 1, 2]) x = x.reshape([-1, 2, 2, self.n_bins, self.dim_t]).reshape( [-1, 4, self.n_bins, self.dim_t] ) return x[:, :, : self.dim_f]
def istft(self, x, freq_pad=None): freq_pad = ( self.freq_pad.repeat([x.shape[0], 1, 1, 1]) if freq_pad is None else freq_pad ) x = torch.cat([x, freq_pad], -2) # c = 4*2 if self.target_name=='*' else 2 x = x.reshape([-1, 2, 2, self.n_bins, self.dim_t]).reshape( [-1, 2, self.n_bins, self.dim_t] ) x = x.permute([0, 2, 3, 1]) x = x.contiguous() x = torch.view_as_complex(x) x = torch.istft( x, n_fft=self.n_fft, hop_length=self.hop, window=self.window, center=True, ) return x.reshape([-1, 2, self.chunk_size])
class MDX: DEFAULT_SR = 44100 # Unit: seconds DEFAULT_CHUNK_SIZE = 0 * DEFAULT_SR DEFAULT_MARGIN_SIZE = 1 * DEFAULT_SR
def __init__( self, model_path: str, params: MDXModel, processor=0 ): # Set the device and the provider (CPU or CUDA) self.device = ( torch.device(f"cuda:{processor}") if processor >= 0 else torch.device("cpu") ) self.provider = ( ["CUDAExecutionProvider"] if processor >= 0 else ["CPUExecutionProvider"] )
self.model = params
# Load the ONNX model using ONNX Runtime self.ort = ort.InferenceSession(model_path, providers=self.provider) # Preload the model for faster performance self.ort.run( None, {"input": torch.rand(1, 4, params.dim_f, params.dim_t).numpy()}, ) self.process = lambda spec: self.ort.run( None, {"input": spec.cpu().numpy()} )[0]
self.prog = None
@staticmethod def get_hash(model_path): try: with open(model_path, "rb") as f: f.seek(-10000 * 1024, 2) model_hash = hashlib.md5(f.read()).hexdigest() except: # noqa model_hash = hashlib.md5(open(model_path, "rb").read()).hexdigest()
return model_hash
@staticmethod def segment( wave, combine=True, chunk_size=DEFAULT_CHUNK_SIZE, margin_size=DEFAULT_MARGIN_SIZE, ): """ Segment or join segmented wave array
Args: wave: (np.array) Wave array to be segmented or joined combine: (bool) If True, combines segmented wave array. If False, segments wave array. chunk_size: (int) Size of each segment (in samples) margin_size: (int) Size of margin between segments (in samples)
Returns: numpy array: Segmented or joined wave array """
if combine: # Initializing as None instead of [] for later numpy array concatenation processed_wave = None for segment_count, segment in enumerate(wave): start = 0 if segment_count == 0 else margin_size end = None if segment_count == len(wave) - 1 else -margin_size if margin_size == 0: end = None if processed_wave is None: # Create array for first segment processed_wave = segment[:, start:end] else: # Concatenate to existing array for subsequent segments processed_wave = np.concatenate( (processed_wave, segment[:, start:end]), axis=-1 )
else: processed_wave = [] sample_count = wave.shape[-1]
if chunk_size <= 0 or chunk_size > sample_count: chunk_size = sample_count
if margin_size > chunk_size: margin_size = chunk_size
for segment_count, skip in enumerate( range(0, sample_count, chunk_size) ): margin = 0 if segment_count == 0 else margin_size end = min(skip + chunk_size + margin_size, sample_count) start = skip - margin
cut = wave[:, start:end].copy() processed_wave.append(cut)
if end == sample_count: break
return processed_wave
def pad_wave(self, wave): """ Pad the wave array to match the required chunk size
Args: wave: (np.array) Wave array to be padded
Returns: tuple: (padded_wave, pad, trim) - padded_wave: Padded wave array - pad: Number of samples that were padded - trim: Number of samples that were trimmed """ n_sample = wave.shape[1] trim = self.model.n_fft // 2 gen_size = self.model.chunk_size - 2 * trim pad = gen_size - n_sample % gen_size
# Padded wave wave_p = np.concatenate( ( np.zeros((2, trim)), wave, np.zeros((2, pad)), np.zeros((2, trim)), ), 1, )
mix_waves = [] for i in range(0, n_sample + pad, gen_size): waves = np.array(wave_p[:, i:i + self.model.chunk_size]) mix_waves.append(waves)
mix_waves = torch.tensor(mix_waves, dtype=torch.float32).to( self.device )
return mix_waves, pad, trim
def _process_wave(self, mix_waves, trim, pad, q: queue.Queue, _id: int): """ Process each wave segment in a multi-threaded environment
Args: mix_waves: (torch.Tensor) Wave segments to be processed trim: (int) Number of samples trimmed during padding pad: (int) Number of samples padded during padding q: (queue.Queue) Queue to hold the processed wave segments _id: (int) Identifier of the processed wave segment
Returns: numpy array: Processed wave segment """ mix_waves = mix_waves.split(1) with torch.no_grad(): pw = [] for mix_wave in mix_waves: self.prog.update() spec = self.model.stft(mix_wave) processed_spec = torch.tensor(self.process(spec)) processed_wav = self.model.istft( processed_spec.to(self.device) ) processed_wav = ( processed_wav[:, :, trim:-trim] .transpose(0, 1) .reshape(2, -1) .cpu() .numpy() ) pw.append(processed_wav) processed_signal = np.concatenate(pw, axis=-1)[:, :-pad] q.put({_id: processed_signal}) return processed_signal
def process_wave(self, wave: np.array, mt_threads=1): """ Process the wave array in a multi-threaded environment
Args: wave: (np.array) Wave array to be processed mt_threads: (int) Number of threads to be used for processing
Returns: numpy array: Processed wave array """ self.prog = tqdm(total=0) chunk = wave.shape[-1] // mt_threads waves = self.segment(wave, False, chunk)
# Create a queue to hold the processed wave segments q = queue.Queue() threads = [] for c, batch in enumerate(waves): mix_waves, pad, trim = self.pad_wave(batch) self.prog.total = len(mix_waves) * mt_threads thread = threading.Thread( target=self._process_wave, args=(mix_waves, trim, pad, q, c) ) thread.start() threads.append(thread) for thread in threads: thread.join() self.prog.close()
processed_batches = [] while not q.empty(): processed_batches.append(q.get()) processed_batches = [ list(wave.values())[0] for wave in sorted( processed_batches, key=lambda d: list(d.keys())[0] ) ] assert len(processed_batches) == len( waves ), "Incomplete processed batches, please reduce batch size!" return self.segment(processed_batches, True, chunk)
@spaces.GPU()def run_mdx( model_params, output_dir, model_path, filename, exclude_main=False, exclude_inversion=False, suffix=None, invert_suffix=None, denoise=False, keep_orig=True, m_threads=2, device_base="cuda",): if device_base == "cuda": device = torch.device("cuda:0") processor_num = 0 device_properties = torch.cuda.get_device_properties(device) vram_gb = device_properties.total_memory / 1024**3 m_threads = 1 if vram_gb < 8 else (8 if vram_gb > 32 else 2) logger.info(f"threads: {m_threads} vram: {vram_gb}") else: device = torch.device("cpu") processor_num = -1 m_threads = 1
model_hash = MDX.get_hash(model_path) mp = model_params.get(model_hash) model = MDXModel( device, dim_f=mp["mdx_dim_f_set"], dim_t=2 ** mp["mdx_dim_t_set"], n_fft=mp["mdx_n_fft_scale_set"], stem_name=mp["primary_stem"], compensation=mp["compensate"], )
mdx_sess = MDX(model_path, model, processor=processor_num) wave, sr = librosa.load(filename, mono=False, sr=44100) # normalizing input wave gives better output peak = max(np.max(wave), abs(np.min(wave))) wave /= peak if denoise: wave_processed = -(mdx_sess.process_wave(-wave, m_threads)) + ( mdx_sess.process_wave(wave, m_threads) ) wave_processed *= 0.5 else: wave_processed = mdx_sess.process_wave(wave, m_threads) # return to previous peak wave_processed *= peak stem_name = model.stem_name if suffix is None else suffix
main_filepath = None if not exclude_main: main_filepath = os.path.join( output_dir, f"{os.path.basename(os.path.splitext(filename)[0])}_{stem_name}.wav", ) sf.write(main_filepath, wave_processed.T, sr)
invert_filepath = None if not exclude_inversion: diff_stem_name = ( stem_naming.get(stem_name) if invert_suffix is None else invert_suffix ) stem_name = ( f"{stem_name}_diff" if diff_stem_name is None else diff_stem_name ) invert_filepath = os.path.join( output_dir, f"{os.path.basename(os.path.splitext(filename)[0])}_{stem_name}.wav", ) sf.write( invert_filepath, (-wave_processed.T * model.compensation) + wave.T, sr, )
if not keep_orig: os.remove(filename)
del mdx_sess, wave_processed, wave gc.collect() torch.cuda.empty_cache() return main_filepath, invert_filepath
MDX_DOWNLOAD_LINK = "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/"UVR_MODELS = [ "UVR-MDX-NET-Voc_FT.onnx", "UVR_MDXNET_KARA_2.onnx", "Reverb_HQ_By_FoxJoy.onnx", "UVR-MDX-NET-Inst_HQ_4.onnx",]BASE_DIR = "." # os.path.dirname(os.path.dirname(os.path.abspath(__file__)))mdxnet_models_dir = os.path.join(BASE_DIR, "mdx_models")output_dir = os.path.join(BASE_DIR, "clean_song_output")
def convert_to_stereo_and_wav(audio_path): wave, sr = librosa.load(audio_path, mono=False, sr=44100)
# check if mono if type(wave[0]) != np.ndarray or audio_path[-4:].lower() != ".wav": # noqa stereo_path = f"{os.path.splitext(audio_path)[0]}_stereo.wav" stereo_path = os.path.join(output_dir, stereo_path)
command = shlex.split( f'ffmpeg -y -loglevel error -i "{audio_path}" -ac 2 -f wav "{stereo_path}"' ) sub_params = { "stdout": subprocess.PIPE, "stderr": subprocess.PIPE, "creationflags": subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0, } process_wav = subprocess.Popen(command, **sub_params) output, errors = process_wav.communicate() if process_wav.returncode != 0 or not os.path.exists(stereo_path): raise Exception("Error processing audio to stereo wav")
return stereo_path else: return audio_path
def get_hash(filepath): with open(filepath, 'rb') as f: file_hash = hashlib.blake2b() while chunk := f.read(8192): file_hash.update(chunk)
return file_hash.hexdigest()[:18]
def random_sleep(): sleep_time = round(random.uniform(5.2, 7.9), 1) time.sleep(sleep_time)
def process_uvr_task( orig_song_path: str = "aud_test.mp3", main_vocals: bool = False, dereverb: bool = True, song_id: str = "mdx", # folder output name only_voiceless: bool = False, remove_files_output_dir: bool = False,):
device_base = "cuda" if torch.cuda.is_available() else "cpu" logger.info(f"Device: {device_base}")
if remove_files_output_dir: remove_directory_contents(output_dir)
with open(os.path.join(mdxnet_models_dir, "data.json")) as infile: mdx_model_params = json.load(infile)
song_output_dir = os.path.join(output_dir, song_id) create_directories(song_output_dir) orig_song_path = convert_to_stereo_and_wav(orig_song_path)
logger.info(f"onnxruntime device >> {ort.get_device()}")
if only_voiceless: logger.info("Voiceless Track Separation...") return run_mdx( mdx_model_params, song_output_dir, os.path.join(mdxnet_models_dir, "UVR-MDX-NET-Inst_HQ_4.onnx"), orig_song_path, suffix="Voiceless", denoise=False, keep_orig=True, exclude_inversion=True, device_base=device_base, )
logger.info("Vocal Track Isolation...") vocals_path, instrumentals_path = run_mdx( mdx_model_params, song_output_dir, os.path.join(mdxnet_models_dir, "UVR-MDX-NET-Voc_FT.onnx"), orig_song_path, denoise=True, keep_orig=True, device_base=device_base, )
if main_vocals: random_sleep() msg_main = "Main Voice Separation from Supporting Vocals..." logger.info(msg_main) gr.Info(msg_main) try: backup_vocals_path, main_vocals_path = run_mdx( mdx_model_params, song_output_dir, os.path.join(mdxnet_models_dir, "UVR_MDXNET_KARA_2.onnx"), vocals_path, suffix="Backup", invert_suffix="Main", denoise=True, device_base=device_base, ) except Exception as e: if "0:00:" in str(e): gr.Info("Waiting 60 seconds for GPU quota") time.sleep(56) random_sleep() backup_vocals_path, main_vocals_path = run_mdx( mdx_model_params, song_output_dir, os.path.join(mdxnet_models_dir, "UVR_MDXNET_KARA_2.onnx"), vocals_path, suffix="Backup", invert_suffix="Main", denoise=True, device_base=device_base, ) else: raise e else: backup_vocals_path, main_vocals_path = None, vocals_path
if dereverb: random_sleep() msg_dereverb = "Vocal Clarity Enhancement through De-Reverberation..." logger.info(msg_dereverb) gr.Info(msg_dereverb) try: _, vocals_dereverb_path = run_mdx( mdx_model_params, song_output_dir, os.path.join(mdxnet_models_dir, "Reverb_HQ_By_FoxJoy.onnx"), main_vocals_path, invert_suffix="DeReverb", exclude_main=True, denoise=True, device_base=device_base, ) except Exception as e: if "0:00:" in str(e): gr.Info("Waiting 60 seconds for GPU quota") time.sleep(56) random_sleep() _, vocals_dereverb_path = run_mdx( mdx_model_params, song_output_dir, os.path.join(mdxnet_models_dir, "Reverb_HQ_By_FoxJoy.onnx"), main_vocals_path, invert_suffix="DeReverb", exclude_main=True, denoise=True, device_base=device_base, ) else: raise e else: vocals_dereverb_path = main_vocals_path
return ( vocals_path, instrumentals_path, backup_vocals_path, main_vocals_path, vocals_dereverb_path, )
def sound_separate(media_file, stem, main, dereverb):
if not media_file: raise ValueError("The audio pls")
if not stem: raise ValueError("Select vocal or background...")
hash_audio = str(get_hash(media_file))
outputs = []
start_time = time.time() if stem == "vocal": try: _, _, _, _, vocal_audio = process_uvr_task( orig_song_path=media_file, song_id=hash_audio+"mdx", main_vocals=main, dereverb=dereverb, remove_files_output_dir=False, ) outputs.append(vocal_audio) except Exception as error: gr.Info(str(error)) logger.error(str(error))
if stem == "background":
background_audio, _ = process_uvr_task( orig_song_path=media_file, song_id=hash_audio+"voiceless", only_voiceless=True, remove_files_output_dir=False, ) # copy_files(background_audio, ".") outputs.append(background_audio)
end_time = time.time() execution_time = end_time - start_time logger.info(f"Execution time: {execution_time} seconds") if not outputs: raise Exception("Error in sound separate")
return outputs
def audio_conf(): return gr.File( label="Audio file", # file_count="multiple", type="filepath", container=True, )
def stem_conf(): return gr.Radio( choices=["vocal", "background"], value="vocal", label="Vocal", # info="", )
def main_conf(): return gr.Checkbox( False, label="Main", # info="", )
def dereverb_conf(): return gr.Checkbox( False, label="Dereverb", # info="", visible=True, )
def button_conf(): return gr.Button( "Inference", variant="primary", )
def output_conf(): return gr.File( label="Result", file_count="multiple", interactive=False, )
def show_vocal_components(input_bool): param = True if input_bool == "vocal" else False return gr.update(visible=param), gr.update( visible=param )
def get_gui(theme): with gr.Blocks(theme=theme) as app: gr.Markdown(title) gr.Markdown(description)
aud = audio_conf() with gr.Column(): with gr.Row(): stem_gui = stem_conf()
with gr.Column(): with gr.Row(): main_gui = main_conf() dereverb_gui = dereverb_conf()
stem_gui.change( show_vocal_components, [stem_gui], [main_gui, dereverb_gui], ) button_base = button_conf() output_base = output_conf()
button_base.click( sound_separate, inputs=[ aud, stem_gui, main_gui, dereverb_gui, ], outputs=[output_base], )
gr.Examples( examples=[ [ "./test.mp3", "vocal", False, False, ], ], fn=sound_separate, inputs=[ aud, stem_gui, main_gui, dereverb_gui, ], outputs=[output_base], cache_examples=False, )
return app
if __name__ == "__main__":
for id_model in UVR_MODELS: download_manager( os.path.join(MDX_DOWNLOAD_LINK, id_model), mdxnet_models_dir )
app = get_gui(theme)
app.queue(default_concurrency_limit=40)
app.launch( max_threads=40, share=False, show_error=True, quiet=False, debug=False, ) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354{ "0ddfc0eb5792638ad5dc27850236c246": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Vocals" }, "26d308f91f3423a67dc69a6d12a8793d": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 9, "mdx_n_fft_scale_set": 8192, "primary_stem": "Other" }, "2cdd429caac38f0194b133884160f2c6": { "compensate": 1.045, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Instrumental" }, "2f5501189a2f6db6349916fabe8c90de": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Vocals" }, "398580b6d5d973af3120df54cee6759d": { "compensate": 1.75, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Vocals" }, "488b3e6f8bd3717d9d7c428476be2d75": { "compensate": 1.035, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Instrumental" }, "4910e7827f335048bdac11fa967772f9": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 7, "mdx_n_fft_scale_set": 4096, "primary_stem": "Drums" }, "53c4baf4d12c3e6c3831bb8f5b532b93": { "compensate": 1.043, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Vocals" }, "5d343409ef0df48c7d78cce9f0106781": { "compensate": 1.075, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Vocals" }, "5f6483271e1efb9bfb59e4a3e6d4d098": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 9, "mdx_n_fft_scale_set": 6144, "primary_stem": "Vocals" }, "65ab5919372a128e4167f5e01a8fda85": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 8192, "primary_stem": "Other" }, "6703e39f36f18aa7855ee1047765621d": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 9, "mdx_n_fft_scale_set": 16384, "primary_stem": "Bass" }, "6b31de20e84392859a3d09d43f089515": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Vocals" }, "867595e9de46f6ab699008295df62798": { "compensate": 1.03, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Vocals" }, "a3cd63058945e777505c01d2507daf37": { "compensate": 1.03, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Vocals" }, "b33d9b3950b6cbf5fe90a32608924700": { "compensate": 1.03, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Vocals" }, "c3b29bdce8c4fa17ec609e16220330ab": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 16384, "primary_stem": "Bass" }, "ceed671467c1f64ebdfac8a2490d0d52": { "compensate": 1.035, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Instrumental" }, "d2a1376f310e4f7fa37fb9b5774eb701": { "compensate": 1.035, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Instrumental" }, "d7bff498db9324db933d913388cba6be": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Vocals" }, "d94058f8c7f1fae4164868ae8ae66b20": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Vocals" }, "dc41ede5961d50f277eb846db17f5319": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 9, "mdx_n_fft_scale_set": 4096, "primary_stem": "Drums" }, "e5572e58abf111f80d8241d2e44e7fa4": { "compensate": 1.028, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Instrumental" }, "e7324c873b1f615c35c1967f912db92a": { "compensate": 1.03, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Vocals" }, "1c56ec0224f1d559c42fd6fd2a67b154": { "compensate": 1.025, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 5120, "primary_stem": "Instrumental" }, "f2df6d6863d8f435436d8b561594ff49": { "compensate": 1.035, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Instrumental" }, "b06327a00d5e5fbc7d96e1781bbdb596": { "compensate": 1.035, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Instrumental" }, "94ff780b977d3ca07c7a343dab2e25dd": { "compensate": 1.039, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Instrumental" }, "73492b58195c3b52d34590d5474452f6": { "compensate": 1.043, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Vocals" }, "970b3f9492014d18fefeedfe4773cb42": { "compensate": 1.009, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Vocals" }, "1d64a6d2c30f709b8c9b4ce1366d96ee": { "compensate": 1.035, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 5120, "primary_stem": "Instrumental" }, "203f2a3955221b64df85a41af87cf8f0": { "compensate": 1.035, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Instrumental" }, "291c2049608edb52648b96e27eb80e95": { "compensate": 1.035, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Instrumental" }, "ead8d05dab12ec571d67549b3aab03fc": { "compensate": 1.035, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Instrumental" }, "cc63408db3d80b4d85b0287d1d7c9632": { "compensate": 1.033, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Instrumental" }, "cd5b2989ad863f116c855db1dfe24e39": { "compensate": 1.035, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 9, "mdx_n_fft_scale_set": 6144, "primary_stem": "Other" }, "55657dd70583b0fedfba5f67df11d711": { "compensate": 1.022, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 6144, "primary_stem": "Instrumental" }, "b6bccda408a436db8500083ef3491e8b": { "compensate": 1.02, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Instrumental" }, "8a88db95c7fb5dbe6a095ff2ffb428b1": { "compensate": 1.026, "mdx_dim_f_set": 2048, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 5120, "primary_stem": "Instrumental" }, "b78da4afc6512f98e4756f5977f5c6b9": { "compensate": 1.021, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Instrumental" }, "77d07b2667ddf05b9e3175941b4454a0": { "compensate": 1.021, "mdx_dim_f_set": 3072, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 7680, "primary_stem": "Vocals" }, "0f2a6bc5b49d87d64728ee40e23bceb1": { "compensate": 1.019, "mdx_dim_f_set": 2560, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 5120, "primary_stem": "Instrumental" }, "b02be2d198d4968a121030cf8950b492": { "compensate": 1.020, "mdx_dim_f_set": 2560, "mdx_dim_t_set": 8, "mdx_n_fft_scale_set": 5120, "primary_stem": "No Crowd" }, "2154254ee89b2945b97a7efed6e88820": { "config_yaml": "model_2_stem_061321.yaml" }, "063aadd735d58150722926dcbf5852a9": { "config_yaml": "model_2_stem_061321.yaml" }, "fe96801369f6a148df2720f5ced88c19": { "config_yaml": "model3.yaml" }, "02e8b226f85fb566e5db894b9931c640": { "config_yaml": "model2.yaml" }, "e3de6d861635ab9c1d766149edd680d6": { "config_yaml": "model1.yaml" }, "3f2936c554ab73ce2e396d54636bd373": { "config_yaml": "modelB.yaml" }, "890d0f6f82d7574bca741a9e8bcb8168": { "config_yaml": "modelB.yaml" }, "63a3cb8c37c474681049be4ad1ba8815": { "config_yaml": "modelB.yaml" }, "a7fc5d719743c7fd6b61bd2b4d48b9f0": { "config_yaml": "modelA.yaml" }, "3567f3dee6e77bf366fcb1c7b8bc3745": { "config_yaml": "modelA.yaml" }, "a28f4d717bd0d34cd2ff7a3b0a3d065e": { "config_yaml": "modelA.yaml" }, "c9971a18da20911822593dc81caa8be9": { "config_yaml": "sndfx.yaml" }, "57d94d5ed705460d21c75a5ac829a605": { "config_yaml": "sndfx.yaml" }, "e7a25f8764f25a52c1b96c4946e66ba2": { "config_yaml": "sndfx.yaml" }, "104081d24e37217086ce5fde09147ee1": { "config_yaml": "model_2_stem_061321.yaml" }, "1e6165b601539f38d0a9330f3facffeb": { "config_yaml": "model_2_stem_061321.yaml" }, "fe0108464ce0d8271be5ab810891bd7c": { "config_yaml": "model_2_stem_full_band.yaml" }}1ffmpeg123soundfilelibrosatorch==2.2.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142import os, zipfile, shutil, subprocess, shlex, sys # noqafrom urllib.parse import urlparseimport reimport logging
def load_file_from_url( url: str, model_dir: str, file_name: str | None = None, overwrite: bool = False, progress: bool = True,) -> str: """Download a file from `url` into `model_dir`, using the file present if possible.
Returns the path to the downloaded file. """ os.makedirs(model_dir, exist_ok=True) if not file_name: parts = urlparse(url) file_name = os.path.basename(parts.path) cached_file = os.path.abspath(os.path.join(model_dir, file_name))
# Overwrite if os.path.exists(cached_file): if overwrite or os.path.getsize(cached_file) == 0: remove_files(cached_file)
# Download if not os.path.exists(cached_file): logger.info(f'Downloading: "{url}" to {cached_file}\n') from torch.hub import download_url_to_file
download_url_to_file(url, cached_file, progress=progress) else: logger.debug(cached_file)
return cached_file
def friendly_name(file: str): if file.startswith("http"): file = urlparse(file).path
file = os.path.basename(file) model_name, extension = os.path.splitext(file) return model_name, extension
def download_manager( url: str, path: str, extension: str = "", overwrite: bool = False, progress: bool = True,): url = url.strip()
name, ext = friendly_name(url) name += ext if not extension else f".{extension}"
if url.startswith("http"): filename = load_file_from_url( url=url, model_dir=path, file_name=name, overwrite=overwrite, progress=progress, ) else: filename = path
return filename
def remove_files(file_list): if isinstance(file_list, str): file_list = [file_list]
for file in file_list: if os.path.exists(file): os.remove(file)
def remove_directory_contents(directory_path): """ Removes all files and subdirectories within a directory.
Parameters: directory_path (str): Path to the directory whose contents need to be removed. """ if os.path.exists(directory_path): for filename in os.listdir(directory_path): file_path = os.path.join(directory_path, filename) try: if os.path.isfile(file_path): os.remove(file_path) elif os.path.isdir(file_path): shutil.rmtree(file_path) except Exception as e: logger.error(f"Failed to delete {file_path}. Reason: {e}") logger.info(f"Content in '{directory_path}' removed.") else: logger.error(f"Directory '{directory_path}' does not exist.")
# Create directory if not existsdef create_directories(directory_path): if isinstance(directory_path, str): directory_path = [directory_path] for one_dir_path in directory_path: if not os.path.exists(one_dir_path): os.makedirs(one_dir_path) logger.debug(f"Directory '{one_dir_path}' created.")
def setup_logger(name_log): logger = logging.getLogger(name_log) logger.setLevel(logging.INFO)
_default_handler = logging.StreamHandler() # Set sys.stderr as stream. _default_handler.flush = sys.stderr.flush logger.addHandler(_default_handler)
logger.propagate = False
handlers = logger.handlers
for handler in handlers: formatter = logging.Formatter("[%(levelname)s] >> %(message)s") handler.setFormatter(formatter)
# logger.handlers
return logger
logger = setup_logger("ss")logger.setLevel(logging.INFO)
README.md+6-5
app.pyAdded+805
mdx_models/data.jsonAdded+354
packages.txtAdded+1
requirements.txtAdded+3
test.mp3Added
utils.pyAdded+142