Skip to content

Commit

Permalink
Merge pull request #46 from GhanaNLP/feat-cleanup
Browse files Browse the repository at this point in the history
Feat cleanup
  • Loading branch information
Lagyamfi authored Oct 26, 2024
2 parents b624ddd + dfb2ca9 commit 6b5a44c
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 11 deletions.
26 changes: 26 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[flake8]
max-line-length = 120
max-complexity = 10
select = C,E,F,W,B,B950
count = True
statistics = True
ignore =
E203,
E302,
E501,
W503,
D100,
D101,
D102,
D103,
D104,
D105,
D106,
D107
exclude =
.git,
__pycache__,
*.egg-info,
.pytest_cache,
.mypy_cache,
notebooks/*
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.DEFAULT_GOAL := help
.PHONY: help lint test

SRD_DIR = khaya
TEST_DIR = tests

ci: lint typecheck test ## Run all CI checks
.PHONY: ci

lint: ## Run linting
poetry run isort $(SRC_DIR) $(TEST_DIR)
poetry run flake8 --max-line-length 120 $(SRC_DIR) $(TEST_DIR)
.PHONY: lint


typecheck: ## Run type checking
poetry run mypy $(SRD_DIR)
.PHONY: typecheck

test: ## Run tests
poetry run coverage run --source=$(SRD_DIR) -m pytest -v $(TEST_DIR) && poetry run coverage report -m
.PHONY: test

clean-py: ## Remove python cache files
find . -name '__pycache__' -type d -exec rm -r {} +
find . -name '*.pyc' -type f -exec rm {} +
rm -rf ./*.egg-info
.PHONY: clean-py

.PHONY: help
help: ## Show help message
@IFS=$$'\n' ; \
help_lines=(`fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##/:/'`); \
printf "%s\n\n" "Usage: make [task]"; \
printf "%-20s %s\n" "task" "help" ; \
printf "%-20s %s\n" "------" "----" ; \
for help_line in $${help_lines[@]}; do \
IFS=$$':' ; \
help_split=($$help_line) ; \
help_command=`echo $${help_split[0]} | sed -e 's/^ *//' -e 's/ *$$//'` ; \
help_info=`echo $${help_split[2]} | sed -e 's/^ *//' -e 's/ *$$//'` ; \
printf '\033[36m'; \
printf "%-20s %s" $$help_command ; \
printf '\033[0m'; \
printf "%s\n" $$help_info; \
done
2 changes: 2 additions & 0 deletions khaya/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .khaya_interface import KhayaInterface as khayaAPI

__all__ = ["khayaAPI"]
2 changes: 1 addition & 1 deletion khaya/asr_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class AsrApi(BaseApi):
def transcribe(self, audio_file_path: str, language="tw") -> Response:
def transcribe(self, audio_file_path: str, language="tw") -> Response | dict[str, str]:
"""
Convert speech to text from audio binary data in an African language using the GhanaNLP STT API.
Expand Down
3 changes: 2 additions & 1 deletion khaya/base_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from abc import ABC
from typing import Optional

import requests


Expand All @@ -13,7 +14,7 @@ def __init__(self, api_key: str, base_url: Optional[str] = None):
"Cache-Control": "no-cache",
}

def _make_request(self, method: str, url: str, **kwargs) -> requests.Response:
def _make_request(self, method: str, url: str, **kwargs) -> requests.Response | dict[str, str]:
"""
Make an HTTP request.
Expand Down
8 changes: 5 additions & 3 deletions khaya/khaya_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from khaya.translation_api import TranslationApi
from khaya.tts_api import TtsApi

# custom type hint for Response or dict[str, str]
ResponseOrDict = Response | dict[str, str]

class KhayaInterface:
"""
Expand Down Expand Up @@ -58,7 +60,7 @@ def __init__(self, api_key: str, base_url: Optional[str] = "https://translation-
self.asr_api = AsrApi(api_key, base_url)
self.tts_api = TtsApi(api_key, base_url)

def translate(self, text: str, language_pair: str = "en-tw") -> Response:
def translate(self, text: str, language_pair: str = "en-tw") -> ResponseOrDict:
"""
Translate text from one language to another.
Expand All @@ -72,7 +74,7 @@ def translate(self, text: str, language_pair: str = "en-tw") -> Response:

return self.translation_api.translate(text, language_pair)

def asr(self, audio_file_path: str, language: str = "tw") -> Response:
def asr(self, audio_file_path: str, language: str = "tw") -> ResponseOrDict:
"""
Get the transcription of an audio file from a given language.
Expand All @@ -85,7 +87,7 @@ def asr(self, audio_file_path: str, language: str = "tw") -> Response:
"""
return self.asr_api.transcribe(audio_file_path, language)

def tts(self, text: str, lang: str) -> Response:
def tts(self, text: str, lang: str) -> ResponseOrDict:
"""
Synthesize speech from text.
Expand Down
2 changes: 1 addition & 1 deletion khaya/translation_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class TranslationApi(BaseApi):
def translate(self, text: str, language_pair: str = "en-tw") -> Response:
def translate(self, text: str, language_pair: str = "en-tw") -> Response | dict[str, str]:
"""
Translate text from one language to another using the GhanaNLP translation API.
Expand Down
3 changes: 2 additions & 1 deletion khaya/tts_api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import json

from requests.models import Response

from khaya.base_api import BaseApi


class TtsApi(BaseApi):
def synthesize(self, text: str, lang: str) -> Response:
def synthesize(self, text: str, lang: str) -> Response | dict[str, str]:
"""
Convert text to speech in a specified African language using the GhanaNLP TTS API.
Expand Down
Loading

0 comments on commit 6b5a44c

Please sign in to comment.