模型简介
模型特点
模型能力
使用案例
library_name: transformers
base_model: openai/whisper-medium
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
相比,平均将词错误率(WER)降低了47%。小型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 |
表:词错误率(WER) KBLab的Whisper模型与OpenAI对应版本的比较。
使用方法
我们提供不同格式的检查点:Hugging Face
、whisper.cpp
(GGML)、onnx
和ctranslate2
(用于faster-whisper
和WhisperX
)。
2025-05-13 更新!
通过Hugging Face加载我们的模型时,默认版本为Stage 2。
截至2025年5月,除了默认版本外,还有两个Stage 2版本,分别是Subtitle和Strict,用于指定转录风格。
通过在.from_pretrained()
中指定revision="subtitle"
,可以访问转录风格更简洁的模型版本。
通过在.from_pretrained()
中指定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-medium"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, use_safetensors=True, cache_dir="cache", revision="strict"
)
三种模型版本的转录风格从最简洁的Subtitle到Stage 2(默认),再到最详细的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-medium"
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-medium"
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-medium", 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-medium/resolve/main/ggml-model-q5_0.bin # 量化版本
# wget https://huggingface.co/KBLab/kb-whisper-medium/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-medium"
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万小时的瑞典语音和文本转录进行训练。模型训练分为两个阶段,每个阶段应用不同的质量过滤器和阈值。
第一阶段采用低阈值(0到0.30 BLEU,取决于数据集),而第二阶段使用更严格的阈值(BLEU >= 0.7
,加权ROUGE-N >= 0.7
,前10个和后10个字符的CER <= 0.2
)。
数据集 | 继续预训练(小时)-- 第一阶段 | 微调(小时)-- 第二阶段 |
---|---|---|
字幕 | 34,261 | 3,110 |
议会 | 21,949 | 5,119 |
ISOF | 54 | 54 |
NST | 250 | 250 |
总计 | 56,514 | 8,533 |
通过Hugging Face加载我们的模型时,默认版本为Stage 2。然而,我们也上传了继续预训练的检查点并进行了标记。您可以通过在.from_pretrained()
中指定revision
来加载这些其他检查点。例如,预训练检查点的标记可以在此处找到:pretrained-checkpoint
。Stage 2默认模型的标记名为standard
。我们还提供了一个不同的Stage 2检查点——转录风格更简洁——名为subtitle
。
评估
WER
| 模型大小 | | F



