Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,121 +1,72 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
import subprocess
|
| 5 |
|
| 6 |
-
def separate_audio(audio_path):
|
| 7 |
|
| 8 |
-
print(f"{audio_path=}")
|
| 9 |
head, tail = os.path.split(audio_path)
|
| 10 |
-
|
| 11 |
gradio_temp_path = head
|
| 12 |
audio_filename = tail.split('.')[0]
|
| 13 |
-
print(f"{gradio_temp_path=}")
|
| 14 |
-
print(f"{audio_filename=}")
|
| 15 |
-
|
| 16 |
-
command = f"spleeter separate -p spleeter:2stems {audio_path}"
|
| 17 |
-
command = command.split()
|
| 18 |
-
print(f"{command=}")
|
| 19 |
-
|
| 20 |
-
result = subprocess.run(command)
|
| 21 |
-
print(result)
|
| 22 |
-
|
| 23 |
-
print("--------")
|
| 24 |
-
accompaniment_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/accompaniment.wav"
|
| 25 |
-
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
| 26 |
-
print(f"{accompaniment_path=}")
|
| 27 |
-
print(os.path.exists(accompaniment_path))
|
| 28 |
-
print(f"{vocals_path=}")
|
| 29 |
-
print(os.path.exists(vocals_path))
|
| 30 |
-
|
| 31 |
-
return vocals_path, accompaniment_path
|
| 32 |
|
| 33 |
-
|
| 34 |
-
def separate_audio_by_stem(audio_path, stem_count):
|
| 35 |
-
|
| 36 |
-
print(f"{audio_path=}")
|
| 37 |
-
head, tail = os.path.split(audio_path)
|
| 38 |
-
|
| 39 |
-
gradio_temp_path = head
|
| 40 |
-
audio_filename = tail.split('.')[0]
|
| 41 |
-
print(f"{gradio_temp_path=}")
|
| 42 |
-
print(f"{audio_filename=}")
|
| 43 |
-
print(f"{stem_count=}")
|
| 44 |
-
|
| 45 |
command = f"spleeter separate -p spleeter:{stem_count}stems {audio_path}"
|
| 46 |
command = command.split()
|
| 47 |
-
print(f"{command=}")
|
| 48 |
|
| 49 |
result = subprocess.run(command)
|
| 50 |
-
print(result)
|
| 51 |
|
|
|
|
| 52 |
if stem_count == 2:
|
| 53 |
accompaniment_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/accompaniment.wav"
|
| 54 |
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
print(f"{vocals_path=} \t exists: {os.path.exists(vocals_path)}")
|
| 58 |
-
|
| 59 |
-
return [
|
| 60 |
-
{'description': 'Accompaniment', 'path':accompaniment_path},
|
| 61 |
-
{'description': 'Vocals', 'path':vocals_path},
|
| 62 |
-
]
|
| 63 |
-
|
| 64 |
elif stem_count == 4:
|
| 65 |
-
|
| 66 |
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
| 67 |
drums_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/drums.wav"
|
| 68 |
bass_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/bass.wav"
|
| 69 |
other_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/other.wav"
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
return [
|
| 77 |
-
{'description': 'Vocals', 'path':vocals_path},
|
| 78 |
-
{'description': 'Drums', 'path':drums_path},
|
| 79 |
-
{'description': 'Bass', 'path':bass_path},
|
| 80 |
-
{'description': 'Other', 'path':other_path},
|
| 81 |
-
]
|
| 82 |
-
|
| 83 |
elif stem_count == 5:
|
| 84 |
-
|
| 85 |
piano_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/piano.wav"
|
| 86 |
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
| 87 |
drums_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/drums.wav"
|
| 88 |
bass_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/bass.wav"
|
| 89 |
other_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/other.wav"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
print(f"{vocals_path=} \t exists: {os.path.exists(vocals_path)}")
|
| 93 |
-
print(f"{drums_path=} \t exists: {os.path.exists(drums_path)}")
|
| 94 |
-
print(f"{bass_path=} \t exists: {os.path.exists(bass_path)}")
|
| 95 |
-
print(f"{other_path=} \t exists: {os.path.exists(other_path)}")
|
| 96 |
-
|
| 97 |
-
return [
|
| 98 |
-
{'description': 'Vocals', 'path':vocals_path},
|
| 99 |
-
{'description': 'Piano', 'path':piano_path},
|
| 100 |
-
{'description': 'Drums', 'path':drums_path},
|
| 101 |
-
{'description': 'Bass', 'path':bass_path},
|
| 102 |
-
{'description': 'Other', 'path':other_path},
|
| 103 |
-
]
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import os
|
| 3 |
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 |
+
outputs = []
|
| 18 |
if stem_count == 2:
|
| 19 |
accompaniment_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/accompaniment.wav"
|
| 20 |
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
| 21 |
+
outputs.append({'description': 'Accompaniment', 'path': accompaniment_path})
|
| 22 |
+
outputs.append({'description': 'Vocals', 'path': vocals_path})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
elif stem_count == 4:
|
|
|
|
| 24 |
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
| 25 |
drums_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/drums.wav"
|
| 26 |
bass_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/bass.wav"
|
| 27 |
other_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/other.wav"
|
| 28 |
+
outputs.extend([
|
| 29 |
+
{'description': 'Vocals', 'path': vocals_path},
|
| 30 |
+
{'description': 'Drums', 'path': drums_path},
|
| 31 |
+
{'description': 'Bass', 'path': bass_path},
|
| 32 |
+
{'description': 'Other', 'path': other_path},
|
| 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 |
+
input_audio = gr.inputs.File(label="Upload Audio File", type=["wav", "mp3"])
|
| 63 |
+
input_stem_count = gr.inputs.Radio([2, 4, 5], label="Stem count")
|
| 64 |
+
|
| 65 |
+
gr.Interface(
|
| 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()
|