{ "cells": [ { "cell_type": "markdown", "source": [ "## MB-iSTFT-VITS2 inference" ], "metadata": { "id": "N0bhDfmXOBRy" } }, { "cell_type": "code", "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import IPython.display as ipd\n", "import librosa\n", "\n", "import os\n", "import json\n", "import math\n", "\n", "import requests\n", "import torch\n", "from torch import nn\n", "from torch.nn import functional as F\n", "from torch.utils.data import DataLoader\n", "\n", "import commons\n", "import utils\n", "from data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate\n", "from models import SynthesizerTrn\n", "from text.symbols import symbols\n", "from text import text_to_sequence\n", "import langdetect\n", "\n", "from scipy.io.wavfile import write\n", "import re" ], "metadata": { "id": "2GppsfIWTo7m" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "def get_text(text, hps):\n", " text_norm = text_to_sequence(text, hps.data.text_cleaners)\n", " if hps.data.add_blank:\n", " text_norm = commons.intersperse(text_norm, 0)\n", " text_norm = torch.LongTensor(text_norm)\n", " return text_norm\n", "\n", "\n", "def langdetector(text): # from PolyLangVITS\n", " try:\n", " lang = langdetect.detect(text).lower()\n", " if lang == 'ko':\n", " return f'[KO]{text}[KO]'\n", " elif lang == 'ja':\n", " return f'[JA]{text}[JA]'\n", " elif lang == 'en':\n", " return f'[EN]{text}[EN]'\n", " elif lang == 'zh-cn':\n", " return f'[ZH]{text}[ZH]'\n", " else:\n", " return text\n", " except Exception as e:\n", " return text\n", "\n", "\n", "def vcss(inputstr):\n", " fltstr = re.sub(r\"[\\[\\]\\(\\)\\{\\}]\", \"\", inputstr)\n", " fltstr = langdetector(fltstr)\n", " stn_tst = get_text(fltstr, hps)\n", "\n", " speed = 1\n", " sid = 0\n", " output_dir = 'output'\n", " with torch.no_grad():\n", " x_tst = stn_tst.cuda().unsqueeze(0)\n", " x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()\n", " audio = net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=.667, noise_scale_w=0.8, length_scale=1 / speed)[0][\n", " 0, 0].data.cpu().float().numpy()\n", "\n", " ipd.display(ipd.Audio(audio, rate=hps.data.sampling_rate, normalize=False))" ], "metadata": { "id": "v85aPkPtOA3m" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "hps = utils.get_hparams_from_file(\"./configs/mini_mb_istft_vits2_base.json\")\n", "\n", "if \"use_mel_posterior_encoder\" in hps.model.keys() and hps.model.use_mel_posterior_encoder == True:\n", " print(\"Using mel posterior encoder for VITS2\")\n", " posterior_channels = 80 # vits2\n", " hps.data.use_mel_posterior_encoder = True\n", "else:\n", " print(\"Using lin posterior encoder for VITS1\")\n", " posterior_channels = hps.data.filter_length // 2 + 1\n", " hps.data.use_mel_posterior_encoder = False\n", "\n", "net_g = SynthesizerTrn(\n", " len(symbols),\n", " posterior_channels,\n", " hps.train.segment_size // hps.data.hop_length,\n", " **hps.model).cuda()\n", "_ = net_g.eval()\n", "\n", "_ = utils.load_checkpoint(\"./models/G_2000.pth\", net_g, None)" ], "metadata": { "id": "S6J9zwzrTvBX" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# - text input\n", "input = \"I try to get the waiter's attention by blinking in morse code\"\n", "vcss(input)" ], "metadata": { "id": "T9nV74YBTx1h" }, "execution_count": null, "outputs": [] } ], "metadata": { "kernelspec": { "display_name": "MB-VITS", "language": "python", "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" }, "vscode": { "interpreter": { "hash": "9f16a3c86bbbd14c7fe4bf55d76086aa28175c5396da58b2a4368fac2eb85de4" } }, "colab": { "provenance": [] } }, "nbformat": 4, "nbformat_minor": 0 }