{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3cd462f1-bad8-41bb-9878-9827ab395e5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import korean\n",
    "import IPython\n",
    "import librosa\n",
    "import librosa.display\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def koreanCleaner(text):\n",
    "    return \"\".join(korean.tokenize(text))\n",
    "\n",
    "def displayWaveImage(path):\n",
    "    x, sr = librosa.load(path)\n",
    "    plt.figure(figsize=(14, 5))\n",
    "    librosa.display.waveplot(x, sr=sr)\n",
    "    \n",
    "ttsPath=\"model/tts/glowtts/coqui_tts-January-03-2022_03+03AM-0000000\"\n",
    "vocoderPath=\"model/vocoder/mbmelgan/coqui_tts-January-03-2022_10+17PM-0000000\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d5cb590e-461f-42a8-9c94-293f2eb84915",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " > Using model: glow_tts\n",
      " > Text: 신은 우리의 수학 문제에는 관심이 없다. 신은 다만 경험적으로 통합할 뿐이다.\n",
      " > Text splitted to sentences.\n",
      "['신은 우리의 수학 문제에는 관심이 없다.', '신은 다만 경험적으로 통합할 뿐이다.']\n",
      " > Processing time: 1.5092484951019287\n",
      " > Real-time factor: 0.29138877589133444\n",
      " > Saving output to output.wav\n",
      "\u001b[0m"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "                <audio  controls=\"controls\" >\n",
       "                    <source src=\"data:audio/x-wav;base64,[encoded data, 304616 characters]\" type=\"audio/x-wav\" />\n",
       "                    Your browser does not support the audio element.\n",
       "                </audio>\n",
       "              "
      ],
      "text/plain": [
       "<IPython.lib.display.Audio object>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 47016 characters]\n",
      "text/plain": [
       "<Figure size 1008x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#inference Using Griffin-Lim Vocoder \n",
    "input_text = \"신은 우리의 수학 문제에는 관심이 없다. 신은 다만 경험적으로 통합할 뿐이다.\"\n",
    "inputText=koreanCleaner(input_text)\n",
    "\n",
    "!tts \\\n",
    "    --text \"$inputText\" \\\n",
    "    --model_path \"$ttsPath\"/checkpoint_280000.pth.tar \\\n",
    "    --config_path \"$ttsPath\"/config.json \\\n",
    "    --out_path output.wav\n",
    "\n",
    "\n",
    "displayWaveImage(\"output.wav\")\n",
    "IPython.display.Audio(\"output.wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "428441eb-f5bc-4fc5-9180-70f9c26d647d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " > Using model: glow_tts\n",
      " > Vocoder Model: multiband_melgan\n",
      " > Generator Model: multiband_melgan_generator\n",
      " > Discriminator Model: melgan_multiscale_discriminator\n",
      " > Text: 신은 우리의 수학 문제에는 관심이 없다. 신은 다만 경험적으로 통합할 뿐이다.\n",
      " > Text splitted to sentences.\n",
      "['신은 우리의 수학 문제에는 관심이 없다.', '신은 다만 경험적으로 통합할 뿐이다.']\n",
      " > Processing time: 0.4013204574584961\n",
      " > Real-time factor: 0.07578374286585228\n",
      " > Saving output to output.wav\n",
      "\u001b[0m"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "                <audio  controls=\"controls\" >\n",
       "                    <source src=\"data:audio/x-wav;base64,[encoded data, 311440 characters]\" type=\"audio/x-wav\" />\n",
       "                    Your browser does not support the audio element.\n",
       "                </audio>\n",
       "              "
      ],
      "text/plain": [
       "<IPython.lib.display.Audio object>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 48424 characters]\n",
      "text/plain": [
       "<Figure size 1008x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "##inference Using trained Vocoder \n",
    "input_text = \"신은 우리의 수학 문제에는 관심이 없다. 신은 다만 경험적으로 통합할 뿐이다.\"\n",
    "inputText=koreanCleaner(input_text)\n",
    "\n",
    "\n",
    "#180000\n",
    "!tts \\\n",
    "    --text \"$inputText\" \\\n",
    "    --model_path \"$ttsPath\"/checkpoint_280000.pth.tar \\\n",
    "    --config_path \"$ttsPath\"/config.json \\\n",
    "    --out_path output.wav \\\n",
    "    --vocoder_path \"$vocoderPath\"/checkpoint_170000.pth.tar \\\n",
    "    --vocoder_config_path \"$vocoderPath\"/config.json\n",
    "\n",
    "displayWaveImage(\"output.wav\")\n",
    "IPython.display.Audio(\"output.wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "247a5b89-4e24-4df7-a74c-b1a6ff11ed1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TensorFlow installation not found - running with reduced feature set.\n",
      "\n",
      "NOTE: Using experimental fast data loading logic. To disable, pass\n",
      "    \"--load_fast=false\" and report issues on GitHub. More details:\n",
      "    https://github.com/tensorflow/tensorboard/issues/4784\n",
      "\n",
      "TensorBoard 2.7.0 at http://user-System-Product-Name:6006/ (Press CTRL+C to quit)\n"
     ]
    }
   ],
   "source": [
    "#tensorboard\n",
    "!tensorboard --bind_all --logdir model/vocoder/mbmelgan/coqui_tts-December-14-2021_05+54PM-0000000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33dacecd-a292-4ae2-8a1d-b644f0aa9ecc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tf2]",
   "language": "python",
   "name": "conda-env-tf2-py"
  },
  "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.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
