PreciousMposa commited on
Commit
16f476f
·
verified ·
1 Parent(s): 8960982

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
app.py CHANGED
@@ -1,21 +1,37 @@
 
 
1
  import gradio as gr
2
  from demucs.separate import main
3
- import os
4
 
5
- def separate(audio_file):
6
- output_dir = "./output"
 
 
 
 
 
7
  os.makedirs(output_dir, exist_ok=True)
8
- main(["-n", "htdemucs", "-d", "cpu", "-o", output_dir, audio_file])
9
-
10
- base = os.path.splitext(os.path.basename(audio_file))[0]
11
- folder = os.path.join(output_dir, "htdemucs", base)
12
- return [os.path.join(folder, f"{stem}.mp3") for stem in ["vocals", "drums", "bass", "other"]]
13
 
14
- iface = gr.Interface(
15
- fn=separate,
16
- inputs=gr.Audio(type="filepath"),
17
- outputs=[gr.Audio(label=s) for s in ["Vocals", "Drums", "Bass", "Other"]],
18
- title="Demucs Stem Separator",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  )
20
 
21
- iface.launch()
 
1
+ import os
2
+ import shutil
3
  import gradio as gr
4
  from demucs.separate import main
 
5
 
6
+ def separate_stems(audio_file):
7
+ input_path = "input.mp3"
8
+ shutil.copy(audio_file, input_path)
9
+
10
+ output_dir = "output"
11
+ if os.path.exists(output_dir):
12
+ shutil.rmtree(output_dir)
13
  os.makedirs(output_dir, exist_ok=True)
 
 
 
 
 
14
 
15
+ # Run Demucs
16
+ main(["-n", "htdemucs", "-o", output_dir, input_path])
17
+
18
+ # Build list of stems to return
19
+ base = os.path.splitext(os.path.basename(input_path))[0]
20
+ stem_path = os.path.join(output_dir, "htdemucs", base)
21
+ stems = [os.path.join(stem_path, f"{stem}.mp3") for stem in ["vocals", "drums", "bass", "other"]]
22
+ return stems
23
+
24
+ demo = gr.Interface(
25
+ fn=separate_stems,
26
+ inputs=gr.Audio(type="filepath", label="Upload Song"),
27
+ outputs=[
28
+ gr.Audio(label="Vocals"),
29
+ gr.Audio(label="Drums"),
30
+ gr.Audio(label="Bass"),
31
+ gr.Audio(label="Other"),
32
+ ],
33
+ title="Demucs v4 Stem Separator",
34
+ description="Upload a song to separate vocals, drums, bass, and other using Facebook's Demucs model.",
35
  )
36
 
37
+ demo.launch()