ONNX 模型库
返回模型

说明文档


language:

  • en
  • zh
  • de
  • es
  • ru
  • ko
  • fr
  • ja
  • pt
  • tr
  • pl
  • ca
  • nl
  • ar
  • sv
  • it
  • id
  • hi
  • fi
  • vi
  • he
  • uk
  • el
  • ms
  • cs
  • ro
  • da
  • hu
  • ta
  • 'no'
  • th
  • ur
  • hr
  • bg
  • lt
  • la
  • mi
  • ml
  • cy
  • sk
  • te
  • fa
  • lv
  • bn
  • sr
  • az
  • sl
  • kn
  • et
  • mk
  • br
  • eu
  • is
  • hy
  • ne
  • mn
  • bs
  • kk
  • sq
  • sw
  • gl
  • mr
  • pa
  • si
  • km
  • sn
  • yo
  • so
  • af
  • oc
  • ka
  • be
  • tg
  • sd
  • gu
  • am
  • yi
  • lo
  • uz
  • fo
  • ht
  • ps
  • tk
  • nn
  • mt
  • sa
  • lb
  • my
  • bo
  • tl
  • mg
  • as
  • tt
  • haw
  • ln
  • ha
  • ba
  • jw
  • su license: mit tags:
  • audio
  • automatic-speech-recognition widget:
  • example_title: Librispeech sample 1 src: https://cdn-media.huggingface.co/speech_samples/sample1.flac
  • example_title: Librispeech sample 2 src: https://cdn-media.huggingface.co/speech_samples/sample2.flac pipeline_tag: automatic-speech-recognition base_model:
  • openai/whisper-large-v3 library_name: transformers

Vaani-OPT

Vaani-OPT 是一个用于自动语音识别(ASR)和语音翻译的最先进模型(基于 Whisper V3 Turbo 微调)。

Vaani-OPT 是 Whisper large-v3 剪枝版本的微调模型。换句话说,它是完全相同的模型,只是解码层数从 32 层减少到了 4 层。 因此,该模型速度更快,但会有轻微的质量下降。

该模型在印度语言上进行了微调,主要侧重于印地语。


使用方法

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 = \"brahmairesearch/vaani-opt\"

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\"])

要转录本地音频文件,只需在调用 pipeline 时传入音频文件的路径:

result = pipe(\"audio.mp3\")

可以通过将多个音频文件指定为列表并设置 batch_size 参数来并行转录:

result = pipe([\"audio_1.mp3\", \"audio_2.mp3\"], batch_size=2)

Transformers 兼容所有 Whisper 解码策略,如温度回退和基于前文 token 的条件约束。以下示例演示了如何启用这些启发式方法:

generate_kwargs = {
    \"max_new_tokens\": 448,
    \"num_beams\": 1,
    \"condition_on_prev_tokens\": False,
    \"compression_ratio_threshold\": 1.35,  # zlib 压缩比阈值(在 token 空间中)
    \"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 会自动预测源音频的语言。如果源音频的语言是已知的,可以将其作为参数传递给 pipeline:

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\"])

<details>

<summary> 如需对生成参数进行更多控制,请直接使用 model + processor API: </summary>

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 = \"brahmairesearch/vaani-opt\"

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\": False,
    \"compression_ratio_threshold\": 1.35,  # zlib 压缩比阈值(在 token 空间中)
    \"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,
}

pred_ids = model.generate(**inputs, **gen_kwargs)
pred_text = processor.batch_decode(pred_ids, skip_special_tokens=True, decode_with_timestamps=False)

print(pred_text)

</details>

额外的速度和内存优化

您可以对 Whisper 应用额外的速度和内存优化,以进一步降低推理速度和显存需求。

分块长音频处理

Whisper 的感受野为 30 秒。要转录超过此时长的音频,需要使用以下两种长音频算法之一:

  1. 顺序处理: 使用"滑动窗口"进行缓冲推理,依次转录 30 秒的切片
  2. 分块处理: 将长音频文件分割成较短的片段(片段之间有少量重叠),独立转录每个片段,然后在边界处拼接结果

在以下情况下应使用顺序长音频算法:

  1. 转录准确性是最重要的因素,速度是次要考虑
  2. 您正在转录批量的长音频文件,在这种情况下顺序处理的延迟与分块处理相当,但准确率可提高 0.5% WER

相反,在以下情况下应使用分块算法:

  1. 转录速度是最重要的因素
  2. 您正在转录单个长音频文件

默认情况下,Transformers 使用顺序算法。要启用分块算法,请向 pipeline 传递 chunk_length_s 参数。对于 large-v3,30 秒的分块长度是最佳的。要对长音频文件启用批处理,请传递 batch_size 参数:

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 = \"brahmairesearch/vaani-opt\"

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=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,
    chunk_length_s=30,
    batch_size=16,  # 推理批大小 - 根据您的设备设置
    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\"])

Torch compile

Whisper 的前向传播与 torch.compile 兼容,可实现 4.5 倍加速。

注意: torch.compile 目前与分块长音频算法或 Flash Attention 2 不兼容 ⚠️

import torch
from torch.nn.attention import SDPBackend, sdpa_kernel
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from datasets import load_dataset
from tqdm import tqdm

torch.set_float32_matmul_precision(\"high\")

device = \"cuda:0\" if torch.cuda.is_available() else \"cpu\"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = \"brahmairesearch/vaani-opt\"

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True
).to(device)

# 启用静态缓存并编译前向传播
model.generation_config.cache_implementation = \"static\"
model.generation_config.max_new_tokens = 256
model.forward = torch.compile(model.forward, mode=\"reduce-overhead\", fullgraph=True)

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\"]

# 2 步预热
for _ in tqdm(range(2), desc=\"Warm-up step\"):
    with sdpa_kernel(SDPBackend.MATH):
        result = pipe(sample.copy(), generate_kwargs={\"min_new_tokens\": 256, \"max_new_tokens\": 256})

# 快速运行
with sdpa_kernel(SDPBackend.MATH):
    result = pipe(sample.copy())

print(result[\"text\"])

Flash Attention 2

如果您的 GPU 支持且您没有使用 torch.compile,我们推荐使用 Flash-Attention 2。 首先安装 Flash Attention

pip install flash-attn --no-build-isolation

然后在 from_pretrained 中传入 attn_implementation=\"flash_attention_2\"

model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation=\"flash_attention_2\")

Torch 缩放点积注意力 (SDPA)

如果您的 GPU 不支持 Flash Attention,我们建议使用 PyTorch 缩放点积注意力 (SDPA)。 此注意力实现对于 PyTorch 2.1.1 或更高版本默认启用。要检查您是否安装了兼容的 PyTorch 版本,请运行以下 Python 代码片段:

from transformers.utils import is_torch_sdpa_available

print(is_torch_sdpa_available())

如果上述代码返回 True,则表示您安装了有效的 PyTorch 版本,SDPA 已默认启用。如果返回 False,您需要按照官方说明升级 PyTorch 版本

安装有效版本的 PyTorch 后,SDPA 将默认启用。也可以通过指定 attn_implementation=\"sdpa\" 来显式设置:

model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation=\"sdpa\")

有关如何使用 SDPA 的更多信息,请参阅 Transformers SDPA 文档

模型详情

Whisper 是一个基于 Transformer 的编码器-解码器模型,也被称为序列到序列模型。Whisper 模型有两种类型:仅英语和多语言。仅英语模型在英语语音识别任务上进行了训练。多语言模型同时在多语言语音识别和语音翻译任务上进行了训练。对于语音识别,模型预测与音频相同语言的转录文本。对于语音翻译,模型预测与音频不同语言的转录文本。

Whisper 检查点有五种不同模型大小的配置。最小的四种既有仅英语版本,也有多语言版本。最大的检查点仅有多语言版本。所有十个预训练检查点都可在 Hugging Face Hub 上获取。检查点总结如下表所示,并附有 Hub 上的模型链接:

大小 参数量 仅英语 多语言
tiny 39 M
base 74 M
small 244 M
medium 769 M
large 1550 M x
large-v2 1550 M x
large-v3 1550 M x
large-v3-turbo 809 M x

微调

预训练的 Whisper 模型展现了对不同数据集和领域的强大泛化能力。然而,通过微调,其在某些语言和任务上的预测能力可以进一步提高。博客文章 Fine-Tune Whisper with 🤗 Transformers 提供了仅需 5 小时标注数据即可微调 Whisper 模型的分步指南。

评估用途

这些模型的主要目标用户是研究当前模型的鲁棒性、泛化能力、能力、偏差和约束的 AI 研究人员。然而,Whisper 作为开发者的 ASR 解决方案也具有很大的实用价值,特别是对于英语语音识别。我们认识到,一旦模型发布,就不可能将访问权限仅限于"预期"用途,也不可能围绕什么是或什么不是研究制定合理的指导方针。

这些模型主要在 ASR 和语音翻译到英语的任务上进行训练和评估。它们在约 10 种语言中表现出强大的 ASR 结果。它们可能展现出额外的能力,特别是如果在某些任务(如语音活动检测、说话人分类或说话人分离)上进行微调,但这些领域尚未经过严格的评估。我们强烈建议用户在特定语境和领域部署模型之前,对其进行严格的评估。

特别地,我们警告不要使用 Whisper 模型转录未经个人同意的录音,或声称将这些模型用于任何形式的主观分类。我们不建议在高风险领域(如决策场景)中使用,因为准确性的缺陷可能导致结果的明显缺陷。这些模型旨在转录和翻译语音,将模型用于分类不仅未经评估,而且也不合适,特别是用于推断人类属性。

训练数据

未提供信息。

性能和局限性

我们的研究表明,与许多现有的 ASR 系统相比,这些模型对口音、背景噪声、技术术语表现出更好的鲁棒性,以及从多种语言零样本翻译到英语的能力;语音识别和翻译的准确性接近最先进水平。

然而,由于模型是使用大规模噪声数据以弱监督方式训练的,预测可能包含音频输入中实际上并未说出的文本(即幻觉)。我们假设这是因为模型结合了对音频中下一个词的预测和音频本身的转录,这是基于它们对语言的通用知识。

我们的模型在不同语言上的表现不均匀,我们观察到在低资源和/或低可发现性语言或我们训练数据较少的语言上准确性较低。模型在特定语言的不同口音和方言上也表现出差异,这可能包括不同性别、种族、年龄或其他人口统计标准的说话者之间较高的词错误率。我们的完整评估结果在本发布附带的论文中呈现。

此外,模型的序列到序列架构使其容易生成重复文本,这可以通过束搜索和温度调度在一定程度上缓解,但不能完全消除。对这些局限性的进一步分析在论文中提供。这种行为和幻觉在低资源和/或低可发现性语言上可能更严重。

更广泛的影响

我们预计 Whisper 模型的转录能力可用于改进无障碍工具。虽然 Whisper 模型不能开箱即用地进行实时转录——但其速度和大小表明,其他人可能能够在它们之上构建允许近乎实时语音识别和翻译的应用程序。基于 Whisper 模型构建的有益应用程序的真正价值表明,这些模型的不同性能可能会产生实际的经济影响。

发布 Whisper 也存在潜在的军民两用担忧。虽然我们希望该技术主要用于有益目的,但使 ASR 技术更加普及可能使更多行为者能够构建强大的监控技术或扩展现有的监控工作,因为速度和准确性允许对大量音频通信进行经济实惠的自动转录和翻译。此外,这些模型可能具有开箱即用地识别特定个体的能力,这反过来带来了与军民两用和不同性能相关的安全担忧。在实践中,我们预计转录成本并不是扩大监控项目规模限制因素。

BibTeX 条目和引用信息

@misc{radford2022whisper,
  doi = {10.48550/ARXIV.2212.04356},
  url = {https://arxiv.org/abs/2212.04356},
  author = {Radford, Alec and Kim, Jong Wook and Xu, Tao and Brockman, Greg and McLeavey, Christine and Sutskever, Ilya},
  title = {Robust Speech Recognition via Large-Scale Weak Supervision},
  publisher = {arXiv},
  year = {2022},
  copyright = {arXiv.org perpetual, non-exclusive license}
}

brahmairesearch/vaani-opt

作者 brahmairesearch

automatic-speech-recognition transformers
↓ 1 ♥ 1

创建时间: 2025-02-17 08:13:48+00:00

更新时间: 2025-02-17 08:58:36+00:00

在 Hugging Face 上查看

文件 (28)

.gitattributes
README.md
added_tokens.json
config.json
generation_config.json
merges.txt
model.safetensors
normalizer.json
onnx/added_tokens.json
onnx/config.json
onnx/decoder_model.onnx ONNX
onnx/decoder_model_merged.onnx ONNX
onnx/decoder_with_past_model.onnx ONNX
onnx/encoder_model.onnx ONNX
onnx/encoder_model.onnx_data
onnx/generation_config.json
onnx/merges.txt
onnx/normalizer.json
onnx/preprocessor_config.json
onnx/special_tokens_map.json
onnx/tokenizer.json
onnx/tokenizer_config.json
onnx/vocab.json
preprocessor_config.json
special_tokens_map.json
tokenizer.json
tokenizer_config.json
vocab.json