|
10 | 10 | from pathlib import Path
|
11 | 11 | from transformers import MarianMTModel, MarianTokenizer
|
12 | 12 | from typing import Sequence
|
13 |
| -from libargos import install_packages |
| 13 | +from libargos import install_packages, packages_downloaded |
| 14 | +from libmarian import package_downloaded |
14 | 15 | import socket
|
15 | 16 | import time
|
16 | 17 |
|
@@ -84,21 +85,21 @@ def translate_data(self):
|
84 | 85 |
|
85 | 86 | def translate(self, text, from_code, to_code):
|
86 | 87 | # 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)): |
88 | 89 | translatedText = argostranslate.translate.translate(text, from_code, to_code)
|
89 | 90 | return translatedText
|
| 91 | + |
90 | 92 | # 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): |
92 | 94 | marian = MarianModel(from_code, to_code)
|
93 | 95 | translatedText = marian.translate([text])
|
94 | 96 | return translatedText[0]
|
| 97 | + |
95 | 98 | # 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)]): |
100 | 100 | translatedText = argostranslate.translate.translate(text, from_code, to_code)
|
101 | 101 | return translatedText
|
| 102 | + |
102 | 103 | # If a package doesn't exist
|
103 | 104 | else:
|
104 | 105 | return "Translation Unavailable"
|
|
0 commit comments