Spaces:
Build error
Build error
Update app.py
Browse files42 unmodified lines434445464647484964 unmodified lines11411511611711811912012112212312412512612712812913013113213313413513613713813914014114214311711811912014812112212319 unmodified lines1431441451741751461761771781791801811821831471481493 unmodified lines15315415519319415619619719819915715842 unmodified lines if os.path.exists(output_dir): shutil.rmtree(output_dir) command = f"python3 -m demucs {model_arg} -o \"{output_dir}\" \"{stable_input_path}\"" progress(0.2, desc="Running Demucs (this may take a minute)...") progress(0.2, desc="Running Demucs (this can take a minute)...") process = await asyncio.create_subprocess_shell( command,64 unmodified lines sliced_audio = y[start_sample:end_sample] return (sample_rate, sliced_audio)
def add_slice_to_pack(current_preview, selection_list): if current_preview is None: gr.Warning("No slice is being previewed to add.") return selection_list, selection_list selection_list.append(current_preview) gr.Info(f"Slice added! You now have {len(selection_list)} slices in your pack.") return selection_list, selection_list
def clear_selection(): gr.Info("Selection cleared.") return [], []
def create_final_pack(selection_list, progress=gr.Progress(track_tqdm=True)): if not selection_list: raise gr.Error("No slices have been selected!") progress(0, desc="Preparing final pack...") zip_path = "Custom_Loop_Pack.zip"; temp_dir = tempfile.mkdtemp() with zipfile.ZipFile(zip_path, 'w') as zf: for i, audio_data in enumerate(selection_list): progress(i / len(selection_list), desc=f"Adding slice {i+1}...") sample_rate, y = audio_data filename = os.path.join(temp_dir, f"slice_{i+1:03d}.wav") sf.write(filename, y, sample_rate, subtype='PCM_16') zf.write(filename, os.path.basename(filename)) shutil.rmtree(temp_dir) progress(1, desc="Pack Ready!") return zip_path, gr.update(visible=True)
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo: gr.Markdown("# 🎵 Loop Architect") onset_times_state = gr.State(value=None) active_stem_state = gr.State(value=None) selected_slices_state = gr.State(value=[]) with gr.Row(): with gr.Column(scale=1):19 unmodified lines gr.Markdown("### Slice Editor") slice_plot = gr.Plot(label="Click a region on the waveform to preview a slice") with gr.Row(): preview_player = gr.Audio(label="Slice Preview", scale=3) preview_player = gr.Audio(label="Slice Preview") add_to_pack_btn = gr.Button("Add to Pack", variant="primary", scale=1) gr.Markdown("### Your Custom Pack") with gr.Row(): create_pack_btn = gr.Button("Create Pack from Selection", variant="primary") clear_selection_btn = gr.Button("Clear Selection") selected_gallery = gr.Gallery(label="Selected Slices", columns=8, object_fit="contain", height="auto") download_zip_file = gr.File(label="Download Your Custom Pack", visible=False)
submit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output]) stem_options.change(fn=update_output_visibility, inputs=stem_options, outputs=[vocals_output, drums_output, bass_output, other_output])3 unmodified lines slice_bass_btn.click(fn=visualize_slices, inputs=bass_output, outputs=[slice_plot, onset_times_state, active_stem_state]) slice_other_btn.click(fn=visualize_slices, inputs=other_output, outputs=[slice_plot, onset_times_state, active_stem_state]) # --- THIS IS THE FINAL CORRECTION --- # The event to get click coordinates on a Plot is .select() slice_plot.select(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player) add_to_pack_btn.click(fn=add_slice_to_pack, inputs=[preview_player, selected_slices_state], outputs=[selected_slices_state, selected_gallery]) clear_selection_btn.click(fn=clear_selection, outputs=[selected_slices_state, selected_gallery]) create_pack_btn.click(fn=create_final_pack, inputs=selected_slices_state, outputs=[download_zip_file, download_zip_file])
demo.launch()app.py+2-45