Skip to content

Commit 52cb54e

Browse files
authored
Merge pull request #730 from FEMR/tEMinatoRs-WELC-noah
Added Sprout Method to make checking for argos and marian paths easier to read.
2 parents 9ac9bd3 + cd7996e commit 52cb54e

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

translator/libargos.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ def get_installed_package_names():
2727
models.append(model_name)
2828
return models
2929

30+
# Returns true if all paths exist in either a tuple or a list of tuples of to/from language codes
31+
def packages_downloaded(paths):
32+
if isinstance(paths, tuple):
33+
return Path(f"{PATH}/translator/argos_models/translate-{paths[0]}_{paths[1]}.argosmodel").exists()
34+
elif isinstance(paths, list):
35+
for path_tuple in list:
36+
if package_paths_exist(path_tuple) == False:
37+
return False
38+
return True
39+
else:
40+
return False
41+
42+
3043
#Installs all packages from local directory
3144
def install_packages():
3245
installed_packages = get_installed_package_names()

translator/server.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
from pathlib import Path
1111
from transformers import MarianMTModel, MarianTokenizer
1212
from typing import Sequence
13-
from libargos import install_packages
13+
from libargos import install_packages, packages_downloaded
14+
from libmarian import package_downloaded
1415
import socket
1516
import time
1617

@@ -84,21 +85,21 @@ def translate_data(self):
8485

8586
def translate(self, text, from_code, to_code):
8687
# Use Argos if Language Package Exists
87-
if Path(f"{PATH}/translator/argos_models/translate-{from_code}_{to_code}.argosmodel").exists():
88+
if packages_downloaded((from_code, to_code)):
8889
translatedText = argostranslate.translate.translate(text, from_code, to_code)
8990
return translatedText
91+
9092
# Use Marian if Language Package Exists in Marian but not Argos
91-
elif Path(f"{PATH}/translator/marian_models/opus-mt-{from_code}-{to_code}").exists():
93+
elif package_downloaded(from_code, to_code):
9294
marian = MarianModel(from_code, to_code)
9395
translatedText = marian.translate([text])
9496
return translatedText[0]
97+
9598
# Use Argos "English in the Middle" if not in Argos and Marian by Default
96-
elif (Path(f"{PATH}/translator/argos_models/translate-{from_code}_en.argosmodel").exists() and \
97-
Path(f"{PATH}/translator/argos_models/translate-{to_code}_en.argosmodel").exists()) or \
98-
(Path(f"{PATH}/translator/argos_models/translate-en_{from_code}.argosmodel").exists() and \
99-
Path(f"{PATH}/translator/argos_models/translate-en_{to_code}.argosmodel").exists()):
99+
elif packages_downloaded([(from_code ,"en"), (to_code, "en"), ("en", from_code), ("en", to_code)]):
100100
translatedText = argostranslate.translate.translate(text, from_code, to_code)
101101
return translatedText
102+
102103
# If a package doesn't exist
103104
else:
104105
return "Translation Unavailable"

0 commit comments

Comments
 (0)