LLMベースの軽量な日本語TTSモデル「Canary TTS」を試す
モデルは以下の2つ
sarashina2.2‑0.5b‑instruct‑v0.1ベース
llm-jp/llm-jp-3-150m-instruct3ベース
ライセンスはどちらもCC BY‑NC 4.0。(使用されているAudio decoderがCC-BY-NC 4.0なのね)
Colaboratory T4で。まず0.5Bから。
モデルカードに従ってインストール・・・
pip install torch torchvision torchaudio
pip install git+https://github.com/getuka/canary-tts.git
と行きたいのだが、PRが上がっている通り、setup.pyにtypoがある
これがマージされたらモデルカードの手順でいいはず。ということで、今回はクローンして、直接setup.pyを修正した。
pip install torch torchvision torchaudio
git clone https://github.com/getuka/canary-tts.git
%cd canary-tts
from setuptools import setup, find_packages
setup(
name='canary_tts', # プロジェクト名
version='0.1.0', # バージョン
packages=find_packages(include=[
'canary_tts', 'canary_tts.*'
]),
install_requires=[
'torch',
'torchvision',
'torchaudio',
'torchao',
'torchtune',
'transformers==4.44.2',
'einops',
'vector-quantize-pytorch',
'protobuf', # 行末にカンマ付与
'accelerate', # 行末にカンマ付与
'ffmpeg', # 行末にカンマ付与
'soundfile', # 行末にカンマ付与
'vector-quantize-pytorch',
"RubyInserter @ git+https://github.com/getuka/RubyInserter.git"
],
)
!pip install -e .
ランタイム再起動が求められるので再起動。
ランタイム再起動後は再度ディレクトリ移動。
%cd canary-tts
モデル・トークナイザー・コーデックをロード。
import torch, torchaudio
from transformers import AutoModelForCausalLM, AutoTokenizer
from canary_tts.xcodec2.modeling_xcodec2 import XCodec2Model
from rubyinserter import add_ruby
model_name = "2121-8/canary-tts-0.5b"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map="auto",
torch_dtype=torch.bfloat16
)
codec = XCodec2Model.from_pretrained("HKUSTAudio/xcodec2")
モデルロード後のVRAM消費は以下の通り。
!nvidia-smi
Wed Apr 23 11:31:56 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.54.15 Driver Version: 550.54.15 CUDA Version: 12.4 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |
| N/A 46C P0 25W / 70W | 1708MiB / 15360MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
推論はParler−TTSと同じプロンプト形式が使える。
A man voice, with a very hight pitch, speaks in a monotone manner. The recording quality is very noises and close-sounding, indicating a good or excellent audio capture.
description = "A man voice, with a very hight pitch, speaks in a monotone manner. The recording quality is very noises and close-sounding, indicating a good or excellent audio capture."
prompt = 'こんにちは。お元気ですか?'
prompt = add_ruby(prompt)
chat = [
{"role": "system", "content": description},
{"role": "user", "content": prompt}
]
tokenized_input = tokenizer.apply_chat_template(chat, add_generation_prompt=True, tokenize=True, return_tensors="pt").to(model.device)
with torch.no_grad():
output = model.generate(
tokenized_input,
max_new_tokens=256,
top_p=0.95,
temperature=0.7,
repetition_penalty=1.05,
)[0]
audio_tokens = output[len(tokenized_input[0]):]
output_audios = codec.decode_code(audio_tokens.unsqueeze(0).unsqueeze(0).cpu())
torchaudio.save("sample.wav", src=output_audios[0].cpu(), sample_rate=16000)
再生
from IPython.display import Audio
display(Audio("sample.wav"))
生成されたものはこれ。
再生後のnvidia-smi
Wed Apr 23 11:35:16 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.54.15 Driver Version: 550.54.15 CUDA Version: 12.4 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |
| N/A 53C P0 26W / 70W | 1792MiB / 15360MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
プロンプトを変えてみる。man から woman に変えただけ。
A woman voice, with a very hight pitch, speaks in a monotone manner. The recording quality is very noises and close-sounding, indicating a good or excellent audio capture.
150Mも。手順は同じでモデル名だけ変えれば良い。
nvidia-smiの結果。軽い。
Wed Apr 23 13:21:07 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.54.15 Driver Version: 550.54.15 CUDA Version: 12.4 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |
| N/A 62C P0 30W / 70W | 404MiB / 15360MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
上と同じプロンプトで。
A man voice, with a very hight pitch, speaks in a monotone manner. The recording quality is very noises and close-sounding, indicating a good or excellent audio capture.
結果。こちらはプロンプト通りにいかず、あと後半部分が無音になってしまった。
自分が試した限りはプロンプトを変えても上手くいかなかった。
まとめ
とても軽量なので色んなところで動かせそう。150Mが上手くいかなかったけど、サイズのせいもあるのかなー、残念。でも0.5Bでも十分軽いよね。
ライセンス的に商用用途では使えないけど、個人レベルでお手軽に広く使えそうなTTS。ありがたい。
参考