Spaces:
Runtime error
Runtime error
Updated full code
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import osimport gradio as grfrom scipy.io.wavfile import writeimport subprocessfrom audio_separator import Separatordef inference(audio):os.makedirs("out", exist_ok=True)write('test.wav', audio[0], audio[1])command = "python3 -m demucs.separate -n htdemucs_6s -d cpu test.wav -o out"process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)print("Demucs script output:", process.stdout.decode())os.makedirs("out", exist_ok=True)write('test.wav', audio[0], audio[1])result = os.system("python3 -m demucs.separate -n htdemucs_6s -d cpu test.wav -o out")print(f"Demucs script result: {result}")# Initialize the Separator with the audio file and model nameseparator = Separator("./out/mdx_extra_q/test/vocals.wav", model_name='UVR_MDXNET_KARA_2',use_cuda=False,output_format='mp3')primary_stem_path, secondary_stem_path = separator.separate()print(f'Primary stem saved at {primary_stem_path}')print(f'Secondary stem saved at {secondary_stem_path}')# Check if files exist before returningfiles = ["./out/htdemucs_6s/test/vocals.wav",secondary_stem_path,primary_stem_path,"./out/htdemucs_6s/test/bass.wav","./out/htdemucs_6s/test/drums.wav","./out/htdemucs_6s/test/other.wav","./out/htdemucs_6s/test/piano.wav","./out/htdemucs_6s/test/guitar.wav",]for file in files:if not os.path.isfile(file):print(f"File not found: {file}")else:print(f"File exists: {file}")return filestitle = "Source Sepration Demo"description = "Music Source Separation in the Waveform Domain. To use it, simply upload your audio"gr.Interface(inference,gr.components.Audio(type="numpy", label="Input"),[gr.components.Audio(type="filepath", label="Full Vocals"),gr.components.Audio(type="filepath", label="Lead Vocals"),gr.components.Audio(type="filepath", label="Chorus"),gr.components.Audio(type="filepath", label="Bass"),gr.components.Audio(type="filepath", label="Drums"),gr.components.Audio(type="filepath", label="Other"),gr.components.Audio(type="filepath", label="Piano"),gr.components.Audio(type="filepath", label="Guitar"),],title=title,description=description,).launch(share=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import osimport gradio as grfrom scipy.io.wavfile import writeimport subprocessfrom audio_separator import Separator # Ensure this is correctly implementeddef inference(audio):os.makedirs("out", exist_ok=True)audio_path = 'test.wav'write(audio_path, audio[0], audio[1])try:# Using subprocess.run for better controlcommand = f"python3 -m demucs.separate -n htdemucs_6s -d cpu {audio_path} -o out"process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)print("Demucs script output:", process.stdout.decode())except subprocess.CalledProcessError as e:print("Error in Demucs script:", e.stderr.decode())return Nonetry:# Separating the stems using your custom separatorseparator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=False, output_format='mp3')primary_stem_path, secondary_stem_path = separator.separate()except Exception as e:print("Error in custom separation:", str(e))return None# Collecting all file pathsfiles = [f"./out/htdemucs_6s/test/{stem}.wav" for stem in ["vocals", "bass", "drums", "other", "piano", "guitar"]]files.extend([primary_stem_path, secondary_stem_path])# Check if files existexisting_files = [file for file in files if os.path.isfile(file)]if not existing_files:print("No files were created.")return Nonereturn existing_files# Gradio Interfacetitle = "Source Separation Demo"description = "Music Source Separation in the Waveform Domain. To use it, simply upload your audio."gr.Interface(inference,gr.components.Audio(type="numpy", label="Input"),[gr.components.Audio(type="filepath", label=stem) for stem in ["Full Vocals", "Lead Vocals", "Chorus", "Bass", "Drums", "Other", "Piano", "Guitar"]],title=title,description=description,).launch()
app.py+39-55