File size: 24,680 Bytes
524ad56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "id": "2q0l56aFQhAM"
   },
   "source": [
    "# Terms of Use\n",
    "\n",
    "### Please solve the authorization problem of the dataset on your own. You shall be solely responsible for any problems caused by the use of non-authorized datasets for training and all consequences thereof.The repository and its maintainer, svc develop team, have nothing to do with the consequences!\n",
    "\n",
    "1. This project is established for academic exchange purposes only and is intended for communication and learning purposes. It is not intended for production environments.\n",
    "2. Any videos based on sovits that are published on video platforms must clearly indicate in the description that they are used for voice changing and specify the input source of the voice or audio, for example, using videos or audios published by others and separating the vocals as input source for conversion, which must provide clear original video or music links. If your own voice or other synthesized voices from other commercial vocal synthesis software are used as the input source for conversion, you must also explain it in the description.\n",
    "3. You shall be solely responsible for any infringement problems caused by the input source. When using other commercial vocal synthesis software as input source, please ensure that you comply with the terms of use of the software. Note that many vocal synthesis engines clearly state in their terms of use that they cannot be used for input source conversion.\n",
    "4. Continuing to use this project is deemed as agreeing to the relevant provisions stated in this repository README. This repository README has the obligation to persuade, and is not responsible for any subsequent problems that may arise.\n",
    "5. If you distribute this repository's code or publish any results produced by this project publicly (including but not limited to video sharing platforms), please indicate the original author and code source (this repository).\n",
    "6. If you use this project for any other plan, please contact and inform the author of this repository in advance. Thank you very much.\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "id": "M_RcDbVPhivj"
   },
   "source": [
    "## **Note:**\n",
    "## **Make sure there is no a directory named `sovits4data` in your google drive at the first time you use this notebook.**\n",
    "## **It will be created to store some necessary files.** \n",
    "## **For sure you can change it to another directory by modifying `sovits_data_dir` variable.**"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "id": "fHaw6hGEa_Nk"
   },
   "source": [
    "# **Initialize environment**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "0gQcIZ8RsOkn"
   },
   "outputs": [],
   "source": [
    "#@title Connect to colab runtime and check GPU\n",
    "\n",
    "#@markdown # Connect to colab runtime and check GPU\n",
    "\n",
    "#@markdown\n",
    "\n",
    "!nvidia-smi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "0YUGpYrXhMck"
   },
   "outputs": [],
   "source": [
    "#@title Clone repository and install requirements\n",
    "\n",
    "#@markdown # Clone repository and install requirements\n",
    "\n",
    "#@markdown\n",
    "\n",
    "#@markdown ### After the execution is completed, the runtime will **automatically restart**\n",
    "\n",
    "#@markdown\n",
    "\n",
    "!git clone https://github.com/svc-develop-team/so-vits-svc -b 4.1-Stable\n",
    "%cd /content/so-vits-svc\n",
    "%pip install --upgrade pip setuptools\n",
    "%pip install -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cu118\n",
    "exit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "wmUkpUmfn_Hs"
   },
   "outputs": [],
   "source": [
    "#@title Mount google drive and select which directories to sync with google drive\n",
    "\n",
    "#@markdown # Mount google drive and select which directories to sync with google drive\n",
    "\n",
    "#@markdown\n",
    "\n",
    "from google.colab import drive\n",
    "drive.mount(\"/content/drive\")\n",
    "\n",
    "#@markdown Directory to store **necessary files**, dont miss the slash at the end👇.\n",
    "sovits_data_dir = \"/content/drive/MyDrive/sovits4data/\"  #@param {type:\"string\"}\n",
    "#@markdown By default it will create a `sovits4data/` folder in your google drive.\n",
    "RAW_DIR = sovits_data_dir + \"raw/\"\n",
    "RESULTS_DIR = sovits_data_dir + \"results/\"\n",
    "FILELISTS_DIR = sovits_data_dir + \"filelists/\"\n",
    "CONFIGS_DIR = sovits_data_dir + \"configs/\"\n",
    "LOGS_DIR = sovits_data_dir + \"logs/44k/\"\n",
    "\n",
    "#@markdown\n",
    "\n",
    "#@markdown ### These folders will be synced with your google drvie\n",
    "\n",
    "#@markdown ### **Strongly recommend to check all.**\n",
    "\n",
    "#@markdown Sync **input audios** and **output audios**\n",
    "sync_raw_and_results = True  #@param {type:\"boolean\"}\n",
    "if sync_raw_and_results:\n",
    "  !mkdir -p {RAW_DIR}\n",
    "  !mkdir -p {RESULTS_DIR}\n",
    "  !rm -rf /content/so-vits-svc/raw\n",
    "  !rm -rf /content/so-vits-svc/results\n",
    "  !ln -s {RAW_DIR} /content/so-vits-svc/raw\n",
    "  !ln -s {RESULTS_DIR} /content/so-vits-svc/results\n",
    "\n",
    "#@markdown Sync **config** and **models**\n",
    "sync_configs_and_logs = True  #@param {type:\"boolean\"}\n",
    "if sync_configs_and_logs:\n",
    "  !mkdir -p {FILELISTS_DIR}\n",
    "  !mkdir -p {CONFIGS_DIR}\n",
    "  !mkdir -p {LOGS_DIR}\n",
    "  !rm -rf /content/so-vits-svc/filelists\n",
    "  !rm -rf /content/so-vits-svc/configs\n",
    "  !rm -rf /content/so-vits-svc/logs/44k\n",
    "  !ln -s {FILELISTS_DIR} /content/so-vits-svc/filelists\n",
    "  !ln -s {CONFIGS_DIR} /content/so-vits-svc/configs\n",
    "  !ln -s {LOGS_DIR} /content/so-vits-svc/logs/44k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "G_PMPCN6wvgZ"
   },
   "outputs": [],
   "source": [
    "#@title Get pretrained model(Optional but strongly recommend).\n",
    "\n",
    "#@markdown # Get pretrained model(Optional but strongly recommend).\n",
    "\n",
    "#@markdown\n",
    "\n",
    "#@markdown - Pre-trained model files: `G_0.pth` `D_0.pth`\n",
    "#@markdown   - Place them under /sovits4data/logs/44k/ in your google drive manualy\n",
    "\n",
    "#@markdown Get them from svc-develop-team(TBD) or anywhere else.\n",
    "\n",
    "#@markdown Although the pretrained model generally does not cause any copyright problems, please pay attention to it. For example, ask the author in advance, or the author has indicated the feasible use in the description clearly.\n",
    "\n",
    "download_pretrained_model = True #@param {type:\"boolean\"}\n",
    "D_0_URL = \"https://huggingface.co/datasets/ms903/sovits4.0-768vec-layer12/resolve/main/sovits_768l12_pre_large_320k/clean_D_320000.pth\" #@param [\"https://huggingface.co/datasets/ms903/sovits4.0-768vec-layer12/resolve/main/sovits_768l12_pre_large_320k/clean_D_320000.pth\", \"https://huggingface.co/1asbgdh/sovits4.0-volemb-vec768/resolve/main/clean_D_320000.pth\"] {allow-input: true}\n",
    "G_0_URL = \"https://huggingface.co/datasets/ms903/sovits4.0-768vec-layer12/resolve/main/sovits_768l12_pre_large_320k/clean_G_320000.pth\" #@param [\"https://huggingface.co/datasets/ms903/sovits4.0-768vec-layer12/resolve/main/sovits_768l12_pre_large_320k/clean_G_320000.pth\", \"https://huggingface.co/1asbgdh/sovits4.0-volemb-vec768/resolve/main/clean_G_320000.pth\"] {allow-input: true}\n",
    "\n",
    "download_pretrained_diffusion_model = True #@param {type:\"boolean\"}\n",
    "diff_model_URL = \"https://huggingface.co/datasets/ms903/Diff-SVC-refactor-pre-trained-model/resolve/main/fix_pitch_add_vctk_600k/model_0.pt\" #@param {type:\"string\"}\n",
    "\n",
    "%cd /content/so-vits-svc\n",
    "\n",
    "if download_pretrained_model:\n",
    "    !curl -L {D_0_URL} -o logs/44k/D_0.pth\n",
    "    !md5sum logs/44k/D_0.pth\n",
    "    !curl -L {G_0_URL} -o logs/44k/G_0.pth\n",
    "    !md5sum logs/44k/G_0.pth\n",
    "\n",
    "if download_pretrained_diffusion_model:\n",
    "    !mkdir -p logs/44k/diffusion\n",
    "    !curl -L {diff_model_URL} -o logs/44k/diffusion/model_0.pt\n",
    "    !md5sum logs/44k/diffusion/model_0.pt"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "id": "k1qadJBFehMo"
   },
   "source": [
    "# **Dataset preprocessing**"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "id": "kBlju6Q3lSM6"
   },
   "source": [
    "Pack and upload your raw dataset(dataset_raw/) to your google drive.\n",
    "\n",
    "Makesure the file structure in your zip file looks like this:\n",
    "\n",
    "```\n",
    "YourZIPforSingleSpeakers.zip\n",
    "└───speaker\n",
    "    ├───xxx1-xxx1.wav\n",
    "    ├───...\n",
    "    └───Lxx-0xx8.wav\n",
    "```\n",
    "\n",
    "```\n",
    "YourZIPforMultipleSpeakers.zip\n",
    "├───speaker0\n",
    "│   ├───xxx1-xxx1.wav\n",
    "│   ├───...\n",
    "│   └───Lxx-0xx8.wav\n",
    "└───speaker1\n",
    "    ├───xx2-0xxx2.wav\n",
    "    ├───...\n",
    "    └───xxx7-xxx007.wav\n",
    "```\n",
    "\n",
    "**Even if there is only one speaker, a folder named `{speaker_name}` is needed.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "U05CXlAipvJR"
   },
   "outputs": [],
   "source": [
    "#@title Get raw dataset from google drive\n",
    "\n",
    "#@markdown # Get raw dataset from google drive\n",
    "\n",
    "#@markdown\n",
    "\n",
    "#@markdown Directory where **your zip file** located in, dont miss the slash at the end👇.\n",
    "sovits_data_dir = \"/content/drive/MyDrive/sovits4data/\"  #@param {type:\"string\"}\n",
    "#@markdown Filename of **your zip file**, do NOT be \"dataset.zip\"\n",
    "zip_filename = \"YourZIPFilenameofRawDataset.zip\"  #@param {type:\"string\"}\n",
    "ZIP_PATH = sovits_data_dir + zip_filename\n",
    "\n",
    "!unzip -od /content/so-vits-svc/dataset_raw {ZIP_PATH}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "_ThKTzYs5CfL"
   },
   "outputs": [],
   "source": [
    "#@title Resample to 44100Hz and mono\n",
    "\n",
    "#@markdown # Resample to 44100Hz and mono\n",
    "\n",
    "#@markdown\n",
    "\n",
    "%cd /content/so-vits-svc\n",
    "!python resample.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "svITReeL5N8K"
   },
   "outputs": [],
   "source": [
    "#@title Divide filelists and generate config.json\n",
    "\n",
    "#@markdown # Divide filelists and generate config.json\n",
    "\n",
    "#@markdown\n",
    "\n",
    "%cd /content/so-vits-svc\n",
    "\n",
    "speech_encoder = \"vec768l12\" #@param [\"vec768l12\", \"vec256l9\", \"hubertsoft\", \"whisper-ppg\", \"whisper-ppg-large\"]\n",
    "use_vol_aug = False #@param {type:\"boolean\"}\n",
    "vol_aug = \"--vol_aug\" if use_vol_aug else \"\"\n",
    "\n",
    "from pretrain.meta import download_dict\n",
    "download_dict = download_dict()\n",
    "\n",
    "url = download_dict[speech_encoder][\"url\"]\n",
    "output = download_dict[speech_encoder][\"output\"]\n",
    "\n",
    "import os\n",
    "if not os.path.exists(output):\n",
    "  !curl -L {url} -o {output}\n",
    "  !md5sum {output}\n",
    "\n",
    "!python preprocess_flist_config.py --speech_encoder={speech_encoder} {vol_aug}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "xHUXMi836DMe"
   },
   "outputs": [],
   "source": [
    "#@title Generate hubert and f0\n",
    "\n",
    "#@markdown # Generate hubert and f0\n",
    "\n",
    "#@markdown\n",
    "%cd /content/so-vits-svc\n",
    "\n",
    "f0_predictor = \"crepe\" #@param [\"crepe\", \"pm\", \"dio\", \"harvest\", \"rmvpe\"]\n",
    "use_diff = True #@param {type:\"boolean\"}\n",
    "\n",
    "import os\n",
    "if f0_predictor == \"rmvpe\" and not os.path.exists(\"./pretrain/rmvpe.pt\"):\n",
    "  !curl -L https://huggingface.co/datasets/ylzz1997/rmvpe_pretrain_model/resolve/main/rmvpe.pt -o pretrain/rmvpe.pt\n",
    "\n",
    "diff_param = \"\"\n",
    "if use_diff:\n",
    "  diff_param = \"--use_diff\"\n",
    "\n",
    "  if not os.path.exists(\"./pretrain/nsf_hifigan/model\"):\n",
    "    !curl -L https://github.com/openvpi/vocoders/releases/download/nsf-hifigan-v1/nsf_hifigan_20221211.zip -o nsf_hifigan_20221211.zip\n",
    "    !md5sum nsf_hifigan_20221211.zip\n",
    "    !unzip nsf_hifigan_20221211.zip\n",
    "    !rm -rf pretrain/nsf_hifigan\n",
    "    !mv -v nsf_hifigan pretrain\n",
    "\n",
    "!python preprocess_hubert_f0.py --f0_predictor={f0_predictor} {diff_param}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "Wo4OTmTAUXgj"
   },
   "outputs": [],
   "source": [
    "#@title Save the preprocessed dataset to google drive\n",
    "\n",
    "#@markdown # Save the preprocessed dataset to google drive\n",
    "\n",
    "#@markdown\n",
    "\n",
    "#@markdown You can save the dataset and related files to your google drive for the next training\n",
    "\n",
    "#@markdown **Directory for saving**, dont miss the slash at the end👇.\n",
    "sovits_data_dir = \"/content/drive/MyDrive/sovits4data/\" #@param {type:\"string\"}\n",
    "\n",
    "#@markdown There will be a `dataset.zip` contained `dataset/` in your google drive, which is preprocessed data.\n",
    "\n",
    "!mkdir -p {sovits_data_dir}\n",
    "!zip -r dataset.zip /content/so-vits-svc/dataset\n",
    "!cp -vr dataset.zip \"{sovits_data_dir}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "P2G6v_6zblWK"
   },
   "outputs": [],
   "source": [
    "#@title Unzip preprocessed dataset from google drive directly if you have preprocessed already.\n",
    "\n",
    "#@markdown # Unzip preprocessed dataset from google drive directly if you have preprocessed already.\n",
    "\n",
    "#@markdown\n",
    "\n",
    "#@markdown Directory where **your preprocessed dataset** located in, dont miss the slash at the end👇.\n",
    "sovits_data_dir = \"/content/drive/MyDrive/sovits4data/\" #@param {type:\"string\"}\n",
    "CONFIG = sovits_data_dir + \"configs/\"\n",
    "FILELISTS = sovits_data_dir + \"filelists/\"\n",
    "DATASET = sovits_data_dir + \"dataset.zip\"\n",
    "\n",
    "!cp -vr {CONFIG} /content/so-vits-svc/\n",
    "!cp -vr {FILELISTS} /content/so-vits-svc/\n",
    "!unzip {DATASET} -d /"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "id": "ENoH-pShel7w"
   },
   "source": [
    "# **Trainning**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "-hEFFTCfZf57"
   },
   "outputs": [],
   "source": [
    "#@title Start training\n",
    "\n",
    "#@markdown # Start training\n",
    "\n",
    "#@markdown If you want to use pre-trained models, upload them to /sovits4data/logs/44k/ in your google drive manualy.\n",
    "\n",
    "#@markdown\n",
    "\n",
    "%cd /content/so-vits-svc\n",
    "\n",
    "#@markdown Whether to enable tensorboard\n",
    "tensorboard_on = True  #@param {type:\"boolean\"}\n",
    "\n",
    "if tensorboard_on:\n",
    "  %load_ext tensorboard\n",
    "  %tensorboard --logdir logs/44k\n",
    "\n",
    "config_path = \"configs/config.json\"\n",
    "\n",
    "from pretrain.meta import get_speech_encoder\n",
    "url, output = get_speech_encoder(config_path)\n",
    "\n",
    "import os\n",
    "if not os.path.exists(output):\n",
    "  !curl -L {url} -o {output}\n",
    "\n",
    "!python train.py -c {config_path} -m 44k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "ZThaMxmIJgWy"
   },
   "outputs": [],
   "source": [
    "#@title Train cluster model (Optional)\n",
    "\n",
    "#@markdown # Train cluster model (Optional)\n",
    "\n",
    "#@markdown #### Details see [README.md#cluster-based-timbre-leakage-control](https://github.com/svc-develop-team/so-vits-svc#cluster-based-timbre-leakage-control)\n",
    "\n",
    "#@markdown\n",
    "\n",
    "%cd /content/so-vits-svc\n",
    "!python cluster/train_cluster.py --gpu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#@title Train index model (Optional)\n",
    "\n",
    "#@markdown # Train index model (Optional)\n",
    "\n",
    "#@markdown #### Details see [README.md#feature-retrieval](https://github.com/svc-develop-team/so-vits-svc#feature-retrieval)\n",
    "\n",
    "#@markdown\n",
    "\n",
    "%cd /content/so-vits-svc\n",
    "!python train_index.py -c configs/config.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#@title Train diffusion model (Optional)\n",
    "\n",
    "#@markdown # Train diffusion model (Optional)\n",
    "\n",
    "#@markdown #### Details see [README.md#-about-shallow-diffusion](https://github.com/svc-develop-team/so-vits-svc#-about-shallow-diffusion)\n",
    "\n",
    "#@markdown\n",
    "\n",
    "%cd /content/so-vits-svc\n",
    "\n",
    "import os\n",
    "if not os.path.exists(\"./pretrain/nsf_hifigan/model\"):\n",
    "  !curl -L https://github.com/openvpi/vocoders/releases/download/nsf-hifigan-v1/nsf_hifigan_20221211.zip -o nsf_hifigan_20221211.zip\n",
    "  !unzip nsf_hifigan_20221211.zip\n",
    "  !rm -rf pretrain/nsf_hifigan\n",
    "  !mv -v nsf_hifigan pretrain\n",
    "\n",
    "#@markdown Whether to enable tensorboard\n",
    "tensorboard_on = True  #@param {type:\"boolean\"}\n",
    "\n",
    "if tensorboard_on:\n",
    "  %load_ext tensorboard\n",
    "  %tensorboard --logdir logs/44k\n",
    "\n",
    "!python train_diff.py -c configs/diffusion.yaml"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# keep colab alive\n",
    "Open the devtools and copy & paste to run the scrips.\n",
    "\n",
    "\n",
    "```JavaScript\n",
    "const ping = () => {\n",
    "  const btn = document.querySelector(\"colab-connect-button\");\n",
    "  const inner_btn = btn.shadowRoot.querySelector(\"#connect\");\n",
    "  if (inner_btn) {\n",
    "    inner_btn.click();\n",
    "    console.log(\"Clicked on connect button\");\n",
    "  } else {\n",
    "    console.log(\"connect button not found\");\n",
    "  }\n",
    "\n",
    "  const nextTime = 50000 + Math.random() * 10000;\n",
    "\n",
    "  setTimeout(ping, nextTime);\n",
    "};\n",
    "\n",
    "ping();\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "id": "oCnbX-OT897k"
   },
   "source": [
    "# **Inference**\n",
    "### Upload wav files from this notebook\n",
    "### **OR**\n",
    "### Upload to `sovits4data/raw/` in your google drive manualy (should be faster)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#title Download nsf_hifigan if you need it\n",
    "\n",
    "%cd /content/so-vits-svc\n",
    "!curl -L https://github.com/openvpi/vocoders/releases/download/nsf-hifigan-v1/nsf_hifigan_20221211.zip -o /content/so-vits-svc/nsf_hifigan_20221211.zip\n",
    "!unzip nsf_hifigan_20221211.zip\n",
    "!rm -rf pretrain/nsf_hifigan\n",
    "!mv -v nsf_hifigan pretrain\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 75
    },
    "executionInfo": {
     "elapsed": 94633,
     "status": "ok",
     "timestamp": 1678591088790,
     "user": {
      "displayName": "謬紗特",
      "userId": "09445825975794260265"
     },
     "user_tz": -480
    },
    "id": "XUsmGkgCMD_Q",
    "outputId": "8bbfde13-030a-4ba0-bbdb-7eb6b89c02b4"
   },
   "outputs": [],
   "source": [
    "#@title Upload wav files, the filename should not contain any special symbols like `#` `$` `(` `)`\n",
    "\n",
    "#@markdown # Upload wav files, the filename should not contain any special symbols like `#` `$` `(` `)`\n",
    "\n",
    "#@markdown\n",
    "\n",
    "%cd /content/so-vits-svc\n",
    "%run wav_upload.py --type audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "dYnKuKTIj3z1"
   },
   "outputs": [],
   "source": [
    "#@title Start inference (and download)\n",
    "\n",
    "#@markdown # Start inference (and download)\n",
    "\n",
    "#@markdown Parameters see [README.MD#Inference](https://github.com/svc-develop-team/so-vits-svc#-inference)\n",
    "\n",
    "#@markdown\n",
    "\n",
    "wav_filename = \"YourWAVFile.wav\"  #@param {type:\"string\"}\n",
    "model_filename = \"G_210000.pth\"  #@param {type:\"string\"}\n",
    "model_path = \"/content/so-vits-svc/logs/44k/\" + model_filename\n",
    "speaker = \"YourSpeaker\"  #@param {type:\"string\"}\n",
    "trans = \"0\"  #@param {type:\"string\"}\n",
    "cluster_infer_ratio = \"0\"  #@param {type:\"string\"}\n",
    "auto_predict_f0 = False  #@param {type:\"boolean\"}\n",
    "apf = \"\"\n",
    "if auto_predict_f0:\n",
    "  apf = \" -a \"\n",
    "\n",
    "f0_predictor = \"crepe\" #@param [\"crepe\", \"pm\", \"dio\", \"harvest\", \"rmvpe\"]\n",
    "\n",
    "enhance = False  #@param {type:\"boolean\"}\n",
    "ehc = \"\"\n",
    "if enhance:\n",
    "  ehc = \" -eh \"\n",
    "#@markdown\n",
    "\n",
    "#@markdown Generally keep default:\n",
    "config_filename = \"config.json\"  #@param {type:\"string\"}\n",
    "config_path = \"/content/so-vits-svc/configs/\" + config_filename\n",
    "\n",
    "from pretrain.meta import get_speech_encoder\n",
    "url, output = get_speech_encoder(config_path)\n",
    "\n",
    "import os\n",
    "\n",
    "if f0_predictor == \"rmvpe\" and not os.path.exists(\"./pretrain/rmvpe.pt\"):\n",
    "  !curl -L https://huggingface.co/datasets/ylzz1997/rmvpe_pretrain_model/resolve/main/rmvpe.pt -o pretrain/rmvpe.pt\n",
    "\n",
    "if not os.path.exists(output):\n",
    "  !curl -L {url} -o {output}\n",
    "\n",
    "kmeans_filenname = \"kmeans_10000.pt\"  #@param {type:\"string\"}\n",
    "kmeans_path = \"/content/so-vits-svc/logs/44k/\" + kmeans_filenname\n",
    "slice_db = \"-40\"  #@param {type:\"string\"}\n",
    "wav_format = \"flac\"  #@param {type:\"string\"}\n",
    "\n",
    "key = \"auto\" if auto_predict_f0 else f\"{trans}key\"\n",
    "cluster_name = \"\" if cluster_infer_ratio == \"0\" else f\"_{cluster_infer_ratio}\"\n",
    "isdiffusion = \"sovits\"\n",
    "wav_output = f\"/content/so-vits-svc/results/{wav_filename}_{key}_{speaker}{cluster_name}_{isdiffusion}_{f0_predictor}.{wav_format}\"\n",
    "\n",
    "%cd /content/so-vits-svc\n",
    "!python inference_main.py -n {wav_filename} -m {model_path} -s {speaker} -t {trans} -cr {cluster_infer_ratio} -c {config_path} -cm {kmeans_path} -sd {slice_db} -wf {wav_format} {apf} --f0_predictor={f0_predictor} {ehc}\n",
    "\n",
    "#@markdown\n",
    "\n",
    "#@markdown If you dont want to download from here, uncheck this.\n",
    "download_after_inference = True  #@param {type:\"boolean\"}\n",
    "\n",
    "if download_after_inference:\n",
    "  from google.colab import files\n",
    "  files.download(wav_output)"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "provenance": [
    {
     "file_id": "19fxpo-ZoL_ShEUeZIZi6Di-YioWrEyhR",
     "timestamp": 1678516497580
    },
    {
     "file_id": "1rCUOOVG7-XQlVZuWRAj5IpGrMM8t07pE",
     "timestamp": 1673086970071
    },
    {
     "file_id": "1Ul5SmzWiSHBj0MaKA0B682C-RZKOycwF",
     "timestamp": 1670483515921
    }
   ]
  },
  "gpuClass": "standard",
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}