Politrees commited on
Commit
4abdf78
1 Parent(s): 75d3f3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -50
app.py CHANGED
@@ -111,21 +111,6 @@ def print_message(input_file, model_name):
111
  print("Separation Model:", model_name)
112
  print("Audio Separation Process...")
113
 
114
- def rename_stems(input_file, output_dir, stems, output_format):
115
- """Rename stems to the format of the input file name with __(StemX) suffix."""
116
- base_name = os.path.splitext(os.path.basename(input_file))[0]
117
- renamed_stems = []
118
- for i, stem in enumerate(stems):
119
- new_name = f"{base_name}_(Stem{i+1}).{output_format}"
120
- new_path = os.path.join(output_dir, new_name)
121
- try:
122
- print("Separation result:", new_name)
123
- os.rename(os.path.join(output_dir, stem), new_path)
124
- renamed_stems.append(new_path)
125
- except Exception as e:
126
- logging.error(f"Failed to rename stem {stem}: {e}")
127
- return renamed_stems
128
-
129
  def prepare_output_dir(input_file, output_dir):
130
  """Create a directory for the output files and clean it if it already exists."""
131
  base_name = os.path.splitext(os.path.basename(input_file))[0]
@@ -135,12 +120,12 @@ def prepare_output_dir(input_file, output_dir):
135
  shutil.rmtree(out_dir)
136
  os.makedirs(out_dir)
137
  except Exception as e:
138
- logging.error(f"Failed to prepare output directory {out_dir}: {e}")
139
- raise
140
  return out_dir
141
 
142
  def roformer_separator(audio, model_key, seg_size, override_seg_size, overlap, pitch_shift, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
143
  """Separate audio using Roformer model."""
 
144
  print_message(audio, model_key)
145
  model = ROFORMER_MODELS[model_key]
146
  try:
@@ -156,7 +141,7 @@ def roformer_separator(audio, model_key, seg_size, override_seg_size, overlap, p
156
  mdxc_params={
157
  "batch_size": 1,
158
  "segment_size": seg_size,
159
- "override_model_segment_size": False,
160
  "overlap": overlap,
161
  "pitch_shift": pitch_shift,
162
  }
@@ -166,18 +151,17 @@ def roformer_separator(audio, model_key, seg_size, override_seg_size, overlap, p
166
  separator.load_model(model_filename=model)
167
 
168
  progress(0.7, desc="Audio separated...")
169
- separation = separator.separate(audio)
170
-
171
- progress(0.9, desc="Stems renamed...")
172
- stems = rename_stems(audio, out_dir, separation, out_format)
173
 
 
174
  return stems[0], stems[1]
175
  except Exception as e:
176
- logging.error(f"Roformer separation failed: {e}")
177
- return None, None
178
 
179
  def mdx23c_separator(audio, model, seg_size, override_seg_size, overlap, pitch_shift, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
180
  """Separate audio using MDX23C model."""
 
181
  print_message(audio, model)
182
  try:
183
  out_dir = prepare_output_dir(audio, out_dir)
@@ -202,18 +186,17 @@ def mdx23c_separator(audio, model, seg_size, override_seg_size, overlap, pitch_s
202
  separator.load_model(model_filename=model)
203
 
204
  progress(0.7, desc="Audio separated...")
205
- separation = separator.separate(audio)
206
-
207
- progress(0.9, desc="Stems renamed...")
208
- stems = rename_stems(audio, out_dir, separation, out_format)
209
 
 
210
  return stems[0], stems[1]
211
  except Exception as e:
212
- logging.error(f"MDX23C separation failed: {e}")
213
- return None, None
214
 
215
  def mdx_separator(audio, model, hop_length, seg_size, overlap, denoise, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
216
  """Separate audio using MDX-NET model."""
 
217
  print_message(audio, model)
218
  try:
219
  out_dir = prepare_output_dir(audio, out_dir)
@@ -238,18 +221,17 @@ def mdx_separator(audio, model, hop_length, seg_size, overlap, denoise, model_di
238
  separator.load_model(model_filename=model)
239
 
240
  progress(0.7, desc="Audio separated...")
241
- separation = separator.separate(audio)
242
-
243
- progress(0.9, desc="Stems renamed...")
244
- stems = rename_stems(audio, out_dir, separation, out_format)
245
 
 
246
  return stems[0], stems[1]
247
  except Exception as e:
248
- logging.error(f"MDX-NET separation failed: {e}")
249
- return None, None
250
 
251
  def vr_separator(audio, model, window_size, aggression, tta, post_process, post_process_threshold, high_end_process, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
252
  """Separate audio using VR ARCH model."""
 
253
  print_message(audio, model)
254
  try:
255
  out_dir = prepare_output_dir(audio, out_dir)
@@ -276,15 +258,13 @@ def vr_separator(audio, model, window_size, aggression, tta, post_process, post_
276
  separator.load_model(model_filename=model)
277
 
278
  progress(0.7, desc="Audio separated...")
279
- separation = separator.separate(audio)
280
-
281
- progress(0.9, desc="Stems renamed...")
282
- stems = rename_stems(audio, out_dir, separation, out_format)
283
 
 
284
  return stems[0], stems[1]
285
  except Exception as e:
286
- logging.error(f"VR ARCH separation failed: {e}")
287
- return None, None
288
 
289
  def demucs_separator(audio, model, seg_size, shifts, overlap, segments_enabled, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
290
  """Separate audio using Demucs model."""
@@ -312,14 +292,22 @@ def demucs_separator(audio, model, seg_size, shifts, overlap, segments_enabled,
312
 
313
  progress(0.7, desc="Audio separated...")
314
  separation = separator.separate(audio)
315
-
316
- progress(0.9, desc="Stems renamed...")
317
- stems = rename_stems(audio, out_dir, separation, out_format)
318
-
319
- return stems[0], stems[1], stems[2], stems[3]
 
 
 
320
  except Exception as e:
321
- logging.error(f"Demucs separation failed: {e}")
322
- return None, None, None, None
 
 
 
 
 
323
 
324
  with gr.Blocks(
325
  title="🎵 PolUVR - Politrees 🎵",
@@ -432,6 +420,11 @@ with gr.Blocks(
432
  with gr.Row():
433
  demucs_stem3 = gr.Audio(label="Stem 3", type="filepath", interactive=False)
434
  demucs_stem4 = gr.Audio(label="Stem 4", type="filepath", interactive=False)
 
 
 
 
 
435
 
436
  roformer_button.click(
437
  roformer_separator,
@@ -518,7 +511,7 @@ with gr.Blocks(
518
  norm_threshold,
519
  amp_threshold,
520
  ],
521
- outputs=[demucs_stem1, demucs_stem2, demucs_stem3, demucs_stem4],
522
  )
523
 
524
  app.launch(share=True)
 
111
  print("Separation Model:", model_name)
112
  print("Audio Separation Process...")
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  def prepare_output_dir(input_file, output_dir):
115
  """Create a directory for the output files and clean it if it already exists."""
116
  base_name = os.path.splitext(os.path.basename(input_file))[0]
 
120
  shutil.rmtree(out_dir)
121
  os.makedirs(out_dir)
122
  except Exception as e:
123
+ raise RuntimeError(f"Failed to prepare output directory {out_dir}: {e}")
 
124
  return out_dir
125
 
126
  def roformer_separator(audio, model_key, seg_size, override_seg_size, overlap, pitch_shift, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
127
  """Separate audio using Roformer model."""
128
+ base_name = os.path.splitext(os.path.basename(audio))[0]
129
  print_message(audio, model_key)
130
  model = ROFORMER_MODELS[model_key]
131
  try:
 
141
  mdxc_params={
142
  "batch_size": 1,
143
  "segment_size": seg_size,
144
+ "override_model_segment_size": override_seg_size,
145
  "overlap": overlap,
146
  "pitch_shift": pitch_shift,
147
  }
 
151
  separator.load_model(model_filename=model)
152
 
153
  progress(0.7, desc="Audio separated...")
154
+ separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
155
+ print(f"Separation complete!\nResults: {', '.join(separation)}")
 
 
156
 
157
+ stems = [os.path.join(out_dir, file_name) for file_name in separation]
158
  return stems[0], stems[1]
159
  except Exception as e:
160
+ raise RuntimeError(f"Roformer separation failed: {e}") from e
 
161
 
162
  def mdx23c_separator(audio, model, seg_size, override_seg_size, overlap, pitch_shift, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
163
  """Separate audio using MDX23C model."""
164
+ base_name = os.path.splitext(os.path.basename(audio))[0]
165
  print_message(audio, model)
166
  try:
167
  out_dir = prepare_output_dir(audio, out_dir)
 
186
  separator.load_model(model_filename=model)
187
 
188
  progress(0.7, desc="Audio separated...")
189
+ separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
190
+ print(f"Separation complete!\nResults: {', '.join(separation)}")
 
 
191
 
192
+ stems = [os.path.join(out_dir, file_name) for file_name in separation]
193
  return stems[0], stems[1]
194
  except Exception as e:
195
+ raise RuntimeError(f"MDX23C separation failed: {e}") from e
 
196
 
197
  def mdx_separator(audio, model, hop_length, seg_size, overlap, denoise, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
198
  """Separate audio using MDX-NET model."""
199
+ base_name = os.path.splitext(os.path.basename(audio))[0]
200
  print_message(audio, model)
201
  try:
202
  out_dir = prepare_output_dir(audio, out_dir)
 
221
  separator.load_model(model_filename=model)
222
 
223
  progress(0.7, desc="Audio separated...")
224
+ separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
225
+ print(f"Separation complete!\nResults: {', '.join(separation)}")
 
 
226
 
227
+ stems = [os.path.join(out_dir, file_name) for file_name in separation]
228
  return stems[0], stems[1]
229
  except Exception as e:
230
+ raise RuntimeError(f"MDX-NET separation failed: {e}") from e
 
231
 
232
  def vr_separator(audio, model, window_size, aggression, tta, post_process, post_process_threshold, high_end_process, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
233
  """Separate audio using VR ARCH model."""
234
+ base_name = os.path.splitext(os.path.basename(audio))[0]
235
  print_message(audio, model)
236
  try:
237
  out_dir = prepare_output_dir(audio, out_dir)
 
258
  separator.load_model(model_filename=model)
259
 
260
  progress(0.7, desc="Audio separated...")
261
+ separation = separator.separate(audio, f"{base_name}_(Stem1)", f"{base_name}_(Stem2)")
262
+ print(f"Separation complete!\nResults: {', '.join(separation)}")
 
 
263
 
264
+ stems = [os.path.join(out_dir, file_name) for file_name in separation]
265
  return stems[0], stems[1]
266
  except Exception as e:
267
+ raise RuntimeError(f"VR ARCH separation failed: {e}") from e
 
268
 
269
  def demucs_separator(audio, model, seg_size, shifts, overlap, segments_enabled, model_dir, out_dir, out_format, norm_thresh, amp_thresh, progress=gr.Progress(track_tqdm=True)):
270
  """Separate audio using Demucs model."""
 
292
 
293
  progress(0.7, desc="Audio separated...")
294
  separation = separator.separate(audio)
295
+ print(f"Separation complete!\nResults: {', '.join(separation)}")
296
+
297
+ stems = [os.path.join(out_dir, file_name) for file_name in separation]
298
+
299
+ if model == "htdemucs_6s.yaml":
300
+ return stems[0], stems[1], stems[2], stems[3], stems[4], stems[5]
301
+ else:
302
+ return stems[0], stems[1], stems[2], stems[3], None, None
303
  except Exception as e:
304
+ raise RuntimeError(f"Demucs separation failed: {e}") from e
305
+
306
+ def update_stems(model):
307
+ if model == "htdemucs_6s.yaml":
308
+ return gr.update(visible=True)
309
+ else:
310
+ return gr.update(visible=False)
311
 
312
  with gr.Blocks(
313
  title="🎵 PolUVR - Politrees 🎵",
 
420
  with gr.Row():
421
  demucs_stem3 = gr.Audio(label="Stem 3", type="filepath", interactive=False)
422
  demucs_stem4 = gr.Audio(label="Stem 4", type="filepath", interactive=False)
423
+ with gr.Row(visible=False) as stem6:
424
+ demucs_stem5 = gr.Audio(label="Stem 5", type="filepath", interactive=False)
425
+ demucs_stem6 = gr.Audio(label="Stem 6", type="filepath", interactive=False)
426
+
427
+ demucs_model.change(update_stems, inputs=[demucs_model], outputs=stem6)
428
 
429
  roformer_button.click(
430
  roformer_separator,
 
511
  norm_threshold,
512
  amp_threshold,
513
  ],
514
+ outputs=[demucs_stem1, demucs_stem2, demucs_stem3, demucs_stem4, demucs_stem5, demucs_stem6],
515
  )
516
 
517
  app.launch(share=True)