Python SDK for the Khaya AI API — translation, ASR, and TTS for African languages
Project description
Khaya SDK
Python SDK for the Khaya AI 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file khaya-0.1.1.tar.gz.
File metadata
- Download URL: khaya-0.1.1.tar.gz
- Upload date:
- Size: 128.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95dcdb74688b4d731d7c8e802588d52871672580777c60650bcd08fb5cb36820
|
|
| MD5 |
2397c3c2be0dbaf311753aea4f44e0bc
|
|
| BLAKE2b-256 |
ee619429a31813230e6dbd6ddc835972e4707d0c50dbeb3a6a159a538ddc11be
|
File details
Details for the file khaya-0.1.1-py3-none-any.whl.
File metadata
- Download URL: khaya-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
360794383f5f04b8295cc104065333735fcf5e62544a9ecbda5641205bd2cc60
|
|
| MD5 |
339377545bb0a5d323cd02db3e7863d8
|
|
| BLAKE2b-256 |
14ebe9bf969da3692f27e9acfd8bff0c1498cf1cf52d878f50bbacd4b58ad32f
|