Ghana NLP Python Library: A Comprehensive Guide
Ever tried setting up the Ghana NLP API on your own to translate English into Twi, Ga, or other African languages? Yeah, the process can be a bit long and tricky with all those API requests body, headers, and payloads to handle. 😅 But no worries, I’ve got your back on this one!
I took some time to work on a Python library that makes it relatively easier for you. You don't have to deal with the long conventional approach using requests
if don't have to. With you just skip all that and get straight to the real stuff—translating, converting speech to text, and more—all in just a few lines of code. Let's dive in!
Why You’ll Love This Library ❤️
Here’s why this Python library should be your go-to:
No more manual setup: Forget about handling the requests directly, headers, or writing boilerplate code. I’ve wrapped all that up for you, with the respective API URLs also.
Quick and easy: Just install the library, plug in your API key, and you’re off to the good part!
Supports key features: Translation, speech-to-text, and text-to-speech—all in one library.
Get Started in 2 Minutes ⏱️
Let’s keep it simple. First, install the Ghana NLP Python library from PyPi:
pip install ghana-nlp
Done! Now all you need is your API key from the Ghana NLP website. Once you’ve got it, set up your Python project like this:
from ghana_nlp import GhanaNLP
nlp = GhanaNLP("your_api_key_here")
Bam! Just like that and you’re ready to roll. No sweat, right?
Translation
English to Twi (Fast and Simple)
Want to translate some text from English to Twi? Here’s how easy it is now:
result = nlp.translate("Good morning", "en-tw")
print(result) # Output: Maaha
You can do this for all supported languages too. That’s it—no complicated requests, no crazy payloads. Just call and go!
Speech to Text (STT)
No Typing Required
Got an audio file in Twi, Ga, or another language? Let the library handle the transcription for you:
result = nlp.stt("your_audio_file.wav")
print(result)
No need for manual typing ever again!
Text to Speech (TTS)
Hear It in African
Wanna hear that Twi accent reading your text aloud? Say no more:
result = nlp.tts("Thank you", lang="tw")
print(result) # audio binary, write a code to either serve to an audio file or play directly
With just a few lines of code, you’ve got your text spoken back to you in any supported language.
What is different now?
Oh my, so much has changed, trust me, we moved from this:
# the requests import
import requests
# a translate function to take the text to be translated and the preferred language
def translate(text, target_language):
url = "https://translation-api.ghananlp.org/v1/translate" # the requests url
headers = {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': '******************', # put your API key here
}
data = {
'in': text,
'lang': target_language
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
return response.json()
else:
return "An error occurred"
# pick a language of your choice
language_pair = "en-tw"
# message input in English
message = input("Type what you want to translate: ")
translation = translate(message, language_pair)
print(f"Translation to Twi:{translation}\n")
to this:
from ghana_nlp import GhanaNLP
api_key = "Your API key here"
nlp = GhanaNLP(api_key)
translation = nlp.translate("How are you?", "en-tw")
print(translation)
Supported Languages 🗣️
Here are the language codes you can work with:
English:
en
Twi:
tw
Ga:
gaa
Ewe:
ee
Fante:
fat
Dagbani:
dag
Gurene:
gur
Yoruba:
yo
Kikuyu:
ki
Luo:
luo
Kimeru:
mer
Mix and match, try different translations, and have fun with it!
Handling Errors (Because, Hey, Things Happen) 🚧
If something goes wrong, like an incorrect API key or an unsupported language code, the library will let you know:
{
"type": "Error type",
"message": "What went wrong"
}
So you can troubleshoot without guessing what’s happening.
You probably won't find this with the versions 0.1.0 and 0.1.1
Where to Get It?
Head over to PyPi and grab the GhanaNLP Python library. It’s open-source, free to use, and designed to make your life easier. If you hit any roadblocks or need more features, I’m all ears—just reach out!
Wrap-up
Ghana NLP Made Easy 😉 That’s it! you can just focus on building good stuff and leave the heavy lifting to the GhanaNLP Python Library.
So, now let's not wait too long! Get out there, build something, and let’s make tech simple and local!
Happy Coding, and keep it easy! 👊
Subscribe to my newsletter
Read articles from Prince Larbi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Prince Larbi
Prince Larbi
Enjoy the journey. yoghurt over coffee!! ...any day