金子邦彦研究室人工知能Windows で動く人工知能関係 Pythonアプリケーション,オープンソースソフトウエア)Whisper のインストール,Whisper を使う Python プログラム(音声からの文字起こし,翻訳)(Python,PyTorch を使用)(Windows 上)

Whisper のインストール,Whisper を使う Python プログラム(音声からの文字起こし,翻訳)(Python,PyTorch を使用)(Windows 上)

要約】 Whisperは,音声からの文字起こしや翻訳に使用されるモデルである.このページで説明するWhisperのインストール(Windows)および動作確認手順に従い,Pythonプログラムを使用して実行することができる.FFmpegをインストールすることで,音声ファイルからの文字起こしを実行し,結果をテキストファイルに保存することも可能である.

目次

  1. 前準備
  2. Whisper のインストール(Windows 上)
  3. Whisper の動作確認(Windows 上)
  4. Whisper を使う Python プログラムの実行(Windows 上)

Whisper

Whisperは,音声からの文字起こし,翻訳 訓練されたモデルが既存のデータセットにゼロショットで適用可能であり、データセット固有のファインチューニングを必要とせずに高品質な結果を達成することを特徴とする.

文献

Alec Radford, Jong Wook Kim, Tao Xu, Greg Brockman, Christine McLeavey, Ilya Sutskever, Robust Speech Recognition via Large-Scale Weak Supervision, arXiv:2212.04356, 2022.

https://cdn.openai.com/papers/whisper.pdf

サイト内の関連ページ

関連する外部ページ

関連項目mallorbc の whisper_mic

前準備

Git のインストール(Windows 上)

Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.

サイト内の関連ページ

Windows での Git のインストール: 別ページ »で説明している.

関連する外部ページ

Git の公式ページ: https://git-scm.com/

Python のインストール(Windows 上)

サイト内の関連ページ

関連する外部ページ

Python の公式ページ: https://www.python.org/

Build Tools for Visual Studio 2022,NVIDIA ドライバ,NVIDIA CUDA ツールキット 11.8,NVIDIA cuDNN 8.6 のインストール(Windows 上)

サイト内の関連ページ

NVIDIA グラフィックスボードを搭載しているパソコンの場合には, NVIDIA ドライバNVIDIA CUDA ツールキットNVIDIA cuDNN のインストールを行う.

関連する外部ページ

PyTorch のインストール(Windows 上)

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. PyTorch のページを確認

    PyTorch のページ: https://pytorch.org/index.html

  3. 次のようなコマンドを実行(実行するコマンドは,PyTorch のページの表示されるコマンドを使う).

    次のコマンドは, PyTorch 2.0 (NVIDIA CUDA 11.8 用) をインストールする. 但し,Anaconda3を使いたい場合には別手順になる.

    事前に NVIDIA CUDA のバージョンを確認しておくこと(ここでは,NVIDIA CUDA ツールキット 11.8 が前もってインストール済みであるとする).

    PyTorch で,GPU が動作している場合には,「torch.cuda.is_available()」により,True が表示される.

    python -m pip install -U --ignore-installed pip
    python -m pip install -U torch torchvision torchaudio numpy --index-url https://download.pytorch.org/whl/cu118
    python -c "import torch; print(torch.__version__, torch.cuda.is_available())" 
    

    [image]

    Anaconda3を使いたい場合には, Anaconda プロンプト (Anaconda Prompt)管理者として実行し, 次のコマンドを実行する. (PyTorch と NVIDIA CUDA との連携がうまくいかない可能性があるため,Anaconda3を使わないことも検討して欲しい).

    conda install -y pytorch torchvision torchaudio pytorch-cuda=11.8 cudnn -c pytorch -c nvidia
    py -c "import torch; print(torch.__version__, torch.cuda.is_available())" 
    

    サイト内の関連ページ

    関連する外部ページ

Whisper のインストール(Windows 上)

FFmpeg のインストール(Windows 上)

Windows での FFmpeg のインストール(Windows 上): 別ページ »で説明している.

Whisper のインストール(Windows 上)

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. ダウンロードとインストール

    python -m pip install -U git+https://github.com/openai/whisper.git
    

    [image]
  3. 関連ファイルをダウンロード

    cd %HOMEPATH%
    rmdir /s /q whisper
    git clone --recursive https://github.com/openai/whisper.git
    

    [image]

Whisper の動作確認(Windows 上)

音声ファイルからの文字起こしを実行してみる.

  1. 次のコマンドを実行

    結果が表示され,テキストファイルにも保存される.

    small, English で実行する場合

    whisper %HOMEPATH%/whisper/tests/jfk.flac --model small --language English
    

    [image]

    large, Japanese で実行する場合

    whisper %HOMEPATH%/whisper/tests/jfk.flac --model large --language Japanese
    
  2. Windows では,次のようなコマンドの実行により,音声ファイルの再生が行われる.

    %HOMEPATH%\whisper\tests\jfk.flac
    

    [image]

Whisper を使う Python プログラムの実行(Windows 上)

実行時にファイルを選択する.ファイルは複数選択可能である.

  1. Windows で,コマンドプロンプトを実行
  2. エディタを起動
    cd %HOMEPATH%\whisper
    notepad small.py
    

    [image]
  3. エディタで,次のプログラムを保存

    このプログラムは, 公式の GitHub のページ: https://github.com/openai/whisperで公開されていたものを変更して使用している.

    import whisper
    
    model = whisper.load_model("small")
    
    import tkinter as tk
    from tkinter import filedialog
    
    root = tk.Tk()
    root.withdraw()
    fpaths = filedialog.askopenfilenames()
    
    for fpath in root.tk.splitlist(fpaths):
        print("file name: ", fpath)
        result = model.transcribe(fpath, language="japanese")
        print(result["text"])
    

    [image]
  4. Python プログラムの実行

    Python プログラムの実行

    Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.

    Python のまとめ: 別ページ »にまとめ

    プログラムを small.pyのようなファイル名で保存したので, 「python small.py」のようなコマンドで行う.

    python small.py
    

    ファイル選択画面が出るので,音声ファイルを選択する.ファイルは複数選択可能である.

    [image]
  5. 結果の確認

    [image]