Blane187 commited on
Commit
a7d3800
1 Parent(s): b237b2f

Create app.py

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