File size: 1,690 Bytes
c56a4ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dd34c4
 
 
 
 
 
c56a4ac
 
 
 
 
 
2dd34c4
c56a4ac
 
2dd34c4
c56a4ac
 
2dd34c4
 
 
c56a4ac
2dd34c4
 
 
c56a4ac
2dd34c4
c56a4ac
 
 
 
 
 
 
 
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
import gradio as gr
import os
import random
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=}")

    # Ensure the output directory exists
    randomnumber = str(random.randint(111111111, 999999999))
    output_folder = f"{gradio_temp_path}/separated_audio/{randomnumber}"
    os.makedirs(output_folder, exist_ok=True)

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

    result = subprocess.run(command, capture_output=True)
    print(result)
    if result.returncode != 0:
        print(f"Error: {result.stderr.decode('utf-8')}")
        return []

    # Check if the separated files exist and return their paths
    paths = []
    stems = ['vocals', 'drums', 'bass', 'other', 'piano']
    for stem in stems[:stem_count]:
        stem_path = os.path.join(output_folder, stem, f"{audio_filename}_{stem}.wav")
        print(f"Checking {stem_path}")
        if os.path.exists(stem_path):
            paths.append(gr.Audio(file=stem_path, label=stem.capitalize()))
        else:
            print(f"File not found: {stem_path}")

    return paths

iface = gr.Interface(
    fn=separate_audio,
    inputs=[gr.Audio(type="filepath"), gr.Radio([2, 4, 5])],
    outputs=[gr.Audio(label="Accompaniment"), gr.Audio(label="Vocals"), gr.Audio(label="Drums"), gr.Audio(label="Bass"), gr.Audio(label="Other"), gr.Audio(label="Piano")]
)

iface.launch()