File size: 1,127 Bytes
519d358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import shutil
import gradio as gr
from demucs.separate import main

def separate_stems(audio_file):
    input_path = "input.mp3"
    shutil.copy(audio_file, input_path)

    output_dir = "output"
    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)
    os.makedirs(output_dir, exist_ok=True)

    # Run Demucs
    main(["-n", "htdemucs", "-o", output_dir, input_path])

    # Build list of stems to return
    base = os.path.splitext(os.path.basename(input_path))[0]
    stem_path = os.path.join(output_dir, "htdemucs", base)
    stems = [os.path.join(stem_path, f"{stem}.mp3") for stem in ["vocals", "drums", "bass", "other"]]
    return stems

demo = gr.Interface(
    fn=separate_stems,
    inputs=gr.Audio(type="filepath", label="Upload Song"),
    outputs=[
        gr.Audio(label="Vocals"),
        gr.Audio(label="Drums"),
        gr.Audio(label="Bass"),
        gr.Audio(label="Other"),
    ],
    title="Demucs v4 Stem Separator",
    description="Upload a song to separate vocals, drums, bass, and other using Facebook's Demucs model.",
)

demo.launch()