{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":5140963,"sourceType":"datasetVersion","datasetId":2950250}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!python --version\n!nvidia-smi","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T16:10:58.705917Z","iopub.execute_input":"2024-12-20T16:10:58.706154Z","iopub.status.idle":"2024-12-20T16:10:58.829543Z","shell.execute_reply.started":"2024-12-20T16:10:58.706125Z","shell.execute_reply":"2024-12-20T16:10:58.828735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!wget https://raw.githubusercontent.com/coqui-ai/TTS/main/TTS/bin/collect_env_info.py\n!python collect_env_info.py","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!sudo apt-get -y install espeak-ng","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T16:13:19.270425Z","iopub.execute_input":"2024-12-20T16:13:19.270802Z","iopub.status.idle":"2024-12-20T16:13:26.399941Z","shell.execute_reply.started":"2024-12-20T16:13:19.270776Z","shell.execute_reply":"2024-12-20T16:13:26.399097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!sudo apt-get install python3.9-distutils\n!sudo apt-get install python3.9","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T16:11:38.53055Z","iopub.execute_input":"2024-12-20T16:11:38.530914Z","iopub.status.idle":"2024-12-20T16:11:51.523517Z","shell.execute_reply.started":"2024-12-20T16:11:38.530882Z","shell.execute_reply":"2024-12-20T16:11:51.522628Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install virtualenv\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T16:14:01.452415Z","iopub.execute_input":"2024-12-20T16:14:01.452824Z","iopub.status.idle":"2024-12-20T16:14:06.614598Z","shell.execute_reply.started":"2024-12-20T16:14:01.452781Z","shell.execute_reply":"2024-12-20T16:14:06.613611Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!virtualenv -p python3.9 my_env\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T16:14:33.226478Z","iopub.execute_input":"2024-12-20T16:14:33.226812Z","iopub.status.idle":"2024-12-20T16:14:34.340997Z","shell.execute_reply.started":"2024-12-20T16:14:33.226788Z","shell.execute_reply":"2024-12-20T16:14:34.33989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!source my_env/bin/activate; pip list","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T16:15:25.713075Z","iopub.execute_input":"2024-12-20T16:15:25.713405Z","iopub.status.idle":"2024-12-20T16:15:26.269741Z","shell.execute_reply.started":"2024-12-20T16:15:25.713382Z","shell.execute_reply":"2024-12-20T16:15:26.268902Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!source my_env/bin/activate; pip -q install coqui-tts==0.25.1","metadata":{"trusted":true,"execution":{"iopub.status.idle":"2024-12-20T16:20:10.459028Z","shell.execute_reply.started":"2024-12-20T16:16:27.070806Z","shell.execute_reply":"2024-12-20T16:20:10.457863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!mkdir train_output\n!mkdir train_output/kagg","metadata":{"execution":{"iopub.status.busy":"2024-12-20T16:23:14.899897Z","iopub.execute_input":"2024-12-20T16:23:14.900382Z","iopub.status.idle":"2024-12-20T16:23:15.134032Z","shell.execute_reply.started":"2024-12-20T16:23:14.900351Z","shell.execute_reply":"2024-12-20T16:23:15.133093Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"code='''import os\n\nfrom trainer import Trainer, TrainerArgs\n\nfrom TTS.tts.configs.shared_configs import BaseDatasetConfig , CharactersConfig\nfrom TTS.config.shared_configs import BaseAudioConfig\nfrom TTS.tts.configs.vits_config import VitsConfig\nfrom TTS.tts.datasets import load_tts_samples\nfrom TTS.tts.models.vits import Vits, VitsAudioConfig\nfrom TTS.tts.utils.text.tokenizer import TTSTokenizer\nfrom TTS.utils.audio import AudioProcessor\nfrom TTS.utils.downloaders import download_thorsten_de\n\noutput_path = os.path.dirname(os.path.abspath(__file__))\ndataset_config = BaseDatasetConfig(\n formatter=\"mozilla\", meta_file_train=\"metadata.csv\", path=\"/kaggle/input/persian-tts-dataset-male\" \n)\n\n\n\naudio_config = BaseAudioConfig(\n sample_rate=22050,\n do_trim_silence=True,\n resample=False,\n mel_fmin=0,\n mel_fmax=None \n)\ncharacter_config=CharactersConfig(\n characters='ءابتثجحخدذرزسشصضطظعغفقلمنهويِپچژکگیآأؤإئًَُّ',\n punctuations='!(),-.:;? ̠،؛؟‌<>',\n phonemes='ˈˌːˑpbtdʈɖcɟkɡqɢʔɴŋɲɳnɱmʙrʀⱱɾɽɸβfvθðszʃʒʂʐçʝxɣχʁħʕhɦɬɮʋɹɻjɰlɭʎʟaegiouwyɪʊ̩æɑɔəɚɛɝɨ̃ʉʌʍ0123456789\"#$%*+/=ABCDEFGHIJKLMNOPRSTUVWXYZ[]^_{}',\n pad=\"\",\n eos=\"\",\n bos=\"\",\n blank=\"\",\n characters_class=\"TTS.tts.utils.text.characters.IPAPhonemes\",\n )\nconfig = VitsConfig(\n audio=audio_config,\n run_name=\"vits_fa_male\",\n batch_size=16,\n eval_batch_size=8,\n batch_group_size=5,\n num_loader_workers=0,\n num_eval_loader_workers=2,\n run_eval=True,\n test_delay_epochs=-1,\n epochs=1000,\n save_step=1000,\n text_cleaner=\"basic_cleaners\",\n use_phonemes=True,\n phoneme_language=\"fa\",\n characters=character_config,\n phoneme_cache_path=os.path.join(output_path, \"phoneme_cache\"),\n compute_input_seq_cache=True,\n print_step=200,\n print_eval=False,\n mixed_precision=False,\n test_sentences=[\n [\"سلطان محمود در زمستانی سخت به طلخک گفت که: با این جامه ی یک لا در این سرما چه می کنی \"],\n [\"مردی نزد بقالی آمد و گفت پیاز هم ده تا دهان بدان خو شبوی سازم.\"],\n [\"از مال خود پاره ای گوشت بستان و زیره بایی معطّر بساز\"],\n [\"یک بار هم از جهنم بگویید.\"],\n [\"یکی اسبی به عاریت خواست\"]\n ],\n output_path=output_path,\n datasets=[dataset_config],\n)\n\n# INITIALIZE THE AUDIO PROCESSOR\n# Audio processor is used for feature extraction and audio I/O.\n# It mainly serves to the dataloader and the training loggers.\nap = AudioProcessor.init_from_config(config)\n\n# INITIALIZE THE TOKENIZER\n# Tokenizer is used to convert text to sequences of token IDs.\n# config is updated with the default characters if not defined in the config.\ntokenizer, config = TTSTokenizer.init_from_config(config)\n\n# LOAD DATA SAMPLES\n# Each sample is a list of ```[text, audio_file_path, speaker_name]```\n# You can define your custom sample loader returning the list of samples.\n# Or define your custom formatter and pass it to the `load_tts_samples`.\n# Check `TTS.tts.datasets.load_tts_samples` for more details.\ntrain_samples, eval_samples = load_tts_samples(\n dataset_config,\n eval_split=True,\n eval_split_max_size=config.eval_split_max_size,\n eval_split_size=config.eval_split_size,\n)\n\n# init model\nmodel = Vits(config, ap, tokenizer, speaker_manager=None)\n\n# init the trainer and 🚀\ntrainer = Trainer(\n TrainerArgs(),\n config,\n output_path,\n model=model,\n train_samples=train_samples,\n eval_samples=eval_samples,\n)\ntrainer.fit()'''\nf=open(\"train_output/train_vits.py\",\"w\",encoding=\"utf-8\")\n\nf.write(code)\n\nf.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T16:27:30.475904Z","iopub.execute_input":"2024-12-20T16:27:30.476277Z","iopub.status.idle":"2024-12-20T16:27:30.482868Z","shell.execute_reply.started":"2024-12-20T16:27:30.476252Z","shell.execute_reply":"2024-12-20T16:27:30.482073Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#!wget \"https://huggingface.co/Kamtera/persian-tts-male-vits/resolve/main/train_vits.py\" -O train_output/train_vits.py\n!wget \"https://huggingface.co/Kamtera/persian-tts-male1-vits/resolve/main/checkpoint_88000.pth\" -O train_output/kagg/checkpoint_88000.pth\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T16:27:39.208334Z","iopub.execute_input":"2024-12-20T16:27:39.208609Z","iopub.status.idle":"2024-12-20T16:27:57.575866Z","shell.execute_reply.started":"2024-12-20T16:27:39.208588Z","shell.execute_reply":"2024-12-20T16:27:57.574961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -ul 'train_output'\n!ls -ul /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2024-12-20T16:27:59.423054Z","iopub.execute_input":"2024-12-20T16:27:59.423481Z","iopub.status.idle":"2024-12-20T16:27:59.656998Z","shell.execute_reply.started":"2024-12-20T16:27:59.423449Z","shell.execute_reply":"2024-12-20T16:27:59.655892Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2024-12-20T16:28:15.363269Z","iopub.execute_input":"2024-12-20T16:28:15.363555Z","iopub.status.idle":"2024-12-20T16:28:18.602097Z","shell.execute_reply.started":"2024-12-20T16:28:15.363534Z","shell.execute_reply":"2024-12-20T16:28:18.601355Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!source my_env/bin/activate; PYTORCH_CUDA_ALLOC_CONF=\"max_split_size_mb:512\" python \"train_output/train_vits.py\" \\\n--restore_path \"train_output/kagg/checkpoint_88000.pth\" \\\n--coqpit.run_name \"vits-male-finetune\" ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-20T16:28:26.283903Z","iopub.execute_input":"2024-12-20T16:28:26.284376Z","iopub.status.idle":"2024-12-20T16:42:34.760357Z","shell.execute_reply.started":"2024-12-20T16:28:26.284348Z","shell.execute_reply":"2024-12-20T16:42:34.759113Z"}},"outputs":[],"execution_count":null}]}