1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WhisperX 使ってみた

Posted at

WhisperX とは.

WhisperXはOpenAIが開発した音声認識モデルWhisperと,発話者分離を行うためのPythonのライブラリpyannote.audioを組み合わせた音声認識器です.

Whisperとpyannote.audioのバージョンをそろえる(=whisperx-v3.3.4を動かす)のに苦労したので,現時点(2025/5/25時点)での環境構築についてまとめます.

自分の環境

NVIDIA ドライバ

CUDA Version: 12.2 は,ドライバーが対応している最大のCUDAバージョンのこと.

$ nvidia-smi
Sun May 25 19:42:49 2025       
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.183.01             Driver Version: 535.183.01   CUDA Version: 12.2     |
|-----------------------------------------+----------------------+----------------------+
| 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  NVIDIA GeForce RTX 3090        Off | 00000000:17:00.0 Off |                  N/A |
| 56%   64C    P2             176W / 350W |  21079MiB / 24576MiB |     48%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
|   1  NVIDIA GeForce RTX 3090        Off | 00000000:65:00.0 Off |                  N/A |
| 68%   67C    P2             210W / 350W |  21065MiB / 24576MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
                                                                                         
+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/A  N/A      1030      G   /usr/lib/xorg/Xorg                            9MiB |
|    0   N/A  N/A      1238      G   /usr/bin/gnome-shell                          6MiB |
|    0   N/A  N/A   3696591      C   python                                    21050MiB |
|    1   N/A  N/A      1030      G   /usr/lib/xorg/Xorg                            4MiB |
|    1   N/A  N/A   3696799      C   python                                    21050MiB |
+---------------------------------------------------------------------------------------+

CUDA

CUDAToolKitとともにインストールされるコンパイラドライバー.
-V オプションでCUDAToolKitのバージョンが表示される,つまりこちらがCUDAのバージョン.

$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243

環境構築

Anaconda 環境は入っているものとします.

環境作成

conda create -n whisperx-v344 python=3.10 -y
conda activate whisperx-v344

インストール(PyTorch + CUDA 12.1)

conda install -y -c nvidia -c pytorch pytorch==2.1.0 torchvision torchaudio pytorch-cuda=12.1
pip install -U "whisperx[diarization]==3.3.4"

テスト

python - <<'PY'
import torch, whisperx, os
print("Torch:", torch.__version__, torch.version.cuda)
print("CUDA available:", torch.cuda.is_available())
model = whisperx.load_model("large-v3", device="cuda", compute_type="float16")
print("Loaded WhisperX model OK")
PY

すべて通ればセットアップ完了です.(と思いましたが,次のプログラムを回したらエラーになったので,cuDNNのインストールをおこなってください.)

使い方

import whisperx
from datetime import timedelta
from pathlib import Path
from tqdm import tqdm
import whisperx.diarize
from dotenv import load_dotenv
import os
import json

load_dotenv()

device = "cuda"
audio_file = (
    "/sample.wav"
)
batch_size = 16
compute_type = "float16"
auth_token = os.getenv("HUGGINGFACE_AUTH_TOKEN")

model = whisperx.load_model("large-v2", device, compute_type=compute_type)
audio = whisperx.load_audio(audio_file)
result = model.transcribe(audio, batch_size=batch_size)

model_a, metadata = whisperx.load_align_model(
    language_code=result["language"], device=device
)

result = whisperx.align(
    result["segments"], model_a, metadata, audio, device, return_char_alignments=False
)

diarize_model = whisperx.diarize.DiarizationPipeline(
    use_auth_token=auth_token, device=device
)
diarize_segments = diarize_model(audio_file)
result = whisperx.assign_word_speakers(diarize_segments, result)

output_path = Path(
    "/sample.json"
)
output_path.parent.mkdir(parents=True, exist_ok=True)

with output_path.open("w", encoding="utf-8") as jf:
    json.dump(result["segments"], jf, ensure_ascii=False, indent=2)

エラー

上のプログラムを実行するとエラーになってしまう...
セットアップは完了したはずが,Could not load library libcudnn_ops_infer.so.8. Error: でエラーが出てしまいます.

cuDNNが見つからない

1. cuDNN をインストール

conda install -y -c nvidia cudnn=8.9

2. パスを通す

# 1 回だけ試す場合
export LD_LIBRARY_PATH="$CONDA_PREFIX/lib:$LD_LIBRARY_PATH"

# 毎回自動で有効にするなら ~/.bashrc などに追記
echo 'export LD_LIBRARY_PATH="$CONDA_PREFIX/lib:$LD_LIBRARY_PATH"' >> ~/.bashrc
source ~/.bashrc

補足

意識すること

  1. GPU アーキテクチャ
  2. NVIDIA ドライバ
  3. CUDA
  4. cuDNN
  5. 言語(Python)
  6. ライブラリ(PyTorch)
  7. アプリケーション

という順に依存しているから,上から順番に確かめていく.

cuDNNが見つからないエラーについて

pytorch-cuda=12.1 だけでは cuDNN の本体ライブラリは入らないらしい.

pytorch-cuda パッケージは「CUDA Runtime とドライバ連係部品」だけを束ねており、cuDNN は依存に含まれていません。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?