Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pretrained LM API for decoder example #2317

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions examples/asr/librispeech_ctc_decoder/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,24 @@

import torch
import torchaudio
from torchaudio.prototype.ctc_decoder import lexicon_decoder
from torchaudio.prototype.ctc_decoder import lexicon_decoder, download_pretrained_files


logger = logging.getLogger(__name__)


def _download_files(lexicon_file, kenlm_file):
torch.hub.download_url_to_file(
"https://pytorch.s3.amazonaws.com/torchaudio/tutorial-assets/ctc-decoding/lexicon-librispeech.txt", lexicon_file
)
torch.hub.download_url_to_file(
"https://pytorch.s3.amazonaws.com/torchaudio/tutorial-assets/ctc-decoding/4-gram-librispeech.bin", kenlm_file
)


def run_inference(args):
# get pretrained wav2vec2.0 model
bundle = getattr(torchaudio.pipelines, args.model)
model = bundle.get_model()
tokens = [label.lower() for label in bundle.get_labels()]

# get decoder files
hub_dir = torch.hub.get_dir()
lexicon_file = f"{hub_dir}/lexicon.txt"
kenlm_file = f"{hub_dir}/kenlm.bin"
_download_files(lexicon_file, kenlm_file)
files = download_pretrained_files("librispeech-4-gram")

decoder = lexicon_decoder(
lexicon=lexicon_file,
tokens=tokens,
lm=kenlm_file,
lexicon=files.lexicon,
tokens=files.tokens,
lm=files.lm,
nbest=args.nbest,
beam_size=args.beam_size,
beam_size_token=args.beam_size_token,
Expand Down