Spaces:
Runtime error
Runtime error
amirgame197
commited on
Commit
•
4c3d27d
1
Parent(s):
e6c92a8
Update app.py
Browse files
app.py
CHANGED
@@ -4,69 +4,49 @@ import tempfile
|
|
4 |
import subprocess
|
5 |
|
6 |
def separate_audio(audio_path, stem_count):
|
7 |
-
|
8 |
head, tail = os.path.split(audio_path)
|
|
|
9 |
gradio_temp_path = head
|
10 |
audio_filename = tail.split('.')[0]
|
|
|
|
|
|
|
11 |
|
12 |
command = f"spleeter separate -p spleeter:{stem_count}stems {audio_path}"
|
13 |
command = command.split()
|
|
|
14 |
|
15 |
result = subprocess.run(command)
|
|
|
16 |
|
17 |
-
|
18 |
if stem_count == 2:
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
elif stem_count == 5:
|
35 |
-
piano_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/piano.wav"
|
36 |
-
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
37 |
-
drums_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/drums.wav"
|
38 |
-
bass_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/bass.wav"
|
39 |
-
other_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/other.wav"
|
40 |
-
outputs.extend([
|
41 |
-
{'description': 'Vocals', 'path': vocals_path},
|
42 |
-
{'description': 'Piano', 'path': piano_path},
|
43 |
-
{'description': 'Drums', 'path': drums_path},
|
44 |
-
{'description': 'Bass', 'path': bass_path},
|
45 |
-
{'description': 'Other', 'path': other_path},
|
46 |
-
])
|
47 |
-
return outputs
|
48 |
-
|
49 |
-
def separate_audio_gradio(audio_file, stem_count):
|
50 |
-
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
51 |
-
temp_file.write(audio_file.read())
|
52 |
temp_file_path = temp_file.name
|
53 |
-
|
54 |
-
separate_audios = separate_audio(temp_file_path, stem_count)
|
55 |
-
|
56 |
-
outputs = []
|
57 |
-
for audio in separate_audios:
|
58 |
-
outputs.append((audio['description'], gr.outputs.Audio(audio['path'])))
|
59 |
-
|
60 |
-
return outputs
|
61 |
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
fn=separate_audio_gradio,
|
67 |
-
inputs=[input_audio, input_stem_count],
|
68 |
-
outputs="label",
|
69 |
-
title="Audio Separator",
|
70 |
-
description="Separate audio into vocals and accompaniment using Spleeter",
|
71 |
-
capture_session=True
|
72 |
-
).launch()
|
|
|
4 |
import subprocess
|
5 |
|
6 |
def separate_audio(audio_path, stem_count):
|
7 |
+
print(f"{audio_path=}")
|
8 |
head, tail = os.path.split(audio_path)
|
9 |
+
|
10 |
gradio_temp_path = head
|
11 |
audio_filename = tail.split('.')[0]
|
12 |
+
print(f"{gradio_temp_path=}")
|
13 |
+
print(f"{audio_filename=}")
|
14 |
+
print(f"{stem_count=}")
|
15 |
|
16 |
command = f"spleeter separate -p spleeter:{stem_count}stems {audio_path}"
|
17 |
command = command.split()
|
18 |
+
print(f"{command=}")
|
19 |
|
20 |
result = subprocess.run(command)
|
21 |
+
print(result)
|
22 |
|
23 |
+
paths = []
|
24 |
if stem_count == 2:
|
25 |
+
paths.append(('Accompaniment', f"{gradio_temp_path}/separated_audio/{audio_filename}/accompaniment.wav"))
|
26 |
+
paths.append(('Vocals', f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"))
|
27 |
+
elif stem_count == 4 or stem_count == 5:
|
28 |
+
paths.append(('Vocals', f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"))
|
29 |
+
paths.append(('Drums', f"{gradio_temp_path}/separated_audio/{audio_filename}/drums.wav"))
|
30 |
+
paths.append(('Bass', f"{gradio_temp_path}/separated_audio/{audio_filename}/bass.wav"))
|
31 |
+
paths.append(('Other', f"{gradio_temp_path}/separated_audio/{audio_filename}/other.wav"))
|
32 |
+
if stem_count == 5:
|
33 |
+
paths.append(('Piano', f"{gradio_temp_path}/separated_audio/{audio_filename}/piano.wav"))
|
34 |
+
|
35 |
+
return paths
|
36 |
+
|
37 |
+
def gradio_interface(audio_file, stem_count):
|
38 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
39 |
+
temp_file.write(audio_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
temp_file_path = temp_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
separated_audios = separate_audio(temp_file_path, stem_count)
|
43 |
+
return [gr.Audio(file_path, label=description) for description, file_path in separated_audios]
|
44 |
+
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=gradio_interface,
|
47 |
+
inputs=[gr.Audio(type="filepath"), gr.Radio([2, 4, 5])],
|
48 |
+
outputs=[gr.Audio(label="Output") for _ in range(5)],
|
49 |
+
live=True
|
50 |
+
)
|
51 |
|
52 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|