File size: 3,196 Bytes
c4bae49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import gradio as gr
from scipy.io.wavfile import write


def separate_audio(audio):
    os.makedirs("out", exist_ok=True)
    write('test.wav', audio[0], audio[1])
    os.system("python3 -m demucs.separate -n htdemucs --two-stems=vocals -d cpu test.wav -o out")
    vocals_file = "./out/htdemucs/test/vocals.wav"
    instrumental_file = "./out/htdemucs/test/no_vocals.wav"
    return vocals_file, instrumental_file


def batch_separate_audio(audio_list):
    os.makedirs("out", exist_ok=True)
    vocals_files = []
    instrumental_files = []
    for idx, audio in enumerate(audio_list):
        write(f'test{idx}.wav', audio[0], audio[1])
        os.system(f"python3 -m demucs.separate -n htdemucs --two-stems=vocals -d cpu test{idx}.wav -o out")
        vocals_file = f"./out/htdemucs/test{idx}/vocals.wav"
        instrumental_file = f"./out/htdemucs/test{idx}/no_vocals.wav"
        vocals_files.append(vocals_file)
        instrumental_files.append(instrumental_file)
    return vocals_files, instrumental_files


def download_file(filepath):
    with open(filepath, "rb") as f:
        file_bytes = f.read()
    return file_bytes


title = "Demucs Music Source Separation (v4)"
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>"
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>"

audio_input = gr.inputs.Audio(label="Input")
vocals_output = gr.outputs.Audio(label="Vocals", type="filepath", download=True)
instrumental_output = gr.outputs.Audio(label="No Vocals / Instrumental", type="filepath", download=True)

examples = [['test.mp3']]

# Create the Gradio interface
gr.Interface(
    fn=separate_audio,
    inputs=audio_input,
    outputs=[vocals_output, instrumental_output],
    title=title,
    description=description,
    article=article,
    examples=examples
).launch(enable_queue=True, share=True)


batch_audio_input = gr.inputs.Audio(label="Input", type="numpy", multiple=True)
batch_vocals_output = gr.outputs.Audio(label="Vocals", type="filepath", download=True, multiple=True)
batch_instrumental_output = gr.outputs.Audio(label="No Vocals / Instrumental", type="filepath", download=True, multiple=True)

batch_examples = [[audio] for audio in examples[0]]

# Create the Gradio interface for batch conversion
gr.Interface(
    fn=batch_separate_audio,
    inputs=batch_audio_input,
    outputs=[batch_vocals_output, batch_instrumental_output],
    title="Demucs Batch Music Source Separation (v4)",
    description=description,
    article=article,
    examples=batch_examples
).launch(enable_queue=True, share=True)