はじめに
WhisperはOpenAIが公開しているSpeech2Textモデルです。音声ファイルを用意すればそれを文字起こししてくれます。以前Pythonから使ったことがあります。【Whisper】日本語音声の文字起こしにチャレンジしました。
【OpenAI/Whisper】日本語音声の文字起こしをする際に専門用語に弱い点を克服する方法。
今回はHugging Faceが公開しているCandleというのを使ってみます。
Rustという言語を使っているようです。GitHubには挑戦的な言葉が書かれています。
Why should I use Candle? Candle's core goal is to make serverless inference possible. Full machine learning frameworks like PyTorch are very large, which makes creating instances on a cluster slow. Candle allows deployment of lightweight binaries. Secondly, Candle lets you remove Python from production workloads. Python overhead can seriously hurt performance, and the GIL is a notorious source of headaches. Finally, Rust is cool! A lot of the HF ecosystem already has Rust crates, like safetensors and tokenizers.
DeepLで翻訳したもの
なぜCandleを使う必要があるのか? Candleのコアゴールは、サーバーレス推論を可能にすることだ。PyTorchのような完全な機械学習フレームワークは非常に大きく、クラスタ上でインスタンスを作成するのに時間がかかります。Candleは軽量バイナリのデプロイを可能にする。 第二に、Candleは本番ワークロードからPythonを取り除くことができる。Pythonのオーバーヘッドはパフォーマンスを著しく低下させ、GILは頭痛の種として悪名高い。 最後に、Rustはクールだ!HFエコシステムの多くは、safetensors や tokenizers のようなRust cratesをすでに持っている。
なんだかワクワクしますね。記事の最後にPythonとの速度比較を載せています。
OS環境
Ubuntu 22.04 on WSL2
WSL2上のまっさらなUbuntu 22.04に一から環境構築しました。
下準備
必要なものをインストールします。sudo apt install build-essential libssl-dev
インストール後、「~/.bashrc」に以下を書き込んでおく必要があります。
export OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu/ export OPENSSL_INCLUDE_DIR=/usr/include/openssl/
書き込んだ後は.bashrcの再読込みをしておきましょう。
. ~/.bashrc
CUDAのインストール
こちらに従いCUDA 12.1.1をインストールしました。インストール後、「~/.bashrc」に以下を書き込んでおく必要があります。
export PATH=/usr/local/cuda-12.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-12.1/lib64:${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}書き込んだ後は.bashrcの再読込みをしておきましょう。
. ~/.bashrc
こちらの1行を実行する必要もあります。
sudo ln -s /usr/lib/wsl/lib/libcuda.so.1 /usr/local/cuda/lib64/libcuda.so
Rustのインストール
こちらに従いました。curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
インストール後に.bashrcの再読込みが必要です。
. ~/.bashrc
実行
git clone https://github.com/huggingface/candle cd candle/candle-examples cargo run --example whisper --release --features "symphonia, cuda"
サンプル音声がダウンロードされて、それをテキストにした結果が出力されます。
それと同時に「candle/target/release/examples」フォルダに「whisper」というファイルが作成されます。
2回目以降はその「whisper」を実行するだけです。
このファイルは名前を変えても、他のディレクトリに移動しても実行可能です。
自分で音声ファイルを用意した場合の実行はこのようになります。
./whisper --input sample.wav
補足
サンプリングレート 16000の「〇〇.wav」ファイルしか受け付けてくれませんでした。FFmpegを使えば変換可能です。sudo apt install ffmpeg ffmpeg -i sample.mp3 -ar 16000 output.wav
Pythonとの速度比較
21秒の日本語音声ファイルに対して「large-v3」を使って比較しました。Candle
time ./whisper --input sample.wav --model large-v3
real 0m7.039s user 0m3.939s sys 0m1.465s
Python
time python python-whisper.py --input sample.wav --model large-v3
real 0m12.477s user 0m18.702s sys 0m3.818s
使用したのは以下のPythonスクリプトです。
import whisper from typer import Typer, Option app = Typer() @app.command() def main( input_file: str=Option(..., "--input", help="input audio file"), model: str=Option(..., help="model") ): whisper_model = whisper.load_model(model) result = whisper_model.transcribe(input_file) print(result["text"]) if __name__ == "__main__": app()
速度はCandleの圧勝です。