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