NeoPy commited on
Commit
7979594
·
verified ·
1 Parent(s): 51bfd68

Update demo.py

Browse files
Files changed (1) hide show
  1. demo.py +91 -32
demo.py CHANGED
@@ -33,8 +33,8 @@ with gr.Blocks(title="🔊", theme=gr.themes.Base(primary_hue="blue", neutral_hu
33
  with gr.Row():
34
  with gr.Column():
35
  with gr.Row():
36
- # Sources must be a list in Gradio 4+
37
- dropbox = gr.Audio(label="Drop your audio here & hit the Reload button.", sources=["upload"])
38
  with gr.Row():
39
  record_button = gr.Audio(sources=["microphone"], label="OR Record audio.", type="filepath")
40
  with gr.Row():
@@ -47,23 +47,38 @@ with gr.Blocks(title="🔊", theme=gr.themes.Base(primary_hue="blue", neutral_hu
47
  with gr.Row():
48
  audio_player = gr.Audio()
49
 
50
- # Updated logic for Gradio 6 (using gr.update)
 
 
 
 
 
51
  input_audio0.change(
 
52
  inputs=[input_audio0],
53
- outputs=[audio_player],
54
- fn=lambda path: path if os.path.exists(path) else None
55
  )
56
 
57
- # Replaced stop_recording (deprecated) with change
 
 
 
 
 
58
  record_button.change(
59
- fn=lambda audio: audio,
60
  inputs=[record_button],
61
  outputs=[input_audio0]
62
  )
63
 
64
- # Updated logic assuming audio is path (type="filepath")
65
- dropbox.upload(
66
- fn=lambda audio: audio,
 
 
 
 
 
67
  inputs=[dropbox],
68
  outputs=[input_audio0]
69
  )
@@ -136,23 +151,24 @@ with gr.Blocks(title="🔊", theme=gr.themes.Base(primary_hue="blue", neutral_hu
136
  def refresh_ui():
137
  # Get updated lists
138
  # Assuming change_choices is imported from original or defined elsewhere
139
- # It needs to return (model_choices, index_choices)
140
- model_choices, index_choices = change_choices()
 
 
 
 
141
 
142
  audio_paths = get_audio_paths('audios')
143
 
144
- # Helper to safely get the first item from list or dict
145
- # Fixes KeyError: 0 when change_choices returns a dictionary
146
  def safe_first(data):
147
- if not data: return ""
 
148
  if isinstance(data, dict):
149
- # If it's a dict, Gradio uses values as the actual data/paths
150
  return next(iter(data.values()))
151
- try:
152
- # Assume it's a list or sequence
153
  return data[0]
154
- except (IndexError, KeyError):
155
- return ""
156
 
157
  default_audio = safe_first(audio_paths)
158
  default_model = safe_first(model_choices)
@@ -256,17 +272,39 @@ with gr.Blocks(title="🔊", theme=gr.themes.Base(primary_hue="blue", neutral_hu
256
  dataset_folder = gr.Textbox(
257
  label="dataset folder", value='dataset'
258
  )
259
- # Replaced gr.Files with gr.File(file_count="multiple")
260
  easy_uploader = gr.File(label="Drop your audio files here", file_count="multiple", file_types=["audio"])
261
  but1 = gr.Button("1. Process", variant="primary")
262
  info1 = gr.Textbox(label="Information", value="", visible=True)
263
 
264
- easy_uploader.upload(inputs=[dataset_folder], outputs=[], fn=lambda folder: os.makedirs(folder, exist_ok=True))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  easy_uploader.upload(
266
- fn=lambda files, folder: [shutil.copy2(f.name, os.path.join(folder, os.path.split(f.name)[1])) for f in files] if folder != "" else gr.Warning('Please enter a folder name for your dataset'),
267
  inputs=[easy_uploader, dataset_folder],
268
  outputs=[]
269
  )
 
270
  gpus6 = gr.Textbox(
271
  label="Enter the GPU numbers to use separated by -, (e.g. 0-1-2)",
272
  value=gpus,
@@ -382,9 +420,14 @@ with gr.Blocks(title="🔊", theme=gr.themes.Base(primary_hue="blue", neutral_hu
382
  interactive=True,
383
  )
384
  with gr.Accordion(label="Change pretrains", open=False):
385
- # Replaced lambda in value definition
386
  def get_pretrained_choices(sr, letter):
387
- return [os.path.abspath(os.path.join('assets/pretrained_v2', file)) for file in os.listdir('assets/pretrained_v2') if file.endswith('.pth') and sr in file and letter in file]
 
 
 
 
 
388
 
389
  pretrained_G14 = gr.Dropdown(
390
  label="pretrained G",
@@ -402,24 +445,40 @@ with gr.Blocks(title="🔊", theme=gr.themes.Base(primary_hue="blue", neutral_hu
402
  )
403
 
404
  def update_pretrained_dropdowns(sr, f0, ver):
405
- g_choices = get_pretrained_choices(sr, 'G')
406
- d_choices = get_pretrained_choices(sr, 'D')
 
407
  return (
408
  gr.update(choices=g_choices, value=g_choices[0] if g_choices else ""),
409
  gr.update(choices=d_choices, value=d_choices[0] if d_choices else "")
410
  )
411
 
412
- # Bind update function to changes in sr2 or version19
413
  sr2.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
414
  version19.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
415
 
416
  with gr.Row():
417
  download_model = gr.Button('5.Download Model')
418
  with gr.Row():
419
- # Replaced gr.Files with gr.File
420
- model_files = gr.File(label='Your Model and Index file can be downloaded here:', file_count="multiple")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421
  download_model.click(
422
- fn=lambda name: os.listdir(f'assets/weights/{name}') + glob.glob(f'logs/{name.split(".")[0]}/added_*.index'),
423
  inputs=[training_name],
424
  outputs=[model_files, info3]
425
  )
@@ -479,7 +538,7 @@ with gr.Blocks(title="🔊", theme=gr.themes.Base(primary_hue="blue", neutral_hu
479
  api_name="train_start_all",
480
  )
481
 
482
- # Populate UI on load instead of using lambdas in value
483
  app.load(
484
  fn=refresh_ui,
485
  inputs=[],
 
33
  with gr.Row():
34
  with gr.Column():
35
  with gr.Row():
36
+ # Fixed: Use correct parameter name
37
+ dropbox = gr.Audio(label="Drop your audio here & hit the Reload button.", type="filepath")
38
  with gr.Row():
39
  record_button = gr.Audio(sources=["microphone"], label="OR Record audio.", type="filepath")
40
  with gr.Row():
 
47
  with gr.Row():
48
  audio_player = gr.Audio()
49
 
50
+ # Fixed: Proper function to update audio player
51
+ def update_audio_player(path):
52
+ if path and os.path.exists(path):
53
+ return path
54
+ return None
55
+
56
  input_audio0.change(
57
+ fn=update_audio_player,
58
  inputs=[input_audio0],
59
+ outputs=[audio_player]
 
60
  )
61
 
62
+ # Fixed: Handle record button properly
63
+ def handle_record(audio):
64
+ if audio:
65
+ return audio
66
+ return ""
67
+
68
  record_button.change(
69
+ fn=handle_record,
70
  inputs=[record_button],
71
  outputs=[input_audio0]
72
  )
73
 
74
+ # Fixed: Handle dropbox upload
75
+ def handle_upload(audio):
76
+ if audio:
77
+ return audio
78
+ return ""
79
+
80
+ dropbox.change(
81
+ fn=handle_upload,
82
  inputs=[dropbox],
83
  outputs=[input_audio0]
84
  )
 
151
  def refresh_ui():
152
  # Get updated lists
153
  # Assuming change_choices is imported from original or defined elsewhere
154
+ try:
155
+ model_choices, index_choices = change_choices()
156
+ except:
157
+ # Fallback if change_choices doesn't return two values
158
+ model_choices = []
159
+ index_choices = []
160
 
161
  audio_paths = get_audio_paths('audios')
162
 
163
+ # Helper to safely get the first item
 
164
  def safe_first(data):
165
+ if not data:
166
+ return ""
167
  if isinstance(data, dict):
 
168
  return next(iter(data.values()))
169
+ if isinstance(data, list) and len(data) > 0:
 
170
  return data[0]
171
+ return ""
 
172
 
173
  default_audio = safe_first(audio_paths)
174
  default_model = safe_first(model_choices)
 
272
  dataset_folder = gr.Textbox(
273
  label="dataset folder", value='dataset'
274
  )
275
+ # Fixed: Use correct file handling
276
  easy_uploader = gr.File(label="Drop your audio files here", file_count="multiple", file_types=["audio"])
277
  but1 = gr.Button("1. Process", variant="primary")
278
  info1 = gr.Textbox(label="Information", value="", visible=True)
279
 
280
+ # Fixed: Proper file upload handling
281
+ def handle_file_upload(files, folder):
282
+ if not folder or folder.strip() == "":
283
+ gr.Warning('Please enter a folder name for your dataset')
284
+ return []
285
+ if not os.path.exists(folder):
286
+ os.makedirs(folder, exist_ok=True)
287
+
288
+ saved_files = []
289
+ for file_obj in files:
290
+ if hasattr(file_obj, 'name'): # Handle Gradio file object
291
+ filename = os.path.basename(file_obj.name)
292
+ dest_path = os.path.join(folder, filename)
293
+ shutil.copy2(file_obj.name, dest_path)
294
+ saved_files.append(dest_path)
295
+ elif isinstance(file_obj, str): # Handle string path
296
+ filename = os.path.basename(file_obj)
297
+ dest_path = os.path.join(folder, filename)
298
+ shutil.copy2(file_obj, dest_path)
299
+ saved_files.append(dest_path)
300
+ return []
301
+
302
  easy_uploader.upload(
303
+ fn=handle_file_upload,
304
  inputs=[easy_uploader, dataset_folder],
305
  outputs=[]
306
  )
307
+
308
  gpus6 = gr.Textbox(
309
  label="Enter the GPU numbers to use separated by -, (e.g. 0-1-2)",
310
  value=gpus,
 
420
  interactive=True,
421
  )
422
  with gr.Accordion(label="Change pretrains", open=False):
423
+ # Fixed: Get pretrained choices
424
  def get_pretrained_choices(sr, letter):
425
+ pretrained_dir = 'assets/pretrained_v2'
426
+ if not os.path.exists(pretrained_dir):
427
+ return []
428
+ return [os.path.abspath(os.path.join(pretrained_dir, file))
429
+ for file in os.listdir(pretrained_dir)
430
+ if file.endswith('.pth') and sr in file and letter in file]
431
 
432
  pretrained_G14 = gr.Dropdown(
433
  label="pretrained G",
 
445
  )
446
 
447
  def update_pretrained_dropdowns(sr, f0, ver):
448
+ sr_str = sr if isinstance(sr, str) else str(sr)
449
+ g_choices = get_pretrained_choices(sr_str, 'G')
450
+ d_choices = get_pretrained_choices(sr_str, 'D')
451
  return (
452
  gr.update(choices=g_choices, value=g_choices[0] if g_choices else ""),
453
  gr.update(choices=d_choices, value=d_choices[0] if d_choices else "")
454
  )
455
 
456
+ # Bind update function to changes
457
  sr2.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
458
  version19.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
459
 
460
  with gr.Row():
461
  download_model = gr.Button('5.Download Model')
462
  with gr.Row():
463
+ # Fixed: File handling for model download
464
+ model_files = gr.File(label='Your Model and Index file can be downloaded here:')
465
+
466
+ def download_model_files(name):
467
+ if not name or name.strip() == "":
468
+ return [], "Please enter a model name"
469
+
470
+ model_path = f'assets/weights/{name}'
471
+ index_pattern = f'logs/{name.split(".")[0]}/added_*.index'
472
+
473
+ files = []
474
+ if os.path.exists(model_path):
475
+ files.extend([os.path.join(model_path, f) for f in os.listdir(model_path)])
476
+ files.extend(glob.glob(index_pattern))
477
+
478
+ return files, f"Found {len(files)} files"
479
+
480
  download_model.click(
481
+ fn=download_model_files,
482
  inputs=[training_name],
483
  outputs=[model_files, info3]
484
  )
 
538
  api_name="train_start_all",
539
  )
540
 
541
+ # Populate UI on load
542
  app.load(
543
  fn=refresh_ui,
544
  inputs=[],