import os import tempfile import subprocess import gradio as gr import spaces MODEL_NAME = "htdemucs" @spaces.GPU(duration=120) def separate_stems(audio_path): if audio_path is None: raise gr.Error("Please upload an audio file.") out_dir = tempfile.mkdtemp() cmd = ["python", "-m", "demucs.separate", "-n", MODEL_NAME, "-o", out_dir, audio_path] subprocess.run(cmd, check=True) track_name = os.path.splitext(os.path.basename(audio_path))[0] stems_dir = os.path.join(out_dir, MODEL_NAME, track_name) vocals = os.path.join(stems_dir, "vocals.wav") drums = os.path.join(stems_dir, "drums.wav") bass = os.path.join(stems_dir, "bass.wav") other = os.path.join(stems_dir, "other.wav") return vocals, drums, bass, other demo = gr.Interface( fn=separate_stems, inputs=gr.Audio(type="filepath", label="Input track"), outputs=[ gr.Audio(type="filepath", label="Vocals"), gr.Audio(type="filepath", label="Drums"), gr.Audio(type="filepath", label="Bass"), gr.Audio(type="filepath", label="Other"), ], title="MasterSplitter - Free Stem Separation", description="Upload a song to split it into vocals, drums, bass and other stems using Demucs.", api_name="split", ) demo.queue().launch()