模型简介
模型特点
模型能力
使用案例
🚀 KB-Whisper Large
瑞典国家图书馆发布了一套全新的Whisper模型,这些模型在超过50,000小时的瑞典语语音数据上进行了训练。在对FLEURS、CommonVoice和NST等数据集的评估中,我们表现最佳的模型与OpenAI的whisper-large-v3
相比,平均将单词错误率(WER)降低了47%。较小尺寸的Whisper模型在瑞典语语音上的性能也有显著提升,其中kb-whisper-small
的表现甚至超过了体积大其六倍的openai/whisper-large-v3
。
🚀 快速开始
本项目提供了不同格式的检查点,包括Hugging Face
、whisper.cpp
(GGML)、onnx
和ctranslate2
(用于faster-whisper
和WhisperX
)。以下是不同方式的使用示例:
💻 使用示例
基础用法
以下是使用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"}
# Add return_timestamps=True for output with timestamps
res = pipe("audio.mp3",
chunk_length_s=30,
generate_kwargs={"task": "transcribe", "language": "sv"})
print(res)
高级用法
以下是使用faster-whisper
、WhisperX
、whisper.cpp / GGML
和onnx (optimum)
以及transformers.js
的高级用法示例:
Faster-whisper
Faster-whisper通过使用ctranslate2
重新实现Whisper,提供了快速高效的推理。
#### faster-whisper model ####
from faster_whisper import WhisperModel
model_id = "KBLab/kb-whisper-large"
model = WhisperModel(
model_id,
device="cuda",
compute_type="float16",
download_root="cache", # cache directory
# condition_on_previous_text = False # Can reduce hallucinations if we don't use prompts
)
# Transcribe audio.wav (convert to 16khz mono wav first via ffmpeg)
segments, info = model.transcribe("audio.wav", condition_on_previous_text=False)
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))
WhisperX
WhisperX提供了一种方便的方法来获取准确的单词级时间戳。该库将Whisper的文本输出与Wav2vec2的准确时间戳相结合。以下是如何将KB-Whisper
与KBLab/wav2vec2-large-voxrex-swedish一起使用的示例:
import whisperx
device = "cuda"
audio_file = "audio.wav"
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(
"KBLab/kb-whisper-large", device, compute_type=compute_type, download_root="cache" # cache_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,
model_name="KBLab/wav2vec2-large-voxrex-swedish",
model_dir="cache", # cache_dir
)
result = whisperx.align(
result["segments"], model_a, metadata, audio, device, return_char_alignments=False
)
print(result["segments"]) # word level timestamps after alignment
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 # Quantized version
# wget https://huggingface.co/KBLab/kb-whisper-large/resolve/main/ggml-model.bin # Non-quantized version
通过在参数-m
后指定模型路径,并将音频文件的路径作为最后一个位置参数来运行推理:
./build/bin/whisper-cli -m ggml-model-q5_0.bin ../audio.wav
onnx (optimum) and transformers.js usage
你可以通过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找到。
📚 详细文档
训练数据
我们的模型在超过50,000小时带有文本转录的瑞典语音频上进行了训练。模型分两个阶段进行训练,每个阶段的特点是应用了不同的质量过滤器和过滤器阈值。
第一阶段采用了较低的阈值(根据数据集,BLEU值在0到0.30之间),而第二阶段使用了更严格的阈值(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加载我们的模型时,默认使用第二阶段。不过,我们也上传了持续预训练的检查点并进行了标记。你可以通过在.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 |
🔧 技术细节
本项目的模型基于OpenAI的whisper-large-v3
,在超过50,000小时的瑞典语语音数据上进行了训练。训练分两个阶段进行,每个阶段应用了不同的质量过滤器和阈值。第一阶段采用较低阈值,第二阶段使用更严格的阈值,以提高模型在瑞典语语音识别上的性能。
📄 许可证
本项目采用apache-2.0
许可证。
致谢
我们感谢欧洲高性能计算联合事业(EuroHPC Joint Undertaking)通过欧洲高性能计算人工智能和数据密集型应用访问项目,授予本项目使用由CINECA(意大利)和LEONARDO联盟托管的欧洲高性能计算超级计算机LEONARDO的权限。
引用
论文参考即将发布。



