模型概述
模型特點
模型能力
使用案例
🚀 KB-Whisper Tiny
瑞典國家圖書館發佈了一套全新的Whisper模型,這些模型在超過50,000小時的瑞典語語音數據上進行了訓練。在對FLEURS、CommonVoice和NST等數據集的評估中,我們表現最佳的模型與OpenAI的whisper-large-v3
相比,平均將單詞錯誤率(WER)降低了47%。較小尺寸的Whisper模型在瑞典語語音上的性能也有了顯著提升,其中kb-whisper-small
的表現甚至超過了openai/whisper-large-v3
(後者的規模是前者的六倍)。
🚀 快速開始
本項目提供了不同格式的檢查點,可用於不同的推理場景。以下是使用不同工具和庫進行推理的示例。
✨ 主要特性
- 高性能:在瑞典語語音識別任務中,相比OpenAI的
whisper-large-v3
,我們的模型大幅降低了單詞錯誤率(WER)。 - 多格式支持:提供
Hugging Face
、whisper.cpp
(GGML)、onnx
和ctranslate2
等不同格式的檢查點。 - 多版本可選:有不同的轉錄風格版本可供選擇,如
subtitle
(更簡潔)和strict
(更詳細)。
📚 詳細文檔
模型信息
屬性 | 詳情 |
---|---|
庫名稱 | transformers |
基礎模型 | openai/whisper-tiny |
支持語言 | 瑞典語(sv) |
任務類型 | 自動語音識別 |
許可證 | apache-2.0 |
訓練數據集 | KBLab/rixvox-v2 |
標籤 | ctranslate2 |
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-tiny"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, use_safetensors=True, cache_dir="cache", revision="strict"
)
這三個模型版本的轉錄風格詳細程度從低到高依次為:Subtitle、Stage 2(默認)和Strict。
使用方法
我們提供了不同格式的檢查點:Hugging Face
、whisper.cpp
(GGML)、onnx
和ctranslate2
(用於faster-whisper
和WhisperX
)。
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-tiny"
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-tiny"
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-tiny", 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-tiny/resolve/main/ggml-model-q5_0.bin # 量化版本
# wget https://huggingface.co/KBLab/kb-whisper-tiny/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-tiny"
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小時的帶有文本轉錄的瑞典語音頻上進行了訓練。模型分兩個階段進行訓練,每個階段的特點是應用了不同的質量過濾器和相應的閾值。
- 階段1使用了較低的閾值(根據數據集的不同,BLEU值在0到0.30之間)。
- 階段2使用了更嚴格的閾值(
BLEU >= 0.7
,加權ROUGE-N>= 0.7
,前10個和後10個字符的CER<= 0.2
)。
數據集 | 階段1 - 繼續預訓練(小時) | 階段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
找到。階段2的默認模型標籤名為standard
。我們提供了兩個不同的階段2檢查點,一個轉錄風格更簡潔,名為subtitle
,另一個更詳細,名為strict
。
評估
與OpenAI模型的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 |
不同KBLab Stage 2版本的WER
模型大小 | FLEURS | CommonVoice | NST | |
---|---|---|---|---|
tiny | standard | 13.2 | 12.9 | 11.2 |
strict | 14.1 | 13.4 | 11.0 | |
subtitle | 13.3 | 12.9 | 11.4 | |
base | standard | 9.1 | 8.7 | 7.8 |
strict | 10.4 | 9.6 | 8.4 | |
subtitle | 9.1 | 8.7 | 7.9 | |
small | standard | 7.3 | 6.4 | 6.6 |
strict | 8.2 | 7.0 | 6.7 | |
subtitle | 7.3 | 6.4 | 6.6 | |
medium | standard | 6.6 | 5.4 | 5.8 |
strict | 6.8 | 5.4 | 6.0 | |
large-v3 | standard | 5.4 | 4.1 | 5.2 |
strict | 5.3 | 4.0 | 5.1 | |
subtitle | 5.3 | 4.1 | 5.3 |
與OpenAI模型的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 |
不同KBLab Stage 2版本的BLEU分數
模型大小 | FLEURS | CommonVoice | NST | |
---|---|---|---|---|
tiny | standard | 76.6 | 73.7 | 74.3 |
strict | 75.3 | 72.9 | 74.6 | |
subtitle | 76.6 | 73.7 | 74.1 | |
base | standard | 83.2 | 79.9 | 78.3 |
strict | 81.0 | 78.4 | 77.5 | |
subtitle | 83.2 | 79.8 | 78.2 | |
small | standard | 86.6 | 83.5 | 79.6 |
strict | 84.9 | 82.4 | 79.3 | |
subtitle | 86.6 | 83.5 | 79.6 | |
medium | standard | 87.6 | 85.0 | 80.2 |
strict | 87.3 | 84.9 | 80.1 | |
large-v3 | standard | 89.8 | 87.2 | 81.1 |
strict | 90.0 | 87.4 | 81.2 | |
subtitle | 89.8 | 87.3 | 81.0 |
🔧 致謝
我們感謝歐洲高性能計算聯合事業(EuroHPC Joint Undertaking)通過歐洲高性能計算人工智能和數據密集型應用訪問計劃,為該項目提供了訪問由CINECA(意大利)託管的歐洲高性能計算機LEONARDO以及LEONARDO聯盟資源的機會。
📄 引用
KB-Whisper是瑞典國家圖書館KBLab的產品。主要貢獻者包括Faton Rekathati、Justyna Sikora、Robin Kurtz、Agnes Toftgård和Leonora Vesterbacka,由Love Börjeson指導。
如果你想引用我們使用KB-Whisper的工作,請參考以下論文: Swedish Whispers; Leveraging a Massive Speech Corpus for Swedish Speech Recognition
一篇論文被Interspeech 2025接受後的更新引用即將發佈...



