File size: 1,911 Bytes
2595db2
7b26bef
 
 
 
2595db2
4c3d27d
7b26bef
4c3d27d
7b26bef
 
4c3d27d
 
 
7b26bef
 
 
4c3d27d
7b26bef
 
4c3d27d
7b26bef
4c3d27d
7b26bef
4c3d27d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2595db2
 
4c3d27d
 
 
 
 
 
 
 
2595db2
4c3d27d
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
import gradio as gr
import os
import tempfile
import subprocess

def separate_audio(audio_path, stem_count):
    print(f"{audio_path=}")
    head, tail = os.path.split(audio_path)

    gradio_temp_path = head
    audio_filename = tail.split('.')[0]
    print(f"{gradio_temp_path=}")
    print(f"{audio_filename=}")
    print(f"{stem_count=}")

    command = f"spleeter separate -p spleeter:{stem_count}stems {audio_path}"
    command = command.split()
    print(f"{command=}")

    result = subprocess.run(command)
    print(result)

    paths = []
    if stem_count == 2:
        paths.append(('Accompaniment', f"{gradio_temp_path}/separated_audio/{audio_filename}/accompaniment.wav"))
        paths.append(('Vocals', f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"))
    elif stem_count == 4 or stem_count == 5:
        paths.append(('Vocals', f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"))
        paths.append(('Drums', f"{gradio_temp_path}/separated_audio/{audio_filename}/drums.wav"))
        paths.append(('Bass', f"{gradio_temp_path}/separated_audio/{audio_filename}/bass.wav"))
        paths.append(('Other', f"{gradio_temp_path}/separated_audio/{audio_filename}/other.wav"))
        if stem_count == 5:
            paths.append(('Piano', f"{gradio_temp_path}/separated_audio/{audio_filename}/piano.wav"))

    return paths

def gradio_interface(audio_file, stem_count):
    with tempfile.NamedTemporaryFile(delete=False) as temp_file:
        temp_file.write(audio_file)
        temp_file_path = temp_file.name

    separated_audios = separate_audio(temp_file_path, stem_count)
    return [gr.Audio(file_path, label=description) for description, file_path in separated_audios]

iface = gr.Interface(
    fn=gradio_interface,
    inputs=[gr.Audio(type="filepath"), gr.Radio([2, 4, 5])],
    outputs=[gr.Audio(label="Output") for _ in range(5)],
)

iface.launch()