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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -19
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(text=question_text)
226
- question_lang = question_lang_result[0]['label']
227
  logger.info(f"[LangDetect] Lingua della domanda: {question_lang}")
228
- except Exception as e:
229
- logger.error(f"Errore nel rilevamento della lingua della domanda: {e}")
230
- question_lang = "en" # Fallback se non riusciamo a rilevare la lingua
 
231
  # Rileva la lingua della risposta
232
  try:
233
- answer_lang_result = lang_detect_client.text_classification(text=model_answer_text)
234
- answer_lang = answer_lang_result[0]['label']
235
  logger.info(f"[LangDetect] Lingua della risposta: {answer_lang}")
236
- except Exception as e:
237
- logger.error(f"Errore nel rilevamento della lingua della risposta: {e}")
238
- answer_lang = "it" # Fallback se non riusciamo a rilevare la lingua
239
 
240
- # Se domanda e risposta sono nella stessa lingua, non traduciamo
241
- if question_lang == answer_lang:
242
- logger.info("[Translate] Nessuna traduzione necessaria: stessa lingua.")
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(text=model_answer_text)
255
- translated_answer = translation_result["translation_text"]
256
- except Exception as e:
257
- logger.error(f"Errore nella traduzione {answer_lang} -> {question_lang}: {e}")
258
- # Se fallisce, restituiamo la risposta originale come fallback
259
- translated_answer = model_answer_text
 
 
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