模型简介
模型特点
模型能力
使用案例
Whisper-Large-V3-法语蒸馏版-Dec16
Whisper-Large-V3-法语蒸馏版是通过将解码器层数从32层缩减至16/8/4/2层,并基于大规模数据集进行蒸馏训练而得到的系列模型(技术细节详见论文)。
这些蒸馏变体在保持性能(根据保留层数)的同时,显著降低了内存占用和推理时间,并有效缓解了长文本转录中的幻觉风险。更值得一提的是,它们可以与原始Whisper-Large-V3-法语模型无缝配合实现推测解码,相比单独使用原始模型,既能提升推理速度,又能保证输出一致性。
本模型已转换为多种格式,支持在transformers、openai-whisper、fasterwhisper、whisper.cpp、candle、mlx等不同库中使用。
目录
性能表现
我们在短文本和长文本转录任务上评估了模型性能,同时在分布内和分布外数据集上进行了全面测试,以评估其准确性、泛化能力和鲁棒性。
请注意,报告的词错误率(WER)是经过以下处理后的结果:将数字转为文本、移除标点(撇号和连字符除外)以及统一转为小写。
所有公开数据集的评估结果可查阅此处。
短文本转录
由于法语领域缺乏现成的跨域(OOD)和长文本测试集,我们使用Zaion Lab的内部测试集进行评估。该数据集包含客服通话的人工标注音频-文本对,以显著的背景噪声和领域专有术语为特点。
长文本转录
长文本转录采用🤗 Hugging Face pipeline快速评估,音频文件被分割为30秒片段并行处理。
使用方法
Hugging Face Pipeline
使用🤗 Hugging Face的pipeline
类可轻松实现音频转录。
对于长文本转录(>30秒),可通过传递chunk_length_s
参数启用分段处理。该方法将音频分割为小段并行处理,然后通过寻找最长公共序列在步长处拼接。虽然这种分块处理方式性能略逊于OpenAI的串行算法,但推理速度可提升9倍。
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_name_or_path = "bofenghuang/whisper-large-v3-french-distil-dec16"
processor = AutoProcessor.from_pretrained(model_name_or_path)
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_name_or_path,
torch_dtype=torch_dtype,
low_cpu_mem_usage=True,
)
model.to(device)
# 初始化pipeline
pipe = pipeline(
"automatic-speech-recognition",
model=model,
feature_extractor=processor.feature_extractor,
tokenizer=processor.tokenizer,
torch_dtype=torch_dtype,
device=device,
# chunk_length_s=30, # 长文本转录参数
max_new_tokens=128,
)
# 示例音频
dataset = load_dataset("bofenghuang/asr-dummy", "fr", split="test")
sample = dataset[0]["audio"]
# 运行pipeline
result = pipe(sample)
print(result["text"])
Hugging Face底层API
也可使用🤗 Hugging Face底层API进行转录,提供更精细的控制:
import torch
from datasets import load_dataset
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
# 加载模型
model_name_or_path = "bofenghuang/whisper-large-v3-french-distil-dec16"
processor = AutoProcessor.from_pretrained(model_name_or_path)
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_name_or_path,
torch_dtype=torch_dtype,
low_cpu_mem_usage=True,
)
model.to(device)
# 示例音频
dataset = load_dataset("bofenghuang/asr-dummy", "fr", split="test")
sample = dataset[0]["audio"]
# 提取特征
input_features = processor(
sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt"
).input_features
# 生成token
predicted_ids = model.generate(
input_features.to(dtype=torch_dtype).to(device), max_new_tokens=128
)
# 解码为文本
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
print(transcription)
推测解码
推测解码通过使用草稿模型(即Whisper的蒸馏版本)实现。该方法保证输出与单独使用主Whisper模型完全一致,推理速度提升2倍,仅带来轻微的内存开销增加。
由于蒸馏Whisper与原始模型编码器相同,推理时只需加载解码器,编码器输出在主模型和草稿模型间共享。
在Hugging Face pipeline中使用推测解码非常简单,只需在生成配置中指定assistant_model
。
import torch
from datasets import load_dataset
from transformers import (
AutoModelForCausalLM,
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_name_or_path = "bofenghuang/whisper-large-v3-french"
processor = AutoProcessor.from_pretrained(model_name_or_path)
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_name_or_path,
torch_dtype=torch_dtype,
low_cpu_mem_usage=True,
)
model.to(device)
# 加载草稿模型
assistant_model_name_or_path = "bofenghuang/whisper-large-v3-french-distil-dec2"
assistant_model = AutoModelForCausalLM.from_pretrained(
assistant_model_name_or_path,
torch_dtype=torch_dtype,
low_cpu_mem_usage=True,
)
assistant_model.to(device)
# 初始化pipeline
pipe = pipeline(
"automatic-speech-recognition",
model=model,
feature_extractor=processor.feature_extractor,
tokenizer=processor.tokenizer,
torch_dtype=torch_dtype,
device=device,
generate_kwargs={"assistant_model": assistant_model},
max_new_tokens=128,
)
# 示例音频
dataset = load_dataset("bofenghuang/asr-dummy", "fr", split="test")
sample = dataset[0]["audio"]
# 运行pipeline
result = pipe(sample)
print(result["text"])
OpenAI Whisper
也可使用OpenAI原始论文中提出的滑动窗口和温度回退的串行长文本解码算法。
首先安装openai-whisper包:
pip install -U openai-whisper
然后下载转换后的模型:
python -c "from huggingface_hub import hf_hub_download; hf_hub_download(repo_id='bofenghuang/whisper-large-v3-french-distil-dec16', filename='original_model.pt', local_dir='./models/whisper-large-v3-french-distil-dec16')"
按仓库说明转录音频文件:
import whisper
from datasets import load_dataset
# 加载模型
model = whisper.load_model("./models/whisper-large-v3-french-distil-dec16/original_model.pt")
# 示例音频
dataset = load_dataset("bofenghuang/asr-dummy", "fr", split="test")
sample = dataset[0]["audio"]["array"].astype("float32")
# 转录
result = model.transcribe(sample, language="fr")
print(result["text"])
Faster Whisper
Faster Whisper是OpenAI Whisper模型及串行长文本解码算法在CTranslate2格式下的重新实现。
相比openai-whisper,它提供最高4倍的推理加速,内存占用更低。模型还可量化为int8格式,进一步提升CPU/GPU效率。
首先安装faster-whisper包:
pip install faster-whisper
下载CTranslate2格式模型:
python -c "from huggingface_hub import snapshot_download; snapshot_download(repo_id='bofenghuang/whisper-large-v3-french-distil-dec16', local_dir='./models/whisper-large-v3-french-distil-dec16', allow_patterns='ctranslate2/*')"
按仓库说明转录音频:
from datasets import load_dataset
from faster_whisper import WhisperModel
# 加载模型
model = WhisperModel("./models/whisper-large-v3-french-distil-dec16/ctranslate2", device="cuda", compute_type="float16") # 使用GPU FP16运行
# 示例音频
dataset = load_dataset("bofenghuang/asr-dummy", "fr", split="test")
sample = dataset[0]["audio"]["array"].astype("float32")
segments, info = model.transcribe(sample, beam_size=5, language="fr")
for segment in segments:
print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
Whisper.cpp
Whisper.cpp是OpenAI Whisper模型的无依赖纯C/C++实现,支持多种后端和平台。
模型可量化为4位或5位整数格式以提升效率。
首先克隆并构建whisper.cpp仓库:
git clone https://github.com/ggerganov/whisper.cpp.git
cd whisper.cpp
# 编译主示例
make
从Hugging Face Hub下载转换后的ggml权重:
# 下载Q5_0方法量化的模型
python -c "from huggingface_hub import hf_hub_download; hf_hub_download(repo_id='bofenghuang/whisper-large-v3-french-distil-dec16', filename='ggml-model-q5_0.bin', local_dir='./models/whisper-large-v3-french-distil-dec16')"
使用以下命令转录音频文件:
./main -m ./models/whisper-large-v3-french-distil-dec16/ggml-model-q5_0.bin -l fr -f /path/to/audio/file --print-colors
Candle
Candle-whisper是OpenAI Whisper模型在candle格式下的实现——一个基于Rust的轻量级ML框架。
首先克隆candle仓库:
git clone https://github.com/huggingface/candle.git
cd candle/candle-examples/examples/whisper
使用以下命令转录音频:
cargo run --example whisper --release -- --model large-v3 --model-id bofenghuang/whisper-large-v3-french-distil-dec16 --language fr --input /path/to/audio/file
如需使用CUDA,在命令行添加--features cuda
:
cargo run --example whisper --release --features cuda -- --model large-v3 --model-id bofenghuang/whisper-large-v3-french-distil-dec16 --



