Views
No views yet
| 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
5#!pip install huggingface_hub==0.10.0
6
7from transformers.file_utils import cached_path, hf_bucket_url
8from importlib.machinery import SourceFileLoader
9from transformers import Wav2Vec2ProcessorWithLM
10from IPython.lib.display import Audio
11import torchaudio
12import torch
13
14# Load model & processor
15model_name = "nguyenvulebinh/wav2vec2-base-vi-vlsp2020"
16model = SourceFileLoader("model", cached_path(hf_bucket_url(model_name,filename="model_handling.py"))).load_module().Wav2Vec2ForCTC.from_pretrained(model_name)
17processor = Wav2Vec2ProcessorWithLM.from_pretrained(model_name)
18
19# Load an example audio (16k)
20audio, sample_rate = torchaudio.load(cached_path(hf_bucket_url(model_name, filename="t2_0000006682.wav")))
21input_data = processor.feature_extractor(audio[0], sampling_rate=16000, return_tensors='pt')
22
23# Infer
24output = model(**input_data)
25
26# Output transcript without LM
27print(processor.tokenizer.decode(output.logits.argmax(dim=-1)[0].detach().cpu().numpy()))
28
29# Output transcript with LM
30print(processor.decode(output.logits.cpu().detach().numpy()[0], beam_width=100).text)