Ghana NLP Python Library
A Python client for the Khaya AI API: translation, speech-to-text and text-to-speech for Ghanaian and other African languages.
Version 0.2 calls the Khaya platform at https://api.khaya.ai. If you're on 0.1.x, see Upgrading from 0.1.
Installation
pip install -U ghana-nlp
Requires Python 3.9 or later.
Setup
Get an API key from the Khaya developer portal, then either pass it in or set KHAYA_API_KEY:
export KHAYA_API_KEY="your-api-key"
from ghana_nlp import GhanaNLP
nlp = GhanaNLP() # reads KHAYA_API_KEY
nlp = GhanaNLP(api_key="your-key") # or pass it in
Translation
Pass a language pair such as eng-twi (English to Twi) or twi-eng:
text = nlp.translate("Good morning", "eng-twi")
print(text) # Mema wo akwaaba
Speech to text
Transcribe a WAV or other audio file:
text = nlp.stt("interview.wav", language="twi")
print(text)
speech_to_text is the same method under a longer name.
Text to speech
tts returns WAV audio as bytes. Pick a language and, optionally, a voice:
audio = nlp.tts("Maakye, akwaaba", lang="twi", speaker="female")
with open("output.wav", "wb") as f:
f.write(audio)
Voices are male_low, male_high and female. text_to_speech is the same method under a longer name.
Errors
Every failure raises GhanaNLPError, with these attributes:
| Attribute | Meaning |
|---|---|
message |
What went wrong |
status_code |
HTTP status, or None if the request never got a response |
code |
Platform code, such as INVALID_API_KEY |
activity_id |
Request ID to quote when contacting support |
from ghana_nlp import GhanaNLP, GhanaNLPError
try:
GhanaNLP().translate("Hello", "eng-twi")
except GhanaNLPError as err:
print(err.status_code, err.code, err.message)
print("Request ID:", err.activity_id)
Upgrading from 0.1
| 0.1 | 0.2 |
|---|---|
Calls translation-api.ghananlp.org |
Calls api.khaya.ai |
Sends Ocp-Apim-Subscription-Key |
Sends Authorization: Bearer <key> |
| Returns error dictionaries | Raises GhanaNLPError |
translate(..., language_pair="en-tw") |
Default is eng-twi |
tts(text, lang="tw") |
Use twi. tw is not a language code on the new API |
tts had no voice option |
tts(..., speaker=...) |
Keys from the legacy GhanaNLP portal don't work with the new API. Create a key in the Khaya developer portal.
Development
python -m unittest tests.test_api # offline unit tests
KHAYA_API_KEY=... python -m unittest tests.test_api # also runs the live test
Changelog
See CHANGELOG.md.
License
MIT. See LICENSE.
Metadata
Release files for ghana-nlp 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ghana_nlp-0.2.0.tar.gz | 13.5 kB | Without provenance | Details for ghana_nlp-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ghana_nlp-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for ghana_nlp-0.2.0-py3-none-any.whl |
Total release size: 19.5 kB
Release files / ghana_nlp-0.2.0.tar.gz
| Download URL | ghana_nlp-0.2.0.tar.gz |
|---|---|
| Size | 13.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
26c25b92f91b24737d68c40bc04f0595fbe949944a411422043ea9232bed0be2
|
|
BLAKE2b-256 checksum How to use checksums |
6eef1ef17a6e811fd107aea1dd37a10b7e7d055946c3ac4903776a19b0ddd0ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.0
|
Release files / ghana_nlp-0.2.0-py3-none-any.whl
| Download URL | ghana_nlp-0.2.0-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
85f16ba62357a6166a1db7736f4c2004ad428b66b66c5fa304dba8b03ea6c95d
|
|
BLAKE2b-256 checksum How to use checksums |
dc84ef04a7a4dfa36480564f24ca0a3585b276a5b2f05c89f61df64999ee57be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.0
|
Security for ghana-nlp 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.