Views
No views yet

1from transformers import Wav2Vec2ForPreTraining, Wav2Vec2Processor
2
3model_name = 'nguyenvulebinh/wav2vec2-base-vi'
4# model_name = 'nguyenvulebinh/wav2vec2-large-vi'
5
6model = Wav2Vec2ForPreTraining.from_pretrained(model_name)
7processor = Wav2Vec2Processor.from_pretrained(model_name)
8| base model | large model | |
|---|---|---|
| without LM | 8.66 | 6.90 |
| with 5-grams LM | 6.53 | 5.32 |
1#pytorch
2#!pip install transformers==4.20.0
3#!pip install https://github.com/kpu/kenlm/archive/master.zip
4#!pip install pyctcdecode==0.4.0
5from transformers.file_utils import cached_path, hf_bucket_url
6from importlib.machinery import SourceFileLoader
7from transformers import Wav2Vec2ProcessorWithLM
8from IPython.lib.display import Audio
9import torchaudio
10import torch
11
12# Load model & processor
13model_name = "nguyenvulebinh/wav2vec2-base-vi-vlsp2020"
14# model_name = "nguyenvulebinh/wav2vec2-large-vi-vlsp2020"
15model = SourceFileLoader("model", cached_path(hf_bucket_url(model_name,filename="model_handling.py"))).load_module().Wav2Vec2ForCTC.from_pretrained(model_name)
16processor = Wav2Vec2ProcessorWithLM.from_pretrained(model_name)
17
18# Load an example audio (16k)
19audio, sample_rate = torchaudio.load(cached_path(hf_bucket_url(model_name, filename="t2_0000006682.wav")))
20input_data = processor.feature_extractor(audio[0], sampling_rate=16000, return_tensors='pt')
21
22# Infer
23output = model(**input_data)
24
25# Output transcript without LM
26print(processor.tokenizer.decode(output.logits.argmax(dim=-1)[0].detach().cpu().numpy()))
27
28# Output transcript with LM
29print(processor.decode(output.logits.cpu().detach().numpy()[0], beam_width=100).text)