logo

Mobile Artificial IntelligenceMobile AI

Babylon.cpp / Guides / Python Wrapper

Python Wrapper

The pre-built Python package bundles compiled libraries for Linux, macOS, and Windows alongside a babylon.py wrapper module. Add local G2P and TTS to any Python project — no compilation required.

Installation

The package is distributed as a CI artifact attached to each release on GitHub. Download the wheel for your platform and install it with pip:

pip install babylon-0.0.2-linux-x86_64.whl

Alternatively, the babylon.py module can be used directly from the source tree alongside the compiled shared library.

Model setup

The Python wrapper requires the same model files as the CLI. Place them in a models/ directory or pass explicit paths to each init function.

FileRequired for
open-phonemizer.onnxG2P phonemization (all engines)
dictionary.jsonG2P dictionary lookup (optional but recommended)
kokoro-quantized.onnxKokoro TTS
voices/*.binKokoro voice styles
curie.onnxVITS TTS (optional)

Kokoro TTS example

Initialise the G2P session first, then the Kokoro session. Call kokoro_tts to synthesise speech and write a WAV file.

import babylon babylon.g2p_init( "models/open-phonemizer.onnx", "models/dictionary.json" ) babylon.kokoro_init("models/kokoro-quantized.onnx") babylon.kokoro_tts( "Hello world", "models/voices/en-US-heart.bin", speed=1.0, output_path="output.wav" ) babylon.kokoro_free() babylon.g2p_free()

G2P (phonemization)

Use g2p to get IPA phonemes, or g2p_tokens to get Kokoro token IDs.

import babylon babylon.g2p_init( "models/open-phonemizer.onnx", "models/dictionary.json" ) ipa = babylon.g2p("Hello world") print(ipa) # hɛloʊ wɜːld tokens = babylon.g2p_tokens("Hello world") print(tokens) # [31, 29, 42, 0, 51, 17, 32, 42] babylon.g2p_free()

VITS TTS example

import babylon babylon.g2p_init( "models/open-phonemizer.onnx", "models/dictionary.json" ) babylon.tts_init("models/curie.onnx") babylon.tts("Hello world", "output.wav") babylon.tts_free() babylon.g2p_free()

API reference

FunctionDescription
g2p_init(model, dictionary)Initialise G2P session
g2p(text)Returns IPA string
g2p_tokens(text)Returns list of Kokoro token IDs
g2p_free()Release G2P session
kokoro_init(model)Initialise Kokoro session
kokoro_tts(text, voice, speed, output_path)Synthesise with Kokoro
kokoro_free()Release Kokoro session
tts_init(model)Initialise VITS session
tts(text, output_path)Synthesise with VITS
tts_free()Release VITS session
Always call g2p_init before any TTS init function — the TTS engines depend on the G2P session being loaded.