#author("2025-04-19T00:42:50+09:00","","") #author("2025-04-19T01:12:58+09:00","","") [[AI]] *音声認識 (kotoba-whisper) [#q0f60ad6] kotoba-whisper-v2.2 ** 特徴 [#me829f03] --julius と比較して日本語認識率よさそう(な気がする。) --julius と比較すると高負荷。GPUあれば問題なくリアルタイム処理可能(RTX 4060の場合) ** 利用にあたって [#lbc709ca] - huggingface.co のアカウント、トークンが必要。 次のURLを参考にアカウント、トークンを生成ください。 https://highreso.jp/edgehub/machinelearning/huggingfacetoken.html - 同アカウントで、下記 URL より利用申請する。 https://huggingface.co/pyannote/speaker-diarization-3.1 -- 申請は、同ページで 「Expand to review and access」を選択し、適宜情報を入力し 「submit」する。しばらくすると、承認されて、同画面で次のように表示される。 Gated model You have been granted access to this model ** インストール [#oa1e3c85] - GPU 設定 [[Windows/wsl/gpu]] 参照 - ツール等のインストール apt update apt install -y sudo wget git curl python3 python3-dev python3-pip ffmpeg -python 環境設定 [[Linux/基本設定]] の pyenv (python) インストール、venv (python 仮想環境) 参照 -python ライブラリインストール # python 仮想環境の状態で以下実行する。 pip install --upgrade pip # pytorch 関連をインストールする。 # https://pytorch.org/ の Install PyTorch にて、自分の環境に合わせて選択し、表示されるコマンドを実行のこと pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126 pip python-multipart # kotoba-whisper-v2.2 に記載されている必要なライブラリをインストール pip --upgrade transformers accelerate torchaudio pip install "punctuators==0.0.5" pip install "pyannote.audio" ** サンプルプログラム用意 [#tc80cfd7] test.py import torch from transformers import pipeline # config model_id = "kotoba-tech/kotoba-whisper-v2.2" torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 device = "cuda:0" if torch.cuda.is_available() else "cpu" model_kwargs = {"attn_implementation": "sdpa"} if torch.cuda.is_available() else {} # load model pipe = pipeline( model=model_id, torch_dtype=torch_dtype, device=device, model_kwargs=model_kwargs, batch_size=8, trust_remote_code=True, ) # run inference result = pipe("sample_diarization_japanese.mp3", chunk_length_s=15) print(result) # sample_diarization_japanese.mp3 # を自分で音声ファイルを作成する場合は、次で作ると良い。(16KHz, モノラル であれば良い) # arecord -r 16000 --vumeter=mono test.wav ** 実行 [#b6fde2a6] - kotoba-whisper-v2.2 より利用しているライブラリの利用許諾を得るために下記実行する。 git config --global credential.helper store # git 認証情報保存するために実行 huggingface-cli login --token "★ここに huggingface で取得したトークンを入れる★" --add-to-git-credential - 作成した python を実行する。 python test.py ※初回、ダウンロード等のため時間かかります。~ ダウンロードされたファイルは、下記に保存されます。 ~/.cache/huggingface/hub/ * もう少し簡単に [#j853889d] * REST API 化 [#v0554125] wav ファイルを送信すると、音声認識結果を返す REST API~ - REST API 実装したソース https://ehobby.jp/gitbucket/kei-n/kotoba-whisper/blob/main/kotoba-whisper/kotoba-whisper-server.py - 起動 pip install fastapi uvicorn uvicorn kotoba-whisper-server:app --host 0.0.0.0 --port 50022 - 利用側 curl https://ehobby.jp/gitbucket/git/kei-n/kotoba-whisper.git git pull https://ehobby.jp/gitbucket/git/kei-n/kotoba-whisper.git curl -X POST http://127.0.0.1:50022/transcribe -F "file=@sample.wav" ※sample.wav ファイルは、事前に 16kHz, モノラルで録音した音声ファイル * 上記 Docker [#tb5e4444] https://ehobby.jp/gitbucket/kei-n/kotoba-whisper 参照 git clone https://ehobby.jp/gitbucket/git/kei-n/kotoba-whisper.git cd kotoba-whisper # Huggingface でトークンを作成しておくこと。 # https://huggingface.co/pyannote/speaker-diarization-3.1 の利用許諾を得ておくこと。 echo "HF_TOKEN=<Huggingface で取得したトークン>" > .env # Docker イメージ作成 (GPU対応の設定、kotoba-whsper インストール等含む) docker-compose build # 起動 docker-compose up -d # REST API サーバーが起動する。 # あとは、次のコマンド等で音声認識させる。 git pull https://ehobby.jp/gitbucket/git/kei-n/kotoba-whisper.git curl -X POST http://127.0.0.1:50022/transcribe -F "file=@sample.wav" ※sample.wav ファイルは、事前に 16kHz, モノラルで録音した音声ファイル 参考URL: https://huggingface.co/kotoba-tech/kotoba-whisper-v2.2