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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from original import *
2
+ import shutil, glob
3
+ from easyfuncs import download_from_url, CachedModels, whisperspeak, whisperspeak_on, stereo_process, sr_process
4
+ os.makedirs("dataset",exist_ok=True)
5
+ os.makedirs("audios",exist_ok=True)
6
+ model_library = CachedModels()
7
+
8
+ with gr.Blocks(title="🔊",theme=gr.themes.Base(primary_hue="rose",neutral_hue="zinc")) as app:
9
+ with gr.Row():
10
+ with gr.Column():
11
+ gr.HTML("<img src='file/a.png' alt='image'>")
12
+ with gr.Column():
13
+ gr.HTML("<a href='https://ko-fi.com/rejekts' target='_blank'><img src='file/kofi_button.png' alt='🤝 Support Me'></a>")
14
+ with gr.Tabs():
15
+ with gr.TabItem("Inference"):
16
+ with gr.Row():
17
+ voice_model = gr.Dropdown(label="Model Voice", choices=sorted(names), value=lambda:sorted(names)[0] if len(sorted(names)) > 0 else '', interactive=True)
18
+ refresh_button = gr.Button("Refresh", variant="primary")
19
+ spk_item = gr.Slider(
20
+ minimum=0,
21
+ maximum=2333,
22
+ step=1,
23
+ label="Speaker ID",
24
+ value=0,
25
+ visible=False,
26
+ interactive=True,
27
+ )
28
+ vc_transform0 = gr.Number(
29
+ label="Pitch",
30
+ value=0
31
+ )
32
+ but0 = gr.Button(value="Convert", variant="primary")
33
+ with gr.Row():
34
+ with gr.Column():
35
+ with gr.Tabs():
36
+ with gr.TabItem("Upload"):
37
+ dropbox = gr.File(label="Drop your audio here & hit the Reload button.")
38
+
39
+ with gr.TabItem("TTS (experimental)", visible=False if whisperspeak_on is None else True):
40
+ with gr.Row():
41
+ tts_text = gr.Textbox(label="Text to Speech", placeholder="Enter text to convert to speech")
42
+ with gr.Row():
43
+ tts_lang = gr.Radio(choices=["en","es","it","pt"],label="",value="en")
44
+ with gr.Row():
45
+ tts_button = gr.Button(value="Speak", variant="primary")
46
+ with gr.Row():
47
+ 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')]
48
+ input_audio0 = gr.Dropdown(
49
+ label="Input Path",
50
+ value=paths_for_files('audios')[0] if len(paths_for_files('audios')) > 0 else '',
51
+ choices=paths_for_files('audios'), # Only show absolute paths for audio files ending in .mp3, .wav, .flac or .ogg
52
+ allow_custom_value=True
53
+ )
54
+ with gr.Row():
55
+ input_player = gr.Audio(label="Input",type="numpy")
56
+ input_audio0.change(
57
+ inputs=[input_audio0],
58
+ outputs=[input_player],
59
+ fn=lambda path: {"value":path,"__type__":"update"} if os.path.exists(path) else None
60
+ )
61
+ record_button.stop_recording(
62
+ fn=lambda audio:audio, #TODO save wav lambda
63
+ inputs=[record_button],
64
+ outputs=[input_audio0])
65
+ dropbox.upload(
66
+ fn=lambda audio:audio.name,
67
+ inputs=[dropbox],
68
+ outputs=[input_audio0])
69
+ tts_button.click(
70
+ fn=whisperspeak,
71
+ inputs=[tts_text,tts_lang],
72
+ outputs=[input_audio0],
73
+ show_progress=True)
74
+ tts_button.click(
75
+ fn=lambda: {"choices":paths_for_files('audios'),"__type__":"update"},
76
+ inputs=[],
77
+ outputs=[input_audio0])
78
+ with gr.Column():
79
+ with gr.Accordion("Change Index", open=False):
80
+ file_index2 = gr.Dropdown(
81
+ label="Change Index",
82
+ choices=sorted(index_paths),
83
+ interactive=True,
84
+ value=sorted(index_paths)[0] if len(sorted(index_paths)) > 0 else ''
85
+ )
86
+ index_rate1 = gr.Slider(
87
+ minimum=0,
88
+ maximum=1,
89
+ label="Index Strength",
90
+ value=0.5,
91
+ interactive=True,
92
+ )
93
+ output_player = gr.Audio(label="Output",interactive=False)
94
+ with gr.Accordion("General Settings", open=False):
95
+ f0method0 = gr.Radio(
96
+ label="Method",
97
+ choices=["pm", "harvest", "crepe", "rmvpe"]
98
+ if config.dml == False
99
+ else ["pm", "harvest", "rmvpe"],
100
+ value="rmvpe",
101
+ interactive=True,
102
+ )
103
+ filter_radius0 = gr.Slider(
104
+ minimum=0,
105
+ maximum=7,
106
+ label="Breathiness Reduction (Harvest only)",
107
+ value=3,
108
+ step=1,
109
+ interactive=True,
110
+ )
111
+ resample_sr0 = gr.Slider(
112
+ minimum=0,
113
+ maximum=48000,
114
+ label="Resample",
115
+ value=0,
116
+ step=1,
117
+ interactive=True,
118
+ visible=False
119
+ )
120
+ rms_mix_rate0 = gr.Slider(
121
+ minimum=0,
122
+ maximum=1,
123
+ label="Volume Normalization",
124
+ value=0,
125
+ interactive=True,
126
+ )
127
+ protect0 = gr.Slider(
128
+ minimum=0,
129
+ maximum=0.5,
130
+ label="Breathiness Protection (0 is enabled, 0.5 is disabled)",
131
+ value=0.33,
132
+ step=0.01,
133
+ interactive=True,
134
+ )
135
+ if voice_model != None:
136
+ try: vc.get_vc(voice_model.value,protect0,protect0)
137
+ except: pass
138
+ with gr.Accordion("Processing Tools (Experimental)", open=True):
139
+ audio_choice = gr.Radio(choices=["Input", "Output"], value="Output", label="Source",interactive=True)
140
+ with gr.Column():
141
+ stereo_button = gr.Button(value="Stereo", variant="primary")
142
+ stereo_button.click(
143
+ fn=stereo_process,
144
+ inputs=[input_player,output_player,audio_choice],
145
+ outputs=[output_player],
146
+ preprocess=True,
147
+ )
148
+ with gr.Column():
149
+ sr_button = gr.Button(value="SuperResolution", variant="primary")
150
+ sr_button.click(
151
+ fn=sr_process,
152
+ inputs=[input_player,output_player,audio_choice],
153
+ outputs=[output_player],
154
+ preprocess=True,
155
+ )
156
+ file_index1 = gr.Textbox(
157
+ label="Index Path",
158
+ interactive=True,
159
+ visible=False#Not used here
160
+ )
161
+ refresh_button.click(
162
+ fn=change_choices,
163
+ inputs=[],
164
+ outputs=[voice_model, file_index2],
165
+ api_name="infer_refresh",
166
+ )
167
+ refresh_button.click(
168
+ 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'
169
+ inputs=[],
170
+ outputs = [input_audio0],
171
+ )
172
+ refresh_button.click(
173
+ 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'
174
+ inputs=[],
175
+ outputs = [input_audio0],
176
+ )
177
+ with gr.Row():
178
+ f0_file = gr.File(label="F0 Path", visible=False)
179
+ with gr.Row():
180
+ vc_output1 = gr.Textbox(label="Information", placeholder="Welcome!",visible=False)
181
+ but0.click(
182
+ vc.vc_single,
183
+ [
184
+ spk_item,
185
+ input_audio0,
186
+ vc_transform0,
187
+ f0_file,
188
+ f0method0,
189
+ file_index1,
190
+ file_index2,
191
+ index_rate1,
192
+ filter_radius0,
193
+ resample_sr0,
194
+ rms_mix_rate0,
195
+ protect0,
196
+ ],
197
+ [vc_output1, output_player],
198
+ api_name="infer_convert",
199
+ )
200
+ voice_model.change(
201
+ fn=vc.get_vc,
202
+ inputs=[voice_model, protect0, protect0],
203
+ outputs=[spk_item, protect0, protect0, file_index2, file_index2],
204
+ api_name="infer_change_voice",
205
+ )
206
+ with gr.TabItem("Download Models"):
207
+ with gr.Row():
208
+ url_input = gr.Textbox(label="URL to model", value="",placeholder="https://...", scale=6)
209
+ name_output = gr.Textbox(label="Save as", value="",placeholder="MyModel",scale=2)
210
+ url_download = gr.Button(value="Download Model",scale=2)
211
+ url_download.click(
212
+ inputs=[url_input,name_output],
213
+ outputs=[url_input],
214
+ fn=download_from_url,
215
+ )
216
+ with gr.Row():
217
+ model_browser = gr.Dropdown(choices=list(model_library.models.keys()),label="OR Search Models (Quality UNKNOWN)",scale=5)
218
+ download_from_browser = gr.Button(value="Get",scale=2)
219
+ download_from_browser.click(
220
+ inputs=[model_browser],
221
+ outputs=[model_browser],
222
+ fn=lambda model: download_from_url(model_library.models[model],model),
223
+ )
224
+ with gr.TabItem("Train"):
225
+ with gr.Row():
226
+ with gr.Column():
227
+ training_name = gr.Textbox(label="Name your model", value="My-Voice",placeholder="My-Voice")
228
+ np7 = gr.Slider(
229
+ minimum=0,
230
+ maximum=config.n_cpu,
231
+ step=1,
232
+ label="Number of CPU processes used to extract pitch features",
233
+ value=int(np.ceil(config.n_cpu / 1.5)),
234
+ interactive=True,
235
+ )
236
+ sr2 = gr.Radio(
237
+ label="Sampling Rate",
238
+ choices=["40k", "32k"],
239
+ value="32k",
240
+ interactive=True,
241
+ visible=False
242
+ )
243
+ if_f0_3 = gr.Radio(
244
+ label="Will your model be used for singing? If not, you can ignore this.",
245
+ choices=[True, False],
246
+ value=True,
247
+ interactive=True,
248
+ visible=False
249
+ )
250
+ version19 = gr.Radio(
251
+ label="Version",
252
+ choices=["v1", "v2"],
253
+ value="v2",
254
+ interactive=True,
255
+ visible=False,
256
+ )
257
+ dataset_folder = gr.Textbox(
258
+ label="dataset folder", value='dataset'
259
+ )
260
+ easy_uploader = gr.Files(label="Drop your audio files here",file_types=['audio'])
261
+ but1 = gr.Button("1. Process", variant="primary")
262
+ info1 = gr.Textbox(label="Information", value="",visible=True)
263
+ easy_uploader.upload(inputs=[dataset_folder],outputs=[],fn=lambda folder:os.makedirs(folder,exist_ok=True))
264
+ easy_uploader.upload(
265
+ 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'),
266
+ inputs=[easy_uploader, dataset_folder],
267
+ outputs=[])
268
+ gpus6 = gr.Textbox(
269
+ label="Enter the GPU numbers to use separated by -, (e.g. 0-1-2)",
270
+ value=gpus,
271
+ interactive=True,
272
+ visible=F0GPUVisible,
273
+ )
274
+ gpu_info9 = gr.Textbox(
275
+ label="GPU Info", value=gpu_info, visible=F0GPUVisible
276
+ )
277
+ spk_id5 = gr.Slider(
278
+ minimum=0,
279
+ maximum=4,
280
+ step=1,
281
+ label="Speaker ID",
282
+ value=0,
283
+ interactive=True,
284
+ visible=False
285
+ )
286
+ but1.click(
287
+ preprocess_dataset,
288
+ [dataset_folder, training_name, sr2, np7],
289
+ [info1],
290
+ api_name="train_preprocess",
291
+ )
292
+ with gr.Column():
293
+ f0method8 = gr.Radio(
294
+ label="F0 extraction method",
295
+ choices=["pm", "harvest", "dio", "rmvpe", "rmvpe_gpu"],
296
+ value="rmvpe_gpu",
297
+ interactive=True,
298
+ )
299
+ gpus_rmvpe = gr.Textbox(
300
+ label="GPU numbers to use separated by -, (e.g. 0-1-2)",
301
+ value="%s-%s" % (gpus, gpus),
302
+ interactive=True,
303
+ visible=F0GPUVisible,
304
+ )
305
+ but2 = gr.Button("2. Extract Features", variant="primary")
306
+ info2 = gr.Textbox(label="Information", value="", max_lines=8)
307
+ f0method8.change(
308
+ fn=change_f0_method,
309
+ inputs=[f0method8],
310
+ outputs=[gpus_rmvpe],
311
+ )
312
+ but2.click(
313
+ extract_f0_feature,
314
+ [
315
+ gpus6,
316
+ np7,
317
+ f0method8,
318
+ if_f0_3,
319
+ training_name,
320
+ version19,
321
+ gpus_rmvpe,
322
+ ],
323
+ [info2],
324
+ api_name="train_extract_f0_feature",
325
+ )
326
+ with gr.Column():
327
+ total_epoch11 = gr.Slider(
328
+ minimum=2,
329
+ maximum=1000,
330
+ step=1,
331
+ label="Epochs (more epochs may improve quality but takes longer)",
332
+ value=150,
333
+ interactive=True,
334
+ )
335
+ but4 = gr.Button("3. Train Index", variant="primary")
336
+ but3 = gr.Button("4. Train Model", variant="primary")
337
+ info3 = gr.Textbox(label="Information", value="", max_lines=10)
338
+ with gr.Accordion(label="General Settings", open=False):
339
+ gpus16 = gr.Textbox(
340
+ label="GPUs separated by -, (e.g. 0-1-2)",
341
+ value="0",
342
+ interactive=True,
343
+ visible=True
344
+ )
345
+ save_epoch10 = gr.Slider(
346
+ minimum=1,
347
+ maximum=50,
348
+ step=1,
349
+ label="Weight Saving Frequency",
350
+ value=25,
351
+ interactive=True,
352
+ )
353
+ batch_size12 = gr.Slider(
354
+ minimum=1,
355
+ maximum=40,
356
+ step=1,
357
+ label="Batch Size",
358
+ value=default_batch_size,
359
+ interactive=True,
360
+ )
361
+ if_save_latest13 = gr.Radio(
362
+ label="Only save the latest model",
363
+ choices=["yes", "no"],
364
+ value="yes",
365
+ interactive=True,
366
+ visible=False
367
+ )
368
+ if_cache_gpu17 = gr.Radio(
369
+ label="If your dataset is UNDER 10 minutes, cache it to train faster",
370
+ choices=["yes", "no"],
371
+ value="no",
372
+ interactive=True,
373
+ )
374
+ if_save_every_weights18 = gr.Radio(
375
+ label="Save small model at every save point",
376
+ choices=["yes", "no"],
377
+ value="yes",
378
+ interactive=True,
379
+ )
380
+ with gr.Accordion(label="Change pretrains", open=False):
381
+ 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]
382
+ pretrained_G14 = gr.Dropdown(
383
+ label="pretrained G",
384
+ # Get a list of all pretrained G model files in assets/pretrained_v2 that end with .pth
385
+ choices = pretrained(sr2.value, 'G'),
386
+ value=pretrained(sr2.value, 'G')[0] if len(pretrained(sr2.value, 'G')) > 0 else '',
387
+ interactive=True,
388
+ visible=True
389
+ )
390
+ pretrained_D15 = gr.Dropdown(
391
+ label="pretrained D",
392
+ choices = pretrained(sr2.value, 'D'),
393
+ value= pretrained(sr2.value, 'D')[0] if len(pretrained(sr2.value, 'G')) > 0 else '',
394
+ visible=True,
395
+ interactive=True
396
+ )
397
+ with gr.Row():
398
+ download_model = gr.Button('5.Download Model')
399
+ with gr.Row():
400
+ model_files = gr.Files(label='Your Model and Index file can be downloaded here:')
401
+ download_model.click(
402
+ fn=lambda name: os.listdir(f'assets/weights/{name}') + glob.glob(f'logs/{name.split(".")[0]}/added_*.index'),
403
+ inputs=[training_name],
404
+ outputs=[model_files, info3])
405
+ with gr.Row():
406
+ sr2.change(
407
+ change_sr2,
408
+ [sr2, if_f0_3, version19],
409
+ [pretrained_G14, pretrained_D15],
410
+ )
411
+ version19.change(
412
+ change_version19,
413
+ [sr2, if_f0_3, version19],
414
+ [pretrained_G14, pretrained_D15, sr2],
415
+ )
416
+ if_f0_3.change(
417
+ change_f0,
418
+ [if_f0_3, sr2, version19],
419
+ [f0method8, pretrained_G14, pretrained_D15],
420
+ )
421
+ with gr.Row():
422
+ but5 = gr.Button("1 Click Training", variant="primary", visible=False)
423
+ but3.click(
424
+ click_train,
425
+ [
426
+ training_name,
427
+ sr2,
428
+ if_f0_3,
429
+ spk_id5,
430
+ save_epoch10,
431
+ total_epoch11,
432
+ batch_size12,
433
+ if_save_latest13,
434
+ pretrained_G14,
435
+ pretrained_D15,
436
+ gpus16,
437
+ if_cache_gpu17,
438
+ if_save_every_weights18,
439
+ version19,
440
+ ],
441
+ info3,
442
+ api_name="train_start",
443
+ )
444
+ but4.click(train_index, [training_name, version19], info3)
445
+ but5.click(
446
+ train1key,
447
+ [
448
+ training_name,
449
+ sr2,
450
+ if_f0_3,
451
+ dataset_folder,
452
+ spk_id5,
453
+ np7,
454
+ f0method8,
455
+ save_epoch10,
456
+ total_epoch11,
457
+ batch_size12,
458
+ if_save_latest13,
459
+ pretrained_G14,
460
+ pretrained_D15,
461
+ gpus16,
462
+ if_cache_gpu17,
463
+ if_save_every_weights18,
464
+ version19,
465
+ gpus_rmvpe,
466
+ ],
467
+ info3,
468
+ api_name="train_start_all",
469
+ )
470
+
471
+ if config.iscolab:
472
+ app.queue(max_size=20).launch(share=True,allowed_paths=["a.png","kofi_button.png"],show_error=True)
473
+ else:
474
+ app.queue(max_size=1022).launch(
475
+ server_name="0.0.0.0",
476
+ inbrowser=not config.noautoopen,
477
+ server_port=config.listen_port,
478
+ quiet=True,
479
+ )