Quickstart

Minimal examples for G2P, pronunciation correction, phoneme ASR, and P2G.

Examples target hama / hama-js 1.7.0. Run JavaScript snippets with Bun, or bundle them for Node using the installation steps. Browser snippets run in client-side code after the Vite / Astro setup, with weights served from /hama-assets/. For ASR, supply a PCM WAV as sample.wav in the working directory (Node/Python) or public directory (browser).

G2P

from hama import G2PModel

model = G2PModel()
result = model.predict(
  "Really? What's the orbital velocity of the moon?",
  preserve_literals="punct",
)
print(result.ipa)
print(result.display_ipa)

Pronunciation Correction

from hama import pronunciation_replace

text = "we met (jon smyth), yesterday"
terms = [{"text": "John Smythe"}]

result = pronunciation_replace(text, terms)
print(result["text"])

ASR

from hama import ASRModel

model = ASRModel()
result = model.transcribe_file("sample.wav")
print(result.phoneme_text)
print(result.word_phoneme_text)

# coarse per-phoneme time spans (CTC is peaky)
for span in model.phoneme_spans(result):
  print(span.phoneme, span.start_ms, span.end_ms)

P2G

from hama import P2GModel

p2g = P2GModel()
result = p2g.predict(["l", "ɛ", "t", "|", "m", "e", "|", "s", "i"])
print(result.text)

# each output token aligned to the phoneme it most attends to
for a in result.alignments:
  print(a.token, a.phoneme_index, a.phoneme)

Audio → Text

After the ASR example above, pass result.word_phoneme_text / result.wordPhonemeText to P2G. This preserves word boundaries; result.phonemes omits them. This is a two-model pipeline.

from hama import P2GModel

# Continue from the ASR example's result.
p2g = P2GModel()
print(p2g.predict(result.word_phoneme_text).text)

For full signatures, options, and return shapes, see APIs.