Whisper - ロバスト音声認識
OpenAIの多言語音声認識モデル。
Whisperを使用するタイミング
次のような場合に使用します:
- 音声テキスト変換(99言語)
- ポッドキャスト/ビデオの文字起こし
- ミーティングノートの自動化
- 英語への翻訳
- ノイズの多い音声の文字起こし
- 多言語音声処理
指標:
- 72,900以上のGitHubスター
- 99言語対応
- 680,000時間の音声で学習
- MITライセンス
代わりに代替案を使用する場合:
- AssemblyAI: マネージドAPI、話者分離
- Deepgram: リアルタイムストリーミングASR
- Google Speech-to-Text: クラウドベース
クイックスタート
インストール
# Python 3.8-3.11が必要
pip install -U openai-whisper
# ffmpegが必要
# macOS: brew install ffmpeg
# Ubuntu: sudo apt install ffmpeg
# Windows: choco install ffmpeg
基本的な文字起こし
import whisper
# モデルを読み込む
model = whisper.load_model("base")
# 文字起こし
result = model.transcribe("audio.mp3")
# テキストを表示
print(result["text"])
# セグメントにアクセス
for segment in result["segments"]:
print(f"[{segment['start']:.2f}s - {segment['end']:.2f}s] {segment['text']}")
モデルサイズ
# 利用可能なモデル
models = ["tiny", "base", "small", "medium", "large", "turbo"]
# 特定のモデルを読み込む
model = whisper.load_model("turbo") # 最速、高品質
| モデル | パラメータ数 | 英語専用 | 多言語 | 速度 | VRAM |
|---|---|---|---|---|---|
| tiny | 39M | ✓ | ✓ | ~32x | ~1 GB |
| base | 74M | ✓ | ✓ | ~16x | ~1 GB |
| small | 244M | ✓ | ✓ | ~6x | ~2 GB |
| medium | 769M | ✓ | ✓ | ~2x | ~5 GB |
| large | 1550M | ✗ | ✓ | 1x | ~10 GB |
| turbo | 809M | ✗ | ✓ | ~8x | ~6 GB |
推奨事項: 速度と品質のバランスが最適なturbo、プロトタイピングにはbaseを使用
文字起こしオプション
言語指定
# 言語の自動検出
result = model.transcribe("audio.mp3")
# 言語を指定(より高速)
result = model.transcribe("audio.mp3", language="en")
# 対応言語: en, es, fr, de, it, pt, ru, ja, ko, zh、さらに89言語
タスク選択
# 文字起こし(デフォルト)
result = model.transcribe("audio.mp3", task="transcribe")
# 英語への翻訳
result = model.transcribe("spanish.mp3", task="translate")
# 入力: スペイン語の音声 → 出力: 英語のテキスト
初期プロンプト
# コンテキストを指定して精度を向上
result = model.transcribe(
"audio.mp3",
initial_prompt="これは機械学習とAIに関する技術ポッドキャストです。"
)
# 次のような場合に役立ちます:
# - 技術用語
# - 固有名詞
# - ドメイン固有の語彙
タイムスタンプ
# 単語レベルのタイムスタンプ
result = model.transcribe("audio.mp3", word_timestamps=True)
for segment in result["segments"]:
for word in segment["words"]:
print(f"{word['word']} ({word['start']:.2f}s - {word['end']:.2f}s)")
温度フォールバック
# 信頼度が低い場合、異なる温度で再試行
result = model.transcribe(
"audio.mp3",
temperature=(0.0, 0.2, 0.4, 0.6, 0.8, 1.0)
)
コマンドラインでの使用
# 基本的な文字起こし
whisper audio.mp3
# モデルを指定
whisper audio.mp3 --model turbo
# 出力形式
whisper audio.mp3 --output_format txt # プレーンテキスト
whisper audio.mp3 --output_format srt # 字幕
whisper audio.mp3 --output_format vtt # WebVTT
whisper audio.mp3 --output_format json # タイムスタンプ付きJSON
# 言語
whisper audio.mp3 --language Spanish
# 翻訳
whisper spanish.mp3 --task translate
バッチ処理
import os
audio_files = ["file1.mp3", "file2.mp3", "file3.mp3"]
for audio_file in audio_files:
print(f"{audio_file}を文字起こし中...")
result = model.transcribe(audio_file)
# ファイルに保存
output_file = audio_file.replace(".mp3", ".txt")
with open(output_file, "w") as f:
f.write(result["text"])
リアルタイム文字起こし
# ストリーミング音声にはfaster-whisperを使用
# pip install faster-whisper
from faster_whisper import WhisperModel
model = WhisperModel("base", device="cuda", compute_type="float16")
# ストリーミングでの文字起こし
segments, info = model.transcribe("audio.mp3", beam_size=5)
for segment in segments:
print(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}")
GPUアクセラレーション
import whisper
# GPUが利用可能な場合は自動的に使用
model = whisper.load_model("turbo")
# CPUを強制
model = whisper.load_model("turbo", device="cpu")
# GPUを強制
model = whisper.load_model("turbo", device="cuda")
# GPUで10~20倍高速
他のツールとの連携
字幕生成
# SRT字幕を生成
whisper video.mp4 --output_format srt --language English
# 出力: video.srt
LangChainとの連携
from langchain.document_loaders import WhisperTranscriptionLoader
loader = WhisperTranscriptionLoader(file_path="audio.mp3")
docs = loader.load()
# RAGで文字起こしを使用
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
vectorstore = Chroma.from_documents(docs, OpenAIEmbeddings())
ビデオから音声を抽出
# ffmpegを使用して音声を抽出
ffmpeg -i video.mp4 -vn -acodec pcm_s16le audio.wav
# その後文字起こし
whisper audio.wav
ベストプラクティス
- turboモデルを使用 - 英語で最適な速度/品質
- 言語を指定 - 自動検出よりも高速
- 初期プロンプトを追加 - 技術用語の精度が向上
- GPUを使用 - 10~20倍高速
- バッチ処理 - より効率的
- WAVに変換 - 互換性が向上
- 長い音声を分割 - 30分未満のチャンクに
- 言語サポートを確認 - 言語によって品質が異なる
- faster-whisperを使用 - openai-whisperより4倍高速
- VRAMを監視 - モデルサイズをハードウェアに合わせる
パフォーマンス
| モデル | リアルタイムファクター(CPU) | リアルタイムファクター(GPU) |
|---|---|---|
| tiny | ~0.32 | ~0.01 |
| base | ~0.16 | ~0.01 |
| turbo | ~0.08 | ~0.01 |
| large | ~1.0 | ~0.05 |
リアルタイムファクター: 0.1 = リアルタイムの10倍高速
言語サポート
最もサポートされている言語:
- 英語 (en)
- スペイン語 (es)
- フランス語 (fr)
- ドイツ語 (de)
- イタリア語 (it)
- ポルトガル語 (pt)
- ロシア語 (ru)
- 日本語 (ja)
- 韓国語 (ko)
- 中国語 (zh)
全リスト: 合計99言語
制限事項
- 幻覚 - テキストを繰り返したり、でっち上げる可能性がある
- 長尺の精度 - 30分以上の音声で精度が低下
- 話者識別 - 話者分離は未対応
- アクセント - 言語によって品質が異なる
- 背景ノイズ - 精度に影響を与える可能性がある
- リアルタイム遅延 - ライブキャプションには不向き
リソース
- GitHub: https://github.com/openai/whisper ⭐ 72,900+
- 論文: https://arxiv.org/abs/2212.04356
- モデルカード: https://github.com/openai/whisper/blob/main/model-card.md
- Colab: リポジトリ内で利用可能
- ライセンス: MIT