语言:
- 英语
- 中文
- 德语
- 西班牙语
- 俄语
- 韩语
- 法语
- 日语
- 葡萄牙语
- 土耳其语
- 波兰语
- 加泰罗尼亚语
- 荷兰语
- 阿拉伯语
- 瑞典语
- 意大利语
- 印尼语
- 印地语
- 芬兰语
- 越南语
- 希伯来语
- 乌克兰语
- 希腊语
- 马来语
- 捷克语
- 罗马尼亚语
- 丹麦语
- 匈牙利语
- 泰米尔语
- 挪威语
- 泰语
- 乌尔都语
- 克罗地亚语
- 保加利亚语
- 立陶宛语
- 拉丁语
- 毛利语
- 马拉雅拉姆语
- 威尔士语
- 斯洛伐克语
- 泰卢固语
- 波斯语
- 拉脱维亚语
- 孟加拉语
- 塞尔维亚语
- 阿塞拜疆语
- 斯洛文尼亚语
- 卡纳达语
- 爱沙尼亚语
- 马其顿语
- 布列塔尼语
- 巴斯克语
- 冰岛语
- 亚美尼亚语
- 尼泊尔语
- 蒙古语
- 波斯尼亚语
- 哈萨克语
- 阿尔巴尼亚语
- 斯瓦希里语
- 加利西亚语
- 马拉地语
- 旁遮普语
- 僧伽罗语
- 高棉语
- 绍纳语
- 约鲁巴语
- 索马里语
- 南非荷兰语
- 奥克语
- 格鲁吉亚语
- 白俄罗斯语
- 塔吉克语
- 信德语
- 古吉拉特语
- 阿姆哈拉语
- 意第绪语
- 老挝语
- 乌兹别克语
- 法罗语
- 海地克里奥尔语
- 普什图语
- 土库曼语
- 新挪威语
- 马耳他语
- 梵语
- 卢森堡语
- 缅甸语
- 藏语
- 他加禄语
- 马尔加什语
- 阿萨姆语
- 鞑靼语
- 夏威夷语
- 林加拉语
- 豪萨语
- 巴什基尔语
- 爪哇语
- 巽他语
许可证: MIT
标签:
- 音频
- 自动语音识别
- unsloth
小部件:
- 示例标题: Librispeech样本1
src: https://cdn-media.huggingface.co/speech_samples/sample1.flac
- 示例标题: Librispeech样本2
src: https://cdn-media.huggingface.co/speech_samples/sample2.flac
管道标签: 自动语音识别
基础模型:
- openai/whisper-large-v3
- openai/whisper-large-v3-turbo
库名称: transformers
Whisper
Whisper是自动语音识别(ASR)和语音翻译的最先进模型,由OpenAI的Alec Radford等人在论文通过大规模弱监督实现稳健语音识别中提出。在超过500万小时的标记数据上训练,Whisper在零样本设置下展示了对许多数据集和领域的强大泛化能力。
Whisper large-v3-turbo是Whisper large-v3的修剪微调版本。换句话说,它是完全相同的模型,只是解码层从32层减少到4层。因此,模型速度大幅提升,但质量略有下降。您可以在此GitHub讨论中找到更多详细信息。
免责声明: 此模型卡的内容部分由Hugging Face团队编写,部分复制粘贴自原始模型卡。
使用
Whisper large-v3-turbo在Hugging Face Transformers中得到支持。要运行模型,首先安装Transformers库。对于此示例,我们还将安装Datasets以从Hugging Face Hub加载玩具音频数据集,以及Accelerate以减少模型加载时间:
pip install --upgrade pip
pip install --upgrade transformers datasets[audio] accelerate
该模型可以与pipeline
类一起使用,以转录任意长度的音频:
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from datasets import load_dataset
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = "openai/whisper-large-v3-turbo"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
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,
)
dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
sample = dataset[0]["audio"]
result = pipe(sample)
print(result["text"])
要转录本地音频文件,只需在调用管道时传递音频文件的路径:
result = pipe("audio.mp3")
可以通过指定音频文件列表并设置batch_size
参数来并行转录多个音频文件:
result = pipe(["audio_1.mp3", "audio_2.mp3"], batch_size=2)
Transformers与所有Whisper解码策略兼容,例如温度回退和条件于先前标记。以下示例演示如何启用这些启发式方法:
generate_kwargs = {
"max_new_tokens": 448,
"num_beams": 1,
"condition_on_prev_tokens": False,
"compression_ratio_threshold": 1.35,
"temperature": (0.0, 0.2, 0.4, 0.6, 0.8, 1.0),
"logprob_threshold": -1.0,
"no_speech_threshold": 0.6,
"return_timestamps": True,
}
result = pipe(sample, generate_kwargs=generate_kwargs)
Whisper自动预测源音频的语言。如果已知源音频语言,可以将其作为参数传递给管道:
result = pipe(sample, generate_kwargs={"language": "english"})
默认情况下,Whisper执行语音转录任务,其中源音频语言与目标文本语言相同。要执行语音翻译,其中目标文本为英语,将任务设置为"translate"
:
result = pipe(sample, generate_kwargs={"task": "translate"})
最后,可以让模型预测时间戳。对于句子级时间戳,传递return_timestamps
参数:
result = pipe(sample, return_timestamps=True)
print(result["chunks"])
对于单词级时间戳:
result = pipe(sample, return_timestamps="word")
print(result["chunks"])
上述参数可以单独使用或组合使用。例如,要执行语音转录任务,其中源音频为法语,并且我们希望返回句子级时间戳,可以使用以下内容:
result = pipe(sample, return_timestamps=True, generate_kwargs={"language": "french", "task": "translate"})
print(result["chunks"])
要更精细地控制生成参数,请直接使用模型+处理器API:
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
from datasets import Audio, load_dataset
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = "openai/whisper-large-v3-turbo"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True
)
model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
dataset = dataset.cast_column("audio", Audio(processor.feature_extractor.sampling_rate))
sample = dataset[0]["audio"]
inputs = processor(
sample["array"],
sampling_rate=sample["sampling_rate"],
return_tensors="pt",
truncation=False,
padding="longest",
return_attention_mask=True,
)
inputs = inputs.to(device, dtype=torch_dtype)
gen_kwargs = {
"max_new_tokens": 448,
"num_beams": 1,
"condition_on_prev_tokens":