Closed7

Style-Bert-VITS2を試す

kun432kun432

ライブラリとして利用

学習等は不要で、シンプルにライブラリとしてTTSだけするならば、以下のnotebookを参考にすれば良さそう。

https://github.com/litagin02/Style-Bert-VITS2/tree/master/library.ipynb

今回はColaboratory T4で。

PyTorchはColabratoryでは標準で入っているので割愛。

!pip freeze | grep -i "torch"
出力
torch @ https://download.pytorch.org/whl/cu121/torch-2.5.1%2Bcu121-cp311-cp311-linux_x86_64.whl
torchaudio @ https://download.pytorch.org/whl/cu121/torchaudio-2.5.1%2Bcu121-cp311-cp311-linux_x86_64.whl
torchsummary==1.5.1
torchvision @ https://download.pytorch.org/whl/cu121/torchvision-0.20.1%2Bcu121-cp311-cp311-linux_x86_64.whl

style-bert-vits2ライブラリのインストール

!pip install style-bert-vits2
!pip freeze | grep -i "style-bert-vits2"
出力
style-bert-vits2==2.5.0

BERTモデルをロード。

from style_bert_vits2.nlp import bert_models
from style_bert_vits2.constants import Languages


bert_models.load_model(Languages.JP, "ku-nlp/deberta-v2-large-japanese-char-wwm")
bert_models.load_tokenizer(Languages.JP, "ku-nlp/deberta-v2-large-japanese-char-wwm")

モデルファイルをダウンロード。デフォルトモデルのものになる。

from pathlib import Path
from huggingface_hub import hf_hub_download


model_file = "jvnv-F1-jp/jvnv-F1-jp_e160_s14000.safetensors"
config_file = "jvnv-F1-jp/config.json"
style_file = "jvnv-F1-jp/style_vectors.npy"

for file in [model_file, config_file, style_file]:
    print(file)
    hf_hub_download("litagin/style_bert_vits2_jvnv", file, local_dir="model_assets")

モデルをロード。デバイスはCPUとなっている。

from style_bert_vits2.tts_model import TTSModel

assets_root = Path("model_assets")

model = TTSModel(
    model_path=assets_root / model_file,
    config_path=assets_root / config_file,
    style_vec_path=assets_root / style_file,
    device="cpu",
)

TTSして再生。CPUだと時間がかかる。以下の例だと1分かかった。

from IPython.display import Audio, display

sr, audio = model.infer(text="こんにちは!今日はいいお天気ですねー。こんな日は競馬観戦に行きたくなりますね!")
display(Audio(audio, rate=sr))

めっちゃ元気な感じで発話された。イントネーションは確かにめちゃめちゃ自然。

GPUだと約2秒で生成された。

model = TTSModel(
    model_path=assets_root / model_file,
    config_path=assets_root / config_file,
    style_vec_path=assets_root / style_file,
    device="cuda:0",  # GPUを指定
)

sr, audio = model.infer(text="こんにちは!今日はいいお天気ですねー。こんな日は競馬観戦に行きたくなりますね!")
display(Audio(audio, rate=sr))

リソースはこれぐらい。最初にCPU試して、次にGPU試した。CPUだと5GB、GPUだと2GBあればいけそう。

kun432kun432

学習

学習もColaboratory用のnotebookが用意されているので、基本それに従えばOKなので、割愛。めちゃめちゃ助かる。

https://colab.research.google.com/github/litagin02/Style-Bert-VITS2/blob/master/colab.ipynb#scrollTo=0GNj8JyDAlm2

あと、上記の手順で進めるとデフォルトの事前学習モデルをベースにした追加学習になる。事前学習を一からスタートするなら、前処理で作成される事前学習モデルのファイルを削除すればいいらしい。以下がとても参考になる。

https://ayousanz.hatenadiary.jp/entry/2024/02/24/043619

ITAコーパスに準拠したとある音声を揃えて実際に試してみた。Colaboratory T4で。パラメータはデフォルトのまま。

  • 追加学習は1000ステップでも十分良い感じだった。10000ステップでかなり精度高く感じた。
  • 事前学習も同じデータで試してみたが、こちらは6000ステップ程度回しても、ノイズ的な感じのものが生成されていたので、学習データ&学習時間が必要になる様子。

自前で音声を用意して事前学習をやってみたい。

kun432kun432

まとめ

評判通り、良い精度で生成してくれるものだった。ちょっと難しそう&ややこしそう、に見えて、触れてなかったのだが、ツールも整備されていて情報も豊富なので始めやすいと思う。もっと早く触ればよかった。

kun432kun432

ローカルでAPIサーバ建ててみる

  • Ubuntu-22.04
  • Python-3.11.5
  • CUDA-12.6

レポジトリクローン

git clone https://github.com/litagin02/Style-Bert-VITS2 && cd Style-Bert-VITS2

Python仮想環境を作成

python -m venv .venv
. .venv/bin/activate

パッケージインストール。自分の場合はPyTorch-2.3.1を想定。

pip install "torch<2.4" "torchaudio<2.4" --index-url https://download.pytorch.org/whl/cu121
pip install -r requirements.txt

デフォルトモデル等をダウンロード。

python initialize.py

FastAPIのサーバを起動

python server_fastapi.py
出力
01-31 14:38:23 |  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.
01-31 14:38:23 |WARNING | server_fastapi.py:126 | CORS allow_origins=['*']. If you don't want, modify config.yml
01-31 14:38:23 |  INFO  | server_fastapi.py:335 | server listen: http://127.0.0.1:5000
01-31 14:38:23 |  INFO  | server_fastapi.py:336 | API docs: http://127.0.0.1:5000/docs
01-31 14:38:23 |  INFO  | server_fastapi.py:337 | Input text length limit: 100. You can change it in server.limit in config.yml

ブラウザでhttp://[サーバのIPアドレス]:5000/docsにアクセス

curlでアクセスしてみる。GETもPOSTもURLパラメータで全部渡すっぽい

TEXT="こんにちは!今日はいいお天気ですね。こんな日は競馬観戦にもってこいですね!"
ENCODED_TEXT=$(echo -n "$TEXT" | jq -sRr @uri)
curl -X 'GET' "http://[サーバのIP]:5000/voice?text=$ENCODED_TEXT" -o output.wav
afplay output.wav

サーバのパラメータはconfig.yamlの下の方にある

config.yaml
(snip)

# server_fastapi's config
server:
  port: 5000
  device: "cuda"
  language: "JP"
  limit: 100
  origins:
    - "*"
kun432kun432

サーバのDockerfileを公開されている方がちらほらいるが、RunPod向けに書かれている方が多い。参考にさせていただきつつ、手元で動かすために書き換えた。

.dockerignore
# 修正
# !/bert/deberta-v2-large-japanese-char-wwm/
!/bert/

# 追加
!/slm/
!/server_fastapi.py
Dockerfile.server
FROM pytorch/pytorch:2.3.1-cuda12.1-cudnn8-runtime
ENV TZ=Asia/Tokyo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt update && apt install -y build-essential libssl-dev libffi-dev cmake git wget ffmpeg nvidia-cuda-toolkit libatlas-base-dev gfortran

WORKDIR /app
RUN pip3 uninstall -y cmake

COPY requirements.txt .
RUN pip3 install -r requirements.txt

# COPY . /app
ENV=LD_LIBRARY_PATH /opt/conda/lib/python3.10/site-packages/nvidia/cublas/lib:/opt/conda/lib/python3.10/site-packages/nvidia/cudnn/lib:${LD_LIBRARY_PATH}

COPY bert bert
COPY configs configs
COPY dict_data dict_data
COPY style_bert_vits2 style_bert_vits2

# for decreasing image size, specify model directory explicitly.
#RUN mkdir -p model_assets/<YOUR_MODEL_NAME>
#COPY model_assets/<YOUR_MODEL_NAME> model_assets/<YOUR_MODEL_NAME>
RUN mkdir -p model_assets
COPY model_assets model_assets

COPY *.yml .
COPY *.py .
COPY server_fastapi.py .

# ENTRYPOINT [ "python" ]
CMD [ "python", "server_fastapi.py" ]

ビルド

DOCKER_BUILDKIT=1 docker build --progress=plain . -f Dockerfile.server -t style-bert-vits2-api:0.1

起動

docker run -ti \
    --rm \
    --gpus all \
    -p 5000:5000 \
    style-bert-vits2-api:0.1

curlで動作確認できればOK

参考

https://note.com/nike_cha_n/n/n843bf0b21725

https://note.com/nyosubro/n/n086acab985ed

https://zenn.dev/kazuph/scraps/3abc12fcc94dd3

このスクラップは2025/01/21にクローズされました