Kryptone commited on
Commit
4d41143
·
verified ·
1 Parent(s): 5b9a54e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py CHANGED
@@ -146,6 +146,73 @@ def zip_to_many(zip_upload):
146
  os.rmdir(temp_dir)
147
  return "Done.", output_path
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  with gr.Blocks(theme='ParityError/Interstellar', title="Global Dataset Maker") as app:
150
  gr.HTML(
151
  "<h1> Welcome to the Cafeteria (formally GDMGS)!</h1>"
@@ -183,6 +250,22 @@ with gr.Blocks(theme='ParityError/Interstellar', title="Global Dataset Maker") a
183
  [audiofileuploader, mindur2, maxdur2, name_for_split_files2, strict],
184
  [gr.Text(label="Output"), gr.File(label="Zipped files")]
185
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
  with gr.Tab("Split audio file by duration"):
188
  gr.Markdown("If you have an audio file thats too long for MVSEP (or any other use cases), use this section to split the audio by duration.")
@@ -338,6 +421,7 @@ with gr.Blocks(theme='ParityError/Interstellar', title="Global Dataset Maker") a
338
  )
339
 
340
  with gr.TabItem("Changelog"):
 
341
  gr.Markdown("v1.4 - Added more MVSep and changed default model.")
342
  gr.Markdown("v1.3 - Added more MVSep models.")
343
  gr.Markdown("v1.2 - Added an option (in mp4 to mp3/wav converter) to have the output file be a random filename.")
 
146
  os.rmdir(temp_dir)
147
  return "Done.", output_path
148
 
149
+ def bulk_split_zip_files(zip_upload, mindur2, maxdur2, name_for_split_files2, strict):
150
+ if zip_upload is None:
151
+ raise gr.Error("Zip file cannot be empty!")
152
+ if mindur2 == maxdur2:
153
+ raise gr.Error(f"Cannot split mindur={mindur2} and maxdur={maxdur2}, min and max are the same number.")
154
+ elif mindur2 > maxdur2:
155
+ raise gr.Error(f"Cannot split mindur={mindur2} and maxdur={maxdur2}, mindur is higher than maxdur.")
156
+ elif name_for_split_files2 is None or name_for_split_files2 == "":
157
+ raise gr.Error("Split files name cannot be empty!")
158
+
159
+ temp_dir = "bulk_zip_temp"
160
+ os.makedirs(temp_dir, exist_ok=True)
161
+
162
+ with zipfile.ZipFile(zip_upload, "r") as zip_ref:
163
+ zip_ref.extractall(temp_dir)
164
+
165
+ split_files = []
166
+
167
+ for fname in os.listdir(temp_dir):
168
+ if not fname.lower().endswith((".wav", ".mp3")):
169
+ continue
170
+
171
+ audio_path = os.path.join(temp_dir, fname)
172
+ audio_regions = auditok.split(
173
+ audio_path,
174
+ min_dur=mindur2,
175
+ max_dur=maxdur2,
176
+ max_silence=0.3,
177
+ energy_threshold=45,
178
+ strict_min_dur=True if strict else False
179
+ )
180
+
181
+ base = os.path.splitext(os.path.basename(audio_path))[0]
182
+ for i, r in enumerate(audio_regions):
183
+ out_name = f"{name_for_split_files2}-{base}-{i+1}.wav"
184
+ filename = r.save(out_name)
185
+ split_files.append(filename)
186
+
187
+ if not split_files:
188
+ for root, dirs, files in os.walk(temp_dir, topdown=False):
189
+ for f in files:
190
+ os.remove(os.path.join(root, f))
191
+ for d in dirs:
192
+ os.rmdir(os.path.join(root, d))
193
+ os.rmdir(temp_dir)
194
+ raise gr.Error("No audio segments were created from the provided ZIP.")
195
+
196
+ zip_file_name = "bulk_split_audio_files.zip"
197
+ with zipfile.ZipFile(zip_file_name, "w") as zip_file:
198
+ for fpath in split_files:
199
+ zip_file.write(fpath, os.path.basename(fpath))
200
+
201
+ for fpath in split_files:
202
+ if os.path.exists(fpath):
203
+ os.remove(fpath)
204
+
205
+ for root, dirs, files in os.walk(temp_dir, topdown=False):
206
+ for f in files:
207
+ os.remove(os.path.join(root, f))
208
+ for d in dirs:
209
+ os.rmdir(os.path.join(root, d))
210
+ os.rmdir(temp_dir)
211
+
212
+ os.remove(zip_upload)
213
+
214
+ return f"File split successfully from ZIP! Amount created: {len(split_files)}", zip_file_name
215
+
216
  with gr.Blocks(theme='ParityError/Interstellar', title="Global Dataset Maker") as app:
217
  gr.HTML(
218
  "<h1> Welcome to the Cafeteria (formally GDMGS)!</h1>"
 
250
  [audiofileuploader, mindur2, maxdur2, name_for_split_files2, strict],
251
  [gr.Text(label="Output"), gr.File(label="Zipped files")]
252
  )
253
+ with gr.Tab("Bulk file splitter from ZIP"):
254
+ gr.Markdown("Upload a ZIP containing multiple audio files, and each of them will be split.")
255
+ with gr.Row():
256
+ with gr.Column():
257
+ with gr.Row():
258
+ zip_file_bulk = gr.File(file_count='single', file_types=[".zip"], label="Zip file")
259
+ mindur_bulk = gr.Number(label="Min duration", minimum=1, maximum=10, value=1)
260
+ maxdur_bulk = gr.Number(label="Max duration", minimum=1, maximum=10, value=5)
261
+ name_for_split_audio_files_bulk = gr.Textbox(label="Name prefix for split files")
262
+ strict_bulk = gr.Checkbox(False, label="Enable strict?", info="Same behavior for single file uploading")
263
+ bulk_split_btn = gr.Button("Bulk split", variant='primary')
264
+ bulk_split_btn.click(
265
+ bulk_split_zip_files,
266
+ [zip_file_bulk, mindur_bulk, maxdur_bulk, name_for_split_audio_files_bulk, strict_bulk],
267
+ [gr.Text(label="Output"), gr.File(label="Zipped split files")]
268
+ )
269
 
270
  with gr.Tab("Split audio file by duration"):
271
  gr.Markdown("If you have an audio file thats too long for MVSEP (or any other use cases), use this section to split the audio by duration.")
 
421
  )
422
 
423
  with gr.TabItem("Changelog"):
424
+ gr.Markdown("v1.5 - Added new tool - Bulk split audio files from a zip.")
425
  gr.Markdown("v1.4 - Added more MVSep and changed default model.")
426
  gr.Markdown("v1.3 - Added more MVSep models.")
427
  gr.Markdown("v1.2 - Added an option (in mp4 to mp3/wav converter) to have the output file be a random filename.")