Skip to main content

Python SDK for the Khaya AI API — translation, ASR, and TTS for African languages

Project description

Khaya SDK

PyPI version CI License: MIT Docs

Python SDK for the GhanaNLP Khaya API — providing translation, automatic speech recognition (ASR), and text-to-speech (TTS) for African languages.

Installation

pip install khaya

Authentication

Get an API key at https://translation.ghananlp.org and set it as an environment variable:

export KHAYA_API_KEY=your_api_key_here

Quick Start

import os
from khaya import KhayaClient

with KhayaClient(os.environ["KHAYA_API_KEY"]) as khaya:
    # Translate text from English to Twi
    result = khaya.translate("Hello, how are you?", "en-tw")
    print(result.json())

    # Transcribe a Twi audio file
    result = khaya.transcribe("path/to/audio.wav", "tw")
    print(result.json())

    # Synthesize speech in Twi
    result = khaya.synthesize("Me ho yɛ", "tw")
    with open("output.mp3", "wb") as f:
        f.write(result.content)

Async Usage

import asyncio
import os
from khaya import KhayaClient

async def main():
    async with KhayaClient(os.environ["KHAYA_API_KEY"]) as khaya:
        result = await khaya.atranslate("Hello", "en-tw")
        print(result.json())

        result = await khaya.atranscribe("path/to/audio.wav", "tw")
        print(result.json())

        result = await khaya.asynthesize("Me ho yɛ", "tw")
        with open("output.mp3", "wb") as f:
            f.write(result.content)

asyncio.run(main())

Error Handling

All errors raise exceptions — never return error dicts. Catch the appropriate exception:

from khaya import KhayaClient
from khaya.exceptions import (
    AuthenticationError,
    RateLimitError,
    TranslationError,
    APIError,
)

khaya = KhayaClient(api_key="your-key")

try:
    result = khaya.translate("Hello", "en-tw")
    print(result.json())
except AuthenticationError:
    print("Invalid API key. Check your KHAYA_API_KEY.")
except RateLimitError as e:
    print(f"Rate limit hit: {e.message}")
except TranslationError as e:
    print(f"Translation failed ({e.status_code}): {e.message}")
except APIError as e:
    print(f"API error ({e.status_code}): {e.message}")

Supported Languages

Translation pairs

Code Language
en English
tw Twi
ee Ewe
gaa Ga
dag Dagbani
dga Dagaare
fat Fante
gur Gurene
nzi Nzema
kpo Ghanaian Pidgin
yo Yoruba
ki Kikuyu

Language pair format: "<source>-<target>", e.g. "en-tw" or "tw-en".

ASR languages

tw, gaa, dag, ee, dga, fat, gur, nzi, kpo, yo

TTS languages

tw, gaa, dag, ee, yo

Configuration

from khaya import KhayaClient
from khaya.config import Settings

config = Settings(
    api_key="your-key",
    timeout=60,          # seconds (default: 30)
    retry_attempts=5,    # retries on transient failures (default: 3)
)
khaya = KhayaClient(api_key="your-key", config=config)

Development

# Install all dependency groups
uv sync --extra test --extra dev

# Run unit tests (no API key required)
uv run pytest -m "not integration"

# Run with coverage
uv run pytest -m "not integration" --cov=src/khaya --cov-report=term-missing

# Lint and type-check
uv run ruff check src/khaya
uv run mypy src/khaya

# Install pre-commit hooks
uv run pre-commit install

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

khaya-0.1.0.tar.gz (128.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

khaya-0.1.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file khaya-0.1.0.tar.gz.

File metadata

  • Download URL: khaya-0.1.0.tar.gz
  • Upload date:
  • Size: 128.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.19

File hashes

Hashes for khaya-0.1.0.tar.gz
Algorithm Hash digest
SHA256 70c196e30409b1d90bddc01740db5735a228970fb175a1a6680e6a56f3cf2795
MD5 a6b3f09bb822bc3816733f9158c20719
BLAKE2b-256 b0c4005ae714ccf9d7ff8b68eed29f7211d764e4eb7d1bf34f1b64203b5a0beb

See more details on using hashes here.

File details

Details for the file khaya-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: khaya-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.19

File hashes

Hashes for khaya-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4609f2fd1e8a0be5cc5b4d545abc5728d37b166d92ad7d6462e33a8292e237a
MD5 b37a5a590f5c341b112c41df37a658af
BLAKE2b-256 7c41ce34cf0d7386fe4ec6a277d417f7a7fb5e24f60b7e0eaa4961470881b3ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page