Spaces:
Sleeping
Sleeping
Redo whole script
Browse files
app.py
CHANGED
|
@@ -1,8 +1,3 @@
|
|
| 1 |
-
# 1. Install all necessary libraries for the full application
|
| 2 |
-
# This line is for Colab. On Hugging Face, these should be in your requirements.txt
|
| 3 |
-
# !pip install gradio "demucs>=4.0.0" librosa soundfile matplotlib
|
| 4 |
-
|
| 5 |
-
# 2. Import libraries
|
| 6 |
import gradio as gr
|
| 7 |
import os
|
| 8 |
import shutil
|
|
@@ -16,9 +11,7 @@ import zipfile
|
|
| 16 |
import tempfile
|
| 17 |
import matplotlib.pyplot as plt
|
| 18 |
import matplotlib
|
| 19 |
-
matplotlib.use('Agg')
|
| 20 |
-
|
| 21 |
-
# --- Helper/Processing Functions ---
|
| 22 |
|
| 23 |
def update_output_visibility(choice):
|
| 24 |
if "2 Stems" in choice:
|
|
@@ -98,42 +91,26 @@ def visualize_slices(stem_audio_data, progress=gr.Progress(track_tqdm=True)):
|
|
| 98 |
fig, ax = plt.subplots(figsize=(10, 3))
|
| 99 |
fig.patch.set_facecolor('#1f2937')
|
| 100 |
ax.set_facecolor('#111827')
|
| 101 |
-
|
| 102 |
librosa.display.waveshow(y, sr=sample_rate, ax=ax, color='#32f6ff', alpha=0.7)
|
| 103 |
-
|
| 104 |
for t in onset_times:
|
| 105 |
ax.axvline(x=t, color='#ff3b3b', linestyle='--', linewidth=1)
|
| 106 |
-
|
| 107 |
-
ax.
|
| 108 |
-
ax.xaxis.label.set_color('gray')
|
| 109 |
-
ax.yaxis.label.set_color('gray')
|
| 110 |
-
ax.set_xlabel("Time (s)")
|
| 111 |
-
ax.set_ylabel("Amplitude")
|
| 112 |
-
ax.set_title("Detected Slices", color='white')
|
| 113 |
plt.tight_layout()
|
| 114 |
|
| 115 |
progress(1, desc="Done!")
|
| 116 |
return fig, onset_times, stem_audio_data
|
| 117 |
|
| 118 |
def preview_slice(active_stem_audio, onset_times, evt: gr.SelectData):
|
| 119 |
-
if active_stem_audio is None or onset_times is None:
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
sample_rate, y = active_stem_audio
|
| 123 |
-
clicked_time = evt.index[0]
|
| 124 |
-
|
| 125 |
-
start_time = 0
|
| 126 |
-
end_time = len(y) / sample_rate
|
| 127 |
-
|
| 128 |
for i, t in enumerate(onset_times):
|
| 129 |
if t > clicked_time:
|
| 130 |
-
end_time = t
|
| 131 |
-
break
|
| 132 |
start_time = t
|
| 133 |
-
|
| 134 |
start_sample = librosa.time_to_samples(start_time, sr=sample_rate)
|
| 135 |
end_sample = librosa.time_to_samples(end_time, sr=sample_rate)
|
| 136 |
-
|
| 137 |
sliced_audio = y[start_sample:end_sample]
|
| 138 |
return (sample_rate, sliced_audio)
|
| 139 |
|
|
@@ -150,31 +127,22 @@ def clear_selection():
|
|
| 150 |
return [], []
|
| 151 |
|
| 152 |
def create_final_pack(selection_list, progress=gr.Progress(track_tqdm=True)):
|
| 153 |
-
if not selection_list:
|
| 154 |
-
raise gr.Error("No slices have been selected to create a pack!")
|
| 155 |
-
|
| 156 |
progress(0, desc="Preparing final pack...")
|
| 157 |
-
zip_path = "Custom_Loop_Pack.zip"
|
| 158 |
-
temp_dir = tempfile.mkdtemp()
|
| 159 |
-
|
| 160 |
with zipfile.ZipFile(zip_path, 'w') as zf:
|
| 161 |
for i, audio_data in enumerate(selection_list):
|
| 162 |
-
progress(i / len(selection_list), desc=f"Adding slice {i+1}
|
| 163 |
sample_rate, y = audio_data
|
| 164 |
filename = os.path.join(temp_dir, f"slice_{i+1:03d}.wav")
|
| 165 |
sf.write(filename, y, sample_rate, subtype='PCM_16')
|
| 166 |
zf.write(filename, os.path.basename(filename))
|
| 167 |
-
|
| 168 |
shutil.rmtree(temp_dir)
|
| 169 |
progress(1, desc="Pack Ready!")
|
| 170 |
return zip_path, gr.update(visible=True)
|
| 171 |
|
| 172 |
-
|
| 173 |
-
# --- Create the full Gradio Interface ---
|
| 174 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:
|
| 175 |
gr.Markdown("# 🎵 Loop Architect")
|
| 176 |
-
|
| 177 |
-
# State components
|
| 178 |
onset_times_state = gr.State(value=None)
|
| 179 |
active_stem_state = gr.State(value=None)
|
| 180 |
selected_slices_state = gr.State(value=[])
|
|
@@ -185,10 +153,6 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red"))
|
|
| 185 |
audio_input = gr.Audio(type="filepath", label="Upload a Track")
|
| 186 |
stem_options = gr.Radio(["4 Stems (Vocals, Drums, Bass, Other)", "2 Stems (Vocals + Instrumental)"], label="Separation Type", value="4 Stems (Vocals, Drums, Bass, Other)")
|
| 187 |
submit_button = gr.Button("Separate Stems")
|
| 188 |
-
with gr.Accordion("Slicing Options", open=True):
|
| 189 |
-
gr.Markdown("These options are for the final pack creation.")
|
| 190 |
-
loop_options_radio = gr.Radio(["One-Shots (All Transients)", "4 Bar Loops", "8 Bar Loops"], label="Loop Type (for 'Slice All')", value="One-Shots (All Transients)")
|
| 191 |
-
sensitivity_slider = gr.Slider(minimum=0.01, maximum=0.5, value=0.05, step=0.01, label="One-Shot Sensitivity")
|
| 192 |
|
| 193 |
with gr.Column(scale=2):
|
| 194 |
with gr.Accordion("Separated Stems", open=True):
|
|
@@ -200,10 +164,10 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red"))
|
|
| 200 |
slice_drums_btn = gr.Button("Visualize Slices", scale=1)
|
| 201 |
with gr.Row():
|
| 202 |
bass_output = gr.Audio(label="Bass", scale=4)
|
| 203 |
-
slice_bass_btn = gr.Button("
|
| 204 |
with gr.Row():
|
| 205 |
other_output = gr.Audio(label="Other / Instrumental", scale=4)
|
| 206 |
-
slice_other_btn = gr.Button("
|
| 207 |
|
| 208 |
gr.Markdown("### Slice Editor")
|
| 209 |
slice_plot = gr.Plot(label="Click a region on the waveform to preview a slice")
|
|
@@ -218,7 +182,6 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red"))
|
|
| 218 |
selected_gallery = gr.Gallery(label="Selected Slices", columns=8, object_fit="contain", height="auto")
|
| 219 |
download_zip_file = gr.File(label="Download Your Custom Pack", visible=False)
|
| 220 |
|
| 221 |
-
# --- Define Event Listeners ---
|
| 222 |
submit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output])
|
| 223 |
stem_options.change(fn=update_output_visibility, inputs=stem_options, outputs=[vocals_output, drums_output, bass_output, other_output])
|
| 224 |
|
|
@@ -227,14 +190,10 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red"))
|
|
| 227 |
slice_bass_btn.click(fn=visualize_slices, inputs=bass_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 228 |
slice_other_btn.click(fn=visualize_slices, inputs=other_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 229 |
|
| 230 |
-
# Correct event listener for plot interaction
|
| 231 |
slice_plot.select(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player)
|
| 232 |
|
| 233 |
add_to_pack_btn.click(fn=add_slice_to_pack, inputs=[preview_player, selected_slices_state], outputs=[selected_slices_state, selected_gallery])
|
| 234 |
clear_selection_btn.click(fn=clear_selection, outputs=[selected_slices_state, selected_gallery])
|
| 235 |
create_pack_btn.click(fn=create_final_pack, inputs=selected_slices_state, outputs=[download_zip_file, download_zip_file])
|
| 236 |
|
| 237 |
-
|
| 238 |
-
# Use demo.launch() for Hugging Face Spaces
|
| 239 |
-
# Use demo.launch(debug=True) for local testing
|
| 240 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import shutil
|
|
|
|
| 11 |
import tempfile
|
| 12 |
import matplotlib.pyplot as plt
|
| 13 |
import matplotlib
|
| 14 |
+
matplotlib.use('Agg')
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def update_output_visibility(choice):
|
| 17 |
if "2 Stems" in choice:
|
|
|
|
| 91 |
fig, ax = plt.subplots(figsize=(10, 3))
|
| 92 |
fig.patch.set_facecolor('#1f2937')
|
| 93 |
ax.set_facecolor('#111827')
|
|
|
|
| 94 |
librosa.display.waveshow(y, sr=sample_rate, ax=ax, color='#32f6ff', alpha=0.7)
|
|
|
|
| 95 |
for t in onset_times:
|
| 96 |
ax.axvline(x=t, color='#ff3b3b', linestyle='--', linewidth=1)
|
| 97 |
+
ax.tick_params(colors='gray'); ax.xaxis.label.set_color('gray'); ax.yaxis.label.set_color('gray')
|
| 98 |
+
ax.set_xlabel("Time (s)"); ax.set_ylabel("Amplitude"); ax.set_title("Detected Slices", color='white')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
plt.tight_layout()
|
| 100 |
|
| 101 |
progress(1, desc="Done!")
|
| 102 |
return fig, onset_times, stem_audio_data
|
| 103 |
|
| 104 |
def preview_slice(active_stem_audio, onset_times, evt: gr.SelectData):
|
| 105 |
+
if active_stem_audio is None or onset_times is None: return None
|
| 106 |
+
sample_rate, y = active_stem_audio; clicked_time = evt.index[0]
|
| 107 |
+
start_time = 0; end_time = len(y) / sample_rate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
for i, t in enumerate(onset_times):
|
| 109 |
if t > clicked_time:
|
| 110 |
+
end_time = t; break
|
|
|
|
| 111 |
start_time = t
|
|
|
|
| 112 |
start_sample = librosa.time_to_samples(start_time, sr=sample_rate)
|
| 113 |
end_sample = librosa.time_to_samples(end_time, sr=sample_rate)
|
|
|
|
| 114 |
sliced_audio = y[start_sample:end_sample]
|
| 115 |
return (sample_rate, sliced_audio)
|
| 116 |
|
|
|
|
| 127 |
return [], []
|
| 128 |
|
| 129 |
def create_final_pack(selection_list, progress=gr.Progress(track_tqdm=True)):
|
| 130 |
+
if not selection_list: raise gr.Error("No slices have been selected!")
|
|
|
|
|
|
|
| 131 |
progress(0, desc="Preparing final pack...")
|
| 132 |
+
zip_path = "Custom_Loop_Pack.zip"; temp_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
| 133 |
with zipfile.ZipFile(zip_path, 'w') as zf:
|
| 134 |
for i, audio_data in enumerate(selection_list):
|
| 135 |
+
progress(i / len(selection_list), desc=f"Adding slice {i+1}...")
|
| 136 |
sample_rate, y = audio_data
|
| 137 |
filename = os.path.join(temp_dir, f"slice_{i+1:03d}.wav")
|
| 138 |
sf.write(filename, y, sample_rate, subtype='PCM_16')
|
| 139 |
zf.write(filename, os.path.basename(filename))
|
|
|
|
| 140 |
shutil.rmtree(temp_dir)
|
| 141 |
progress(1, desc="Pack Ready!")
|
| 142 |
return zip_path, gr.update(visible=True)
|
| 143 |
|
|
|
|
|
|
|
| 144 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:
|
| 145 |
gr.Markdown("# 🎵 Loop Architect")
|
|
|
|
|
|
|
| 146 |
onset_times_state = gr.State(value=None)
|
| 147 |
active_stem_state = gr.State(value=None)
|
| 148 |
selected_slices_state = gr.State(value=[])
|
|
|
|
| 153 |
audio_input = gr.Audio(type="filepath", label="Upload a Track")
|
| 154 |
stem_options = gr.Radio(["4 Stems (Vocals, Drums, Bass, Other)", "2 Stems (Vocals + Instrumental)"], label="Separation Type", value="4 Stems (Vocals, Drums, Bass, Other)")
|
| 155 |
submit_button = gr.Button("Separate Stems")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
with gr.Column(scale=2):
|
| 158 |
with gr.Accordion("Separated Stems", open=True):
|
|
|
|
| 164 |
slice_drums_btn = gr.Button("Visualize Slices", scale=1)
|
| 165 |
with gr.Row():
|
| 166 |
bass_output = gr.Audio(label="Bass", scale=4)
|
| 167 |
+
slice_bass_btn = gr.Button("Visualize Slices", scale=1)
|
| 168 |
with gr.Row():
|
| 169 |
other_output = gr.Audio(label="Other / Instrumental", scale=4)
|
| 170 |
+
slice_other_btn = gr.Button("Visualize Slices", scale=1)
|
| 171 |
|
| 172 |
gr.Markdown("### Slice Editor")
|
| 173 |
slice_plot = gr.Plot(label="Click a region on the waveform to preview a slice")
|
|
|
|
| 182 |
selected_gallery = gr.Gallery(label="Selected Slices", columns=8, object_fit="contain", height="auto")
|
| 183 |
download_zip_file = gr.File(label="Download Your Custom Pack", visible=False)
|
| 184 |
|
|
|
|
| 185 |
submit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output])
|
| 186 |
stem_options.change(fn=update_output_visibility, inputs=stem_options, outputs=[vocals_output, drums_output, bass_output, other_output])
|
| 187 |
|
|
|
|
| 190 |
slice_bass_btn.click(fn=visualize_slices, inputs=bass_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 191 |
slice_other_btn.click(fn=visualize_slices, inputs=other_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 192 |
|
|
|
|
| 193 |
slice_plot.select(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player)
|
| 194 |
|
| 195 |
add_to_pack_btn.click(fn=add_slice_to_pack, inputs=[preview_player, selected_slices_state], outputs=[selected_slices_state, selected_gallery])
|
| 196 |
clear_selection_btn.click(fn=clear_selection, outputs=[selected_slices_state, selected_gallery])
|
| 197 |
create_pack_btn.click(fn=create_final_pack, inputs=selected_slices_state, outputs=[download_zip_file, download_zip_file])
|
| 198 |
|
| 199 |
+
demo.launch()
|
|
|
|
|
|
|
|
|