Spaces:
Runtime error
Runtime error
Commit
·
c4bae49
1
Parent(s):
775a8b5
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from scipy.io.wavfile import write
|
4 |
+
|
5 |
+
|
6 |
+
def separate_audio(audio):
|
7 |
+
os.makedirs("out", exist_ok=True)
|
8 |
+
write('test.wav', audio[0], audio[1])
|
9 |
+
os.system("python3 -m demucs.separate -n htdemucs --two-stems=vocals -d cpu test.wav -o out")
|
10 |
+
vocals_file = "./out/htdemucs/test/vocals.wav"
|
11 |
+
instrumental_file = "./out/htdemucs/test/no_vocals.wav"
|
12 |
+
return vocals_file, instrumental_file
|
13 |
+
|
14 |
+
|
15 |
+
def batch_separate_audio(audio_list):
|
16 |
+
os.makedirs("out", exist_ok=True)
|
17 |
+
vocals_files = []
|
18 |
+
instrumental_files = []
|
19 |
+
for idx, audio in enumerate(audio_list):
|
20 |
+
write(f'test{idx}.wav', audio[0], audio[1])
|
21 |
+
os.system(f"python3 -m demucs.separate -n htdemucs --two-stems=vocals -d cpu test{idx}.wav -o out")
|
22 |
+
vocals_file = f"./out/htdemucs/test{idx}/vocals.wav"
|
23 |
+
instrumental_file = f"./out/htdemucs/test{idx}/no_vocals.wav"
|
24 |
+
vocals_files.append(vocals_file)
|
25 |
+
instrumental_files.append(instrumental_file)
|
26 |
+
return vocals_files, instrumental_files
|
27 |
+
|
28 |
+
|
29 |
+
def download_file(filepath):
|
30 |
+
with open(filepath, "rb") as f:
|
31 |
+
file_bytes = f.read()
|
32 |
+
return file_bytes
|
33 |
+
|
34 |
+
|
35 |
+
title = "Demucs Music Source Separation (v4)"
|
36 |
+
description = "This is the latest 'bleeding edge version' which enables the new v4 Hybrid Transformer model. <br> for this space, 2 stem separation (Karaoke Mode) is enabled and CPU mode which has been optimized for best quality & processing time. <p>| Gradio demo for Demucs(v4): Music Source Separation in the Waveform Domain. To use it, simply upload your audio, or click one of the examples to load them. Read more at the links below.</p>"
|
37 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1911.13254' target='_blank'>Music Source Separation in the Waveform Domain</a> | <a href='https://github.com/facebookresearch/demucs' target='_blank'>Github Repo</a> | <a href='https://www.thafx.com' target='_blank'>//THAFX</a></p>"
|
38 |
+
|
39 |
+
audio_input = gr.inputs.Audio(label="Input")
|
40 |
+
vocals_output = gr.outputs.Audio(label="Vocals", type="filepath", download=True)
|
41 |
+
instrumental_output = gr.outputs.Audio(label="No Vocals / Instrumental", type="filepath", download=True)
|
42 |
+
|
43 |
+
examples = [['test.mp3']]
|
44 |
+
|
45 |
+
# Create the Gradio interface
|
46 |
+
gr.Interface(
|
47 |
+
fn=separate_audio,
|
48 |
+
inputs=audio_input,
|
49 |
+
outputs=[vocals_output, instrumental_output],
|
50 |
+
title=title,
|
51 |
+
description=description,
|
52 |
+
article=article,
|
53 |
+
examples=examples
|
54 |
+
).launch(enable_queue=True, share=True)
|
55 |
+
|
56 |
+
|
57 |
+
batch_audio_input = gr.inputs.Audio(label="Input", type="numpy", multiple=True)
|
58 |
+
batch_vocals_output = gr.outputs.Audio(label="Vocals", type="filepath", download=True, multiple=True)
|
59 |
+
batch_instrumental_output = gr.outputs.Audio(label="No Vocals / Instrumental", type="filepath", download=True, multiple=True)
|
60 |
+
|
61 |
+
batch_examples = [[audio] for audio in examples[0]]
|
62 |
+
|
63 |
+
# Create the Gradio interface for batch conversion
|
64 |
+
gr.Interface(
|
65 |
+
fn=batch_separate_audio,
|
66 |
+
inputs=batch_audio_input,
|
67 |
+
outputs=[batch_vocals_output, batch_instrumental_output],
|
68 |
+
title="Demucs Batch Music Source Separation (v4)",
|
69 |
+
description=description,
|
70 |
+
article=article,
|
71 |
+
examples=batch_examples
|
72 |
+
).launch(enable_queue=True, share=True)
|