systran--faster-whisper
304 行
15 KiB
Markdown
304 行
15 KiB
Markdown
<!-- WEHUB_ZH_README -->
|
||
> [!NOTE]
|
||
> 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
|
||
> [English](./README.en.md) · [原始项目](https://github.com/SYSTRAN/faster-whisper) · [上游 README](https://github.com/SYSTRAN/faster-whisper/blob/HEAD/README.md)
|
||
> 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
|
||
|
||
[](https://github.com/SYSTRAN/faster-whisper/actions?query=workflow%3ACI) [](https://badge.fury.io/py/faster-whisper)
|
||
|
||
# 使用 CTranslate2 的 Faster Whisper 转录
|
||
|
||
**faster-whisper** 是使用 [CTranslate2](https://github.com/OpenNMT/CTranslate2/), 对 OpenAI Whisper 模型的重新实现,CTranslate2 是一个用于 Transformer 模型的快速推理引擎。
|
||
|
||
在相同精度下,该实现的运行速度最高可达 [openai/whisper](https://github.com/openai/whisper) 的 4 倍,同时占用更少内存。通过在 CPU 和 GPU 上使用 8 位量化,效率还可以进一步提升。
|
||
|
||
## 基准测试
|
||
|
||
### Whisper
|
||
|
||
作为参考,以下是使用不同实现转录 [**13 分钟**](https://www.youtube.com/watch?v=0u7tTptBo9I) 音频所需的时间和内存占用:
|
||
|
||
* [openai/whisper](https://github.com/openai/whisper)@[v20240930](https://github.com/openai/whisper/tree/v20240930)
|
||
* [whisper.cpp](https://github.com/ggerganov/whisper.cpp)@[v1.7.2](https://github.com/ggerganov/whisper.cpp/tree/v1.7.2)
|
||
* [transformers](https://github.com/huggingface/transformers)@[v4.46.3](https://github.com/huggingface/transformers/tree/v4.46.3)
|
||
* [faster-whisper](https://github.com/SYSTRAN/faster-whisper)@[v1.1.0](https://github.com/SYSTRAN/faster-whisper/tree/v1.1.0)
|
||
|
||
### GPU 上的 Large-v2 模型
|
||
|
||
| 实现 | 精度 | Beam size | 时间 | VRAM 占用 |
|
||
| --- | --- | --- | --- | --- |
|
||
| openai/whisper | fp16 | 5 | 2m23s | 4708MB |
|
||
| whisper.cpp (Flash Attention) | fp16 | 5 | 1m05s | 4127MB |
|
||
| transformers (SDPA)[^1] | fp16 | 5 | 1m52s | 4960MB |
|
||
| faster-whisper | fp16 | 5 | 1m03s | 4525MB |
|
||
| faster-whisper (`batch_size=8`) | fp16 | 5 | 17s | 6090MB |
|
||
| faster-whisper | int8 | 5 | 59s | 2926MB |
|
||
| faster-whisper (`batch_size=8`) | int8 | 5 | 16s | 4500MB |
|
||
|
||
### GPU 上的 distil-whisper-large-v3 模型
|
||
|
||
| 实现 | 精度 | Beam size | 时间 | YT Commons WER |
|
||
| --- | --- | --- | --- | --- |
|
||
| transformers (SDPA) (`batch_size=16`) | fp16 | 5 | 46m12s | 14.801 |
|
||
| faster-whisper (`batch_size=16`) | fp16 | 5 | 25m50s | 13.527 |
|
||
|
||
*GPU 基准测试在配备 8GB 显存的 NVIDIA RTX 3070 Ti 上,使用 CUDA 12.4 执行。*
|
||
[^1]: 对于 batch size > 1 的任何情况,transformers 会出现 OOM
|
||
|
||
### CPU 上的 Small 模型
|
||
|
||
| 实现 | 精度 | Beam size | 时间 | RAM 占用 |
|
||
| --- | --- | --- | --- | --- |
|
||
| openai/whisper | fp32 | 5 | 6m58s | 2335MB |
|
||
| whisper.cpp | fp32 | 5 | 2m05s | 1049MB |
|
||
| whisper.cpp (OpenVINO) | fp32 | 5 | 1m45s | 1642MB |
|
||
| faster-whisper | fp32 | 5 | 2m37s | 2257MB |
|
||
| faster-whisper (`batch_size=8`) | fp32 | 5 | 1m06s | 4230MB |
|
||
| faster-whisper | int8 | 5 | 1m42s | 1477MB |
|
||
| faster-whisper (`batch_size=8`) | int8 | 5 | 51s | 3608MB |
|
||
|
||
*在 Intel Core i7-12700K 上使用 8 个线程执行。*
|
||
|
||
|
||
## 环境要求
|
||
|
||
* Python 3.9 或更高版本
|
||
|
||
与 openai-whisper 不同,系统**不**需要安装 FFmpeg。音频解码通过 Python 库 [PyAV](https://github.com/PyAV-Org/PyAV) 完成,该库在其软件包中捆绑了 FFmpeg 库。
|
||
|
||
### GPU
|
||
|
||
GPU 执行需要安装以下 NVIDIA 库:
|
||
|
||
* [cuBLAS for CUDA 12](https://developer.nvidia.com/cublas)
|
||
* [cuDNN 9 for CUDA 12](https://developer.nvidia.com/cudnn)
|
||
|
||
**注意**:最新版本的 `ctranslate2` 仅支持 CUDA 12 和 cuDNN 9。对于 CUDA 11 和 cuDNN 8,目前的变通方法是降级到 `ctranslate2` 的 `3.24.0` 版本;对于 CUDA 12 和 cuDNN 8,则降级到 `ctranslate2` 的 `4.4.0` 版本(可通过 `pip install --force-reinstall ctranslate2==4.4.0` 完成,或在 `requirements.txt` 中指定版本)。
|
||
|
||
安装上述 NVIDIA 库有多种方式。推荐方式见官方 NVIDIA 文档,但我们也建议在下方采用其他安装方法。
|
||
|
||
<details>
|
||
<summary>其他安装方法(点击展开)</summary>
|
||
|
||
|
||
**注意:** 对于以下所有方法,请记住上述关于 CUDA 版本的说明。根据你的环境,你可能需要安装与下方说明中列出的 CUDA 12 库相对应的 _CUDA 11_ 版本库。
|
||
|
||
#### 使用 Docker
|
||
|
||
这些库(cuBLAS、cuDNN)已安装在官方 NVIDIA CUDA Docker 镜像中:`nvidia/cuda:12.3.2-cudnn9-runtime-ubuntu22.04`。
|
||
|
||
#### 使用 `pip` 安装(仅 Linux)
|
||
|
||
在 Linux 上,可以使用 `pip` 安装这些库。请注意,在启动 Python 之前必须设置 `LD_LIBRARY_PATH`。
|
||
|
||
```bash
|
||
pip install nvidia-cublas-cu12 nvidia-cudnn-cu12==9.*
|
||
|
||
export LD_LIBRARY_PATH=`python3 -c 'import os; import nvidia.cublas.lib; import nvidia.cudnn.lib; print(os.path.dirname(nvidia.cublas.lib.__file__) + ":" + os.path.dirname(nvidia.cudnn.lib.__file__))'`
|
||
```
|
||
|
||
#### 从 Purfview 的仓库下载库(Windows 和 Linux)
|
||
|
||
Purfview 的 [whisper-standalone-win](https://github.com/Purfview/whisper-standalone-win) 为 Windows 和 Linux 提供了所需的 NVIDIA 库,打包在[单个压缩包](https://github.com/Purfview/whisper-standalone-win/releases/tag/libs). 中。解压后将库放置在包含在 `PATH` 中的目录里。
|
||
|
||
</details>
|
||
|
||
## 安装
|
||
|
||
该模块可从 [PyPI](https://pypi.org/project/faster-whisper/): 安装:
|
||
|
||
```bash
|
||
pip install faster-whisper
|
||
```
|
||
|
||
<details>
|
||
<summary>其他安装方法(点击展开)</summary>
|
||
|
||
### 安装 master 分支
|
||
|
||
```bash
|
||
pip install --force-reinstall "faster-whisper @ https://github.com/SYSTRAN/faster-whisper/archive/refs/heads/master.tar.gz"
|
||
```
|
||
|
||
### 安装特定提交
|
||
|
||
```bash
|
||
pip install --force-reinstall "faster-whisper @ https://github.com/SYSTRAN/faster-whisper/archive/a4f1cc8f11433e454c3934442b5e1a4ed5e865c3.tar.gz"
|
||
```
|
||
|
||
</details>
|
||
|
||
## 用法
|
||
|
||
### Faster-whisper
|
||
|
||
```python
|
||
from faster_whisper import WhisperModel
|
||
|
||
model_size = "large-v3"
|
||
|
||
# Run on GPU with FP16
|
||
model = WhisperModel(model_size, device="cuda", compute_type="float16")
|
||
|
||
# or run on GPU with INT8
|
||
# model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
|
||
# or run on CPU with INT8
|
||
# model = WhisperModel(model_size, device="cpu", compute_type="int8")
|
||
|
||
segments, info = model.transcribe("audio.mp3", beam_size=5)
|
||
|
||
print("Detected language '%s' with probability %f" % (info.language, info.language_probability))
|
||
|
||
for segment in segments:
|
||
print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
|
||
```
|
||
|
||
**警告:** `segments` 是一个*生成器*(generator),因此只有在你对其进行迭代时,转录才会开始。可以通过将片段收集到列表中,或使用 `for` 循环,来运行转录直至完成:
|
||
|
||
```python
|
||
segments, _ = model.transcribe("audio.mp3")
|
||
segments = list(segments) # The transcription will actually run here.
|
||
```
|
||
|
||
### 批量转录
|
||
|
||
以下代码片段演示了如何对示例音频文件运行批量转录。`BatchedInferencePipeline.transcribe` 可直接替代 `WhisperModel.transcribe`
|
||
|
||
```python
|
||
from faster_whisper import WhisperModel, BatchedInferencePipeline
|
||
|
||
model = WhisperModel("turbo", device="cuda", compute_type="float16")
|
||
batched_model = BatchedInferencePipeline(model=model)
|
||
segments, info = batched_model.transcribe("audio.mp3", batch_size=16)
|
||
|
||
for segment in segments:
|
||
print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
|
||
```
|
||
|
||
### Faster Distil-Whisper
|
||
|
||
Distil-Whisper 检查点与 Faster-Whisper 软件包兼容。特别是,最新的 [distil-large-v3](https://huggingface.co/distil-whisper/distil-large-v3)
|
||
检查点本质上就是为配合 Faster-Whisper 转录算法而设计的。以下代码片段演示了如何在指定音频文件上使用 distil-large-v3 运行推理:
|
||
|
||
```python
|
||
from faster_whisper import WhisperModel
|
||
|
||
model_size = "distil-large-v3"
|
||
|
||
model = WhisperModel(model_size, device="cuda", compute_type="float16")
|
||
segments, info = model.transcribe("audio.mp3", beam_size=5, language="en", condition_on_previous_text=False)
|
||
|
||
for segment in segments:
|
||
print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
|
||
```
|
||
|
||
有关 distil-large-v3 模型的更多信息,请参阅原始 [model card](https://huggingface.co/distil-whisper/distil-large-v3).
|
||
|
||
### 词级时间戳(Word-level timestamps)
|
||
|
||
```python
|
||
segments, _ = model.transcribe("audio.mp3", word_timestamps=True)
|
||
|
||
for segment in segments:
|
||
for word in segment.words:
|
||
print("[%.2fs -> %.2fs] %s" % (word.start, word.end, word.word))
|
||
```
|
||
|
||
### VAD 滤波器(VAD filter)
|
||
|
||
该库集成了 [Silero VAD](https://github.com/snakers4/silero-vad) 模型,用于滤除音频中没有语音的部分:
|
||
|
||
```python
|
||
segments, _ = model.transcribe("audio.mp3", vad_filter=True)
|
||
```
|
||
|
||
默认行为较为保守,仅移除长于 2 秒的静音。可在 [source code](https://github.com/SYSTRAN/faster-whisper/blob/master/faster_whisper/vad.py). 中查看可用的 VAD 参数及其默认值。可通过字典参数 `vad_parameters` 进行自定义:
|
||
|
||
```python
|
||
segments, _ = model.transcribe(
|
||
"audio.mp3",
|
||
vad_filter=True,
|
||
vad_parameters=dict(min_silence_duration_ms=500),
|
||
)
|
||
```
|
||
批量转写默认启用 Vad 滤波器。
|
||
|
||
### 日志(Logging)
|
||
|
||
可按如下方式配置库的日志级别:
|
||
|
||
```python
|
||
import logging
|
||
|
||
logging.basicConfig()
|
||
logging.getLogger("faster_whisper").setLevel(logging.DEBUG)
|
||
```
|
||
|
||
### 进一步探索(Going further)
|
||
|
||
在 [`WhisperModel`](https://github.com/SYSTRAN/faster-whisper/blob/master/faster_whisper/transcribe.py) 类实现中查看更多模型与转写选项。
|
||
|
||
## 社区集成(Community integrations)
|
||
|
||
以下是使用 faster-whisper 的开源项目列表(非穷尽)。欢迎将你的项目添加到列表中!
|
||
|
||
|
||
* [speaches](https://github.com/speaches-ai/speaches) 是基于 `faster-whisper` 的 OpenAI 兼容服务器。它易于通过 Docker 部署,可与 OpenAI SDKs/CLI 配合使用,并支持流式传输与实时转写。
|
||
* [WhisperX](https://github.com/m-bain/whisperX) 是一款屡获殊荣的 Python 库,通过 wav2vec2 对齐提供说话人分离(speaker diarization)和精确的词级时间戳
|
||
* [whisper-ctranslate2](https://github.com/Softcatala/whisper-ctranslate2) 是基于 faster-whisper 的命令行客户端,并与 openai/whisper 的原始客户端兼容。
|
||
* [whisper-diarize](https://github.com/MahmoudAshraf97/whisper-diarization) 是一款基于 faster-whisper 和 NVIDIA NeMo 的说话人分离工具。
|
||
* [whisper-standalone-win](https://github.com/Purfview/whisper-standalone-win) faster-whisper 在 Windows、Linux 和 macOS 上的独立 CLI 可执行文件。
|
||
* [asr-sd-pipeline](https://github.com/hedrergudene/asr-sd-pipeline) 提供基于 AzureML pipelines 实现的可扩展、模块化、端到端的多说话人语音转文本解决方案。
|
||
* [Open-Lyrics](https://github.com/zh-plus/Open-Lyrics) 是一款 Python 库,使用 faster-whisper 转写语音文件,并利用 OpenAI-GPT 将结果文本翻译/润色为所需语言的 `.lrc` 文件。
|
||
* [wscribe](https://github.com/geekodour/wscribe) 是一款灵活的转录生成工具,支持 faster-whisper,可导出词级转录,导出的转录可使用 [wscribe-editor](https://github.com/geekodour/wscribe-editor) 进行编辑
|
||
* [aTrain](https://github.com/BANDAS-Center/aTrain) 是格拉茨大学 BANDAS-Center 开发的 faster-whisper 图形用户界面实现,用于在 Windows([Windows Store App](https://apps.microsoft.com/detail/atrain/9N15Q44SZNS2)))和 Linux 上进行转写与说话人分离。
|
||
* [Whisper-Streaming](https://github.com/ufal/whisper_streaming) 为离线类 Whisper 语音转文本模型实现实时模式,并以 faster-whisper 作为最推荐的后端。它实现了基于实际源复杂度的自适应延迟流式策略,并展示了当前最先进水平。
|
||
* [WhisperLive](https://github.com/collabora/WhisperLive) 是 OpenAI Whisper 的近实时实现,使用 faster-whisper 作为后端实时转写音频。
|
||
* [Faster-Whisper-Transcriber](https://github.com/BBC-Esq/ctranslate2-faster-whisper-transcriber) 是一款简单但可靠的语音转写器,提供用户友好的界面。
|
||
* [Open-dubbing](https://github.com/softcatala/open-dubbing) open dubbing 是一款 AI 配音系统,使用机器学习模型自动将音频对话翻译并同步到不同语言。
|
||
* [Whisper-FastAPI](https://github.com/heimoshuiyu/whisper-fastapi) whisper-fastapi 是一个非常简单的脚本,提供兼容 OpenAI、HomeAssistant 和 Konele(Android 语音输入)格式的 API 后端。
|
||
|
||
## 模型转换(Model conversion)
|
||
|
||
当按尺寸加载模型(例如 `WhisperModel("large-v3")`)时,相应的 CTranslate2 模型会自动从 [Hugging Face Hub](https://huggingface.co/Systran). 下载。
|
||
|
||
我们还提供了一个脚本,用于转换任何与 Transformers 库兼容的 Whisper 模型。这些模型可以是原始 OpenAI 模型,也可以是用户微调后的模型。
|
||
|
||
例如,下面的命令会转换 [original "large-v3" Whisper model](https://huggingface.co/openai/whisper-large-v3) 并以 FP16 格式保存权重:
|
||
|
||
```bash
|
||
pip install transformers[torch]>=4.23
|
||
|
||
ct2-transformers-converter --model openai/whisper-large-v3 --output_dir whisper-large-v3-ct2
|
||
--copy_files tokenizer.json preprocessor_config.json --quantization float16
|
||
```
|
||
|
||
* 选项 `--model` 接受 Hub 上的模型名称或模型目录路径。
|
||
* 如果未使用选项 `--copy_files tokenizer.json`,则在后续加载模型时会自动下载 tokenizer 配置。
|
||
|
||
也可以从代码中进行模型转换。请参阅 [conversion API](https://opennmt.net/CTranslate2/python/ctranslate2.converters.TransformersConverter.html).
|
||
|
||
### 加载已转换的模型
|
||
|
||
1. 直接从本地目录加载模型:
|
||
```python
|
||
model = faster_whisper.WhisperModel("whisper-large-v3-ct2")
|
||
```
|
||
|
||
2. [Upload your model to the Hugging Face Hub](https://huggingface.co/docs/transformers/model_sharing#upload-with-the-web-interface) 并按名称加载:
|
||
```python
|
||
model = faster_whisper.WhisperModel("username/whisper-large-v3-ct2")
|
||
```
|
||
|
||
## 与其他实现的性能对比
|
||
|
||
如果你正在将性能与其他 Whisper 实现进行对比,应确保在相似设置下运行对比。特别要注意:
|
||
|
||
* 确认使用相同的转写选项,尤其是相同的 beam size。例如在 openai/whisper 中,`model.transcribe` 默认 beam size 为 1,而此处默认 beam size 为 5。
|
||
* 转写速度与转录文本中的词数密切相关,因此请确保其他实现与本实现的 WER(Word Error Rate,词错误率)相近。
|
||
* 在 CPU 上运行时,请确保设置相同的线程数。许多框架会读取环境变量 `OMP_NUM_THREADS`,可在运行脚本时设置:
|
||
|
||
```bash
|
||
OMP_NUM_THREADS=4 python3 my_script.py
|
||
```
|