added try except loop for model
Browse files
app.py
CHANGED
@@ -12,6 +12,9 @@ user_input:
|
|
12 |
|
13 |
Returns:
|
14 |
string: translated string of text
|
|
|
|
|
|
|
15 |
"""
|
16 |
from __future__ import annotations
|
17 |
from typing import Iterable
|
@@ -61,11 +64,9 @@ class myTheme(Base):
|
|
61 |
)
|
62 |
super().set(
|
63 |
body_background_fill="repeating-linear-gradient(135deg, *primary_800, *primary_800 10px, *primary_900 10px, *primary_900 20px)",
|
64 |
-
|
65 |
-
button_primary_background_fill="linear-gradient(45deg, *primary_300, *secondary_400)",
|
66 |
button_primary_background_fill_hover="linear-gradient(45deg, *primary_200, *secondary_300)",
|
67 |
button_primary_text_color="white",
|
68 |
-
button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
|
69 |
slider_color="*secondary_300",
|
70 |
slider_color_dark="*secondary_600",
|
71 |
block_title_text_weight="600",
|
@@ -114,8 +115,11 @@ def opus_trans(article, target_language):
|
|
114 |
if result_lang != target_lang:
|
115 |
task_name = f"translation_{result_lang}_to_{target_lang}"
|
116 |
model_name = f"Helsinki-NLP/opus-mt-{result_lang}-{target_lang}"
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
119 |
else:
|
120 |
translated = "Error: You chose the same language as the article detected language. Please reselect language and try again."
|
121 |
return translated
|
|
|
12 |
|
13 |
Returns:
|
14 |
string: translated string of text
|
15 |
+
|
16 |
+
try this : https://pypi.org/project/EasyNMT/
|
17 |
+
and this : https://huggingface.co/IDEA-CCNL/Randeng-Deltalm-362M-En-Zh
|
18 |
"""
|
19 |
from __future__ import annotations
|
20 |
from typing import Iterable
|
|
|
64 |
)
|
65 |
super().set(
|
66 |
body_background_fill="repeating-linear-gradient(135deg, *primary_800, *primary_800 10px, *primary_900 10px, *primary_900 20px)",
|
67 |
+
button_primary_background_fill="linear-gradient(90deg, *primary_600, *secondary_800)",
|
|
|
68 |
button_primary_background_fill_hover="linear-gradient(45deg, *primary_200, *secondary_300)",
|
69 |
button_primary_text_color="white",
|
|
|
70 |
slider_color="*secondary_300",
|
71 |
slider_color_dark="*secondary_600",
|
72 |
block_title_text_weight="600",
|
|
|
115 |
if result_lang != target_lang:
|
116 |
task_name = f"translation_{result_lang}_to_{target_lang}"
|
117 |
model_name = f"Helsinki-NLP/opus-mt-{result_lang}-{target_lang}"
|
118 |
+
try:
|
119 |
+
translator = pipeline(task_name, model=model_name, tokenizer=model_name)
|
120 |
+
translated = translator(article)[0]["translation_text"]
|
121 |
+
except:
|
122 |
+
translated = "Error: Model doesn't exist"
|
123 |
else:
|
124 |
translated = "Error: You chose the same language as the article detected language. Please reselect language and try again."
|
125 |
return translated
|