Download code/example.py from sanjithrj/tiny-ear-asr: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/sanjithrj/tiny-ear-asr/resolve/main/code/example.py
- Command line
-
hf download hf://sanjithrj/tiny-ear-asr/code/example.py
-
curl -L -o example.py https://huggingface.co/sanjithrj/tiny-ear-asr/resolve/main/code/example.py
1.09 kB
| """Transcribe a wav file with the PyTorch weights (streaming 640 ms chunks, 2 loop passes). | |
| python example.py audio.wav [../weights/model.safetensors]""" | |
| import json, math, os, sys | |
| import sentencepiece as spm, soundfile as sf, torch | |
| from scipy.signal import resample_poly | |
| from safetensors.torch import load_file | |
| from tinyear.model import ASR | |
| from tinyear.decode import tdt_greedy | |
| HERE = os.path.dirname(os.path.abspath(__file__)); W = os.path.join(HERE, "..", "weights") | |
| ckpt = sys.argv[2] if len(sys.argv) > 2 else f"{W}/model.safetensors" | |
| model = ASR(**json.load(open(f"{W}/config.json"))); model.load_state_dict(load_file(ckpt)); model.eval() | |
| sp = spm.SentencePieceProcessor(model_file=f"{W}/tokenizer.model") | |
| w, sr = sf.read(sys.argv[1], dtype="float32", always_2d=True); w = w.mean(1) | |
| if sr != 16000: g = math.gcd(16000, sr); w = resample_poly(w, 16000 // g, sr // g).astype("float32") | |
| wav = torch.tensor(w)[None] | |
| with torch.no_grad(): | |
| enc, ctc, _, fl = model(wav, torch.tensor([wav.shape[1]]), chunk=8, passes=2) | |
| print(sp.decode(tdt_greedy(model, enc.float(), fl)[0])) | |