Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import tempfile
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
# Set Streamlit app title
|
7 |
+
st.title("Audio Separation App")
|
8 |
+
|
9 |
+
# Function to process the audio file
|
10 |
+
def separate_audio(audio_path):
|
11 |
+
|
12 |
+
print(f"{audio_path=}")
|
13 |
+
head, tail = os.path.split(audio_path)
|
14 |
+
|
15 |
+
gradio_temp_path = head
|
16 |
+
audio_filename = tail.split('.')[0]
|
17 |
+
print(f"{gradio_temp_path=}")
|
18 |
+
print(f"{audio_filename=}")
|
19 |
+
|
20 |
+
command = f"spleeter separate -p spleeter:2stems {audio_path}"
|
21 |
+
command = command.split()
|
22 |
+
print(f"{command=}")
|
23 |
+
|
24 |
+
result = subprocess.run(command)
|
25 |
+
print(result)
|
26 |
+
|
27 |
+
print("--------")
|
28 |
+
accompaniment_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/accompaniment.wav"
|
29 |
+
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
30 |
+
print(f"{accompaniment_path=}")
|
31 |
+
print(os.path.exists(accompaniment_path))
|
32 |
+
print(f"{vocals_path=}")
|
33 |
+
print(os.path.exists(vocals_path))
|
34 |
+
|
35 |
+
return vocals_path, accompaniment_path
|
36 |
+
|
37 |
+
|
38 |
+
def separate_audio_by_stem(audio_path, stem_count):
|
39 |
+
|
40 |
+
print(f"{audio_path=}")
|
41 |
+
head, tail = os.path.split(audio_path)
|
42 |
+
|
43 |
+
gradio_temp_path = head
|
44 |
+
audio_filename = tail.split('.')[0]
|
45 |
+
print(f"{gradio_temp_path=}")
|
46 |
+
print(f"{audio_filename=}")
|
47 |
+
print(f"{stem_count=}")
|
48 |
+
|
49 |
+
command = f"spleeter separate -p spleeter:{stem_count}stems {audio_path}"
|
50 |
+
command = command.split()
|
51 |
+
print(f"{command=}")
|
52 |
+
|
53 |
+
result = subprocess.run(command)
|
54 |
+
print(result)
|
55 |
+
|
56 |
+
if stem_count == 2:
|
57 |
+
accompaniment_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/accompaniment.wav"
|
58 |
+
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
59 |
+
|
60 |
+
print(f"{accompaniment_path=} \t exists: {os.path.exists(accompaniment_path)}")
|
61 |
+
print(f"{vocals_path=} \t exists: {os.path.exists(vocals_path)}")
|
62 |
+
|
63 |
+
return [
|
64 |
+
{'description': 'Accompaniment', 'path':accompaniment_path},
|
65 |
+
{'description': 'Vocals', 'path':vocals_path},
|
66 |
+
]
|
67 |
+
|
68 |
+
elif stem_count == 4:
|
69 |
+
|
70 |
+
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
71 |
+
drums_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/drums.wav"
|
72 |
+
bass_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/bass.wav"
|
73 |
+
other_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/other.wav"
|
74 |
+
|
75 |
+
print(f"{vocals_path=} \t exists: {os.path.exists(vocals_path)}")
|
76 |
+
print(f"{drums_path=} \t exists: {os.path.exists(drums_path)}")
|
77 |
+
print(f"{bass_path=} \t exists: {os.path.exists(bass_path)}")
|
78 |
+
print(f"{other_path=} \t exists: {os.path.exists(other_path)}")
|
79 |
+
|
80 |
+
return [
|
81 |
+
{'description': 'Vocals', 'path':vocals_path},
|
82 |
+
{'description': 'Drums', 'path':drums_path},
|
83 |
+
{'description': 'Bass', 'path':bass_path},
|
84 |
+
{'description': 'Other', 'path':other_path},
|
85 |
+
]
|
86 |
+
|
87 |
+
elif stem_count == 5:
|
88 |
+
|
89 |
+
piano_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/piano.wav"
|
90 |
+
vocals_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/vocals.wav"
|
91 |
+
drums_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/drums.wav"
|
92 |
+
bass_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/bass.wav"
|
93 |
+
other_path = f"{gradio_temp_path}/separated_audio/{audio_filename}/other.wav"
|
94 |
+
|
95 |
+
print(f"{piano_path=} \t exists: {os.path.exists(vocals_path)}")
|
96 |
+
print(f"{vocals_path=} \t exists: {os.path.exists(vocals_path)}")
|
97 |
+
print(f"{drums_path=} \t exists: {os.path.exists(drums_path)}")
|
98 |
+
print(f"{bass_path=} \t exists: {os.path.exists(bass_path)}")
|
99 |
+
print(f"{other_path=} \t exists: {os.path.exists(other_path)}")
|
100 |
+
|
101 |
+
return [
|
102 |
+
{'description': 'Vocals', 'path':vocals_path},
|
103 |
+
{'description': 'Piano', 'path':piano_path},
|
104 |
+
{'description': 'Drums', 'path':drums_path},
|
105 |
+
{'description': 'Bass', 'path':bass_path},
|
106 |
+
{'description': 'Other', 'path':other_path},
|
107 |
+
]
|
108 |
+
|
109 |
+
# Streamlit app content
|
110 |
+
st.write("Upload an audio file")
|
111 |
+
|
112 |
+
uploaded_file = st.file_uploader("Choose a file", type=["wav","mp3"])
|
113 |
+
selected_stem_count = st.radio("Select stem count", (2,4,5))
|
114 |
+
|
115 |
+
if uploaded_file is not None:
|
116 |
+
|
117 |
+
if st.button("Submit"):
|
118 |
+
|
119 |
+
# Save the uploaded file to a temporary location
|
120 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
121 |
+
temp_file.write(uploaded_file.read())
|
122 |
+
temp_file_path = temp_file.name
|
123 |
+
|
124 |
+
# Process the uploaded audio file
|
125 |
+
separate_audios = separate_audio_by_stem(temp_file_path, selected_stem_count)
|
126 |
+
|
127 |
+
# Display the output files for download
|
128 |
+
st.write("Output Files:")
|
129 |
+
for audio in separate_audios:
|
130 |
+
st.write(audio['description'])
|
131 |
+
st.audio(audio['path'], format="audio/wav", start_time=0)
|