Views
No views yet
| Model | Params / M | Rel. Latency | Short-Form | Sequential Long-Form | Chunked Long-Form |
|---|---|---|---|---|---|
| large-v3 | 1550 | 1.0 | 8.4 | 10.0 | 11.0 |
| distil-large-v3 | 756 | 6.3 | 9.7 | 10.8 | 10.9 |
| distil-large-v2 | 756 | 5.8 | 10.1 | 15.6 | 11.6 |
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-v3"
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")return_timestamps=True and return the "chunks" output:1result = pipe(sample, return_timestamps=True)
2print(result["chunks"])model.generate, including num_beams for beam-search, return_timestamps
for segment-level timestamps, and prompt_ids for prompting. See the docstrings
for more details.1import torch
2from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
3from datasets import Audio, 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-v3"
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
18dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
19dataset = dataset.cast_column("audio", Audio(processor.feature_extractor.sampling_rate))
20sample = dataset[0]["audio"]
21
22input_features = processor(
23 sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt"
24).input_features
25
26input_features = input_features.to(device, dtype=torch_dtype)
27
28gen_kwargs = {
29 "max_new_tokens": 128,
30 "num_beams": 1,
31 "return_timestamps": False,
32}
33
34pred_ids = model.generate(input_features, **gen_kwargs)
35pred_text = processor.batch_decode(pred_ids, skip_special_tokens=True, decode_with_timestamps=gen_kwargs["return_timestamps"])
36
37print(pred_text)pipeline
class can be used to transcribe long audio files with the sequential algorithm 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-v3"
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("distil-whisper/librispeech_long", "clean", split="validation")
29sample = dataset[0]["audio"]
30
31result = pipe(sample)
32print(result["text"])1import torch
2from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
3from datasets import Audio, 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-v3"
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
18dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
19dataset = dataset.cast_column("audio", Audio(processor.feature_extractor.sampling_rate))
20sample = dataset[0]["audio"]
21
22inputs = processor(
23 sample["array"],
24 sampling_rate=sample["sampling_rate"],
25 return_tensors="pt",
26 truncation=False,
27 padding="longest",
28 return_attention_mask=True,
29)
30inputs = inputs.to(device, dtype=torch_dtype)
31
32gen_kwargs = {
33 "max_new_tokens": 448,
34 "num_beams": 1,
35 "condition_on_prev_tokens": False,
36 "compression_ratio_threshold": 1.35, # zlib compression ratio threshold (in token space)
37 "temperature": (0.0, 0.2, 0.4, 0.6, 0.8, 1.0),
38 "logprob_threshold": -1.0,
39 "no_speech_threshold": 0.6,
40 "return_timestamps": True,
41}
42
43pred_ids = model.generate(**i nputs, **gen_kwargs)
44pred_text = processor.batch_decode(pred_ids, skip_special_tokens=True, decode_with_timestamps=False)
45
46print(pred_text)chunk_length_s parameter to the pipeline. For distil-large-v3, a chunk length of 25-seconds
is optimal. To activate batching over long audio files, 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-v3"
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=25,
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-v3"
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-v3"
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-isolationattn_implementation="flash_attention_2" 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, attn_implementation="flash_attention_2")1from transformers.utils import is_torch_sdpa_available
2
3print(is_torch_sdpa_available())True, you have a valid version of PyTorch installed and SDPA is activated by default. If it
returns False, you need to upgrade your PyTorch version according to the official instructionsattn_implementation="sdpa" as follows: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, attn_implementation="sdpa")git clone https://github.com/ggerganov/whisper.cpp.git
cd whisper.cpppip install --upgrade huggingface_hub1from huggingface_hub import hf_hub_download
2
3hf_hub_download(repo_id='distil-whisper/distil-large-v3-ggml', filename='ggml-distil-large-v3.bin', local_dir='./models')wget:wget https://huggingface.co/distil-whisper/distil-large-v3-ggml/resolve/main/ggml-distil-large-v3.bin -P ./modelsmake -j && ./main -m models/ggml-distil-large-v3.bin -f samples/jfk.wav1pip install --upgrade pip
2pip install --upgrade git+https://github.com/SYSTRAN/faster-whisper datasets[audio]1import torch
2from faster_whisper import WhisperModel
3from datasets import load_dataset
4
5# define our torch configuration
6device = "cuda:0" if torch.cuda.is_available() else "cpu"
7compute_type = "float16" if torch.cuda.is_available() else "float32"
8
9# load model on GPU if available, else cpu
10model = WhisperModel("distil-large-v3", device=device, compute_type=compute_type)
11
12# load toy dataset for example
13dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
14sample = dataset[1]["audio"]["path"]
15
16segments, info = model.transcribe(sample, beam_size=1)
17
18for segment in segments:
19 print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))audio argument to transcribe:segments, info = model.transcribe("audio.mp3", beam_size=1)openai-whisper package installed.
For this example, we'll also install 🤗 Datasets to load a toy audio dataset from the Hugging Face Hub:1pip install --upgrade pip
2pip install --upgrade openai-whisper datasets[audio]1from huggingface_hub import hf_hub_download
2from datasets import load_dataset
3from whisper import load_model, transcribe
4
5model_path = hf_hub_download(repo_id="distil-whisper/distil-large-v3-openai", filename="model.bin")
6model = load_model(model_path)
7
8dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
9sample = dataset[0]["audio"]["path"]
10
11pred_out = transcribe(model, audio=sample, language="en")
12print(pred_out["text"])audio argument to transcribe:pred_out = transcribe(model, audio=sample, language="en")npm i @xenova/transformers1import { pipeline } from '@xenova/transformers';
2
3const transcriber = await pipeline('automatic-speech-recognition', 'distil-whisper/distil-large-v3');
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 --features symphonia -- --model distil-large-v3--input flag:cargo run --example whisper --release --features symphonia -- --model distil-large-v3 --input audio.wavmetal feature when you run the example:cargo run --example whisper --release --features="symphonia,metal" -- --model distil-large-v3error: target `whisper` in package `candle-examples` requires the features: `symphonia`
Consider enabling them by passing, e.g., `--features="symphonia"`cargo installation:cargo cleancargo run --example whisper --release --features symphonia -- --model distil-large-v3
condition_on_prev_tokens argument, and context windows up to 448 tokens.1pip install --upgrade pip
2pip install --upgrade transformers datasets[audio] evaluate jiwer1from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
2from datasets import load_dataset
3from evaluate import load
4import torch
5from tqdm import tqdm
6
7# define our torch configuration
8device = "cuda:0" if torch.cuda.is_available() else "cpu"
9torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
10
11model_id = "distil-whisper/distil-large-v3"
12
13# load the model + processor
14model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, use_safetensors=True, low_cpu_mem_usage=True)
15model = model.to(device)
16processor = AutoProcessor.from_pretrained(model_id)
17
18# load the dataset with streaming mode
19dataset = load_dataset("librispeech_asr", "clean", split="validation", streaming=True)
20
21# define the evaluation metric
22wer_metric = load("wer")
23
24def inference(batch):
25 # 1. Pre-process the audio data to log-mel spectrogram inputs
26 audio = [sample["array"] for sample in batch["audio"]]
27 input_features = processor(audio, sampling_rate=batch["audio"][0]["sampling_rate"], return_tensors="pt").input_features
28 input_features = input_features.to(device, dtype=torch_dtype)
29
30 # 2. Auto-regressively generate the predicted token ids
31 pred_ids = model.generate(input_features, max_new_tokens=128)
32
33 # 3. Decode the token ids to the final transcription
34 batch["transcription"] = processor.batch_decode(pred_ids, skip_special_tokens=True)
35 batch["reference"] = batch["text"]
36 return batch
37
38# batch size 16 inference
39dataset = dataset.map(function=inference, batched=True, batch_size=16)
40
41all_transcriptions = []
42all_references = []
43
44# iterate over the dataset and run inference
45for result in tqdm(dataset, desc="Evaluating..."):
46 all_transcriptions.append(result["transcription"])
47 all_references.append(result["reference"])
48
49# normalize predictions and references
50all_transcriptions = [processor.normalize(transcription) for transcription in all_transcriptions]
51all_references = [processor.normalize(reference) for reference in all_references]
52
53# compute the WER metric
54wer = 100 * wer_metric.compute(predictions=all_transcriptions, references=all_references)
55print(wer)
562.428920763531516| 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}
}