该存储库提供快速自动语音识别(使用大型 v2 实现 70 倍实时),具有字级时间戳和说话人二值化。
Whisper是 OpenAI 开发的 ASR 模型,在不同音频的大型数据集上进行训练。虽然它确实产生了高度准确的转录,但相应的时间戳是话语级别的,而不是每个单词的,并且可能不准确几秒钟。 OpenAI 的耳语本身并不支持批处理。
基于音素的 ASR一套经过微调的模型,用于识别区分一个单词和另一个单词的最小语音单位,例如“tap”中的元素 p。一个流行的示例模型是 wav2vec2.0。
强制对齐是指将正字法转录与录音对齐以自动生成音素级别分割的过程。
语音活动检测 (VAD)是检测人类语音是否存在。
说话人分类是根据每个说话人的身份将包含人类语音的音频流划分为同质片段的过程。
GPU 执行需要在系统上安装 NVIDIA 库 cuBLAS 11.x 和 cuDNN 8.x。请参阅 CTranslate2 文档。
conda create --name whisperx python=3.10
conda activate whisperx
conda install pytorch==2.0.0 torchaudio==2.0.0 pytorch-cuda=11.8 -c pytorch -c nvidia
请参阅此处的其他方法。
pip install git+https://github.com/m-bain/whisperx.git
如果已安装,请将软件包更新到最新提交
pip install git+https://github.com/m-bain/whisperx.git --upgrade
如果希望修改此包,请克隆并以可编辑模式安装:
$ git clone https://github.com/m-bain/whisperX.git
$ cd whisperX
$ pip install -e .
您可能还需要安装 ffmpeg、rust 等。请按照此处的 openAI 说明进行操作:https://github.com/openai/whisper#setup。
要启用“Speaker Diarization” ,请在--hf_token
参数后面添加您可以从 Here 生成的 Hugging Face 访问令牌(读取),并接受以下模型的用户协议:Segmentation 和 Speaker-Diarization-3.1(如果您选择使用Speaker) -Diarization 2.x,请遵循此处的要求。)
笔记
截至 2023 年 10 月 11 日,whisperX 中存在一个有关 pyannote/Speaker-Diarization-3.0 性能缓慢的已知问题。这是由于 fast-whisper 和 pyannote-audio 3.0.0 之间的依赖冲突造成的。请参阅此问题以了解更多详细信息和潜在的解决方法。
在示例片段上运行耳语(使用默认参数,耳语小)添加--highlight_words True
以可视化 .srt 文件中的单词计时。
whisperx examples/sample01.wav
使用WhisperX强制对齐到 wav2vec2.0 Large 的结果:
将此与原始的耳语进行比较,其中许多转录不同步:
为了提高时间戳精度,以更高的 GPU mem 为代价,使用更大的模型(发现更大的对齐模型没有那么有帮助,请参阅论文)例如
whisperx examples/sample01.wav --model large-v2 --align_model WAV2VEC2_ASR_LARGE_LV60K_960H --batch_size 4
要使用说话者 ID 标记文字记录(如果已知,则设置说话者数量,例如--min_speakers 2
--max_speakers 2
):
whisperx examples/sample01.wav --model large-v2 --diarize --highlight_words True
要在 CPU 而不是 GPU 上运行(以及在 Mac OS X 上运行):
whisperx examples/sample01.wav --compute_type int8
音素 ASR 对齐模型是特定于语言的,对于测试的语言,这些模型是从 torchaudio 管道或 Huggingface 中自动选取的。只需传递--language
代码,并使用 Whisper --model large
。
目前为{en, fr, de, es, it, ja, zh, nl, uk, pt}
提供默认模型。如果检测到的语言不在此列表中,您需要从 Huggingface 模型中心找到基于音素的 ASR 模型,并在您的数据上进行测试。
whisperx --model large-v2 --language de examples/sample_de_01.wav
请在此处查看其他语言的更多示例。
import whisperx
import gc
device = "cuda"
audio_file = "audio.mp3"
batch_size = 16 # reduce if low on GPU mem
compute_type = "float16" # change to "int8" if low on GPU mem (may reduce accuracy)
# 1. Transcribe with original whisper (batched)
model = whisperx . load_model ( "large-v2" , device , compute_type = compute_type )
# save model to local path (optional)
# model_dir = "/path/"
# model = whisperx.load_model("large-v2", device, compute_type=compute_type, download_root=model_dir)
audio = whisperx . load_audio ( audio_file )
result = model . transcribe ( audio , batch_size = batch_size )
print ( result [ "segments" ]) # before alignment
# delete model if low on GPU resources
# import gc; gc.collect(); torch.cuda.empty_cache(); del model
# 2. Align whisper output
model_a , metadata = whisperx . load_align_model ( language_code = result [ "language" ], device = device )
result = whisperx . align ( result [ "segments" ], model_a , metadata , audio , device , return_char_alignments = False )
print ( result [ "segments" ]) # after alignment
# delete model if low on GPU resources
# import gc; gc.collect(); torch.cuda.empty_cache(); del model_a
# 3. Assign speaker labels
diarize_model = whisperx . DiarizationPipeline ( use_auth_token = YOUR_HF_TOKEN , device = device )
# add min/max number of speakers if known
diarize_segments = diarize_model ( audio )
# diarize_model(audio, min_speakers=min_speakers, max_speakers=max_speakers)
result = whisperx . assign_word_speakers ( diarize_segments , result )
print ( diarize_segments )
print ( result [ "segments" ]) # segments are now assigned speaker IDs
如果您无法访问自己的 GPU,请使用上面的链接尝试 WhisperX。
有关批处理和对齐、VAD 的效果以及所选对齐模型的具体细节,请参阅预印本论文。
要减少 GPU 内存要求,请尝试以下任一方法(2. 和 3. 可能会影响质量):
--batch_size 4
--model base
--compute_type int8
与 openai 的耳语转录差异:
--without_timestamps True
,这可确保批处理中每个样本进行 1 次前向传递。但是,这可能会导致默认耳语输出出现差异。--condition_on_prev_text
默认设置为False
(减少幻觉) 如果您会说多种语言,您可以为该项目做出贡献的一个主要方法是在 Huggingface 上找到音素模型(或训练您自己的模型)并在目标语言的语音上测试它们。如果结果看起来不错,请发送拉取请求和一些显示其成功的示例。
错误查找和拉取请求也受到高度赞赏,以保持该项目的继续进行,因为它已经偏离了最初的研究范围。
多语言初始化
基于语言检测的自动对齐模型选择
Python使用
纳入说话人分类
模型刷新,适用于低 GPU 内存资源
更快的耳语后端
添加 max-line 等参见(openai 的耳语 utils.py)
句子级分段(nltk 工具箱)
改进对齐逻辑
通过分类和单词突出显示更新示例
字幕 .ass 输出 <- 将其带回来(在 v3 中删除)
添加基准测试代码(用于 spd/WER 和分词的 TEDLIUM)
允许 silero-vad 作为替代 VAD 选项
改进分类(单词级别)。比最初想象的更难...
如有疑问,请联系 [email protected]。
这项工作和我的博士学位得到了 VGG(视觉几何小组)和牛津大学的支持。
当然,这是建立在openAI的耳语之上的。借用 PyTorch 强制对齐教程中的重要对齐代码,并使用精彩的 pyannote VAD / Diarization https://github.com/pyannote/pyannote-audio
来自 [pyannote audio] 的有价值的 VAD 和二值化模型[https://github.com/pyannote/pyannote-audio]
来自 fast-whisper 和 CTranslate2 的出色后端
那些在经济上支持这项工作的人
最后,感谢该项目的操作系统贡献者,让其继续下去并发现错误。
@article { bain2022whisperx ,
title = { WhisperX: Time-Accurate Speech Transcription of Long-Form Audio } ,
author = { Bain, Max and Huh, Jaesung and Han, Tengda and Zisserman, Andrew } ,
journal = { INTERSPEECH 2023 } ,
year = { 2023 }
}