Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -65,6 +65,7 @@ class Translators:
|
|
| 65 |
self.model_name = model_name
|
| 66 |
self.sl, self.tl = sl, tl
|
| 67 |
self.input_text = input_text
|
|
|
|
| 68 |
|
| 69 |
def google(self):
|
| 70 |
url = os.environ['GCLIENT'] + f'sl={self.sl}&tl={self.tl}&q={self.input_text}'
|
|
@@ -98,8 +99,7 @@ class Translators:
|
|
| 98 |
|
| 99 |
def HelsinkiNLP_mulmul(self):
|
| 100 |
try:
|
| 101 |
-
|
| 102 |
-
pipe = pipeline("translation", model=model_name)
|
| 103 |
iso1_dict = {iso[1]: (iso[0], iso[2], iso[3]) for iso in non_empty_isos}
|
| 104 |
iso3tl = iso1_dict.get(self.tl)[2] # 'deu', 'ron', 'eng', 'fra'
|
| 105 |
translation = pipe(f'>>{iso3tl}<< {self.input_text}')
|
|
@@ -110,17 +110,18 @@ class Translators:
|
|
| 110 |
def HelsinkiNLP(self):
|
| 111 |
try: # Standard bilingual model
|
| 112 |
model_name = f"Helsinki-NLP/opus-mt-{self.sl}-{self.tl}"
|
| 113 |
-
pipe = pipeline("translation", model=model_name, device
|
| 114 |
translation = pipe(self.input_text)
|
| 115 |
return translation[0]['translation_text'], f'Translated from {self.sl} to {self.tl} with {model_name}.'
|
| 116 |
except EnvironmentError:
|
| 117 |
try: # Tatoeba models
|
| 118 |
model_name = f"Helsinki-NLP/opus-tatoeba-{self.sl}-{self.tl}"
|
| 119 |
-
pipe = pipeline("translation", model=model_name, device
|
| 120 |
translation = pipe(self.input_text)
|
| 121 |
return translation[0]['translation_text'], f'Translated from {self.sl} to {self.tl} with {model_name}.'
|
| 122 |
except EnvironmentError as error:
|
| 123 |
-
|
|
|
|
| 124 |
except KeyError as error:
|
| 125 |
return f"Error: Translation direction {self.sl} to {self.tl} is not supported by Helsinki Translation Models", error
|
| 126 |
|
|
@@ -363,7 +364,7 @@ def translate_text(input_text: str, s_language: str, t_language: str, model_name
|
|
| 363 |
if model_name == "Helsinki-NLP/opus-mt-tc-bible-big-mul-mul":
|
| 364 |
translated_text, message_text = Translators(model_name, sl, tl, input_text).HelsinkiNLP_mulmul()
|
| 365 |
|
| 366 |
-
elif model_name
|
| 367 |
translated_text, message_text = Translators(model_name, sl, tl, input_text).HelsinkiNLP()
|
| 368 |
|
| 369 |
elif model_name == 'Argos':
|
|
|
|
| 65 |
self.model_name = model_name
|
| 66 |
self.sl, self.tl = sl, tl
|
| 67 |
self.input_text = input_text
|
| 68 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 69 |
|
| 70 |
def google(self):
|
| 71 |
url = os.environ['GCLIENT'] + f'sl={self.sl}&tl={self.tl}&q={self.input_text}'
|
|
|
|
| 99 |
|
| 100 |
def HelsinkiNLP_mulmul(self):
|
| 101 |
try:
|
| 102 |
+
pipe = pipeline("translation", model=self.model_name, device=self.device)
|
|
|
|
| 103 |
iso1_dict = {iso[1]: (iso[0], iso[2], iso[3]) for iso in non_empty_isos}
|
| 104 |
iso3tl = iso1_dict.get(self.tl)[2] # 'deu', 'ron', 'eng', 'fra'
|
| 105 |
translation = pipe(f'>>{iso3tl}<< {self.input_text}')
|
|
|
|
| 110 |
def HelsinkiNLP(self):
|
| 111 |
try: # Standard bilingual model
|
| 112 |
model_name = f"Helsinki-NLP/opus-mt-{self.sl}-{self.tl}"
|
| 113 |
+
pipe = pipeline("translation", model=model_name, device=self.device)
|
| 114 |
translation = pipe(self.input_text)
|
| 115 |
return translation[0]['translation_text'], f'Translated from {self.sl} to {self.tl} with {model_name}.'
|
| 116 |
except EnvironmentError:
|
| 117 |
try: # Tatoeba models
|
| 118 |
model_name = f"Helsinki-NLP/opus-tatoeba-{self.sl}-{self.tl}"
|
| 119 |
+
pipe = pipeline("translation", model=model_name, device=self.device)
|
| 120 |
translation = pipe(self.input_text)
|
| 121 |
return translation[0]['translation_text'], f'Translated from {self.sl} to {self.tl} with {model_name}.'
|
| 122 |
except EnvironmentError as error:
|
| 123 |
+
self.model_name = "Helsinki-NLP/opus-mt-tc-bible-big-mul-mul" # Last resort: try multi to multi
|
| 124 |
+
return self.HelsinkiNLP_mulmul()
|
| 125 |
except KeyError as error:
|
| 126 |
return f"Error: Translation direction {self.sl} to {self.tl} is not supported by Helsinki Translation Models", error
|
| 127 |
|
|
|
|
| 364 |
if model_name == "Helsinki-NLP/opus-mt-tc-bible-big-mul-mul":
|
| 365 |
translated_text, message_text = Translators(model_name, sl, tl, input_text).HelsinkiNLP_mulmul()
|
| 366 |
|
| 367 |
+
elif model_name == "Helsinki-NLP":
|
| 368 |
translated_text, message_text = Translators(model_name, sl, tl, input_text).HelsinkiNLP()
|
| 369 |
|
| 370 |
elif model_name == 'Argos':
|