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) import { G2PNodeModel } from "hama-js/g2p";
const model = await G2PNodeModel.create();
const result = await model.predict(
"Really? What's the orbital velocity of the moon?",
{ preserveLiterals: "punct" },
);
console.log(result.ipa);
console.log(result.displayIpa); import { G2PBrowserModel } from "hama-js/g2p/browser";
const model = await G2PBrowserModel.create({
encoderUrl: "/hama-assets/encoder.hama",
decoderStepUrl: "/hama-assets/decoder_step.hama",
});
const result = await model.predict("안녕하세요", {
preserveLiterals: "punct",
});
console.log(result.ipa);
console.log(result.displayIpa); 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"]) import { pronunciationReplace } from "hama-js";
const text = "we met (jon smyth), yesterday";
const terms = [{ text: "John Smythe" }];
const result = await pronunciationReplace(text, terms);
console.log(result.text); import { G2PBrowserModel } from "hama-js/g2p/browser";
const model = await G2PBrowserModel.create({
encoderUrl: "/hama-assets/encoder.hama",
decoderStepUrl: "/hama-assets/decoder_step.hama",
});
const result = await model.pronunciationReplace(
"we met (jon smyth), yesterday",
[{ text: "John Smythe" }],
);
console.log(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) import { ASRNodeModel } from "hama-js/asr";
const model = await ASRNodeModel.create();
const result = await model.transcribeWavFile("sample.wav");
console.log(result.phonemeText);
// coarse per-phoneme time spans (CTC is peaky)
for (const span of model.phonemeSpans(result)) {
console.log(span.phoneme, span.startMs, span.endMs);
} import { ASRBrowserModel } from "hama-js/asr/browser";
const model = await ASRBrowserModel.create({
modelUrl: "/hama-assets/asr_waveform.hama",
});
const response = await fetch("/sample.wav");
if (!response.ok) throw new Error("Failed to load sample.wav");
const audio = new AudioContext();
const buffer = await audio.decodeAudioData(await response.arrayBuffer());
// Use a mono WAV; pass the decoded rate so hama can resample to 16 kHz.
const result = await model.transcribeWaveform(
buffer.getChannelData(0), buffer.sampleRate,
);
await audio.close();
console.log(result.phonemeText);
// coarse per-phoneme time spans (CTC is peaky)
for (const span of model.phonemeSpans(result)) {
console.log(span.phoneme, span.startMs, span.endMs);
} 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) import { P2GNodeModel } from "hama-js/p2g";
const p2g = await P2GNodeModel.create();
const result = p2g.predict("l ɛ t | m e | s i");
console.log(result.text);
// each output token aligned to the phoneme it most attends to
for (const a of result.alignments) {
console.log(a.token, a.phonemeIndex, a.phoneme);
} import { P2GBrowserModel } from "hama-js/p2g/browser";
const p2g = await P2GBrowserModel.create({ modelUrl: "/hama-assets/p2g.hama" });
const result = p2g.predict("l ɛ t | m e | s i");
console.log(result.text);
// each output token aligned to the phoneme it most attends to
for (const a of result.alignments) {
console.log(a.token, a.phonemeIndex, 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) import { P2GNodeModel } from "hama-js/p2g";
// Continue from the ASR example's result.
const p2g = await P2GNodeModel.create();
console.log(p2g.predict(result.wordPhonemeText).text); import { P2GBrowserModel } from "hama-js/p2g/browser";
// Continue from the ASR example's result.
const p2g = await P2GBrowserModel.create({
modelUrl: "/hama-assets/p2g.hama",
});
console.log(p2g.predict(result.wordPhonemeText).text); For full signatures, options, and return shapes, see APIs.