Views
No views yet
| Model | Params / M | Rel. Latency ↑ | Short-Form WER ↓ | Long-Form WER ↓ |
|---|---|---|---|---|
| large-v3 | 1550 | 1.0 | 8.4 | 11.0 |
| large-v2 | 1550 | 1.0 | 9.1 | 11.7 |
| distil-large-v3 | 756 | 6.3 | 9.7 | 10.8 |
| distil-large-v2 | 756 | 5.8 | 10.1 | 11.6 |
| distil-medium.en | 394 | 6.8 | 11.1 | 12.4 |
| distil-small.en | 166 | 5.6 | 12.1 | 12.8 |
1pip install --upgrade pip
2pip install --upgrade transformers accelerate datasets[audio]pipeline
class to transcribe short-form audio files (< 30-seconds) as follows:1import torch
2from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
3from datasets import load_dataset
4
5
6device = "cuda:0" if torch.cuda.is_available() else "cpu"
7torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
8
9model_id = "distil-whisper/distil-large-v2"
10
11model = AutoModelForSpeechSeq2Seq.from_pretrained(
12 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
13)
14model.to(device)
15
16processor = AutoProcessor.from_pretrained(model_id)
17
18pipe = pipeline(
19 "automatic-speech-recognition",
20 model=model,
21 tokenizer=processor.tokenizer,
22 feature_extractor=processor.feature_extractor,
23 max_new_tokens=128,
24 torch_dtype=torch_dtype,
25 device=device,
26)
27
28dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
29sample = dataset[0]["audio"]
30
31result = pipe(sample)
32print(result["text"])1- result = pipe(sample)
2+ result = pipe("audio.mp3")chunk_length_s parameter to the pipeline. For Distil-Whisper, a chunk length of 15-seconds
is optimal. To activate batching, pass the argument batch_size:1import torch
2from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
3from datasets import load_dataset
4
5
6device = "cuda:0" if torch.cuda.is_available() else "cpu"
7torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
8
9model_id = "distil-whisper/distil-large-v2"
10
11model = AutoModelForSpeechSeq2Seq.from_pretrained(
12 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
13)
14model.to(device)
15
16processor = AutoProcessor.from_pretrained(model_id)
17
18pipe = pipeline(
19 "automatic-speech-recognition",
20 model=model,
21 tokenizer=processor.tokenizer,
22 feature_extractor=processor.feature_extractor,
23 max_new_tokens=128,
24 chunk_length_s=15,
25 batch_size=16,
26 torch_dtype=torch_dtype,
27 device=device,
28)
29
30dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
31sample = dataset[0]["audio"]
32
33result = pipe(sample)
34print(result["text"])1from transformers import pipeline, AutoModelForCausalLM, AutoModelForSpeechSeq2Seq, AutoProcessor
2import torch
3from datasets import load_dataset
4
5device = "cuda:0" if torch.cuda.is_available() else "cpu"
6torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
7
8assistant_model_id = "distil-whisper/distil-large-v2"
9
10assistant_model = AutoModelForCausalLM.from_pretrained(
11 assistant_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
12)
13assistant_model.to(device)
14
15model_id = "openai/whisper-large-v2"
16
17model = AutoModelForSpeechSeq2Seq.from_pretrained(
18 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
19)
20model.to(device)
21
22processor = AutoProcessor.from_pretrained(model_id)
23
24pipe = pipeline(
25 "automatic-speech-recognition",
26 model=model,
27 tokenizer=processor.tokenizer,
28 feature_extractor=processor.feature_extractor,
29 max_new_tokens=128,
30 generate_kwargs={"assistant_model": assistant_model},
31 torch_dtype=torch_dtype,
32 device=device,
33)
34
35dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
36sample = dataset[0]["audio"]
37
38result = pipe(sample)
39print(result["text"])pip install flash-attn --no-build-isolationuse_flash_attention_2=True to from_pretrained:1- model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True)
2+ model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True, use_flash_attention_2=True)pip install --upgrade optimum1model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True)
2+ model = model.to_bettertransformer()openai-whisperopenai-whisper package installed:pip install --upgrade openai-whisper1import torch
2from datasets import load_dataset
3from huggingface_hub import hf_hub_download
4from whisper import load_model, transcribe
5
6distil_large_v2 = hf_hub_download(repo_id="distil-whisper/distil-large-v2", filename="original-model.bin")
7model = load_model(distil_large_v2)
8
9dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
10sample = dataset[0]["audio"]["array"]
11sample = torch.from_numpy(sample).float()
12
13pred_out = transcribe(model, audio=sample)
14print(pred_out["text"])audio argument to transcribe:pred_out = transcribe(model, audio="audio.mp3")distil-large-v2 is 2x faster than large-v2, while performing to within 0.1% WER over long-form audio.git clone https://github.com/ggerganov/whisper.cpp.git
cd whisper.cppdistil-medium.en from the Hugging Face Hub:python -c "from huggingface_hub import hf_hub_download; hf_hub_download(repo_id='distil-whisper/distil-large-v2', filename='ggml-large-32-2.en.bin', local_dir='./models')"huggingface_hub package installed, you can also download the weights with wget:wget https://huggingface.co/distil-whisper/distil-large-v2/resolve/main/ggml-large-32-2.en.bin -P ./modelsmake -j && ./main -m models/ggml-large-32-2.en.bin -f samples/jfk.wav1import { pipeline } from '@huggingface/transformers';
2
3const transcriber = await pipeline('automatic-speech-recognition', 'distil-whisper/distil-large-v2');
4
5const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav';
6const output = await transcriber(url);
7// { text: " And so, my fellow Americans, ask not what your country can do for you. Ask what you can do for your country." }candle-core as explained herecandle repository locally:git clone https://github.com/huggingface/candle.gitcd candle/candle-examples/examples/whispercargo run --example whisper --release -- --model distil-large-v2--input flag:cargo run --example whisper --release -- --model distil-large-v2 --input audio.wav
1pip install --upgrade pip
2pip install --upgrade transformers datasets[audio] evaluate jiwer1from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
2from transformers.models.whisper.english_normalizer import EnglishTextNormalizer
3from datasets import load_dataset
4from evaluate import load
5import torch
6from tqdm import tqdm
7
8# define our torch configuration
9device = "cuda:0" if torch.cuda.is_available() else "cpu"
10torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
11
12model_id = "distil-whisper/distil-large-v2"
13
14# load the model + processor
15model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, use_safetensors=True, low_cpu_mem_usage=True)
16model = model.to(device)
17processor = AutoProcessor.from_pretrained(model_id)
18
19# load the dataset with streaming mode
20dataset = load_dataset("librispeech_asr", "clean", split="validation", streaming=True)
21
22# define the evaluation metric
23wer_metric = load("wer")
24normalizer = EnglishTextNormalizer(processor.tokenizer.english_spelling_normalizer)
25
26def inference(batch):
27 # 1. Pre-process the audio data to log-mel spectrogram inputs
28 audio = [sample["array"] for sample in batch["audio"]]
29 input_features = processor(audio, sampling_rate=batch["audio"][0]["sampling_rate"], return_tensors="pt").input_features
30 input_features = input_features.to(device, dtype=torch_dtype)
31
32 # 2. Auto-regressively generate the predicted token ids
33 pred_ids = model.generate(input_features, max_new_tokens=128, language="en", task="transcribe")
34
35 # 3. Decode the token ids to the final transcription
36 batch["transcription"] = processor.batch_decode(pred_ids, skip_special_tokens=True)
37 batch["reference"] = batch["text"]
38 return batch
39
40dataset = dataset.map(function=inference, batched=True, batch_size=16)
41
42all_transcriptions = []
43all_references = []
44
45# iterate over the dataset and run inference
46for i, result in tqdm(enumerate(dataset), desc="Evaluating..."):
47 all_transcriptions.append(result["transcription"])
48 all_references.append(result["reference"])
49
50# normalize predictions and references
51all_transcriptions = [normalizer(transcription) for transcription in all_transcriptions]
52all_references = [normalizer(reference) for reference in all_references]
53
54# compute the WER metric
55wer = 100 * wer_metric.compute(predictions=all_transcriptions, references=all_references)
56print(wer)
572.983685535968466| Dataset | Size / h | Speakers | Domain | Licence |
|---|---|---|---|---|
| People's Speech | 12,000 | unknown | Internet Archive | CC-BY-SA-4.0 |
| Common Voice 13 | 3,000 | unknown | Narrated Wikipedia | CC0-1.0 |
| GigaSpeech | 2,500 | unknown | Audiobook, podcast, YouTube | apache-2.0 |
| Fisher | 1,960 | 11,900 | Telephone conversations | LDC |
| LibriSpeech | 960 | 2,480 | Audiobooks | CC-BY-4.0 |
| VoxPopuli | 540 | 1,310 | European Parliament | CC0 |
| TED-LIUM | 450 | 2,030 | TED talks | CC-BY-NC-ND 3.0 |
| SwitchBoard | 260 | 540 | Telephone conversations | LDC |
| AMI | 100 | unknown | Meetings | CC-BY-4.0 |
| Total | 21,770 | 18,260+ |
@misc{gandhi2023distilwhisper,
title={Distil-Whisper: Robust Knowledge Distillation via Large-Scale Pseudo Labelling},
author={Sanchit Gandhi and Patrick von Platen and Alexander M. Rush},
year={2023},
eprint={2311.00430},
archivePrefix={arXiv},
primaryClass={cs.CL}
}@rsonavane for releasing an early iteration of Distil-Whisper on the LibriSpeech dataset