AshenClock commited on
Commit
12365bc
·
verified ·
1 Parent(s): 0496b55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
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
- 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:
@@ -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(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:
@@ -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(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
 
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