Update app.py
Browse files
app.py
CHANGED
@@ -89,7 +89,8 @@ except Exception as e:
|
|
89 |
LANGUAGE_CODES = {
|
90 |
"kapampangan": "pam",
|
91 |
"filipino": "fil", # Replaced tagalog with filipino
|
92 |
-
"english": "eng"
|
|
|
93 |
}
|
94 |
|
95 |
# TTS Models (Kapampangan, Tagalog, English)
|
@@ -126,8 +127,7 @@ TRANSLATION_MODELS = {
|
|
126 |
"pam-eng": "Coco-18/opus-mt-pam-en",
|
127 |
"eng-pam": "Coco-18/opus-mt-en-pam",
|
128 |
"tgl-eng": "Helsinki-NLP/opus-mt-tl-en",
|
129 |
-
"eng-tgl": "Helsinki-NLP/opus-mt-en-tl"
|
130 |
-
# Special model for pam-fil translations in both directions
|
131 |
"phi": "Coco-18/opus-mt-phi"
|
132 |
}
|
133 |
|
@@ -175,17 +175,18 @@ def home():
|
|
175 |
@app.route("/health", methods=["GET"])
|
176 |
def health_check():
|
177 |
# Initialize direct language pair statuses based on loaded models
|
178 |
-
translation_status = {
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
}
|
184 |
|
185 |
-
# Add special phi model status
|
186 |
phi_status = "loaded" if "phi" in translation_models and translation_models["phi"] is not None else "failed"
|
187 |
translation_status["pam-fil"] = phi_status
|
188 |
translation_status["fil-pam"] = phi_status
|
|
|
|
|
189 |
|
190 |
health_status = {
|
191 |
"api_status": "online",
|
@@ -396,8 +397,22 @@ def translate_text():
|
|
396 |
|
397 |
logger.info(f"π Translating from {source_language} to {target_language}: '{source_text}'")
|
398 |
|
399 |
-
# Special handling for pam-fil and
|
|
|
|
|
|
|
|
|
|
|
400 |
if (source_code == "pam" and target_code == "fil") or (source_code == "fil" and target_code == "pam"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
401 |
model_key = "phi"
|
402 |
|
403 |
# Check if we have the phi model
|
@@ -411,7 +426,9 @@ def translate_text():
|
|
411 |
tokenizer = translation_tokenizers[model_key]
|
412 |
|
413 |
# Prepend target language token to input
|
414 |
-
input_text = f">>{
|
|
|
|
|
415 |
|
416 |
# Tokenize the text
|
417 |
tokenized = tokenizer(input_text, return_tensors="pt", padding=True)
|
|
|
89 |
LANGUAGE_CODES = {
|
90 |
"kapampangan": "pam",
|
91 |
"filipino": "fil", # Replaced tagalog with filipino
|
92 |
+
"english": "eng",
|
93 |
+
"tagalog": "tgl",
|
94 |
}
|
95 |
|
96 |
# TTS Models (Kapampangan, Tagalog, English)
|
|
|
127 |
"pam-eng": "Coco-18/opus-mt-pam-en",
|
128 |
"eng-pam": "Coco-18/opus-mt-en-pam",
|
129 |
"tgl-eng": "Helsinki-NLP/opus-mt-tl-en",
|
130 |
+
"eng-tgl": "Helsinki-NLP/opus-mt-en-tl",
|
|
|
131 |
"phi": "Coco-18/opus-mt-phi"
|
132 |
}
|
133 |
|
|
|
175 |
@app.route("/health", methods=["GET"])
|
176 |
def health_check():
|
177 |
# Initialize direct language pair statuses based on loaded models
|
178 |
+
translation_status = {}
|
179 |
+
|
180 |
+
# Add status for direct model pairs
|
181 |
+
for lang_pair in ["pam-eng", "eng-pam", "tgl-eng", "eng-tgl"]:
|
182 |
+
translation_status[lang_pair] = "loaded" if lang_pair in translation_models and translation_models[lang_pair] is not None else "failed"
|
|
|
183 |
|
184 |
+
# Add special phi model status
|
185 |
phi_status = "loaded" if "phi" in translation_models and translation_models["phi"] is not None else "failed"
|
186 |
translation_status["pam-fil"] = phi_status
|
187 |
translation_status["fil-pam"] = phi_status
|
188 |
+
translation_status["pam-tgl"] = phi_status # Using phi model but replacing tgl with fil
|
189 |
+
translation_status["tgl-pam"] = phi_status # Using phi model but replacing tgl with fil
|
190 |
|
191 |
health_status = {
|
192 |
"api_status": "online",
|
|
|
397 |
|
398 |
logger.info(f"π Translating from {source_language} to {target_language}: '{source_text}'")
|
399 |
|
400 |
+
# Special handling for pam-fil, fil-pam, pam-tgl and tgl-pam using the phi model
|
401 |
+
use_phi_model = False
|
402 |
+
actual_source_code = source_code
|
403 |
+
actual_target_code = target_code
|
404 |
+
|
405 |
+
# Check if we need to use the phi model with fil replacement
|
406 |
if (source_code == "pam" and target_code == "fil") or (source_code == "fil" and target_code == "pam"):
|
407 |
+
use_phi_model = True
|
408 |
+
elif (source_code == "pam" and target_code == "tgl"):
|
409 |
+
use_phi_model = True
|
410 |
+
actual_target_code = "fil" # Replace tgl with fil for the phi model
|
411 |
+
elif (source_code == "tgl" and target_code == "pam"):
|
412 |
+
use_phi_model = True
|
413 |
+
actual_source_code = "fil" # Replace tgl with fil for the phi model
|
414 |
+
|
415 |
+
if use_phi_model:
|
416 |
model_key = "phi"
|
417 |
|
418 |
# Check if we have the phi model
|
|
|
426 |
tokenizer = translation_tokenizers[model_key]
|
427 |
|
428 |
# Prepend target language token to input
|
429 |
+
input_text = f">>{actual_target_code}<< {source_text}"
|
430 |
+
|
431 |
+
logger.info(f"π Using phi model with input: '{input_text}'")
|
432 |
|
433 |
# Tokenize the text
|
434 |
tokenized = tokenizer(input_text, return_tensors="pt", padding=True)
|