Skip to content

Commit

Permalink
Remove deepl dep.
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-plus committed Oct 25, 2023
1 parent dd1873d commit c1c8cb8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
56 changes: 28 additions & 28 deletions openlrc/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from abc import ABC, abstractmethod
from typing import Union, List

import deepl
import requests

from openlrc.chatbot import GPTBot
Expand Down Expand Up @@ -160,30 +159,31 @@ def translate(self, texts: Union[str, List[str]], src_lang, target_lang):

return json.dumps(response, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': '))


class DeepLTranslator(Translator):
def __init__(self):
self.key = os.environ['DEEPL_KEY']
self.translator = deepl.Translator(self.key)

def _check_limit(self, texts: List[str]):
usage = self.translator.get_usage()
char_num = sum([len(text) for text in texts])

if usage.character.count + char_num > usage.character.limit:
raise RuntimeError(f'This translate call would exceed DeepL character limit: {usage.character.limit}')

def translate(self, texts: Union[str, List[str]], src_lang, target_lang):
if not isinstance(texts, list):
texts = [texts]

self._check_limit(texts)

translations = self.translator.translate_text(texts, target_lang=target_lang)

if not isinstance(translations, list):
translations = [translations]

translations = [translation.text for translation in translations]

return translations
# Not integrated by the openlrc main function because of performance
#
# class DeepLTranslator(Translator):
# def __init__(self):
# self.key = os.environ['DEEPL_KEY']
# self.translator = deepl.Translator(self.key)
#
# def _check_limit(self, texts: List[str]):
# usage = self.translator.get_usage()
# char_num = sum([len(text) for text in texts])
#
# if usage.character.count + char_num > usage.character.limit:
# raise RuntimeError(f'This translate call would exceed DeepL character limit: {usage.character.limit}')
#
# def translate(self, texts: Union[str, List[str]], src_lang, target_lang):
# if not isinstance(texts, list):
# texts = [texts]
#
# self._check_limit(texts)
#
# translations = self.translator.translate_text(texts, target_lang=target_lang)
#
# if not isinstance(translations, list):
# translations = [translations]
#
# translations = [translation.text for translation in translations]
#
# return translations
17 changes: 17 additions & 0 deletions tests/test_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ def test_empty_text_list_translation(self):
translator = GPTTranslator()
translations = translator.translate(texts, 'en', 'es')
assert translations == []

# Not integrated by the openlrc main function because of performance
#
# class TestDeepLTranslator(unittest.TestCase):
# def test_single_chunk_translation(self):
# text = 'Hello, how are you?'
# translator = DeepLTranslator()
# translation = translator.translate(text, 'en', 'es')[0]
#
# assert get_similarity(translation, 'Hola, ¿cómo estás?') > 0.618
#
# def test_multiple_chunk_translation(self):
# texts = ['Hello, how are you?', 'I am fine, thank you.']
# translator = DeepLTranslator()
# translations = translator.translate(texts, 'en', 'es')
# assert get_similarity(translations[0], 'Hola, ¿cómo estás?') > 0.618
# assert get_similarity(translations[1], 'Estoy bien, gracias.') > 0.618

0 comments on commit c1c8cb8

Please sign in to comment.