Update app.py
Browse files
app.py
CHANGED
|
@@ -221,18 +221,20 @@ def classify_and_translate(question_text: str, model_answer_text: str):
|
|
| 221 |
dalle due lingue effettive.
|
| 222 |
"""
|
| 223 |
def _extract_label(res):
|
| 224 |
-
# Compat: può essere [ {label,score} ] oppure [ [ {label,score}, ... ] ]
|
| 225 |
try:
|
| 226 |
first = res[0]
|
| 227 |
if isinstance(first, list) and first:
|
| 228 |
first = first[0]
|
| 229 |
-
|
|
|
|
|
|
|
| 230 |
except Exception:
|
| 231 |
return None
|
| 232 |
|
| 233 |
# Rileva la lingua della domanda
|
| 234 |
try:
|
| 235 |
-
question_lang_result = lang_detect_client.text_classification(
|
| 236 |
question_lang = _extract_label(question_lang_result)
|
| 237 |
logger.info(f"[LangDetect] Lingua della domanda: {question_lang}")
|
| 238 |
except Exception:
|
|
@@ -241,7 +243,7 @@ def classify_and_translate(question_text: str, model_answer_text: str):
|
|
| 241 |
|
| 242 |
# Rileva la lingua della risposta
|
| 243 |
try:
|
| 244 |
-
answer_lang_result = lang_detect_client.text_classification(
|
| 245 |
answer_lang = _extract_label(answer_lang_result)
|
| 246 |
logger.info(f"[LangDetect] Lingua della risposta: {answer_lang}")
|
| 247 |
except Exception:
|
|
@@ -263,11 +265,17 @@ def classify_and_translate(question_text: str, model_answer_text: str):
|
|
| 263 |
|
| 264 |
# Traduzione della risposta
|
| 265 |
try:
|
| 266 |
-
translation_result = translator_client.translation(
|
| 267 |
-
# Compat: oggetto con
|
| 268 |
-
translated_answer =
|
| 269 |
-
if
|
| 270 |
-
translated_answer = translation_result.get("translation_text"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
except Exception:
|
| 272 |
logger.exception(f"Errore nella traduzione {answer_lang} -> {question_lang}")
|
| 273 |
translated_answer = model_answer_text # Se fallisce, restituiamo l'originale
|
|
|
|
| 221 |
dalle due lingue effettive.
|
| 222 |
"""
|
| 223 |
def _extract_label(res):
|
| 224 |
+
# Compat: può essere [ {label,score} ] oppure [ [ {label,score}, ... ] ] oppure oggetti con .label
|
| 225 |
try:
|
| 226 |
first = res[0]
|
| 227 |
if isinstance(first, list) and first:
|
| 228 |
first = first[0]
|
| 229 |
+
if isinstance(first, dict):
|
| 230 |
+
return first.get("label")
|
| 231 |
+
return getattr(first, "label", None)
|
| 232 |
except Exception:
|
| 233 |
return None
|
| 234 |
|
| 235 |
# Rileva la lingua della domanda
|
| 236 |
try:
|
| 237 |
+
question_lang_result = lang_detect_client.text_classification(question_text) # argomento posizionale
|
| 238 |
question_lang = _extract_label(question_lang_result)
|
| 239 |
logger.info(f"[LangDetect] Lingua della domanda: {question_lang}")
|
| 240 |
except Exception:
|
|
|
|
| 243 |
|
| 244 |
# Rileva la lingua della risposta
|
| 245 |
try:
|
| 246 |
+
answer_lang_result = lang_detect_client.text_classification(model_answer_text) # argomento posizionale
|
| 247 |
answer_lang = _extract_label(answer_lang_result)
|
| 248 |
logger.info(f"[LangDetect] Lingua della risposta: {answer_lang}")
|
| 249 |
except Exception:
|
|
|
|
| 265 |
|
| 266 |
# Traduzione della risposta
|
| 267 |
try:
|
| 268 |
+
translation_result = translator_client.translation(model_answer_text) # argomento posizionale
|
| 269 |
+
# Compat: oggetto con .translation_text oppure dict o stringa
|
| 270 |
+
translated_answer = None
|
| 271 |
+
if isinstance(translation_result, dict):
|
| 272 |
+
translated_answer = translation_result.get("translation_text")
|
| 273 |
+
elif hasattr(translation_result, "translation_text"):
|
| 274 |
+
translated_answer = translation_result.translation_text
|
| 275 |
+
elif isinstance(translation_result, str):
|
| 276 |
+
translated_answer = translation_result
|
| 277 |
+
if not translated_answer:
|
| 278 |
+
translated_answer = model_answer_text
|
| 279 |
except Exception:
|
| 280 |
logger.exception(f"Errore nella traduzione {answer_lang} -> {question_lang}")
|
| 281 |
translated_answer = model_answer_text # Se fallisce, restituiamo l'originale
|