ONNX Model Zoo

zijuncheng/word-to-phoneme-onnx

By zijuncheng · ↓ 0 · ♥ 0 · Updated 2025-07-02

What's in this repo

This page lists the files in zijuncheng/word-to-phoneme-onnx, including w2p_bart.onnx.

  • 1 ONNX models (.onnx files)
  • 5 files, including 2 .json, 1 .md, 1 .onnx
  • Last updated 2025-07-02

ONNX files

File Size Download
w2p_bart.onnx — Download

How to use

These ONNX files can be loaded with ONNX Runtime. To inspect a model graph, upload the file to the Netron viewer. Download links point to the original files on Hugging Face.

README

How to load the model and run inferences

Download all the files to a local directory model_dir

Initiate ONNX Session
from torch.onnx import export
import onnxruntime as ort
from transformers import PreTrainedTokenizerFast

session = ort.InferenceSession("w2p_bart.onnx")
tokenizer = PreTrainedTokenizerFast.from_pretrained(model_dir)

bos_token_id = tokenizer.bos_token_id
eos_token_id = tokenizer.eos_token_id

Batch Inference
def g2p_onnx_batch(text, max_len=16):
    # 1. Preprocess: tokenize and space-out characters
    words = text.strip().split()
    spaced_words = [" ".join(list(word)) for word in words]

    encoded = tokenizer(spaced_words, return_tensors="np", padding=True, truncation=True, max_length=32)
    input_ids = encoded["input_ids"]
    attention_mask = encoded["attention_mask"]

    batch_size = input_ids.shape[0]
    decoder_input_ids = np.full((batch_size, 1), bos_token_id, dtype=np.int64)

    finished = np.zeros(batch_size, dtype=bool)

    for _ in range(max_len):
        ort_inputs = {
            "input_ids": input_ids,
            "attention_mask": attention_mask,
            "decoder_input_ids": decoder_input_ids
        }
        logits = session.run(["logits"], ort_inputs)[0]
        next_token_logits = logits[:, -1, :]
        next_token_ids = np.argmax(next_token_logits, axis=-1)

        decoder_input_ids = np.concatenate([decoder_input_ids, next_token_ids[:, None]], axis=1)

        finished |= (next_token_ids == eos_token_id)
        if finished.all():
            break

    decoded = tokenizer.batch_decode(decoder_input_ids, skip_special_tokens=True)
    phonemes = [r.replace(" ", "") for r in decoded]
    return " ".join(phonemes)

Example:

result = g2p_onnx_batch("banana apple question")
print(result)

This should return:

bənˈænə ˈæpᵊl kwˈɛsʧᵊn

Model info

onnx region:us
↓ 0 ♥ 0

Created: 2025-07-02

Updated: 2025-07-02

View on Hugging Face

Files (5)

.gitattributes
README.md
tokenizer.json
tokenizer_config.json
w2p_bart.onnx ONNX

Related models