模型简介
模型特点
模型能力
使用案例
library_name: transformers base_model: openai/whisper-large-v3 language:
- sv pipeline_tag: automatic-speech-recognition license: apache-2.0 datasets:
- KBLab/rixvox-v2 tags:
- ctranslate2
KB-Whisper大模型
瑞典国家图书馆发布了一系列基于超过5万小时瑞典语音训练的Whisper新模型。在FLEURS、CommonVoice和NST的评估中,我们表现最佳的模型相比OpenAI的whisper-large-v3
平均降低了47%的词错误率(WER)。更小尺寸的Whisper模型在瑞典语音上的表现也有显著提升,其中kb-whisper-small
甚至超越了体积是其六倍的openai/whisper-large-v3
。
模型尺寸 | FLEURS | CommonVoice | NST | |
---|---|---|---|---|
tiny | KBLab | 13.2 | 12.9 | 11.2 |
OpenAI | 59.2 | 67.8 | 85.2 | |
base | KBLab | 9.1 | 8.7 | 7.8 |
OpenAI | 39.6 | 52.1 | 53.4 | |
small | KBLab | 7.3 | 6.4 | 6.6 |
OpenAI | 20.6 | 26.4 | 26.4 | |
medium | KBLab | 6.6 | 5.4 | 5.8 |
OpenAI | 12.1 | 15.8 | 17.1 | |
large-v3 | KBLab | 5.4 | 4.1 | 5.2 |
OpenAI | 7.8 | 9.5 | 11.3 |
表:KBLab的Whisper模型与对应OpenAI版本的**词错误率(WER)**对比。
使用方法
我们提供多种格式的模型检查点:Hugging Face
、whisper.cpp
(GGML)、onnx
和ctranslate2
(用于faster-whisper
和WhisperX
)。
2025-05-13更新!
通过Hugging Face加载我们的模型时,默认使用第二阶段版本。截至2025年5月,除默认版本外,还有两种第二阶段版本,分别是字幕和严格,用于指定转录风格。通过在.from_pretrained()
中指定revision="subtitle"
,可以访问转录风格更简洁的模型版本;指定revision="strict"
则可以访问更逐字逐句的模型版本。以下是.from_pretrained()
函数中传递该参数的示例:
import torch
from datasets import load_dataset
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = "KBLab/kb-whisper-large"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, use_safetensors=True, cache_dir="cache", revision="strict"
)
三种模型版本的转录风格从最简洁的字幕,到默认的第二阶段,再到最详细的严格,逐级递增。
Hugging Face
使用KB-Whisper
与Hugging Face进行推理的示例:
import torch
from datasets import load_dataset
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = "KBLab/kb-whisper-large"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, use_safetensors=True, cache_dir="cache"
)
model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
torch_dtype=torch_dtype,
device=device,
)
generate_kwargs = {"task": "transcribe", "language": "sv"}
# 添加return_timestamps=True以输出带时间戳的结果
res = pipe("audio.mp3",
chunk_length_s=30,
generate_kwargs={"task": "transcribe", "language": "sv"})
print(res)
Faster-whisper
Faster-whisper通过使用ctranslate2
重新实现Whisper,提供了快速高效的推理。
#### faster-whisper模型 ####
from faster_whisper import WhisperModel
model_id = "KBLab/kb-whisper-large"
model = WhisperModel(
model_id,
device="cuda",
compute_type="float16",
download_root="cache", # 缓存目录
# condition_on_previous_text = False # 如果不使用提示,可以减少幻觉
)
# 转录audio.wav(首先通过ffmpeg转换为16khz单声道wav)
segments, info = model.transcribe("audio.wav", condition_on_previous_text=False)
print("检测到语言'%s',概率为%f" % (info.language, info.language_probability))
for segment in segments:
print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
WhisperX
WhisperX提供了一种获取精确到单词级别时间戳的便捷方法。该库将Whisper的文本输出与Wav2vec2的精确时间戳进行(强制对齐)结合。以下是如何将KB-Whisper
与KBLab/wav2vec2-large-voxrex-swedish结合使用的示例。
import whisperx
device = "cuda"
audio_file = "audio.wav"
batch_size = 16 # 如果GPU内存不足,可以减少
compute_type = "float16" # 如果GPU内存不足,可以改为"int8"(可能降低准确性)
# 1. 使用原始whisper转录(批处理)
model = whisperx.load_model(
"KBLab/kb-whisper-large", device, compute_type=compute_type, download_root="cache" # 缓存目录
)
audio = whisperx.load_audio(audio_file)
result = model.transcribe(audio, batch_size=batch_size)
print(result["segments"]) # 对齐前
# 如果GPU资源不足,删除模型
# import gc; gc.collect(); torch.cuda.empty_cache(); del model
# 2. 对齐whisper输出
model_a, metadata = whisperx.load_align_model(
language_code=result["language"],
device=device,
model_name="KBLab/wav2vec2-large-voxrex-swedish",
model_dir="cache", # 缓存目录
)
result = whisperx.align(
result["segments"], model_a, metadata, audio, device, return_char_alignments=False
)
print(result["segments"]) # 对齐后的单词级别时间戳
Whisper.cpp / GGML
我们提供了用于whisper.cpp
和MacWhisper
应用的GGML检查点。要使用我们的模型与whisper.cpp
,首先克隆仓库并构建库:
git clone https://github.com/ggerganov/whisper.cpp.git
cd whisper.cpp
cmake -B build
cmake --build build --config Release
要使用模型,您需要下载我们上传的GGML检查点之一。您可以点击这里的下载按钮,或使用wget
下载:
wget https://huggingface.co/KBLab/kb-whisper-large/resolve/main/ggml-model-q5_0.bin # 量化版本
# wget https://huggingface.co/KBLab/kb-whisper-large/resolve/main/ggml-model.bin # 非量化版本
通过指定-m
参数后的模型路径以及音频文件的路径作为最后一个位置参数来运行推理。
./build/bin/whisper-cli -m ggml-model-q5_0.bin ../audio.wav
onnx(optimum)和transformers.js用法
您可以通过Hugging Face的optimum
库以下列方式使用onnx
检查点:
from optimum.onnxruntime import ORTModelForSpeechSeq2Seq
from transformers import AutoProcessor
model_id = "KBLab/kb-whisper-large"
processor = AutoProcessor.from_pretrained(model_id, cache_dir="cache")
model = ORTModelForSpeechSeq2Seq.from_pretrained(
model_id,
cache_dir="cache",
subfolder="onnx",
)
import soundfile as sf
audio = sf.read("audio.wav")
inputs = processor.feature_extractor(audio[0], sampling_rate=16000, return_tensors="pt")
gen_tokens = model.generate(**inputs, max_length=300)
processor.decode(gen_tokens[0], skip_special_tokens=True)
一个使用transformers.js
和KB-Whisper
在浏览器中本地运行推理的应用示例可以在https://whisper.mesu.re/找到(由Pierre Mesure创建)。设置此类应用的JavaScript模板可以在https://github.com/xenova/whisper-web找到。
训练数据
我们的模型基于超过5万小时的瑞典语音及其文本转录进行训练。模型训练分为两个阶段,每个阶段应用了不同的质量过滤器和阈值。
第一阶段采用低阈值(根据数据集不同,BLEU值在0到0.30之间),而第二阶段使用更严格的阈值(BLEU >= 0.7
,加权ROUGE-N >= 0.7
,首尾10个字符的CER <= 0.2
)。
数据集 | 继续预训练(小时)-- 第一阶段 | 微调(小时)-- 第二阶段 |
---|---|---|
字幕 | 34,261 | 3,110 |
议会 | 21,949 | 5,119 |
ISOF | 54 | 54 |
NST | 250 | 250 |
总计 | 56,514 | 8,533 |
通过Hugging Face加载我们的模型时,默认使用第二阶段版本。不过,我们也上传了继续预训练的检查点并进行了标记。您可以通过在.from_pretrained()
中指定revision
来加载这些其他检查点。预训练检查点的标记可以在这里找到:pretrained-checkpoint
。第二阶段默认模型的标记名为standard
。我们还提供了一个转录风格更简洁的第二阶段检查点,名为subtitle
。
评估
WER
模型尺寸 | FLEURS | CommonVoice | NST | |
---|---|---|---|---|
tiny | KBLab | 13.2 | 12.9 | 11.2 |
OpenAI | 59.2 | 67.8 | 85.2 | |
base | KBLab | 9.1 | 8.7 | 7.8 |
OpenAI | 39.6 | 52.1 | 53.4 | |
small | KBLab | 7.3 | 6.4 | 6.6 |
OpenAI | 20.6 | 26.4 | 26.4 | |
medium | KBLab | 6.6 | 5.4 | 5.8 |
OpenAI | 12.1 | 15.8 | 17.1 | |
large-v3 | KBLab | 5.4 | 4.1 | 5.2 |
OpenAI | 7.8 | 9.5 | 11.3 |
BLEU分数
模型尺寸 | FLEURS | CommonVoice | NST | |
---|---|---|---|---|
tiny | KBLab | 76.6 | 73.7 | 74.3 |
OpenAI | 26.9 | 21.1 | 24.0 | |
base | KBLab | 83.2 | 79.9 | 78.3 |
OpenAI | 41.1 | 32.5 | 36.9 | |
small | KBLab | 86.6 | 83.5 | 79.6 |
OpenAI | 64.0 | 56.5 | 58.2 | |
medium | KBLab | 87.6 | 85.0 | 80.2 |
OpenAI | 77.1 | 70.1 | 68.9 | |
large-v3 | KBLab | 89.8 | 87.2 | 81.1 |
OpenAI | 84.9 | 79.1 | 75.1 |
致谢
我们感谢EuroHPC联合事业为本项目提供了访问EuroHPC超级计算机LEONARDO的机会,该计算机由CINECA(意大利)和LEONARDO联盟托管,通过EuroHPC AI和数据密集型应用访问调用。
引用
论文参考即将发布。



