Windows10にWhisperを入れ、Pythonで実行してみた

Python Whisper

インストール環境

項目内容
OSWindows 10 Home 22H2
プロセッサIntel(R) Core(TM) i7-9700T CPU @ 2.00GHz 1.99 GHz
RAM16.0 GB
Python3.10.8

Whisper インストール

インストール

githubにいろいろな説明やサンプルがありました。

https://github.com/openai/whisper

pip install -U openai-whisper
Python 3.11.1 ではエラーとなってしまいました。。。(エラー内容)
PS D:\python\whisper> pip install -U openai-whisper
Collecting openai-whisper
  Using cached openai-whisper-20230308.tar.gz (1.2 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting numba
  Using cached numba-0.56.4.tar.gz (2.4 MB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ?─> [8 lines of output]
      Traceback (most recent call last):
        File "", line 2, in 
        File "", line 34, in 
        File "C:\Users\hiroy\AppData\Local\Temp\pip-install-b4uyfc0n\numba_23530fe01836473291880a611a013e0b\setup.py", line 51, in 
          _guard_py_ver()
        File "C:\Users\hiroy\AppData\Local\Temp\pip-install-b4uyfc0n\numba_23530fe01836473291880a611a013e0b\setup.py", line 48, in _guard_py_ver
          raise RuntimeError(msg.format(cur_py, min_py, max_py))
      RuntimeError: Cannot install on Python version 3.11.1; only versions >=3.7,<3.11 are supported.
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
?─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

現時点で、対応しているバージョンが 3.73.11 までのようです(2023/03/10時点)

気を取り直して Python 3.10.8 でインストール → 無事成功!

いろいろなパッケージがインストールされました。

certifi            2022.12.7
charset-normalizer 3.1.0
colorama           0.4.6
ffmpeg-python      0.2.0
filelock           3.9.0
future             0.18.3
huggingface-hub    0.13.1
idna               3.4
llvmlite           0.39.1
more-itertools     9.1.0
numba              0.56.4
numpy              1.23.5
openai-whisper     20230308
packaging          23.0
pip                23.0.1
PyYAML             6.0
regex              2022.10.31
requests           2.28.2
setuptools         67.6.0
tokenizers         0.13.2
torch              1.13.1
tqdm               4.65.0
transformers       4.26.1
typing_extensions  4.5.0
urllib3            1.26.14
wheel              0.38.4

インストール確認

whisper の ヘルプを見てみる
whisper --help

ヘルプが表示されたので、無事インストールは出来ました。

usage: whisper [-h] [--model {tiny.en,tiny,base.en,base,small.en,small,medium.en,medium,large-v1,large-v2,large}] [--model_dir MODEL_DIR] [--device DEVICE] [--output_dir OUTPUT_DIR] [--output_format {txt,vtt,srt,tsv,json,all}]  
               [--verbose VERBOSE] [--task {transcribe,translate}]
               [--language {af,am,ar,as,az,ba,be,bg,bn,bo,br,bs,ca,cs,cy,da,de,el,en,es,et,eu,fa,fi,fo,fr,gl,gu,ha,haw,he,hi,hr,ht,hu,hy,id,is,it,ja,jw,ka,kk,km,kn,ko,la,lb,ln,lo,lt,lv,mg,mi,mk,ml,mn,mr,ms,mt,my,ne,nl,nn,no,oc,p
a,pl,ps,pt,ro,ru,sa,sd,si,sk,sl,sn,so,sq,sr,su,sv,sw,ta,te,tg,th,tk,tl,tr,tt,uk,ur,uz,vi,yi,yo,zh,Afrikaans,Albanian,Amharic,Arabic,Armenian,Assamese,Azerbaijani,Bashkir,Basque,Belarusian,Bengali,Bosnian,Breton,Bulgarian,Burmese
,Castilian,Catalan,Chinese,Croatian,Czech,Danish,Dutch,English,Estonian,Faroese,Finnish,Flemish,French,Galician,Georgian,German,Greek,Gujarati,Haitian,Haitian Creole,Hausa,Hawaiian,Hebrew,Hindi,Hungarian,Icelandic,Indonesian,Ita
lian,Japanese,Javanese,Kannada,Kazakh,Khmer,Korean,Lao,Latin,Latvian,Letzeburgesch,Lingala,Lithuanian,Luxembourgish,Macedonian,Malagasy,Malay,Malayalam,Maltese,Maori,Marathi,Moldavian,Moldovan,Mongolian,Myanmar,Nepali,Norwegian,
Nynorsk,Occitan,Panjabi,Pashto,Persian,Polish,Portuguese,Punjabi,Pushto,Romanian,Russian,Sanskrit,Serbian,Shona,Sindhi,Sinhala,Sinhalese,Slovak,Slovenian,Somali,Spanish,Sundanese,Swahili,Swedish,Tagalog,Tajik,Tamil,Tatar,Telugu,
Thai,Tibetan,Turkish,Turkmen,Ukrainian,Urdu,Uzbek,Valencian,Vietnamese,Welsh,Yiddish,Yoruba}]
               [--temperature TEMPERATURE] [--best_of BEST_OF] [--beam_size BEAM_SIZE] [--patience PATIENCE] [--length_penalty LENGTH_PENALTY] [--suppress_tokens SUPPRESS_TOKENS] [--initial_prompt INITIAL_PROMPT]
               [--condition_on_previous_text CONDITION_ON_PREVIOUS_TEXT] [--fp16 FP16] [--temperature_increment_on_fallback TEMPERATURE_INCREMENT_ON_FALLBACK] [--compression_ratio_threshold COMPRESSION_RATIO_THRESHOLD]
               [--logprob_threshold LOGPROB_THRESHOLD] [--no_speech_threshold NO_SPEECH_THRESHOLD] [--word_timestamps WORD_TIMESTAMPS] [--prepend_punctuations PREPEND_PUNCTUATIONS] [--append_punctuations APPEND_PUNCTUATIONS]    
               [--threads THREADS]
               audio [audio ...]

positional arguments:
  audio                 audio file(s) to transcribe

options:
  -h, --help            show this help message and exit
  --model {tiny.en,tiny,base.en,base,small.en,small,medium.en,medium,large-v1,large-v2,large}
                        name of the Whisper model to use (default: small)
  --model_dir MODEL_DIR
                        the path to save model files; uses ~/.cache/whisper by default (default: None)
  --device DEVICE       device to use for PyTorch inference (default: cpu)
  --output_dir OUTPUT_DIR, -o OUTPUT_DIR
                        directory to save the outputs (default: .)
  --output_format {txt,vtt,srt,tsv,json,all}, -f {txt,vtt,srt,tsv,json,all}
                        format of the output file; if not specified, all available formats will be produced (default: all)
  --verbose VERBOSE     whether to print out the progress and debug messages (default: True)
  --task {transcribe,translate}
                        if the gzip compression ratio is higher than this value, treat the decoding as failed (default: 2.4)
  --logprob_threshold LOGPROB_THRESHOLD
                        if the average log probability is lower than this value, treat the decoding as failed (default: -1.0)
  --no_speech_threshold NO_SPEECH_THRESHOLD
                        if the probability of the <|nospeech|> token is higher than this value AND the decoding has failed due to `logprob_threshold`, consider the segment as silence (default: 0.6)
  --word_timestamps WORD_TIMESTAMPS
                        (experimental) extract word-level timestamps and refine the results based on them (default: False)
  --prepend_punctuations PREPEND_PUNCTUATIONS
                        if word_timestamps is True, merge these punctuation symbols with the next word (default: "'“¿([{-)
  --append_punctuations APPEND_PUNCTUATIONS
                        if word_timestamps is True, merge these punctuation symbols with the previous word (default: "'.。,,!!??::”)]}、)
  --threads THREADS     number of threads used by torch for CPU inference; supercedes MKL_NUM_THREADS/OMP_NUM_THREADS (default: 0)

容量

結構ファイル容量ありますね。

ffmpeg インストール(設置と環境変数設定)

ダウンロード

https://ffmpeg.org/download.html#build-windows

Windowsなので、Windowsマーク → 上段の「Windows builds from gyan.dev」を今回選択した。

何やらいろいろなバージョンがあるが、今回は「release builds」にある「latest release version: 6.0 2023-03-04」の「ffmpeg-release-essentials.zip」をダウンロードした。

設置

zipファイルを解凍して、今回はCドライブの「soft」フォルダ内に保存した。

環境変数登録

先ほど設置したフォルダ内にある「bin」フォルダに対して環境変数を登録します。

インストール確認

ffmpeg のバージョンffmpeg -versionを見てみる
ffmpeg -version

バージョンが表示されたので、無事設置は出来ました。

ffmpeg version 6.0-essentials_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers                                                                                                           
built with gcc 12.2.0 (Rev10, Built by MSYS2 project)                                                                                                                                                    
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib 
--enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --
enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enab
le-dxva2 --enable-libvpl --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex
 --enable-libvorbis --enable-librubberband
libavutil      58.  2.100 / 58.  2.100
libavcodec     60.  3.100 / 60.  3.100
libavformat    60.  3.100 / 60.  3.100
libavdevice    60.  1.100 / 60.  1.100
libavfilter     9.  3.100 /  9.  3.100
libswscale      7.  1.100 /  7.  1.100
libswresample   4. 10.100 /  4. 10.100
libpostproc    57.  1.100 / 57.  1.100

試してみる

githubにサンプルコード、サンプル音声ファイルがありましたので、

https://github.com/openai/whisper/blob/main/tests/jfk.flac

import whisper

model = whisper.load_model("base")
result = model.transcribe("tests_jfk.flac")
print(result["text"])

何やらWarningがでていますが、無事文字起こしが完了しました!

And so my fellow Americans, ask not what your country can do for you, ask what you can do for your country.

↑最初何かのエラーかと思ってしまいました。。。

DeepLで翻訳したら「だから同胞のアメリカ人たちよ、自分の国が自分のために何ができるかではなく、自分が自分の国のために何ができるかを問え。」

😱!何!?アメリカ万歳!?

・・・サンプルの音声ね😅も

モデル "large”

精度が高いらしいので、試してみたところ、まずはダウンロードが始まった。2.87GB

日本語も問題なく文字お越しができました!

[00:30.000 --> 00:47.000] 生きたいと願った たった一度生まれてきた奇跡は計り知れない
[00:47.000 --> 00:56.000] だから だからこそ 怖いのかもしれない
[00:56.000 --> 01:08.000] レールに乗っかるのも 無限に広がる自由も

プロフィール背景画像 プロフィール画像
名前:田中寛之
今まで約20年間、出版社、マスコミ、化粧品会社、システム会社・病院・クリニックにてシステムエンジニアとして勤務。現在、個人事業主「ONLINE-WORLD」として活動し、42歳からPythonを始めました!
プロフィール背景画像 プロフィール画像
名前:田中寛之
今まで約20年間、出版社、マスコミ、化粧品会社、システム会社・病院・クリニックにてシステムエンジニアとして勤務。現在、個人事業主「ONLINE-WORLD」として活動し、42歳からPythonを始めました!