Views
No views yet
1>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
2>>> from datasets import load_dataset
3
4>>> # load model and processor
5>>> processor = WhisperProcessor.from_pretrained("intronhealth/afrispeech-whisper-medium-all")
6>>> model = WhisperForConditionalGeneration.from_pretrained("intronhealth/afrispeech-whisper-medium-all")
7>>> model.config.forced_decoder_ids = None
8
9>>> # load dummy dataset and read audio files
10>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
11>>> sample = ds[0]["audio"]
12>>> input_features = processor(sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt").input_features
13
14>>> # generate token ids
15>>> predicted_ids = model.generate(input_features)
16>>> # decode token ids to text
17>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=False)
18['<|startoftranscript|><|en|><|transcribe|><|notimestamps|> Mr. Quilter is the apostle of the middle classes and we are glad to welcome his gospel.<|endoftext|>']
19
20>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
21[' Mr. Quilter is the apostle of the middle classes and we are glad to welcome his gospel.']skip_special_tokens=True.pipeline
method. Chunking is enabled by setting chunk_length_s=30 when instantiating the pipeline. With chunking enabled, the pipeline
can be run with batched inference. It can also be extended to predict sequence level timestamps by passing return_timestamps=True:1>>> import torch
2>>> from transformers import pipeline
3>>> from datasets import load_dataset
4
5>>> device = "cuda:0" if torch.cuda.is_available() else "cpu"
6
7>>> pipe = pipeline(
8>>> "automatic-speech-recognition",
9>>> model="intronhealth/afrispeech-whisper-medium-all",
10>>> chunk_length_s=30,
11>>> device=device,
12>>> )
13
14>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
15>>> sample = ds[0]["audio"]
16
17>>> prediction = pipe(sample.copy(), batch_size=8)["text"]
18" Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel."
19
20>>> # we can also return timestamps for the predictions
21>>> prediction = pipe(sample.copy(), batch_size=8, return_timestamps=True)["chunks"]
22[{'text': ' Mr. Quilter is the apostle of the middle classes and we are glad to welcome his gospel.',
23 'timestamp': (0.0, 5.44)}]