Skip to content

KhayaInterface

KhayaInterface

KhayaInterface(api_key: str, base_url: Optional[str] = 'https://translation-api.ghananlp.org')

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

api_key

str

The API key to use for authenticating requests to the Khaya API.

required

base_url

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
def __init__(self, api_key: str, base_url: Optional[str] = "https://translation-api.ghananlp.org"):
    self.api_key = api_key
    self.base_url = base_url
    self.translation_api = TranslationApi(api_key, base_url)
    self.asr_api = AsrApi(api_key, base_url)
    self.tts_api = TtsApi(api_key, base_url)

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
audio_file_path
str

The path to the audio file to transcribe.

required
language
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
def asr(self, audio_file_path: str, language: str = "tw") -> Response:
    """
    Get the transcription of an audio file from a given language.

    Args:
        audio_file_path: The path to the audio file to transcribe.
        language: The language of the audio file. Default is "tw".

    Returns:
        A Response object containing the transcription of the audio file.
    """
    return self.asr_api.transcribe(audio_file_path, language)

translate

translate(text: str, language_pair: str = 'en-tw') -> Response

Translate text from one language to another.

Parameters:

Name Type Description Default
text
str

The text to translate.

required
language_pair
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
def translate(self, text: str, language_pair: str = "en-tw") -> Response:
    """
    Translate text from one language to another.

    Args:
        text: The text to translate.
        language_pair: The language pair to translate the text to. Default is "en-tw".

    Returns:
        A Response object containing the translated text.
    """

    return self.translation_api.translate(text, language_pair)

tts

tts(text: str, lang: str) -> Response

Synthesize speech from text.

Parameters:

Name Type Description Default
text
str

The text to synthesize.

required
lang
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
def tts(self, text: str, lang: str) -> Response:
    """
    Synthesize speech from text.

    Args:
        text: The text to synthesize.
        lang: The language of the text. Default is "tw".

    Returns:
        A Response object containing the synthesized speech.
    """
    return self.tts_api.synthesize(text, lang)