Skip to content

Commit

Permalink
Cached CTranslate Translators
Browse files Browse the repository at this point in the history
  • Loading branch information
argosopentech committed Oct 31, 2020
1 parent 7a9fa1f commit 26802a3
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions argostranslate/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ def __init__(self, from_lang, to_lang, pkg):
self.from_lang = from_lang
self.to_lang = to_lang
self.pkg = pkg
self.translator = None

def translate(self, input_text):
if self.translator == None:
model_path = str(self.pkg.package_path / 'model')
self.translator = ctranslate2.Translator(model_path)
paragraphs = self.split_into_paragraphs(input_text)
translated_paragraphs = []
for paragraph in paragraphs:
translated_paragraphs.append(
apply_packaged_translation(self.pkg, paragraph))
apply_packaged_translation(self.pkg, paragraph, self.translator))
return self.combine_paragraphs(translated_paragraphs)

class IdentityTranslation(ITranslation):
Expand Down Expand Up @@ -189,7 +193,7 @@ def translate(self, input_text):
return self.combine_paragraphs(translated_paragraphs)


def apply_packaged_translation(pkg, input_text):
def apply_packaged_translation(pkg, input_text, translator):
"""Applies the translation in pkg to translate input_text.
Args:
Expand All @@ -201,8 +205,6 @@ def apply_packaged_translation(pkg, input_text):
"""

model_path = str(pkg.package_path / 'model')
translator = ctranslate2.Translator(model_path)
sp_model_path = str(pkg.package_path / 'sentencepiece.model')
sp_processor = spm.SentencePieceProcessor(model_file=sp_model_path)
stanza_pipeline = stanza.Pipeline(lang=pkg.from_code,
Expand Down

0 comments on commit 26802a3

Please sign in to comment.