📚

style bert vits2のtext-to-speech APIをgoogle colabで動作させる方法

2024/12/28に公開

私のm2 MacはGPUを搭載しておらず、CPUで動作させると音声合成に時間がかかってしまって、スムーズな会話ができない問題がありました。

google colabはT4 GPUを無料枠でも使えますので、これを利用するとGPU投資しなくてもttsのAPIを作れちゃいます。

google colabにstyle bert vits2をインストール

基本はREADMEのインストール手順と同じです。

google colabなので、コマンド実行には!をつける必要があります

[1] !git clone https://github.com/litagin02/Style-Bert-VITS2.git

Cloning into 'Style-Bert-VITS2'...
remote: Enumerating objects: 6659, done.
remote: Counting objects: 100% (109/109), done.
remote: Compressing objects: 100% (52/52), done.
remote: Total 6659 (delta 70), reused 57 (delta 57), pack-reused 6550 (from 2)
Receiving objects: 100% (6659/6659), 13.25 MiB | 16.35 MiB/s, done.
Resolving deltas: 100% (4321/4321), done.

[2] cd Style-Bert-VITS2

/content/Style-Bert-VITS2

[3] !pip install -r requirements.txt -Uqq

[4] !uv venv venv

Using CPython 3.10.12 interpreter at: /usr/bin/python3
Creating virtual environment at: venv
Activate with: source venv/bin/activate

[5] !source venv/bin/activate

[6] !python initialize.py # 必要なモデルとデフォルトTTSモデルをダウンロード

さて、ここで!python initialize.pyを実行してサーバーを立ち上げても、google colab外からアクセスはできません。

そのため、ngrokを使ってトンネリングを確立する必要があります。

トンネリング

あらかじめngrokのAuthTokenは取得しておきましょう。

再びgoogle colabに戻って以下のコマンドを実行します

!pip install -Uqq pyngrok
from pyngrok import ngrok

# authtokenを設定
ngrok.set_auth_token("ここにngrokのauthtokenを設定")

# トンネルを作成 (ポート5000を指定)
tunnel = ngrok.connect(5000)

# 公開URLを表示
print(f"Public URL: {tunnel.public_url}")

これで以下のようにhttpsアドレスが発行されます

Public URL: https://1234-56-78-910-60.ngrok-free.app

サーバー立ち上げ

これで、fastapiのサーバーを立ち上げて、ngrokのアドレスにアクセスすると外部からアクセスできます。

!python server_fastapi.py

12-30 15:37:45 |  INFO  | config.py:233 | A configuration file config.yml has been generated based on the default configuration file default_config.yml.
12-30 15:37:45 |  INFO  | config.py:236 | Please do not modify default_config.yml. Instead, modify config.yml.
12-30 15:37:45 | DEBUG  | __init__.py:92 | try starting pyopenjtalk worker server
12-30 15:37:46 | DEBUG  | __init__.py:130 | pyopenjtalk worker server started
12-30 15:37:52 |  INFO  | bert_models.py:92 | Loaded the JP BERT model from /content/Style-Bert-VITS2/bert/deberta-v2-large-japanese-char-wwm
12-30 15:37:52 |  INFO  | bert_models.py:154 | Loaded the JP BERT tokenizer from /content/Style-Bert-VITS2/bert/deberta-v2-large-japanese-char-wwm
12-30 15:37:56 |  INFO  | bert_models.py:92 | Loaded the EN BERT model from /content/Style-Bert-VITS2/bert/deberta-v3-large
12-30 15:37:57 |  INFO  | bert_models.py:154 | Loaded the EN BERT tokenizer from /content/Style-Bert-VITS2/bert/deberta-v3-large
Some weights of the model checkpoint at /content/Style-Bert-VITS2/bert/chinese-roberta-wwm-ext-large were not used when initializing BertForMaskedLM: ['bert.pooler.dense.bias', 'bert.pooler.dense.weight', 'cls.seq_relationship.bias', 'cls.seq_relationship.weight']
- This IS expected if you are initializing BertForMaskedLM from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing BertForMaskedLM from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
12-30 15:38:05 |  INFO  | bert_models.py:92 | Loaded the ZH BERT model from /content/Style-Bert-VITS2/bert/chinese-roberta-wwm-ext-large
12-30 15:38:05 |  INFO  | bert_models.py:154 | Loaded the ZH BERT tokenizer from /content/Style-Bert-VITS2/bert/chinese-roberta-wwm-ext-large
12-30 15:38:05 |WARNING | tts_model.py:397 | No model files found in model_assets/.cache, so skip it
12-30 15:38:05 |  INFO  | server_fastapi.py:113 | Loading models...
12-30 15:38:05 |  INFO  | server_fastapi.py:120 | The maximum length of the text is 100. If you want to change it, modify config.yml. Set limit to -1 to remove the limit.
12-30 15:38:05 |WARNING | server_fastapi.py:126 | CORS allow_origins=['*']. If you don't want, modify config.yml
12-30 15:38:05 |  INFO  | server_fastapi.py:335 | server listen: http://127.0.0.1:5000
12-30 15:38:05 |  INFO  | server_fastapi.py:336 | API docs: http://127.0.0.1:5000/docs

動作確認

swaggerでAPIの動作チェック

https://1234-56-78-910-60.ngrok-free.app/docs#/

上記にアクセスすると、以下のようなswagger画面が表示されます。

curlでの動作チェック

model_idの0を1とか2に変えると、色々な音声で合成できます。

https://1234-56-78-910-60.ngrok-free.app/voice?text=こんにちは、これはテストです&model_id=0&speaker_id=0&sdp_ratio=0.2&noise=0.6&noisew=0.8&length=1&language=JP&auto_split=true&split_interval=0.5&assist_text_weight=1&style=Neutral&style_weight=1

ただ、どの音声もアニメ的な声なので、生理的に気持ち悪い感じがします・・・

Discussion