Add support for GigaAM-CTC-v2 (#2135)

Related to #2098.
This commit is contained in:
Roman Inflianskas
2025-04-19 17:13:39 +03:00
committed by GitHub
parent 84ed5d4288
commit 01110cc22b
3 changed files with 133 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
import gigaam
import onnx
import torch
from onnxruntime.quantization import QuantType, quantize_dynamic
def add_meta_data(filename: str, meta_data: dict[str, str]):
"""Add meta data to an ONNX model. It is changed in-place.
Args:
filename:
Filename of the ONNX model to be changed.
meta_data:
Key-value pairs.
"""
model = onnx.load(filename)
while len(model.metadata_props):
model.metadata_props.pop()
for key, value in meta_data.items():
meta = model.metadata_props.add()
meta.key = key
meta.value = str(value)
onnx.save(model, filename)
def main() -> None:
model_name = "v2_ctc"
model = gigaam.load_model(model_name, fp16_encoder=False, use_flash=False, download_root=".")
with open("./tokens.txt", "w", encoding="utf-8") as f:
for i, s in enumerate(model.cfg["labels"]):
f.write(f"{s} {i}\n")
f.write(f"<blk> {i+1}\n")
print("Saved to tokens.txt")
model.to_onnx(".")
meta_data = {
"vocab_size": len(model.cfg["labels"]) + 1,
"normalize_type": "",
"subsampling_factor": 4,
"model_type": "EncDecCTCModel",
"version": "1",
"model_author": "https://github.com/salute-developers/GigaAM",
"license": "https://github.com/salute-developers/GigaAM/blob/main/LICENSE",
"language": "Russian",
"is_giga_am": 1,
}
add_meta_data(f"./{model_name}.onnx", meta_data)
quantize_dynamic(
model_input=f"./{model_name}.onnx",
model_output="./model.int8.onnx",
weight_type=QuantType.QUInt8,
)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -ex
function install_gigaam() {
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
BRANCH='main'
python3 -m pip install git+https://github.com/salute-developers/GigaAM.git@$BRANCH#egg=gigaam
python3 -m pip install -qq kaldi-native-fbank
}
function download_files() {
curl -SL -O https://huggingface.co/csukuangfj/tmp-files/resolve/main/GigaAM/example.wav
curl -SL -O https://github.com/salute-developers/GigaAM/blob/main/LICENSE
}
install_gigaam
download_files
python3 ./export-onnx-ctc-v2.py
ls -lh
python3 ./test-onnx-ctc.py