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