Update app.py
Browse files
app.py
CHANGED
|
@@ -220,26 +220,37 @@ def classify_and_translate(question_text: str, model_answer_text: str):
|
|
| 220 |
viene creato 'al volo' poiché la direzione di traduzione dipende
|
| 221 |
dalle due lingue effettive.
|
| 222 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
# Rileva la lingua della domanda
|
| 224 |
try:
|
| 225 |
-
question_lang_result = lang_detect_client.text_classification(
|
| 226 |
-
question_lang = question_lang_result
|
| 227 |
logger.info(f"[LangDetect] Lingua della domanda: {question_lang}")
|
| 228 |
-
except Exception
|
| 229 |
-
logger.
|
| 230 |
-
question_lang =
|
|
|
|
| 231 |
# Rileva la lingua della risposta
|
| 232 |
try:
|
| 233 |
-
answer_lang_result = lang_detect_client.text_classification(
|
| 234 |
-
answer_lang = answer_lang_result
|
| 235 |
logger.info(f"[LangDetect] Lingua della risposta: {answer_lang}")
|
| 236 |
-
except Exception
|
| 237 |
-
logger.
|
| 238 |
-
answer_lang =
|
| 239 |
|
| 240 |
-
# Se
|
| 241 |
-
if question_lang == answer_lang:
|
| 242 |
-
logger.info("[Translate]
|
| 243 |
return model_answer_text
|
| 244 |
|
| 245 |
# Altrimenti, costruiamo "al volo" il modello di traduzione appropriato
|
|
@@ -249,14 +260,17 @@ def classify_and_translate(question_text: str, model_answer_text: str):
|
|
| 249 |
token=HF_API_KEY,
|
| 250 |
model=translator_model
|
| 251 |
)
|
|
|
|
| 252 |
# Traduzione della risposta
|
| 253 |
try:
|
| 254 |
-
translation_result = translator_client.translation(
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
| 260 |
|
| 261 |
return translated_answer
|
| 262 |
|
|
|
|
| 220 |
viene creato 'al volo' poiché la direzione di traduzione dipende
|
| 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 |
+
return getattr(first, "label", None) if not isinstance(first, dict) else first.get("label")
|
| 230 |
+
except Exception:
|
| 231 |
+
return None
|
| 232 |
+
|
| 233 |
# Rileva la lingua della domanda
|
| 234 |
try:
|
| 235 |
+
question_lang_result = lang_detect_client.text_classification(inputs=question_text, top_k=1)
|
| 236 |
+
question_lang = _extract_label(question_lang_result)
|
| 237 |
logger.info(f"[LangDetect] Lingua della domanda: {question_lang}")
|
| 238 |
+
except Exception:
|
| 239 |
+
logger.exception("Errore nel rilevamento della lingua della domanda")
|
| 240 |
+
question_lang = None # Fallback prudente: se fallisce, non traduciamo
|
| 241 |
+
|
| 242 |
# Rileva la lingua della risposta
|
| 243 |
try:
|
| 244 |
+
answer_lang_result = lang_detect_client.text_classification(inputs=model_answer_text, top_k=1)
|
| 245 |
+
answer_lang = _extract_label(answer_lang_result)
|
| 246 |
logger.info(f"[LangDetect] Lingua della risposta: {answer_lang}")
|
| 247 |
+
except Exception:
|
| 248 |
+
logger.exception("Errore nel rilevamento della lingua della risposta")
|
| 249 |
+
answer_lang = None # Fallback prudente: se fallisce, non traduciamo
|
| 250 |
|
| 251 |
+
# Se detection fallisce o le lingue coincidono, non traduciamo
|
| 252 |
+
if not question_lang or not answer_lang or question_lang == answer_lang:
|
| 253 |
+
logger.info("[Translate] Niente traduzione (lingue uguali o detection fallita).")
|
| 254 |
return model_answer_text
|
| 255 |
|
| 256 |
# Altrimenti, costruiamo "al volo" il modello di traduzione appropriato
|
|
|
|
| 260 |
token=HF_API_KEY,
|
| 261 |
model=translator_model
|
| 262 |
)
|
| 263 |
+
|
| 264 |
# Traduzione della risposta
|
| 265 |
try:
|
| 266 |
+
translation_result = translator_client.translation(inputs=model_answer_text)
|
| 267 |
+
# Compat: oggetto con attributo .translation_text o dict
|
| 268 |
+
translated_answer = getattr(translation_result, "translation_text", None)
|
| 269 |
+
if translated_answer is None and isinstance(translation_result, dict):
|
| 270 |
+
translated_answer = translation_result.get("translation_text", model_answer_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
|
| 274 |
|
| 275 |
return translated_answer
|
| 276 |
|