{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyNeYcg5YiUjFhkeVj9OycIp",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "gpuClass": "standard"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/w-okada/voice-changer/blob/dev/MMVCTrainerFrontendDemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Voice Changer Simple (デモ版)\n",
        "---\n",
        "\n",
        "このノートはVoice ChangerをColab上で動かすデモ版です。\n",
        "\n",
        "正式版はローカルPCのDocker上で動かすアプリケーションです。\n",
        "\n",
        "正式版は、多くの場合より少ないタイムラグで滑らかに音声を変換できます。\n",
        "\n",
        "詳細な使用方法はこちらの[リポジトリ](https://github.com/w-okada/voice-changer)からご確認ください。\n"
      ],
      "metadata": {
        "id": "Lbbmx_Vjl0zo"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# GPUを確認\n",
        "GPUを用いたほうが高速に処理が行えます。\n",
        "\n",
        "下記のコマンドでGPUが確認できない場合は、上のメニューから\n",
        "\n",
        "「ランタイム」→「ランタイムの変更」→「ハードウェア アクセラレータ」\n",
        "\n",
        "でGPUを選択してください。"
      ],
      "metadata": {
        "id": "oUKi1NYMmXrr"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# (1) GPUの確認\n",
        "!nvidia-smi"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vV1t7PBRm-o6",
        "outputId": "15081c48-84f0-4798-be64-38cf15c28966"
      },
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# リポジトリのクローン\n",
        "リポジトリをクローンします"
      ],
      "metadata": {
        "id": "sLBfykjBnjWc"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# (2) リポジトリのクローン\n",
        "!git clone --depth 1 https://github.com/isletennos/MMVC_Trainer.git -b v1.3.1.3 /MMVC_Trainer\n",
        "!git clone --depth 1 https://github.com/w-okada/voice-changer.git -b dev\n",
        "%cd voice-changer/demo/\n",
        "\n",
        "!cp ../template/setting_mmvc_colab.json ../frontend/dist/assets/setting.json"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "86wTFmqsNMnD",
        "outputId": "5151e168-15ec-4e4e-b50c-07867876294e"
      },
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "fatal: destination path '/MMVC_Trainer' already exists and is not an empty directory.\n",
            "fatal: destination path 'voice-changer' already exists and is not an empty directory.\n",
            "/content/voice-changer/demo\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# モジュールのインストール\n",
        "\n",
        "必要なモジュールをインストールします。"
      ],
      "metadata": {
        "id": "8Na2PbLZSWgZ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# (3) 設定ファイルの確認\n",
        "!apt-get install -y espeak libsndfile1-dev &> /dev/null\n",
        "!pip install unidecode &> /dev/null\n",
        "!pip install phonemizer &> /dev/null\n",
        "!pip install retry &> /dev/null\n",
        "!pip install python-socketio &> /dev/null\n",
        "!pip install fastapi &> /dev/null\n",
        "!pip install python-multipart &> /dev/null\n",
        "!pip install uvicorn &> /dev/null\n",
        "!pip install websockets &> /dev/null\n",
        "!pip install pyOpenSSL &> /dev/null\n",
        "!pip install pyopenjtalk==0.2.0 &> /dev/null\n",
        "\n",
        "%cd MMVC_Trainer/monotonic_align/\n",
        "!python setup.py build_ext --inplace\n",
        "%cd ../../"
      ],
      "metadata": {
        "id": "LwZAAuqxX7yY"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# サーバの起動\n",
        "\n",
        "サーバを起動します。(4-1)\n",
        "\n",
        "サーバの起動状況を確認します。(4-2) \n",
        "\n",
        "このセルは繰り返し実行することになるのでCtrl+Retでセルを実行してください。\n",
        "\n",
        "下記のようなテキストが表示されたら起動完了です。\n",
        "\n",
        "**`DEBUG:asyncio:Using selector: EpollSelector`**\n",
        "\n",
        "```\n",
        "    Phase name:__main__\n",
        "    PHASE3:__main__\n",
        "    PHASE1:__main__\n",
        "Start MMVC SocketIO Server\n",
        "    CONFIG:None, MODEL:None\n",
        "DEBUG:asyncio:Using selector: EpollSelector\n",
        "```\n",
        "\n"
      ],
      "metadata": {
        "id": "-_2OcN9Borke"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!mkdir -p /MMVC_Trainer/info\n",
        "!mkdir -p /MMVC_Trainer/logs"
      ],
      "metadata": {
        "id": "pwUQ0_RzGK_g"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# (4-1) サーバの起動\n",
        "import random\n",
        "PORT = 10000 + random.randint(1, 9999)\n",
        "LOG_FILE = f\"LOG_FILE_{PORT}\"\n",
        "\n",
        "get_ipython().system_raw(f'python3 MMVCServerSIO.py -p {PORT} --colab True >{LOG_FILE} 2>&1 &')\n",
        "#print(f\"PORT:{PORT}, LOG_FILE:{LOG_FILE}\")"
      ],
      "metadata": {
        "id": "G-nMdPxEW1rc"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# (4-2) サーバの起動確認\n",
        "!sleep 5\n",
        "!tail -20 {LOG_FILE}"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "chu06KpAjEK6",
        "outputId": "26995bdf-063e-43ee-84fd-4aecc7e7b7e6"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[32m    Phase name:__main__\u001b[0m\n",
            "\u001b[32m    PHASE3:__main__\u001b[0m\n",
            "\u001b[32m    PHASE1:__main__\u001b[0m\n",
            "\u001b[17mStart MMVC SocketIO Server\u001b[0m\n",
            "\u001b[34m    CONFIG:None, MODEL:None\u001b[0m\n",
            "DEBUG:asyncio:Using selector: EpollSelector\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# プロキシを起動\n",
        "ウェブサーバへのアクセスをするためのプロキシを起動します。\n",
        "\n",
        "表示されたURLをクリックして開くと別タブでアプリが開きます。\n",
        "\n",
        "Colabなので、ロードにある程度時間がかかります(30秒くらい)。"
      ],
      "metadata": {
        "id": "WhxcFLQEpctq"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# (5) プロキシを起動\n",
        "from google.colab.output import eval_js\n",
        "proxy = eval_js( \"google.colab.kernel.proxyPort(\" + str(PORT) + \")\" )\n",
        "print(f\"{proxy}trainer/\")"
      ],
      "metadata": {
        "id": "nkRjZm95l87C",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "845d1582-d776-4154-984c-e256dcce6b38"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "https://ffm3z64cmhr-496ff2e9c6d22116-12768-colab.googleusercontent.com/trainer/\n"
          ]
        }
      ]
    }
  ]
}