Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,10 +13,10 @@ df = pl.read_parquet("isolanguages.parquet")
|
|
| 13 |
non_empty_isos = df.slice(1).filter(pl.col("ISO639-1") != "").rows()
|
| 14 |
# all_langs = languagecodes.iso_languages_byname
|
| 15 |
all_langs = {iso[0]: (iso[1], iso[2], iso[3]) for iso in non_empty_isos} # {'Romanian': ('ro', 'rum', 'ron')}
|
| 16 |
-
|
| 17 |
-
|
| 18 |
langs = list(favourite_langs.keys())
|
| 19 |
-
langs.extend(list(all_langs.keys()))
|
| 20 |
|
| 21 |
models = ["Helsinki-NLP",
|
| 22 |
"Helsinki-NLP/opus-mt-tc-bible-big-mul-mul", "Helsinki-NLP/opus-mt-tc-bible-big-mul-deu_eng_nld",
|
|
@@ -106,10 +106,10 @@ class Translators:
|
|
| 106 |
def HelsinkiNLP_mulroa(self):
|
| 107 |
try:
|
| 108 |
pipe = pipeline("translation", model=self.model_name, device=self.device)
|
| 109 |
-
|
| 110 |
-
iso3tl =
|
| 111 |
translation = pipe(f'>>{iso3tl}<< {self.input_text}')
|
| 112 |
-
return translation[0]['translation_text'], f'Translated from {self.sl} to {self.tl} with {self.model_name}.'
|
| 113 |
except Exception as error:
|
| 114 |
return f"Error translating with model: {self.model_name}! Try other available language combination.", error
|
| 115 |
|
|
@@ -118,16 +118,16 @@ class Translators:
|
|
| 118 |
model_name = f"Helsinki-NLP/opus-mt-{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:
|
| 123 |
try: # Tatoeba models
|
| 124 |
model_name = f"Helsinki-NLP/opus-tatoeba-{self.sl}-{self.tl}"
|
| 125 |
pipe = pipeline("translation", model=model_name, device=self.device)
|
| 126 |
translation = pipe(self.input_text)
|
| 127 |
-
return translation[0]['translation_text'], f'Translated from {self.sl} to {self.tl} with {model_name}.'
|
| 128 |
except EnvironmentError as error:
|
| 129 |
self.model_name = "Helsinki-NLP/opus-mt-tc-bible-big-mul-mul" # Last resort: try multi to multi
|
| 130 |
-
return self.
|
| 131 |
except KeyError as error:
|
| 132 |
return f"Error: Translation direction {self.sl} to {self.tl} is not supported by Helsinki Translation Models", error
|
| 133 |
|
|
|
|
| 13 |
non_empty_isos = df.slice(1).filter(pl.col("ISO639-1") != "").rows()
|
| 14 |
# all_langs = languagecodes.iso_languages_byname
|
| 15 |
all_langs = {iso[0]: (iso[1], iso[2], iso[3]) for iso in non_empty_isos} # {'Romanian': ('ro', 'rum', 'ron')}
|
| 16 |
+
# iso1_to_name = {codes[0]: lang for entry in all_langs for lang, codes in entry.items()} # {'ro': 'Romanian', 'de': 'German'}
|
| 17 |
+
iso1_to_name = {iso[1]: (iso[0] for iso in non_empty_isos} # {'ro': 'Romanian', 'de': 'German'}
|
| 18 |
langs = list(favourite_langs.keys())
|
| 19 |
+
langs.extend(list(all_langs.keys())) # Language options as list, add favourite languages first
|
| 20 |
|
| 21 |
models = ["Helsinki-NLP",
|
| 22 |
"Helsinki-NLP/opus-mt-tc-bible-big-mul-mul", "Helsinki-NLP/opus-mt-tc-bible-big-mul-deu_eng_nld",
|
|
|
|
| 106 |
def HelsinkiNLP_mulroa(self):
|
| 107 |
try:
|
| 108 |
pipe = pipeline("translation", model=self.model_name, device=self.device)
|
| 109 |
+
iso1to3 = {iso[1]: iso[3] for iso in non_empty_isos} # {'ro': 'ron'}
|
| 110 |
+
iso3tl = iso1to3.get(self.tl) # 'deu', 'ron', 'eng', 'fra'
|
| 111 |
translation = pipe(f'>>{iso3tl}<< {self.input_text}')
|
| 112 |
+
return translation[0]['translation_text'], f'Translated from {iso1_to_name[self.sl]} to {iso1_to_name[self.tl]} with {self.model_name}.'
|
| 113 |
except Exception as error:
|
| 114 |
return f"Error translating with model: {self.model_name}! Try other available language combination.", error
|
| 115 |
|
|
|
|
| 118 |
model_name = f"Helsinki-NLP/opus-mt-{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 {iso1_to_name[self.sl]} to {iso1_to_name[self.tl]} with {model_name}.'
|
| 122 |
except EnvironmentError:
|
| 123 |
try: # Tatoeba models
|
| 124 |
model_name = f"Helsinki-NLP/opus-tatoeba-{self.sl}-{self.tl}"
|
| 125 |
pipe = pipeline("translation", model=model_name, device=self.device)
|
| 126 |
translation = pipe(self.input_text)
|
| 127 |
+
return translation[0]['translation_text'], f'Translated from {iso1_to_name[self.sl]} to {iso1_to_name[self.tl]} with {model_name}.'
|
| 128 |
except EnvironmentError as error:
|
| 129 |
self.model_name = "Helsinki-NLP/opus-mt-tc-bible-big-mul-mul" # Last resort: try multi to multi
|
| 130 |
+
return self.HelsinkiNLP_mulroa()
|
| 131 |
except KeyError as error:
|
| 132 |
return f"Error: Translation direction {self.sl} to {self.tl} is not supported by Helsinki Translation Models", error
|
| 133 |
|