Whisper
Whisper 是一种通用语音识别模型。它是在大量不同音频数据集上进行训练的,也是一个多任务模型,可以执行多语言语音识别、语音翻译和语言识别。官方地址 github.com/openai/whis…
方法
一个Transformer序列到序列模型被训练在多种语音处理任务上,包括多语言语音识别、语音翻译、口语语言识别以及语音活动检测。这些任务被共同表示为一系列由解码器预测的令牌,使得单一模型能够替代传统语音处理管道中的多个阶段。多任务训练格式采用了一组特殊令牌,作为任务指定符或分类目标。
设置
Whisper使用Python 3.9.9和PyTorch 1.10.1来训练和测试我们的模型,但代码库预期与Python 3.8至3.11版本及近期的PyTorch版本兼容。该代码库还依赖于几个Python包,特别是OpenAI的tiktoken,用于其快速的分词器实现。你可以使用以下命令下载并安装(或更新至)Whisper的最新版本:
pip install -U openai-whisper
或者,以下命令将从这个仓库拉取并安装最新的提交,以及它的Python依赖项:
pip install git+https://github.com/openai/whisper.git
要将包更新到此仓库的最新版本,请运行:
pip install --upgrade --no-deps --force-reinstall git+https://github.com/openai/whisper.git
它还需要在您的系统上安装命令行工具 ffmpeg,该工具大多数包管理器都提供。
# on Ubuntu or Debian
sudo apt update && sudo apt install ffmpeg
# on Arch Linux
sudo pacman -S ffmpeg
# on MacOS using Homebrew (https://brew.sh/)
brew install ffmpeg
# on Windows using Chocolatey (https://chocolatey.org/)
choco install ffmpeg
# on Windows using Scoop (https://scoop.sh/)
scoop install ffmpeg
You may need rust installed as well, in case tiktoken does not provide a pre-built wheel for your platform. If you see installation errors during the pip install command above, please follow the Getting started page to install Rust development environment. Additionally, you may need to configure the PATH environment variable, e.g. export PATH="$HOME/.cargo/bin:$PATH". If the installation fails with No module named 'setuptools_rust', you need to install setuptools_rust, e.g. by running:
pip install setuptools-rust
可用型号和语言
您可能还需要安装 rust,以防 tiktoken 没有为您的平台提供预编译的轮子。如果在上述 pip install 命令过程中遇到安装错误,请按照 入门页面 的指引来安装 Rust 开发环境。此外,您可能需要配置 PATH 环境变量,例如 export PATH="$HOME/.cargo/bin:$PATH"。如果安装过程中出现 No module named 'setuptools_rust' 错误,您需要安装 setuptools_rust,可以通过运行类似命令来完成。
| 尺寸 | 参数大小 | 纯英文型号 | 多语言模型 | 所需显存 | 相对速度 |
|---|---|---|---|---|---|
| tiny | 39 M | tiny.en | tiny | ~1 GB | ~32x |
| base | 74 M | base.en | base | ~1 GB | ~16x |
| small | 244 M | small.en | small | ~2 GB | ~6x |
| medium | 769 M | medium.en | medium | ~5 GB | ~2x |
| large | 1550 M | 不适用 | large | ~10 GB | 1x |
针对仅英文应用的.en模型往往表现更佳,尤其是对于tiny.en和base.en模型。我们观察到,对于small.en和medium.en模型,这种差异变得不那么明显。
Whisper的表现因语言而异,幅度很大。下图展示了使用WER(词错误率)或CER(字符错误率,以斜体显示)在Common Voice 15和Fleurs数据集上评估的large-v3和large-v2模型按语言划分的性能分解。其他模型和数据集对应的更多WER/CER指标,以及用于翻译评估的BLEU(双语评估替代)分数,可以在论文的附录D.1、D.2和D.4中找到。
命令行使用
以下命令将使用 medium 模型转录音频文件中的语音:
whisper audio.flac audio.mp3 audio.wav --model medium
默认设置(选择small模型)非常适合转录英语。要转录包含非英语语音的音频文件,您可以使用以下--language选项指定语言:
whisper japanese.wav --language Japanese
添加--task translate 会将演讲翻译成英语:
whisper japanese.wav --language Japanese --task translate
运行以下命令查看所有可用选项:
whisper --help
有关所有可用语言的列表,请参阅tokenizer.py
Python使用
也可以在 Python 中执行转录:
import whisper
model = whisper.load_model("base")
result = model.transcribe("audio.mp3")
print(result["text"])
在内部,该 transcribe() 方法读取整个文件并使用 30 秒滑动窗口处理音频,对每个窗口执行自回归序列到序列预测。
whisper.detect_language() 下面是和的示例用法whisper.decode() 它提供了对模型的较低级别访问。
import whisper
model = whisper.load_model("base")
# load audio and pad/trim it to fit 30 seconds
audio = whisper.load_audio("audio.mp3")
audio = whisper.pad_or_trim(audio)
# make log-Mel spectrogram and move to the same device as the model
mel = whisper.log_mel_spectrogram(audio).to(model.device)
# detect the spoken language
_, probs = model.detect_language(mel)
print(f"Detected language: {max(probs, key=probs.get)}")
# decode the audio
options = whisper.DecodingOptions()
result = whisper.decode(model, mel, options)
# print the recognized text
print(result.text)
推荐项目
兼容openai接口api服务 gitee.com/taisan/whis…