模型简介
模型特点
模型能力
使用案例
🚀 Kotoba-Whisper (v1.0)
Kotoba-Whisper是一系列用于日语自动语音识别(ASR)的蒸馏Whisper模型,由Asahi Ushio和Kotoba Technologies合作开发。该模型比Whisper large-v3快6.3倍,同时保持了较低的错误率。
faster-whisper权重,whisper.cpp权重,带有stable-ts/标点的管道
新闻: 该模型的新版本可在https://huggingface.co/kotoba-tech/kotoba-whisper-v2.0获取!
🚀 快速开始
Kotoba-Whisper 是一系列用于日语自动语音识别(ASR)的蒸馏Whisper模型,由Asahi Ushio和Kotoba Technologies合作开发。遵循distil-whisper的原始工作(Robust Knowledge Distillation via Large-Scale Pseudo Labelling),我们使用OpenAI的Whisper large-v3作为教师模型,学生模型包含教师large-v3模型的完整编码器和一个解码器,该解码器的两层从large-v3模型的第一层和最后一层初始化。Kotoba-Whisper比large-v3 快6.3倍,同时保持与large-v3相同的低错误率。
作为初始版本,我们发布了 kotoba-whisper-v1.0,它在ReazonSpeech的large
子集上进行训练(ReazonSpeech是从日本电视音频记录中提取的最大的日语语音转录配对数据集)。在去除字错误率(WER)超过10的转录后(详见WER过滤),该数据集包含1253小时的音频和16861235个字符的转录(平均5秒音频对应18个文本标记)。该模型以256的批量大小训练了8个epoch,采样率为16kHz,重现kotoba-whisper的训练和评估代码可在https://github.com/kotoba-tech/kotoba-whisper找到。
Kotoba-whisper-v1.0在ReazonSpeech的领域内保留测试集上的字符错误率(CER)和词错误率(WER)优于openai/whisper-large-v3,在包括JSUT basic 5000和CommonVoice 8.0日语子集的领域外测试集上也取得了有竞争力的CER和WER(详见评估)。
✨ 主要特性
- 速度快:Kotoba-Whisper比Whisper large-v3快6.3倍。
- 准确率高:在多个测试集上保持较低的CER和WER。
- 支持多种用法:支持短音频转录、长音频转录、带提示转录等。
📦 安装指南
Kotoba-Whisper从Hugging Face 🤗 Transformers库的4.39版本开始支持。要运行该模型,首先安装最新版本的Transformers:
pip install --upgrade pip
pip install --upgrade transformers accelerate
💻 使用示例
基础用法
以下是使用Kotoba-Whisper进行短音频转录的示例:
import torch
from transformers import pipeline
from datasets import load_dataset
# 配置
model_id = "kotoba-tech/kotoba-whisper-v1.0"
torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model_kwargs = {"attn_implementation": "sdpa"} if torch.cuda.is_available() else {}
generate_kwargs = {"language": "ja", "task": "transcribe"}
# 加载模型
pipe = pipeline(
"automatic-speech-recognition",
model=model_id,
torch_dtype=torch_dtype,
device=device,
model_kwargs=model_kwargs
)
# 加载示例音频
dataset = load_dataset("japanese-asr/ja_asr.reazonspeech_test", split="test")
sample = dataset[0]["audio"]
# 运行推理
result = pipe(sample, generate_kwargs=generate_kwargs)
print(result["text"])
- 要转录本地音频文件,只需在调用管道时传递音频文件的路径(确保音频采样率为16kHz):
- result = pipe(sample, generate_kwargs=generate_kwargs)
+ result = pipe("audio.mp3", generate_kwargs=generate_kwargs)
- 要获取分段级别的时间戳,传递参数
return_timestamps=True
并返回"chunks"
输出:
result = pipe(sample, return_timestamps=True, generate_kwargs=generate_kwargs)
print(result["chunks"])
高级用法
顺序长音频转录
Kotoba-whisper设计为与OpenAI的顺序长音频转录算法兼容。该算法使用滑动窗口对长音频文件(> 30秒)进行缓冲推理,与分块长音频算法相比,返回的转录更准确。默认情况下,如果将长音频文件传递给模型,它将使用顺序长音频转录。顺序长音频算法适用于以下任何一种场景:
- 转录准确性是最重要的因素,而延迟不太重要。
- 您正在转录批量长音频文件,在这种情况下,顺序算法的延迟与分块算法相当,但WER最多可提高0.5%。
如果您正在转录单个长音频文件,并且延迟是最重要的因素,您应该使用下面描述的分块算法。有关不同算法的详细解释,请参考Distil-Whisper论文的第5节。可以使用pipeline
类按如下方式使用顺序算法转录长音频文件:
import torch
from transformers import pipeline
from datasets import load_dataset
# 配置
model_id = "kotoba-tech/kotoba-whisper-v1.0"
torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model_kwargs = {"attn_implementation": "sdpa"} if torch.cuda.is_available() else {}
generate_kwargs = {"language": "japanese", "task": "transcribe"}
# 加载模型
pipe = pipeline(
"automatic-speech-recognition",
model=model_id,
torch_dtype=torch_dtype,
device=device,
model_kwargs=model_kwargs
)
# 加载示例音频(连接实例以创建长音频)
dataset = load_dataset("japanese-asr/ja_asr.reazonspeech_test", split="test")
sample = {"array": np.concatenate([i["array"] for i in dataset[:20]["audio"]]), "sampling_rate": dataset[0]['audio']['sampling_rate']}
# 运行推理
result = pipe(sample, generate_kwargs=generate_kwargs)
print(result["text"])
分块长音频转录
当转录单个大音频文件并且需要最快的推理速度时,应使用此算法。在这种情况下,分块算法比OpenAI的顺序长音频实现快达9倍(详见Distil-Whisper论文的表7)。要启用分块,将chunk_length_s
参数传递给pipeline
。对于distil-large-v3,25秒的分块长度是最优的。要对长音频文件进行批处理,传递参数batch_size
:
import torch
from transformers import pipeline
from datasets import load_dataset
# 配置
model_id = "kotoba-tech/kotoba-whisper-v1.0"
torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model_kwargs = {"attn_implementation": "sdpa"} if torch.cuda.is_available() else {}
generate_kwargs = {"language": "japanese", "task": "transcribe"}
# 加载模型
pipe = pipeline(
"automatic-speech-recognition",
model=model_id,
torch_dtype=torch_dtype,
device=device,
model_kwargs=model_kwargs,
batch_size=16
)
# 加载示例音频(连接实例以创建长音频)
dataset = load_dataset("japanese-asr/ja_asr.reazonspeech_test", split="test")
sample = {"array": np.concatenate([i["array"] for i in dataset[:20]["audio"]]), "sampling_rate": dataset[0]['audio']['sampling_rate']}
# 运行推理
result = pipe(sample, chunk_length_s=15, generate_kwargs=generate_kwargs)
print(result["text"])
带提示转录
Kotoba-whisper可以按如下方式进行带提示的转录:
import re
import torch
from transformers import pipeline
from datasets import load_dataset
# 配置
model_id = "kotoba-tech/kotoba-whisper-v1.0"
torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model_kwargs = {"attn_implementation": "sdpa"} if torch.cuda.is_available() else {}
generate_kwargs = {"language": "ja", "task": "transcribe"}
# 加载模型
pipe = pipeline(
"automatic-speech-recognition",
model=model_id,
torch_dtype=torch_dtype,
device=device,
model_kwargs=model_kwargs
)
# 加载示例音频
dataset = load_dataset("japanese-asr/ja_asr.reazonspeech_test", split="test")
# --- 无提示 ---
text = pipe(dataset[10]["audio"], generate_kwargs=generate_kwargs)['text']
print(text)
# 81歳、力強い走りに変わってきます。
# --- 有提示 ---: 让我们将 `81` 改为 `91`。
prompt = "91歳"
generate_kwargs['prompt_ids'] = pipe.tokenizer.get_prompt_ids(prompt, return_tensors="pt").to(device)
text = pipe(dataset[10]["audio"], generate_kwargs=generate_kwargs)['text']
# 目前ASR的管道会在转录开头添加提示,因此将其移除
text = re.sub(rf"\A\s*{prompt}\s*", "", text)
print(text)
# あっぶったでもスルガさん、91歳、力強い走りに変わってきます。
📚 详细文档
模型详情
请参阅https://huggingface.co/distil-whisper/distil-large-v3#model-details。
训练
模型训练详情请参考https://github.com/kotoba-tech/kotoba-whisper。蒸馏中使用的数据集和所有模型变体可在https://huggingface.co/japanese-asr找到。
评估
以下代码片段展示了如何在CommonVoice 8.0的日语子集上评估kotoba-whisper模型。首先,我们需要安装所需的包,包括🤗 Datasets用于加载音频数据,🤗 Evaluate用于进行WER计算:
pip install --upgrade pip
pip install --upgrade transformers datasets[audio] evaluate jiwer
然后可以使用以下示例端到端运行评估:
import torch
from transformers import pipeline
from datasets import load_dataset
from evaluate import load
from transformers.models.whisper.english_normalizer import BasicTextNormalizer
# 模型配置
model_id = "kotoba-tech/kotoba-whisper-v1.0"
torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model_kwargs = {"attn_implementation": "sdpa"} if torch.cuda.is_available() else {}
generate_kwargs = {"language": "japanese", "task": "transcribe"}
normalizer = BasicTextNormalizer()
# 数据配置
dataset_name = "japanese-asr/ja_asr.reazonspeech_test"
audio_column = 'audio'
text_column = 'transcription'
# 加载模型
pipe = pipeline(
"automatic-speech-recognition",
model=model_id,
torch_dtype=torch_dtype,
device=device,
model_kwargs=model_kwargs,
batch_size=16
)
# 加载数据集并以16kHz采样音频
dataset = load_dataset(dataset_name, split="test")
transcriptions = pipe(dataset['audio'])
transcriptions = [normalizer(i['text']).replace(" ", "") for i in transcriptions]
references = [normalizer(i).replace(" ", "") for i in dataset['transcription']]
# 计算CER指标
cer_metric = load("cer")
cer = 100 * cer_metric.compute(predictions=transcriptions, references=references)
print(cer)
用于评估的主要日语ASR数据集的Hugging Face链接总结在此处。例如,要在JSUT Basic5000上评估模型,更改dataset_name
:
- dataset_name = "japanese-asr/ja_asr.reazonspeech_test"
+ dataset_name = "japanese-asr/ja_asr.jsut_basic5000"
🔧 技术细节
- 模型架构:Kotoba-Whisper使用蒸馏技术,学生模型包含教师large-v3模型的完整编码器和一个解码器,该解码器的两层从large-v3模型的第一层和最后一层初始化。
- 训练数据:使用ReazonSpeech的
large
子集进行训练。 - 评估指标:使用字符错误率(CER)和词错误率(WER)进行评估。
📄 许可证
本项目采用Apache-2.0许可证。
致谢
- OpenAI提供了Whisper模型。
- Hugging Face 🤗 Transformers实现了模型集成。
- Hugging Face 🤗 提供了Distil-Whisper代码库。
- Reazon Human Interaction Lab提供了ReazonSpeech数据集。



