Sutirtha/AudioSourceSeparationChorus
0
1import os2import gradio as gr3from scipy.io.wavfile import write4import subprocess5 6from audio_separator import Separator # Ensure this is correctly implemented7 8def inference(audio):9 os.makedirs("out", exist_ok=True)10 audio_path = 'test.wav'11 write(audio_path, audio[0], audio[1])12 13 try:14 # Using subprocess.run for better control15 command = f"python3 -m demucs.separate -n htdemucs_6s -d cuda {audio_path} -o out"16 process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)17 print("Demucs script output:", process.stdout.decode())18 except subprocess.CalledProcessError as e:19 print("Error in Demucs script:", e.stderr.decode())20 return None21 22 try:23 # Separating the stems using your custom separator24 separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=True, output_format='mp3')25 primary_stem_path, secondary_stem_path = separator.separate()26 except Exception as e:27 print("Error in custom separation:", str(e))28 return None29 30 # Collecting all file paths31 files = [f"./out/htdemucs_6s/test/{stem}.wav" for stem in ["vocals", "bass", "drums", "other", "piano", "guitar"]]32 files.extend([secondary_stem_path,primary_stem_path ])33 34 # Check if files exist35 existing_files = [file for file in files if os.path.isfile(file)]36 if not existing_files:37 print("No files were created.")38 return None39 40 return existing_files41 42# Gradio Interface43title = "Source Separation Demo"44description = "Music Source Separation in the Waveform Domain. To use it, simply upload your audio."45gr.Interface(46 inference, 47 gr.components.Audio(type="numpy", label="Input"), 48 [gr.components.Audio(type="filepath", label=stem) for stem in ["Full Vocals","Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Chorus" ]],49 title=title,50 description=description,51).launch()52 