KhayaInterface
KhayaInterface
KhayaInterface is a class that provides a high-level interface to the Khaya API. It provides methods for translating text, transcribing audio, and synthesizing speech.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The API key to use for authenticating requests to the Khaya API. |
required |
|
Optional[str]
|
The base URL of the Khaya API. Default is "https://translation-api.ghananlp.org". |
'https://translation-api.ghananlp.org'
|
Returns:
Type | Description |
---|---|
An instance of the KhayaInterface class. |
Example:
from khaya.khaya_interface import KhayaInterface
import os
# Initialize the Khaya API interface with your API key assuming you have one saved
# in an environment variable called KHAYA_API_KEY
api_key = os.environ.get("KHAYA_API_KEY")
khaya = KhayaInterface(api_key)
# Translate text from English to Twi
translation_response = khaya.translate("Hello, how are you?", "en-tw")
print(translation_response.json())
# Transcribe an audio file
asr_response = khaya.asr("path/to/audio/file.wav", "tw")
print(asr_response.json())
# Synthesize speech
tts_response = khaya.tts("Hello, how are you?", "en")
# Save the synthesized speech to a file
with open("output.mp3", "wb") as f:
f.write(tts_response.content)
Methods:
Name | Description |
---|---|
asr |
Get the transcription of an audio file from a given language. |
translate |
Translate text from one language to another. |
tts |
Synthesize speech from text. |
Source code in khaya/khaya_interface.py
54 55 56 57 58 59 |
|
Functions
asr
asr(audio_file_path: str, language: str = 'tw') -> Response
Get the transcription of an audio file from a given language.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The path to the audio file to transcribe. |
required |
|
str
|
The language of the audio file. Default is "tw". |
'tw'
|
Returns:
Type | Description |
---|---|
Response
|
A Response object containing the transcription of the audio file. |
Source code in khaya/khaya_interface.py
75 76 77 78 79 80 81 82 83 84 85 86 |
|
translate
translate(text: str, language_pair: str = 'en-tw') -> Response
Translate text from one language to another.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The text to translate. |
required |
|
str
|
The language pair to translate the text to. Default is "en-tw". |
'en-tw'
|
Returns:
Type | Description |
---|---|
Response
|
A Response object containing the translated text. |
Source code in khaya/khaya_interface.py
61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
tts
Synthesize speech from text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The text to synthesize. |
required |
|
str
|
The language of the text. Default is "tw". |
required |
Returns:
Type | Description |
---|---|
Response
|
A Response object containing the synthesized speech. |
Source code in khaya/khaya_interface.py
88 89 90 91 92 93 94 95 96 97 98 99 |
|