Views
No views yet
model_ctc.onnx (FP model, 420.22 MB)model_ctc_quantized.onnx (INT8 quantized, 133.94 MB)model_ctc.onnx: full-size ONNX modelmodel_ctc_quantized.onnx: quantized ONNX modelmodel_config.yaml: NeMo config (preprocessor + vocabulary)local_onnx_asr_inference.ipynb: notebook for local testinginstruction.txt: post-upload guide for Hugging Face Spacespip install onnxruntime soundfile scipy numpy pyyaml omegaconf torch "nemo_toolkit[asr]"1import numpy as np
2import onnxruntime as ort
3import soundfile as sf
4import torch
5import yaml
6from omegaconf import OmegaConf
7from scipy.signal import resample_poly
8from nemo.collections.asr.modules import AudioToMelSpectrogramPreprocessor
9
10ONNX_PATH = "model_ctc_quantized.onnx" # or "model_ctc.onnx"
11CONFIG_PATH = "model_config.yaml"
12AUDIO_PATH = "sample.wav"
13
14# Load config
15try:
16 conf = OmegaConf.load(CONFIG_PATH)
17except Exception:
18 with open(CONFIG_PATH, "r", encoding="utf-8") as f:
19 conf = OmegaConf.create(yaml.safe_load(f))
20
21preprocessor_cfg = OmegaConf.to_container(conf.preprocessor, resolve=True)
22preprocessor_cfg.pop("_target_", None)
23preprocessor = AudioToMelSpectrogramPreprocessor(**preprocessor_cfg)
24preprocessor.eval()
25SAMPLE_RATE = preprocessor_cfg["sample_rate"]
26
27vocabulary = (
28 conf.get("aux_ctc", {}).get("decoder", {}).get("vocabulary", None)
29 or conf.get("decoder", {}).get("vocabulary", None)
30)
31
32session = ort.InferenceSession(ONNX_PATH, providers=["CPUExecutionProvider"])
33session_ins = session.get_inputs()
34main_input = next((x for x in session_ins if "length" not in x.name.lower()), session_ins[0])
35length_input = next((x for x in session_ins if "length" in x.name.lower()), None)
36
37def _length_dtype(meta):
38 return np.int32 if meta and "int32" in meta.type else np.int64
39
40def decode_ctc(logits, encoded_len, vocab):
41 greedy = logits[0].argmax(axis=-1)[: int(encoded_len[0])]
42 blank_id = logits.shape[-1] - 1
43 collapsed, prev = [], None
44 for t in greedy:
45 t = int(t)
46 if t == prev or t == blank_id:
47 prev = t
48 continue
49 collapsed.append(t)
50 prev = t
51
52 if not vocab:
53 return str(collapsed)
54
55 text = ""
56 for i in collapsed:
57 if 0 <= i < len(vocab):
58 tok = vocab[i]
59 if tok.startswith("##"):
60 text += tok[2:]
61 elif tok.startswith("▁"):
62 text += " " + tok[1:]
63 else:
64 text += tok
65 return text.strip().replace("▁", " ")
66
67def transcribe(audio_path: str) -> str:
68 audio, sr = sf.read(audio_path)
69 if audio.ndim == 2:
70 audio = audio.mean(axis=1)
71 if sr != SAMPLE_RATE:
72 audio = resample_poly(audio, SAMPLE_RATE, sr)
73
74 audio = np.clip(audio, -1.0, 1.0).astype(np.float32)
75 audio_len = np.array([audio.shape[0]], dtype=np.int64)
76
77 ort_inputs = {}
78 if len(main_input.shape) == 2:
79 ort_inputs[main_input.name] = audio[None, :]
80 if length_input is not None:
81 ort_inputs[length_input.name] = audio_len.astype(_length_dtype(length_input))
82 elif len(main_input.shape) == 3:
83 with torch.no_grad():
84 mel, mel_len = preprocessor(
85 input_signal=torch.from_numpy(audio[None, :]),
86 length=torch.from_numpy(audio_len),
87 )
88 ort_inputs[main_input.name] = mel.numpy().astype(np.float32)
89 if length_input is not None:
90 ort_inputs[length_input.name] = mel_len.numpy().astype(_length_dtype(length_input))
91
92 outputs = session.run(None, ort_inputs)
93 logits = next((x for x in outputs if getattr(x, "ndim", 0) == 3), None)
94 encoded_len = next((x for x in outputs if getattr(x, "ndim", 0) == 1), None)
95 if encoded_len is None:
96 encoded_len = np.array([logits.shape[1]], dtype=np.int64)
97
98 return decode_ctc(logits, encoded_len, vocabulary)
99
100print(transcribe(AUDIO_PATH))1from huggingface_hub import hf_hub_download
2
3repo_id = "gam30/nepali-automatic-speech-recognition"
4onnx_path = hf_hub_download(repo_id=repo_id, filename="model_ctc_quantized.onnx")
5config_path = hf_hub_download(repo_id=repo_id, filename="model_config.yaml")ONNX_PATH and CONFIG_PATH.local_onnx_asr_inference.ipynb is the reference test workflow.gam30. (2025). Nepali Automatic Speech Recognition (ONNX CTC) [Model]. Hugging Face. https://huggingface.co/gam30/nepali-automatic-speech-recognitionPlease note: This model is based on the architecture from ai4bharat/indicconformer_stt_ne_hybrid_ctc_rnnt_large. When citing, please also acknowledge the original base model authors.