Skip to content

Commit

Permalink
1.4.1 - remove langcodes dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjoyo committed Apr 4, 2024
1 parent cdfd4b0 commit 9b78d0a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 91 deletions.
3 changes: 0 additions & 3 deletions bpm-ai/bpm_ai/translate/schema.py

This file was deleted.

7 changes: 2 additions & 5 deletions bpm-ai/bpm_ai/translate/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from bpm_ai.common.errors import MissingParameterError, LanguageNotFoundError
from bpm_ai.common.multimodal import transcribe_audio, prepare_images_for_llm_prompt, ocr_documents
from bpm_ai.translate.schema import get_translation_output_schema
from bpm_ai.translate.util import get_translation_output_schema, get_lang_code


@trace("bpm-ai-translate", ["llm"])
Expand Down Expand Up @@ -68,10 +68,7 @@ async def translate_nmt(
input_items = await transcribe_audio(input_items, asr)

try:
import langcodes
target_language_code = langcodes.find(target_language).language
except ImportError:
raise ImportError('langcodes is not installed')
target_language_code = get_lang_code(target_language)
except LookupError:
raise LanguageNotFoundError(f"Could not identify target language '{target_language}'.")

Expand Down
30 changes: 30 additions & 0 deletions bpm-ai/bpm_ai/translate/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from enum import Enum


def get_translation_output_schema(input_data: dict, target_language: str):
return {k: f'{k} translated into {target_language}' for k in input_data.keys()}


class Language(Enum):
ENGLISH = "en"
FRENCH = "fr"
GERMAN = "de"
SPANISH = "es"
ITALIAN = "it"
PORTUGUESE = "pt"
DUTCH = "nl"
DANISH = "da"
SWEDISH = "sv"
NYNORSK = "no"
NORWEGIAN = "no"
BOKMAL = "no"
FINNISH = "fi"
POLISH = "pl"
UKRAINIAN = "uk"


language_dict = {lang.lower(): Language[lang].value for lang in Language.__members__.keys()}


def get_lang_code(language: str) -> str:
return language_dict[language.strip().lower()]
Loading

0 comments on commit 9b78d0a

Please sign in to comment.