🚀 Whisper量化Llamafile項目
本項目為whisper模型生成了一系列llamafiles,解決了語音識別模型在特定場景下的部署和使用問題,為開發者提供了便捷的語音識別解決方案。
🚀 快速開始
運行模型
chmod +x <model>.llamafile
./<model>.llamafile
使用示例
基礎用法
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"
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,
max_new_tokens=128,
chunk_length_s=30,
batch_size=16,
return_timestamps=True,
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")
result = pipe(sample, generate_kwargs={"language": "english"})
result = pipe(sample, generate_kwargs={"task": "translate"})
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"])
✨ 主要特性
- 為多語言的Whisper模型提供量化的llamafile,包括q8、q5k量化版本,也提供原始模型。
- 每個llamafile可啟動服務器,將音頻文件轉換為合適的.wav格式,並在終端輸出中打印/著色解碼文本。
- 支持使用
pipeline
類轉錄任意長度的音頻文件,採用分塊算法,比OpenAI提出的順序算法快9倍。
- 可自動預測源音頻的語言,也可手動指定。
- 支持語音轉錄和語音翻譯任務,還可獲取句子級和單詞級的時間戳。
📦 安裝指南
pip install --upgrade pip
pip install --upgrade git+https://github.com/huggingface/transformers.git accelerate datasets[audio]
額外的速度和內存優化
Flash Attention
pip install flash-attn --no-build-isolation
- model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True)
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True, use_flash_attention_2=True)
Torch Scale-Product-Attention (SDPA)
pip install --upgrade optimum
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True)
+ model = model.to_bettertransformer()
📚 詳細文檔
模型詳情
Whisper是一個基於Transformer的編碼器 - 解碼器模型,也稱為_序列到序列_模型。它在100萬小時的弱標記音頻和400萬小時使用Whisper large-v2
收集的偽標記音頻上進行訓練。
模型在僅英語數據或多語言數據上進行訓練。僅英語模型用於語音識別任務,多語言模型用於語音識別和語音翻譯任務。
Whisper檢查點有五種不同模型大小的配置,最小的四個在僅英語或多語言數據上訓練,最大的檢查點僅為多語言模型。所有十個預訓練檢查點都可以在Hugging Face 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 |
🔗 |
訓練數據
模型在100萬小時的弱標記音頻和400萬小時使用Whisper large-v2
收集的偽標記音頻上進行訓練。在給定語言中的轉錄性能與該語言的訓練數據量直接相關。
性能和侷限性
- 優點:與許多現有的自動語音識別(ASR)系統相比,該模型對口音、背景噪音、專業語言具有更強的魯棒性,並且在多語言到英語的零樣本翻譯方面表現出色,語音識別和翻譯的準確性接近當前的先進水平。
- 缺點:由於模型是使用大規模噪聲數據進行弱監督訓練的,預測結果可能包含音頻輸入中實際未說出的文本(即幻覺)。模型在不同語言上的表現不均衡,在資源較少和/或可發現性較低的語言上準確性較低。此外,模型的序列到序列架構容易生成重複文本,儘管可以通過束搜索和溫度調度在一定程度上緩解,但無法完全解決。
更廣泛的影響
- 積極方面:Whisper模型的轉錄能力可用於改進輔助工具,儘管不能直接用於即時轉錄,但基於其構建的應用程序可能實現接近即時的語音識別和翻譯。
- 潛在風險:發佈Whisper模型可能帶來潛在的雙重用途問題,可能使更多人能夠構建強大的監控技術或擴大現有監控工作。此外,不建議在高風險領域使用該模型,因為其準確性缺陷可能導致嚴重的後果。
評估使用
- 主要用戶:主要面向研究當前模型的魯棒性、泛化能力、性能、偏差和侷限性的AI研究人員。同時,對於開發者,尤其是英語語音識別領域的開發者,Whisper也是一個潛在有用的ASR解決方案。
- 使用建議:強烈建議用戶在特定上下文和領域中對模型進行充分評估後再進行部署。特別要注意,不要在未經個人同意的情況下轉錄其錄音,也不要將模型用於主觀分類任務。不建議在高風險決策領域使用該模型。
🔧 技術細節
模型差異
Whisper large-v3
與之前的大型模型具有相同的架構,但有以下細微差異:
- 輸入使用128個梅爾頻率 bins,而不是80個。
- 新增了粵語的語言標記。
訓練過程
large-v3
模型在100萬小時的弱標記音頻和400萬小時使用Whisper large-v2
收集的偽標記音頻上進行訓練,訓練了2.0個週期。
速度和內存優化
📄 許可證
本項目採用Apache-2.0許可證。
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}
}