SaltProphet commited on
Commit
f6e6833
·
verified ·
1 Parent(s): b8e59cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -45
app.py CHANGED
@@ -43,7 +43,7 @@ async def separate_stems(audio_file_path, stem_choice, progress=gr.Progress(trac
43
  if os.path.exists(output_dir): shutil.rmtree(output_dir)
44
 
45
  command = f"python3 -m demucs {model_arg} -o \"{output_dir}\" \"{stable_input_path}\""
46
- progress(0.2, desc="Running Demucs (this may take a minute)...")
47
 
48
  process = await asyncio.create_subprocess_shell(
49
  command,
@@ -114,38 +114,10 @@ def preview_slice(active_stem_audio, onset_times, evt: gr.SelectData):
114
  sliced_audio = y[start_sample:end_sample]
115
  return (sample_rate, sliced_audio)
116
 
117
- def add_slice_to_pack(current_preview, selection_list):
118
- if current_preview is None:
119
- gr.Warning("No slice is being previewed to add.")
120
- return selection_list, selection_list
121
- selection_list.append(current_preview)
122
- gr.Info(f"Slice added! You now have {len(selection_list)} slices in your pack.")
123
- return selection_list, selection_list
124
-
125
- def clear_selection():
126
- gr.Info("Selection cleared.")
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=[])
149
 
150
  with gr.Row():
151
  with gr.Column(scale=1):
@@ -171,16 +143,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red"))
171
 
172
  gr.Markdown("### Slice Editor")
173
  slice_plot = gr.Plot(label="Click a region on the waveform to preview a slice")
174
- with gr.Row():
175
- preview_player = gr.Audio(label="Slice Preview", scale=3)
176
- add_to_pack_btn = gr.Button("Add to Pack", variant="primary", scale=1)
177
-
178
- gr.Markdown("### Your Custom Pack")
179
- with gr.Row():
180
- create_pack_btn = gr.Button("Create Pack from Selection", variant="primary")
181
- clear_selection_btn = gr.Button("Clear Selection")
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])
@@ -190,12 +153,6 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red"))
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
- # --- THIS IS THE FINAL CORRECTION ---
194
- # The event to get click coordinates on a Plot is .select()
195
  slice_plot.select(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player)
196
-
197
- add_to_pack_btn.click(fn=add_slice_to_pack, inputs=[preview_player, selected_slices_state], outputs=[selected_slices_state, selected_gallery])
198
- clear_selection_btn.click(fn=clear_selection, outputs=[selected_slices_state, selected_gallery])
199
- create_pack_btn.click(fn=create_final_pack, inputs=selected_slices_state, outputs=[download_zip_file, download_zip_file])
200
 
201
  demo.launch()
 
43
  if os.path.exists(output_dir): shutil.rmtree(output_dir)
44
 
45
  command = f"python3 -m demucs {model_arg} -o \"{output_dir}\" \"{stable_input_path}\""
46
+ progress(0.2, desc="Running Demucs (this can take a minute)...")
47
 
48
  process = await asyncio.create_subprocess_shell(
49
  command,
 
114
  sliced_audio = y[start_sample:end_sample]
115
  return (sample_rate, sliced_audio)
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:
118
  gr.Markdown("# 🎵 Loop Architect")
119
  onset_times_state = gr.State(value=None)
120
  active_stem_state = gr.State(value=None)
 
121
 
122
  with gr.Row():
123
  with gr.Column(scale=1):
 
143
 
144
  gr.Markdown("### Slice Editor")
145
  slice_plot = gr.Plot(label="Click a region on the waveform to preview a slice")
146
+ preview_player = gr.Audio(label="Slice Preview")
 
 
 
 
 
 
 
 
 
147
 
148
  submit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output])
149
  stem_options.change(fn=update_output_visibility, inputs=stem_options, outputs=[vocals_output, drums_output, bass_output, other_output])
 
153
  slice_bass_btn.click(fn=visualize_slices, inputs=bass_output, outputs=[slice_plot, onset_times_state, active_stem_state])
154
  slice_other_btn.click(fn=visualize_slices, inputs=other_output, outputs=[slice_plot, onset_times_state, active_stem_state])
155
 
 
 
156
  slice_plot.select(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player)
 
 
 
 
157
 
158
  demo.launch()