NeoPy commited on
Commit
73403a8
·
verified ·
1 Parent(s): f1fff8d

Update demo.py

Browse files
Files changed (1) hide show
  1. demo.py +169 -122
demo.py CHANGED
@@ -1,14 +1,20 @@
1
  from original import *
2
  import shutil, glob
3
  from easyfuncs import download_from_url, CachedModels
4
- os.makedirs("dataset",exist_ok=True)
 
 
5
  model_library = CachedModels()
6
 
7
- with gr.Blocks(title="🔊",theme=gr.themes.Base(primary_hue="blue",neutral_hue="zinc")) as app:
 
 
 
 
8
  with gr.Tabs():
9
- with gr.TabItem("Inference"):
10
  with gr.Row():
11
- voice_model = gr.Dropdown(label="Model Voice", choices=sorted(names), value=lambda:sorted(names)[0] if len(sorted(names)) > 0 else '', interactive=True)
12
  refresh_button = gr.Button("Refresh", variant="primary")
13
  spk_item = gr.Slider(
14
  minimum=0,
@@ -27,39 +33,48 @@ with gr.Blocks(title="🔊",theme=gr.themes.Base(primary_hue="blue",neutral_hue=
27
  with gr.Row():
28
  with gr.Column():
29
  with gr.Row():
30
- dropbox = gr.Audio(label="Drop your audio here & hit the Reload button.")
 
31
  with gr.Row():
32
- record_button=gr.Audio(sources="microphone", label="OR Record audio.", type="filepath")
33
  with gr.Row():
34
- paths_for_files = lambda path:[os.path.abspath(os.path.join(path, f)) for f in os.listdir(path) if os.path.splitext(f)[1].lower() in ('.mp3', '.wav', '.flac', '.ogg')]
35
  input_audio0 = gr.Dropdown(
36
  label="Input Path",
37
- value=paths_for_files('audios')[0] if len(paths_for_files('audios')) > 0 else '',
38
- choices=paths_for_files('audios'), # Only show absolute paths for audio files ending in .mp3, .wav, .flac or .ogg
39
  allow_custom_value=True
40
  )
41
  with gr.Row():
42
  audio_player = gr.Audio()
 
 
43
  input_audio0.change(
44
  inputs=[input_audio0],
45
  outputs=[audio_player],
46
- fn=lambda path: {"value":path,"__type__":"update"} if os.path.exists(path) else None
47
  )
48
- record_button.stop_recording(
49
- fn=lambda audio:audio, #TODO save wav lambda
 
 
50
  inputs=[record_button],
51
- outputs=[input_audio0])
 
 
 
52
  dropbox.upload(
53
- fn=lambda audio:audio.name,
54
  inputs=[dropbox],
55
- outputs=[input_audio0])
 
 
56
  with gr.Column():
57
  with gr.Accordion("Change Index", open=False):
58
  file_index2 = gr.Dropdown(
59
  label="Change Index",
60
- choices=sorted(index_paths),
61
  interactive=True,
62
- value=sorted(index_paths)[0] if len(sorted(index_paths)) > 0 else ''
63
  )
64
  index_rate1 = gr.Slider(
65
  minimum=0,
@@ -110,32 +125,42 @@ with gr.Blocks(title="🔊",theme=gr.themes.Base(primary_hue="blue",neutral_hue=
110
  step=0.01,
111
  interactive=True,
112
  )
113
- if voice_model != None: vc.get_vc(voice_model.value,protect0,protect0)
114
  file_index1 = gr.Textbox(
115
  label="Index Path",
116
  interactive=True,
117
- visible=False#Not used here
118
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  refresh_button.click(
120
- fn=change_choices,
121
  inputs=[],
122
- outputs=[voice_model, file_index2],
123
  api_name="infer_refresh",
124
  )
125
- refresh_button.click(
126
- fn=lambda:{"choices":paths_for_files('audios'),"__type__":"update"}, #TODO check if properly returns a sorted list of audio files in the 'audios' folder that have the extensions '.wav', '.mp3', '.ogg', or '.flac'
127
- inputs=[],
128
- outputs = [input_audio0],
129
- )
130
- refresh_button.click(
131
- fn=lambda:{"value":paths_for_files('audios')[0],"__type__":"update"} if len(paths_for_files('audios')) > 0 else {"value":"","__type__":"update"}, #TODO check if properly returns a sorted list of audio files in the 'audios' folder that have the extensions '.wav', '.mp3', '.ogg', or '.flac'
132
- inputs=[],
133
- outputs = [input_audio0],
134
- )
135
  with gr.Row():
136
  f0_file = gr.File(label="F0 Path", visible=False)
137
  with gr.Row():
138
- vc_output1 = gr.Textbox(label="Information", placeholder="Welcome!",visible=False)
139
  but0.click(
140
  vc.vc_single,
141
  [
@@ -161,28 +186,30 @@ with gr.Blocks(title="🔊",theme=gr.themes.Base(primary_hue="blue",neutral_hue=
161
  outputs=[spk_item, protect0, protect0, file_index2, file_index2],
162
  api_name="infer_change_voice",
163
  )
164
- with gr.TabItem("Download Models"):
 
165
  with gr.Row():
166
- url_input = gr.Textbox(label="URL to model", value="",placeholder="https://...", scale=6)
167
- name_output = gr.Textbox(label="Save as", value="",placeholder="MyModel",scale=2)
168
- url_download = gr.Button(value="Download Model",scale=2)
169
  url_download.click(
170
- inputs=[url_input,name_output],
171
  outputs=[url_input],
172
  fn=download_from_url,
173
  )
174
  with gr.Row():
175
- model_browser = gr.Dropdown(choices=list(model_library.models.keys()),label="OR Search Models (Quality UNKNOWN)",scale=5)
176
- download_from_browser = gr.Button(value="Get",scale=2)
177
  download_from_browser.click(
178
  inputs=[model_browser],
179
  outputs=[model_browser],
180
- fn=lambda model: download_from_url(model_library.models[model],model),
181
  )
182
- with gr.TabItem("Train"):
 
183
  with gr.Row():
184
  with gr.Column():
185
- training_name = gr.Textbox(label="Name your model", value="My-Voice",placeholder="My-Voice")
186
  np7 = gr.Slider(
187
  minimum=0,
188
  maximum=config.n_cpu,
@@ -215,14 +242,17 @@ with gr.Blocks(title="🔊",theme=gr.themes.Base(primary_hue="blue",neutral_hue=
215
  dataset_folder = gr.Textbox(
216
  label="dataset folder", value='dataset'
217
  )
218
- easy_uploader = gr.Files(label="Drop your audio files here",file_types=['audio'])
 
219
  but1 = gr.Button("1. Process", variant="primary")
220
- info1 = gr.Textbox(label="Information", value="",visible=True)
221
- easy_uploader.upload(inputs=[dataset_folder],outputs=[],fn=lambda folder:os.makedirs(folder,exist_ok=True))
 
222
  easy_uploader.upload(
223
- 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'),
224
  inputs=[easy_uploader, dataset_folder],
225
- outputs=[])
 
226
  gpus6 = gr.Textbox(
227
  label="Enter the GPU numbers to use separated by -, (e.g. 0-1-2)",
228
  value=gpus,
@@ -247,6 +277,7 @@ with gr.Blocks(title="🔊",theme=gr.themes.Base(primary_hue="blue",neutral_hue=
247
  [info1],
248
  api_name="train_preprocess",
249
  )
 
250
  with gr.Column():
251
  f0method8 = gr.Radio(
252
  label="F0 extraction method",
@@ -281,6 +312,7 @@ with gr.Blocks(title="🔊",theme=gr.themes.Base(primary_hue="blue",neutral_hue=
281
  [info2],
282
  api_name="train_extract_f0_feature",
283
  )
 
284
  with gr.Column():
285
  total_epoch11 = gr.Slider(
286
  minimum=2,
@@ -336,102 +368,117 @@ with gr.Blocks(title="🔊",theme=gr.themes.Base(primary_hue="blue",neutral_hue=
336
  interactive=True,
337
  )
338
  with gr.Accordion(label="Change pretrains", open=False):
339
- pretrained = lambda sr, letter: [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]
 
 
 
340
  pretrained_G14 = gr.Dropdown(
341
  label="pretrained G",
342
- # Get a list of all pretrained G model files in assets/pretrained_v2 that end with .pth
343
- choices = pretrained(sr2.value, 'G'),
344
- value=pretrained(sr2.value, 'G')[0] if len(pretrained(sr2.value, 'G')) > 0 else '',
345
  interactive=True,
346
  visible=True
347
  )
348
  pretrained_D15 = gr.Dropdown(
349
  label="pretrained D",
350
- choices = pretrained(sr2.value, 'D'),
351
- value= pretrained(sr2.value, 'D')[0] if len(pretrained(sr2.value, 'G')) > 0 else '',
352
  visible=True,
353
  interactive=True
354
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
355
  with gr.Row():
356
  download_model = gr.Button('5.Download Model')
357
  with gr.Row():
358
- model_files = gr.Files(label='Your Model and Index file can be downloaded here:')
 
359
  download_model.click(
360
  fn=lambda name: os.listdir(f'assets/weights/{name}') + glob.glob(f'logs/{name.split(".")[0]}/added_*.index'),
361
  inputs=[training_name],
362
- outputs=[model_files, info3])
363
- with gr.Row():
364
- sr2.change(
365
- change_sr2,
366
- [sr2, if_f0_3, version19],
367
- [pretrained_G14, pretrained_D15],
368
- )
369
- version19.change(
370
- change_version19,
371
- [sr2, if_f0_3, version19],
372
- [pretrained_G14, pretrained_D15, sr2],
373
- )
374
- if_f0_3.change(
375
- change_f0,
376
- [if_f0_3, sr2, version19],
377
- [f0method8, pretrained_G14, pretrained_D15],
378
- )
379
- with gr.Row():
380
- but5 = gr.Button("1 Click Training", variant="primary", visible=False)
381
- but3.click(
382
- click_train,
383
- [
384
- training_name,
385
- sr2,
386
- if_f0_3,
387
- spk_id5,
388
- save_epoch10,
389
- total_epoch11,
390
- batch_size12,
391
- if_save_latest13,
392
- pretrained_G14,
393
- pretrained_D15,
394
- gpus16,
395
- if_cache_gpu17,
396
- if_save_every_weights18,
397
- version19,
398
- ],
399
- info3,
400
- api_name="train_start",
401
- )
402
- but4.click(train_index, [training_name, version19], info3)
403
- but5.click(
404
- train1key,
405
- [
406
- training_name,
407
- sr2,
408
- if_f0_3,
409
- dataset_folder,
410
- spk_id5,
411
- np7,
412
- f0method8,
413
- save_epoch10,
414
- total_epoch11,
415
- batch_size12,
416
- if_save_latest13,
417
- pretrained_G14,
418
- pretrained_D15,
419
- gpus16,
420
- if_cache_gpu17,
421
- if_save_every_weights18,
422
- version19,
423
- gpus_rmvpe,
424
- ],
425
- info3,
426
- api_name="train_start_all",
427
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
 
429
  if config.iscolab:
430
- app.queue().launch(share=True)
431
  else:
432
- app.queue().launch(
433
  server_name="0.0.0.0",
434
  inbrowser=not config.noautoopen,
435
  server_port=config.listen_port,
436
  quiet=True,
437
- )
 
 
1
  from original import *
2
  import shutil, glob
3
  from easyfuncs import download_from_url, CachedModels
4
+ import os
5
+
6
+ os.makedirs("dataset", exist_ok=True)
7
  model_library = CachedModels()
8
 
9
+ # Helper moved outside to avoid lambda issues in UI definition
10
+ def get_audio_paths(path):
11
+ return [os.path.abspath(os.path.join(path, f)) for f in os.listdir(path) if os.path.splitext(f)[1].lower() in ('.mp3', '.wav', '.flac', '.ogg')]
12
+
13
+ with gr.Blocks(title="🔊", theme=gr.themes.Base(primary_hue="blue", neutral_hue="zinc")) as app:
14
  with gr.Tabs():
15
+ with gr.Tab("Inference"):
16
  with gr.Row():
17
+ voice_model = gr.Dropdown(label="Model Voice", choices=[], value="", interactive=True)
18
  refresh_button = gr.Button("Refresh", variant="primary")
19
  spk_item = gr.Slider(
20
  minimum=0,
 
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():
 
41
  input_audio0 = gr.Dropdown(
42
  label="Input Path",
43
+ value="",
44
+ choices=[],
45
  allow_custom_value=True
46
  )
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
+ )
70
+
71
  with gr.Column():
72
  with gr.Accordion("Change Index", open=False):
73
  file_index2 = gr.Dropdown(
74
  label="Change Index",
75
+ choices=[],
76
  interactive=True,
77
+ value=""
78
  )
79
  index_rate1 = gr.Slider(
80
  minimum=0,
 
125
  step=0.01,
126
  interactive=True,
127
  )
128
+
129
  file_index1 = gr.Textbox(
130
  label="Index Path",
131
  interactive=True,
132
+ visible=False
133
  )
134
+
135
+ # Consolidated refresh logic
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
+ default_audio = audio_paths[0] if audio_paths else ""
144
+ default_model = model_choices[0] if model_choices else ""
145
+ default_index = index_choices[0] if index_choices else ""
146
+
147
+ return (
148
+ gr.update(choices=model_choices, value=default_model), # voice_model
149
+ gr.update(choices=index_choices, value=default_index), # file_index2
150
+ gr.update(choices=audio_paths, value=default_audio) # input_audio0
151
+ )
152
+
153
  refresh_button.click(
154
+ fn=refresh_ui,
155
  inputs=[],
156
+ outputs=[voice_model, file_index2, input_audio0],
157
  api_name="infer_refresh",
158
  )
159
+
 
 
 
 
 
 
 
 
 
160
  with gr.Row():
161
  f0_file = gr.File(label="F0 Path", visible=False)
162
  with gr.Row():
163
+ vc_output1 = gr.Textbox(label="Information", placeholder="Welcome!", visible=False)
164
  but0.click(
165
  vc.vc_single,
166
  [
 
186
  outputs=[spk_item, protect0, protect0, file_index2, file_index2],
187
  api_name="infer_change_voice",
188
  )
189
+
190
+ with gr.Tab("Download Models"):
191
  with gr.Row():
192
+ url_input = gr.Textbox(label="URL to model", value="", placeholder="https://...", scale=6)
193
+ name_output = gr.Textbox(label="Save as", value="", placeholder="MyModel", scale=2)
194
+ url_download = gr.Button(value="Download Model", scale=2)
195
  url_download.click(
196
+ inputs=[url_input, name_output],
197
  outputs=[url_input],
198
  fn=download_from_url,
199
  )
200
  with gr.Row():
201
+ model_browser = gr.Dropdown(choices=list(model_library.models.keys()), label="OR Search Models (Quality UNKNOWN)", scale=5)
202
+ download_from_browser = gr.Button(value="Get", scale=2)
203
  download_from_browser.click(
204
  inputs=[model_browser],
205
  outputs=[model_browser],
206
+ fn=lambda model: download_from_url(model_library.models[model], model),
207
  )
208
+
209
+ with gr.Tab("Train"):
210
  with gr.Row():
211
  with gr.Column():
212
+ training_name = gr.Textbox(label="Name your model", value="My-Voice", placeholder="My-Voice")
213
  np7 = gr.Slider(
214
  minimum=0,
215
  maximum=config.n_cpu,
 
242
  dataset_folder = gr.Textbox(
243
  label="dataset folder", value='dataset'
244
  )
245
+ # Replaced gr.Files with gr.File(file_count="multiple")
246
+ easy_uploader = gr.File(label="Drop your audio files here", file_count="multiple", file_types=["audio"])
247
  but1 = gr.Button("1. Process", variant="primary")
248
+ info1 = gr.Textbox(label="Information", value="", visible=True)
249
+
250
+ easy_uploader.upload(inputs=[dataset_folder], outputs=[], fn=lambda folder: os.makedirs(folder, exist_ok=True))
251
  easy_uploader.upload(
252
+ 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'),
253
  inputs=[easy_uploader, dataset_folder],
254
+ outputs=[]
255
+ )
256
  gpus6 = gr.Textbox(
257
  label="Enter the GPU numbers to use separated by -, (e.g. 0-1-2)",
258
  value=gpus,
 
277
  [info1],
278
  api_name="train_preprocess",
279
  )
280
+
281
  with gr.Column():
282
  f0method8 = gr.Radio(
283
  label="F0 extraction method",
 
312
  [info2],
313
  api_name="train_extract_f0_feature",
314
  )
315
+
316
  with gr.Column():
317
  total_epoch11 = gr.Slider(
318
  minimum=2,
 
368
  interactive=True,
369
  )
370
  with gr.Accordion(label="Change pretrains", open=False):
371
+ # Replaced lambda in value definition
372
+ def get_pretrained_choices(sr, letter):
373
+ 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]
374
+
375
  pretrained_G14 = gr.Dropdown(
376
  label="pretrained G",
377
+ choices=[],
378
+ value="",
 
379
  interactive=True,
380
  visible=True
381
  )
382
  pretrained_D15 = gr.Dropdown(
383
  label="pretrained D",
384
+ choices=[],
385
+ value="",
386
  visible=True,
387
  interactive=True
388
  )
389
+
390
+ def update_pretrained_dropdowns(sr, f0, ver):
391
+ g_choices = get_pretrained_choices(sr, 'G')
392
+ d_choices = get_pretrained_choices(sr, 'D')
393
+ return (
394
+ gr.update(choices=g_choices, value=g_choices[0] if g_choices else ""),
395
+ gr.update(choices=d_choices, value=d_choices[0] if d_choices else "")
396
+ )
397
+
398
+ # Bind update function to changes in sr2 or version19
399
+ sr2.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
400
+ version19.change(fn=update_pretrained_dropdowns, inputs=[sr2, if_f0_3, version19], outputs=[pretrained_G14, pretrained_D15])
401
+
402
  with gr.Row():
403
  download_model = gr.Button('5.Download Model')
404
  with gr.Row():
405
+ # Replaced gr.Files with gr.File
406
+ model_files = gr.File(label='Your Model and Index file can be downloaded here:', file_count="multiple")
407
  download_model.click(
408
  fn=lambda name: os.listdir(f'assets/weights/{name}') + glob.glob(f'logs/{name.split(".")[0]}/added_*.index'),
409
  inputs=[training_name],
410
+ outputs=[model_files, info3]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
411
  )
412
+
413
+ if_f0_3.change(
414
+ change_f0,
415
+ [if_f0_3, sr2, version19],
416
+ [f0method8, pretrained_G14, pretrained_D15],
417
+ )
418
+
419
+ but5 = gr.Button("1 Click Training", variant="primary", visible=False)
420
+ but3.click(
421
+ click_train,
422
+ [
423
+ training_name,
424
+ sr2,
425
+ if_f0_3,
426
+ spk_id5,
427
+ save_epoch10,
428
+ total_epoch11,
429
+ batch_size12,
430
+ if_save_latest13,
431
+ pretrained_G14,
432
+ pretrained_D15,
433
+ gpus16,
434
+ if_cache_gpu17,
435
+ if_save_every_weights18,
436
+ version19,
437
+ ],
438
+ info3,
439
+ api_name="train_start",
440
+ )
441
+ but4.click(train_index, [training_name, version19], info3)
442
+ but5.click(
443
+ train1key,
444
+ [
445
+ training_name,
446
+ sr2,
447
+ if_f0_3,
448
+ dataset_folder,
449
+ spk_id5,
450
+ np7,
451
+ f0method8,
452
+ save_epoch10,
453
+ total_epoch11,
454
+ batch_size12,
455
+ if_save_latest13,
456
+ pretrained_G14,
457
+ pretrained_D15,
458
+ gpus16,
459
+ if_cache_gpu17,
460
+ if_save_every_weights18,
461
+ version19,
462
+ gpus_rmvpe,
463
+ ],
464
+ info3,
465
+ api_name="train_start_all",
466
+ )
467
+
468
+ # Populate UI on load instead of using lambdas in value
469
+ app.load(
470
+ fn=refresh_ui,
471
+ inputs=[],
472
+ outputs=[voice_model, file_index2, input_audio0]
473
+ )
474
 
475
  if config.iscolab:
476
+ app.launch(share=True, queue=True)
477
  else:
478
+ app.launch(
479
  server_name="0.0.0.0",
480
  inbrowser=not config.noautoopen,
481
  server_port=config.listen_port,
482
  quiet=True,
483
+ queue=True
484
+ )