Views
No views yet

pipeline with both chunked (chunk_length_s=30) and original sequential decoding methods.
pipeline class for audio transcription. For long-form transcription (over 30 seconds), it will perform sequential decoding as described in OpenAI's paper. If you need faster inference, you can use the chunk_length_s argument for chunked parallel decoding, which provides 9x faster inference speed but may slightly compromise performance compared to OpenAI's sequential algorithm.1import torch
2from datasets import load_dataset
3from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
4
5device = "cuda:0" if torch.cuda.is_available() else "cpu"
6torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
7
8# Load model
9model_name_or_path = "bofenghuang/whisper-large-v3-distil-fr-v0.2"
10processor = AutoProcessor.from_pretrained(model_name_or_path)
11model = AutoModelForSpeechSeq2Seq.from_pretrained(
12 model_name_or_path,
13 torch_dtype=torch_dtype,
14 low_cpu_mem_usage=True,
15)
16model.to(device)
17
18# Init pipeline
19pipe = pipeline(
20 "automatic-speech-recognition",
21 model=model,
22 feature_extractor=processor.feature_extractor,
23 tokenizer=processor.tokenizer,
24 torch_dtype=torch_dtype,
25 device=device,
26 # chunk_length_s=30, # for chunked decoding
27 max_new_tokens=128,
28)
29
30# Example audio
31dataset = load_dataset("bofenghuang/asr-dummy", "fr", split="test")
32sample = dataset[0]["audio"]
33
34# Run pipeline
35result = pipe(sample)
36print(result["text"])1import torch
2from datasets import load_dataset
3from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
4
5device = "cuda:0" if torch.cuda.is_available() else "cpu"
6torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
7
8# Load model
9model_name_or_path = "bofenghuang/whisper-large-v3-distil-fr-v0.2"
10processor = AutoProcessor.from_pretrained(model_name_or_path)
11model = AutoModelForSpeechSeq2Seq.from_pretrained(
12 model_name_or_path,
13 torch_dtype=torch_dtype,
14 low_cpu_mem_usage=True,
15)
16model.to(device)
17
18# Example audio
19dataset = load_dataset("bofenghuang/asr-dummy", "fr", split="test")
20sample = dataset[0]["audio"]
21
22# Extract feautres
23input_features = processor(
24 sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt"
25).input_features
26
27
28# Generate tokens
29predicted_ids = model.generate(
30 input_features.to(dtype=torch_dtype).to(device), max_new_tokens=128
31)
32
33# Detokenize to text
34transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
35print(transcription)assistant_model within the generation configurations.1import torch
2from datasets import load_dataset
3from transformers import (
4 AutoModelForCausalLM,
5 AutoModelForSpeechSeq2Seq,
6 AutoProcessor,
7 pipeline,
8)
9
10device = "cuda:0" if torch.cuda.is_available() else "cpu"
11torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
12
13# Load model
14model_name_or_path = "openai/whisper-large-v3"
15processor = AutoProcessor.from_pretrained(model_name_or_path)
16model = AutoModelForSpeechSeq2Seq.from_pretrained(
17 model_name_or_path,
18 torch_dtype=torch_dtype,
19 low_cpu_mem_usage=True,
20)
21model.to(device)
22
23# Load draft model
24assistant_model_name_or_path = "bofenghuang/whisper-large-v3-distil-fr-v0.2"
25assistant_model = AutoModelForCausalLM.from_pretrained(
26 assistant_model_name_or_path,
27 torch_dtype=torch_dtype,
28 low_cpu_mem_usage=True,
29)
30assistant_model.to(device)
31
32# Init pipeline
33pipe = pipeline(
34 "automatic-speech-recognition",
35 model=model,
36 feature_extractor=processor.feature_extractor,
37 tokenizer=processor.tokenizer,
38 torch_dtype=torch_dtype,
39 device=device,
40 generate_kwargs={"assistant_model": assistant_model},
41 max_new_tokens=128,
42)
43
44# Example audio
45dataset = load_dataset("bofenghuang/asr-dummy", "fr", split="test")
46sample = dataset[0]["audio"]
47
48# Run pipeline
49result = pipe(sample)
50print(result["text"])pip install -U openai-whisperhuggingface-cli download --include original_model.pt --local-dir ./models/whisper-large-v3-distil-fr-v0.2 bofenghuang/whisper-large-v3-distil-fr-v0.21import whisper
2from datasets import load_dataset
3
4# Load model
5model_name_or_path = "./models/whisper-large-v3-distil-fr-v0.2/original_model.pt"
6model = whisper.load_model(model_name_or_path)
7
8# Example audio
9dataset = load_dataset("bofenghuang/asr-dummy", "fr", split="test")
10sample = dataset[0]["audio"]["array"].astype("float32")
11
12# Transcribe
13result = model.transcribe(sample, language="fr")
14print(result["text"])pip install faster-whisperhuggingface-cli download --include ctranslate2/* --local-dir ./models/whisper-large-v3-distil-fr-v0.2 bofenghuang/whisper-large-v3-distil-fr-v0.21from datasets import load_dataset
2from faster_whisper import WhisperModel
3
4# Load model
5model_name_or_path = "./models/whisper-large-v3-distil-fr-v0.2/ctranslate2"
6model = WhisperModel(model_name_or_path", device="cuda", compute_type="float16") # Run on GPU with FP16
7
8# Example audio
9dataset = load_dataset("bofenghuang/asr-dummy", "fr", split="test")
10sample = dataset[0]["audio"]["array"].astype("float32")
11
12segments, info = model.transcribe(sample, beam_size=5, language="fr")
13
14for segment in segments:
15 print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))1git clone https://github.com/ggerganov/whisper.cpp.git
2cd whisper.cpp
3
4# build the main example
5make1# Download model quantized with Q5_0 method
2huggingface-cli download --include ggml-model* --local-dir ./models/whisper-large-v3-distil-fr-v0.2 bofenghuang/whisper-large-v3-distil-fr-v0.2./main -m ./models/whisper-large-v3-distil-fr-v0.2/ggml-model-q5_0.bin -l fr -f /path/to/audio/file --print-colors1git clone https://github.com/huggingface/candle.git
2cd candle/candle-examples/examples/whispercargo run --example whisper --release -- --model large-v3 --model-id bofenghuang/whisper-large-v3-distil-fr-v0.2 --language fr --input /path/to/audio/file--features cuda to the example command line:cargo run --example whisper --release --features cuda -- --model large-v3 --model-id bofenghuang/whisper-large-v3-distil-fr-v0.2 --language fr --input /path/to/audio/file1git clone https://github.com/ml-explore/mlx-examples.git
2cd mlx-examples/whisperpip install -r requirements.txt1# Download
2huggingface-cli download --include original_model.pt --local-dir ./models/whisper-large-v3-distil-fr-v0.2 bofenghuang/whisper-large-v3-distil-fr-v0.2
3# Convert into .npz
4python convert.py --torch-name-or-path ./models/whisper-large-v3-distil-fr-v0.2/original_model.pt --mlx-path ./mlx_models/whisper-large-v3-distil-fr-v0.21import whisper
2
3result = whisper.transcribe("/path/to/audio/file", path_or_hf_repo="mlx_models/whisper-large-v3-distil-fr-v0.2", language="fr")
4print(result["text"])| Dataset | Total Duration (h) | Filtered Duration (h) <20% WER |
|---|---|---|
| mcv | 800.37 | 687.02 |
| mls | 1076.58 | 1043.87 |
| voxpopuli | 199.03 | 177.11 |
| mtedx | 170.31 | 147.48 |
| african_accented_french | 7.69 | 7.69 |
| yodas-fr000 | 2395.82 | 1502.82 |
| yodas-fr100 | 4978.16 | 1887.36 |
| yodas-fr101 | 4966.07 | 1882.39 |
| yodas-fr102 | 4992.84 | 1877.40 |
| yodas-fr103 | 3161.39 | 1189.32 |
| total | 22748.26 | 10402.46 |