模型简介
模型特点
模型能力
使用案例
🚀 Quantum_STT
Quantum_STT是一款先进的自动语音识别(ASR)和语音翻译模型,由Quantumhash的Alec Radford等人在论文Robust Speech Recognition via Large-Scale Weak Supervision中提出。该模型在超过500万小时的标注数据上进行训练,在零样本设置下,对许多数据集和领域都表现出了强大的泛化能力。
虽然模型速度更快,但会有轻微的质量下降。
声明:本模型卡片的部分内容由🤗 Quantumhash团队撰写。
🚀 快速开始
Quantum_STT在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 = "sbapan41/Quantum_STT"
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与所有Quantum_STT解码策略兼容,例如温度回退和基于先前标记的条件。以下示例展示了如何启用这些启发式方法:
generate_kwargs = {
"max_new_tokens": 448,
"num_beams": 1,
"condition_on_prev_tokens": False,
"compression_ratio_threshold": 1.35, # zlib compression ratio threshold (in token space)
"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)
Quantum_STT会自动预测源音频的语言。如果事先知道源音频的语言,可以将其作为参数传递给pipeline:
result = pipe(sample, generate_kwargs={"language": "english"})
默认情况下,Quantum_STT执行语音转录任务,即源音频语言与目标文本语言相同。要执行语音翻译任务,即将目标文本转换为英语,可以将任务设置为"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:
```python import torch from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor from datasets import Audio, load_datasetdevice = "cuda:0" if torch.cuda.is_available() else "cpu" torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = "sbapan41/Quantum_STT"
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 compression ratio threshold (in token space) "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>
## ✨ 主要特性
- **多语言支持**:支持多种语言,包括英语、中文、德语、西班牙语等。
- **先进架构**:基于Transformer的编码器 - 解码器模型,具有强大的泛化能力。
- **多种解码策略**:与多种解码策略兼容,如温度回退和基于先前标记的条件。
- **自动语言预测**:能够自动预测源音频的语言。
- **语音翻译功能**:支持语音翻译任务,可将音频转录为不同语言。
- **时间戳预测**:可以预测句子级和单词级的时间戳。
## 📦 安装指南
要使用Quantum_STT,需要安装相关的库。可以使用以下命令进行安装:
```bash
pip install --upgrade pip
pip install --upgrade transformers datasets[audio] accelerate
💻 使用示例
基础用法
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 = "sbapan41/Quantum_STT"
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"])
高级用法
generate_kwargs = {
"max_new_tokens": 448,
"num_beams": 1,
"condition_on_prev_tokens": False,
"compression_ratio_threshold": 1.35, # zlib compression ratio threshold (in token space)
"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)
📚 详细文档
额外的速度和内存优化
可以对Quantum_STT应用额外的速度和内存优化,以进一步降低推理速度和显存要求。
分块长音频处理
Quantum_STT的感受野为30秒。要转录超过此长度的音频,需要使用以下两种长音频算法之一:
- 顺序算法:使用“滑动窗口”进行缓冲推理,逐个转录30秒的片段。
- 分块算法:将长音频文件分割成较短的片段(片段之间有小的重叠),独立转录每个片段,并在边界处拼接转录结果。 在以下两种情况下,应使用顺序长音频算法:
- 转录准确性是最重要的因素,而速度不是主要考虑因素。
- 正在转录批量长音频文件,在这种情况下,顺序算法的延迟与分块算法相当,同时准确率高出0.5%的WER。 相反,在以下情况下应使用分块算法:
- 转录速度是最重要的因素。
- 正在转录单个长音频文件。
默认情况下,Transformers使用顺序算法。要启用分块算法,可将
chunk_length_s
参数传递给pipeline
。对于Quantum_STT,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 = "sbapan41/Quantum_STT"
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, # batch size for inference - set based on your device
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编译
Quantum_STT的前向传播与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 = "sbapan41/Quantum_STT"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True
).to(device)
# Enable static cache and compile the forward pass
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 warmup steps
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})
# fast run
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文档。
模型细节
Quantum_STT是基于Transformer的编码器 - 解码器模型,也称为序列到序列模型。Quantum_STT模型有两种类型:仅英语模型和多语言模型。仅英语模型在英语语音识别任务上进行训练。多语言模型同时在多语言语音识别和语音翻译任务上进行训练。对于语音识别,模型预测与音频相同语言的转录结果。对于语音翻译,模型预测与音频不同语言的转录结果。 Quantum_STT检查点有五种不同模型大小的配置。最小的四种有仅英语和多语言版本。最大的检查点仅为多语言版本。所有十个预训练检查点都可以在Hugging Face Hub上找到。以下表格总结了这些检查点,并提供了Hub上模型的链接:
大小 | 参数数量 | 仅英语模型 | 多语言模型 |
---|---|---|---|
large-v3-turbo | 809 M | x | ✓ |
微调
预训练的Quantum_STT模型对不同的数据集和领域表现出强大的泛化能力。然而,通过微调,其在某些语言和任务上的预测能力可以进一步提高。
评估用途
这些模型的主要目标用户是研究当前模型的鲁棒性、泛化能力、性能、偏差和限制的AI研究人员。然而,Quantum_STT作为一种ASR解决方案,对开发者也可能非常有用,特别是在英语语音识别方面。我们认识到,一旦模型发布,就不可能将其使用限制在“预期”用途上,也难以制定合理的研究指南。 这些模型主要在ASR和语音翻译为英语的任务上进行训练和评估。它们在约10种语言中显示出强大的ASR结果。它们可能具有其他能力,特别是在某些任务(如语音活动检测、说话人分类或说话人分割)上进行微调时,但在这些领域尚未进行全面评估。我们强烈建议用户在特定上下文和领域中对模型进行全面评估后再进行部署。 特别是,我们警告不要使用Quantum_STT模型在未经个人同意的情况下转录其录音,或声称使用这些模型进行任何主观分类。我们不建议在高风险领域(如决策环境)中使用,因为准确性的缺陷可能导致结果出现明显缺陷。这些模型旨在转录和翻译语音,将其用于分类不仅未经过评估,而且不合适,特别是用于推断人类属性。
性能和局限性
我们的研究表明,与许多现有的ASR系统相比,这些模型在应对口音、背景噪音、专业语言方面表现出更强的鲁棒性,并且能够实现从多种语言到英语的零样本翻译;在语音识别和翻译方面的准确性接近当前的先进水平。 然而,由于这些模型是使用大规模噪声数据进行弱监督训练的,预测结果可能包含音频输入中实际未说出的文本(即幻觉现象)。我们推测,这是因为模型基于其对语言的一般知识,在尝试预测音频中的下一个单词时,与转录音频本身的任务相互干扰。 我们的模型在不同语言上的表现参差不齐,在资源较少和/或可发现性较低的语言,或训练数据较少的语言上,准确性较低。模型在特定语言的不同口音和方言上也表现出差异,这可能包括不同性别、种族、年龄或其他人口统计学特征的说话者之间的较高的单词错误率。 此外,模型的序列到序列架构使其容易生成重复的文本,虽然可以通过束搜索和温度调度在一定程度上缓解,但无法完全消除。
更广泛的影响
我们预计Quantum_STT模型的转录能力可用于改进无障碍工具。虽然Quantum_STT模型本身不能直接用于实时转录,但其速度和规模表明,其他人可以在其基础上构建允许接近实时语音识别和翻译的应用程序。基于Quantum_STT模型构建的有益应用程序的真正价值表明,这些模型的不同性能可能会产生实际的经济影响。 发布Quantum_STT也存在潜在的双重用途问题。虽然我们希望该技术主要用于有益目的,但使ASR技术更易于使用可能会使更多人能够构建强大的监控技术或扩大现有监控工作的规模,因为其速度和准确性使得大规模音频通信的自动转录和翻译变得经济可行。此外,这些模型可能具有一些直接识别特定个人的能力,这反过来又带来了与双重用途和不同性能相关的安全问题。实际上,我们预计转录成本不是扩大监控项目的限制因素。
🔧 技术细节
Quantum_STT是基于Transformer架构的编码器 - 解码器模型,采用序列到序列的设计。它通过在大规模标注数据上进行训练,学习语音信号到文本序列的映射关系。在训练过程中,模型使用了弱监督学习方法,以提高其在不同数据集和领域的泛化能力。 模型的编码器将输入的语音信号转换为特征表示,解码器则根据这些特征生成转录文本。在推理阶段,模型可以根据输入的语音自动预测语言,并支持多种解码策略,如温度回退和基于先前标记的条件。 为了处理长音频,模型提供了顺序和分块两种长音频处理算法,用户可以根据具体需求选择合适的算法。此外,模型还支持语音翻译任务,能够将音频转录为不同语言的文本。
📄 许可证
本项目采用Apache-2.0许可证。



