1
1

お手軽でカンタンなFaster-Whisper環境構築(1)

Last updated at Posted at 2024-03-05

はじめに

Faster-Whisperを使ってみたいけど、環境構築が面倒だなーと感じている私のような輩のために、比較的カンタンでクリーンな環境構築の方法を紹介します。
ここで"クリーンな環境"とは「OSのパッケージを汚さずに設定できてカンタンに元通り」という意味合いです。

結論

VoiceVoxインストール済み環境なら、追加インストールなしでfaster-whisperを起動できます。
VoiceVoxのインストールはカンタンです。つまりfaster-whisperもカンタン。

Faster-Whisperとは?

STTのWhisperをローカルで高速に動かせるパッケージらしいです。大勢の方がPostしたり記事にしたりしているので、詳しい説明はそちらで。

公式はこちら(かな?)
https://github.com/SYSTRAN/faster-whisper

何が面倒なの?

WhisperをGPUで実行しようとすると、CUDAとかcuDNNとかをローカル環境にインストールする必要があります。
これらのライブラリが結構頻繁にバージョンアップされていまして、あっちのアプリは11.5で、こっちは11.8で、最近12が必須になりました。という感じで、バージョンに翻弄される訳です。世間のスマートな方々はdockerとかで対処されているのかもしれませんが。
また、nvidiaのリポジトリを追加してサードパーティパッケージをOSにインストールすると、OSのアップグレードのときに盛大にトラブって苦労することも。
しかも、ほとんど使わない関連ファイルで何ギガもディスクを占有されるももったいない話です。
そういう理由で、自分の場合は可能な限り最低限のファイルをコピーするだけで済ませたいのです。

環境

私が試した環境をメモしておきます
OS: Ubuntu 22.04 LST
GPU: nvidia t1000 8GB (nvidia-driver-535)

とりあえず試してみよう

何はともあれ、CUDAとcuDNNがインストールされていない環境で、何が足りないか試してみましょう。

まずは、恒例のpython仮想環境を作成。

$ mkdir ~/LLM/faster-whisper
$ cd ~/LLM/faster-whisper
$ python3 -m venv .venv --prompt 'Whisp'
$ source .venv/bin/activate
$ .venv/bin/python3 -m pip install -U pip
$ pip install faster-whisper

とりあえずパッケージはインストールできるようですね。次はサンプルコードを実行してみましょう。公式の参考コードをコピペして実行してみます。

test.py
from faster_whisper import WhisperModel

model_size = "large-v3"

# Run on GPU with FP16
model = WhisperModel(model_size, device="cuda", compute_type="float16")

# or run on GPU with INT8
# model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
# or run on CPU with INT8
# model = WhisperModel(model_size, device="cpu", compute_type="int8")

segments, info = model.transcribe("audio.mp3", beam_size=5)

print("Detected language '%s' with probability %f" % (info.language, info.language_probability))

for segment in segments:
    print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))

では、実行

$ python3 test.py
Could not load library libcudnn_ops_infer.so.8. Error: libcudnn_ops_infer.so.8: cannot open shared object file: No such file or directory
中止 (コアダンプ)

当然ですが、エラーで止まりますね。ラッキーなことに足りないライブラリのファイル名が表示されました。

ライブラリを探してみる

普通ならここで、cuDNNのインストールに移るわけですが......
このファイル名、どこかで見たことあるんですよ。なので、ディスクの中を大捜索してみます。

$ find ~ -name 'libcudnn_ops_infer.so.8'
/home/hoge/LLM/Voicevox/linux-nvidia/libcudnn_ops_infer.so.8

発見しました。VoiceVoxのランタイムとして付属していたのですね。では、これを使って実行してみましょう。環境変数LD_LIBRARY_PATHを使います。

$ export LD_LIBRARY_PATH=~/LLM/Voicevox/linux-nvidia
$ python3 test.py
Traceback (most recent call last):
  File "/home/hoge/LLM/fast-whisper/sample/test.py", line 14, in <module>
    segments, info = model.transcribe( audio_file, beam_size=5)
  File "/home/hoge/.local/lib/python3.10/site-packages/faster_whisper/transcribe.py", line 344, in transcribe
    encoder_output = self.encode(segment)
  File "/home/hoge/.local/lib/python3.10/site-packages/faster_whisper/transcribe.py", line 762, in encode
    return self.model.encode(features, to_cpu=to_cpu)
RuntimeError: Library libcublas.so.12 is not found or cannot be loaded

うーん、今度は、cubalsですね。さっきのVoiceVoxのランタイムに、libcublas.so.11は含まれているのですが....やっぱり、きちんとcuDNNをインストールしないとイケないのでしょうか?

ちょっと古いバージョンを試してみる

少し悩んで思いつきました。Faster-Whisperの古いバージョンならlibcublas.so.11だけで動くんじゃないか?
早速、バージョンを戻しましょう。まずは、どんなバージョンがあるか確認します。

$ pip install faster-whisper==
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement faster-whisper== (from versions: 0.2.0, 0.3.0, 0.4.0, 0.4.1, 0.5.0, 0.5.1, 0.6.0, 0.7.0, 0.7.1, 0.8.0, 0.9.0, 0.10.0, 0.10.1, 1.0.0, 1.0.1)
ERROR: No matching distribution found for faster-whisper==

区切りが良さそうな、0.10.1にしてみます。

pip install 'faster-whisper==0.10.1'

実行してみます。

$ python3 test.py
Detected language 'ja' with probability 0.968262
[0.00s -> 5.12s] はいすいませんどちらまで
[5.12s -> 6.82s] ナンバーグランドカ月まで
[6.82s -> 7.64s] ナンバーグランドカ月
[7.64s -> 9.38s] 1000日前の
[9.38s -> 11.08s] 1000日前の
[11.08s -> 14.62s] ナンバーグランドカ月さんお願いします
[14.62s -> 18.48s] 変わりましたでしょ新大阪のタクシー乗り場
[18.48s -> 22.62s] そうですねあそこなんか行き先とかいろいろ分かれてたのか
[22.62s -> 24.64s] もう今全部一緒になりましたね
[24.64s -> 29.80s] 小型の乗り場ももうなくなりましたね

動きました! VoiceVoxのランタイムを使えば、Faster-Whipser Ver.0.10.1なら動きます!
ちなみに上記の音声は、中川家のタクシー(https://www.youtube.com/watch?v=Ic-Go9aTC7s&t=318) これをここまで認識できるなんてスゴイ

参考)VoiceVoxのインストール

VoiceVoxのインストールはカンタンです。下記のリリースページからエンジンだけダウンロードして7zコマンドで解凍すればOKです。

VoiceVoxエンジン
https://github.com/VOICEVOX/voicevox_engine

VoiceVox全般についてはこちら
https://voicevox.hiroshiba.jp/

(2024-03-17追記)

こちらの記事の手順のほうがオススメです

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1