Views
No views yet
1import torch
2import torchaudio
3
4from transformers import AutoModelForCTC, Wav2Vec2Processor
5
6processor = Wav2Vec2Processor.from_pretrained("bhuang/wav2vec2-xls-r-1b-voxpopuli-fr")
7model = AutoModelForCTC.from_pretrained("bhuang/wav2vec2-xls-r-1b-voxpopuli-fr").cuda()
8
9# path to your audio file
10wav_path = "example.wav"
11waveform, sample_rate = torchaudio.load(wav_path)
12waveform = waveform.squeeze(axis=0) # mono
13
14# resample
15if sample_rate != 16_000:
16 resampler = torchaudio.transforms.Resample(sample_rate, 16_000)
17 waveform = resampler(waveform)
18
19# normalize
20input_dict = processor(waveform, sampling_rate=16_000, return_tensors="pt")
21
22with torch.inference_mode():
23 logits = model(input_dict.input_values.to("cuda")).logits
24
25# decode
26predicted_ids = torch.argmax(logits, dim=-1)
27predicted_sentence = processor.batch_decode(predicted_ids)[0]1import torch
2import torchaudio
3
4from transformers import AutoModelForCTC, Wav2Vec2ProcessorWithLM
5
6processor_with_lm = Wav2Vec2ProcessorWithLM.from_pretrained("bhuang/wav2vec2-xls-r-1b-voxpopuli-fr")
7model = AutoModelForCTC.from_pretrained("bhuang/wav2vec2-xls-r-1b-voxpopuli-fr").cuda()
8
9model_sampling_rate = processor_with_lm.feature_extractor.sampling_rate
10
11# path to your audio file
12wav_path = "example.wav"
13waveform, sample_rate = torchaudio.load(wav_path)
14waveform = waveform.squeeze(axis=0) # mono
15
16# resample
17if sample_rate != 16_000:
18 resampler = torchaudio.transforms.Resample(sample_rate, 16_000)
19 waveform = resampler(waveform)
20
21# normalize
22input_dict = processor_with_lm(waveform, sampling_rate=16_000, return_tensors="pt")
23
24with torch.inference_mode():
25 logits = model(input_dict.input_values.to("cuda")).logits
26
27predicted_sentence = processor_with_lm.batch_decode(logits.cpu().numpy()).text[0]polinaeterna/voxpopuli1python eval.py \
2 --model_id "bhuang/wav2vec2-xls-r-1b-voxpopuli-fr" \
3 --dataset "polinaeterna/voxpopuli" \
4 --config "fr" \
5 --split "test" \
6 --log_outputs \
7 --outdir "outputs/results_polinaeterna_voxpopuli_with_lm"mozilla-foundation/common_voice_9_01python eval.py \
2 --model_id "bhuang/wav2vec2-xls-r-1b-voxpopuli-fr" \
3 --dataset "mozilla-foundation/common_voice_9_0" \
4 --config "fr" \
5 --split "test" \
6 --log_outputs \
7 --outdir "outputs/results_mozilla-foundatio_common_voice_9_0_with_lm"speech-recognition-community-v2/dev_data1python eval.py \
2 --model_id "bhuang/wav2vec2-xls-r-1b-voxpopuli-fr" \
3 --dataset "speech-recognition-community-v2/dev_data" \
4 --config "fr" \
5 --split "validation" \
6 --chunk_length_s 5.0 \
7 --stride_length_s 1.0 \
8 --log_outputs \
9 --outdir "outputs/results_speech-recognition-community-v2_dev_data_with_lm"