Update app.py
Browse files
app.py
CHANGED
|
@@ -201,27 +201,14 @@ FINE ONTOLOGIA.
|
|
| 201 |
return prompt
|
| 202 |
|
| 203 |
|
| 204 |
-
|
| 205 |
def classify_and_translate(question_text: str, model_answer_text: str):
|
| 206 |
"""
|
| 207 |
Classifica la lingua della domanda e della risposta, quindi traduce la risposta
|
| 208 |
se la lingua è diversa da quella della domanda. L'idea è di restituire una
|
| 209 |
risposta nella stessa lingua dell'utente.
|
| 210 |
-
|
| 211 |
-
Parametri:
|
| 212 |
-
- question_text: Testo della domanda dell'utente.
|
| 213 |
-
- model_answer_text: Risposta del modello (in qualsiasi lingua).
|
| 214 |
-
|
| 215 |
-
Restituisce:
|
| 216 |
-
- La risposta tradotta nella lingua della domanda o la risposta originale
|
| 217 |
-
se entrambe le lingue coincidono.
|
| 218 |
-
NB: Qui l'oggetto 'lang_detect_client' (per rilevamento lingua) è già
|
| 219 |
-
stato inizializzato all'avvio dell'app. Mentre il 'translator_client'
|
| 220 |
-
viene creato 'al volo' poiché la direzione di traduzione dipende
|
| 221 |
-
dalle due lingue effettive.
|
| 222 |
"""
|
| 223 |
def _extract_label(res):
|
| 224 |
-
# Compat:
|
| 225 |
try:
|
| 226 |
first = res[0]
|
| 227 |
if isinstance(first, list) and first:
|
|
@@ -232,57 +219,54 @@ def classify_and_translate(question_text: str, model_answer_text: str):
|
|
| 232 |
except Exception:
|
| 233 |
return None
|
| 234 |
|
| 235 |
-
#
|
| 236 |
try:
|
| 237 |
-
question_lang_result = lang_detect_client.text_classification(question_text) #
|
| 238 |
question_lang = _extract_label(question_lang_result)
|
| 239 |
logger.info(f"[LangDetect] Lingua della domanda: {question_lang}")
|
| 240 |
except Exception:
|
| 241 |
logger.exception("Errore nel rilevamento della lingua della domanda")
|
| 242 |
-
question_lang = None #
|
| 243 |
|
| 244 |
-
#
|
| 245 |
try:
|
| 246 |
-
answer_lang_result = lang_detect_client.text_classification(model_answer_text) #
|
| 247 |
answer_lang = _extract_label(answer_lang_result)
|
| 248 |
logger.info(f"[LangDetect] Lingua della risposta: {answer_lang}")
|
| 249 |
except Exception:
|
| 250 |
logger.exception("Errore nel rilevamento della lingua della risposta")
|
| 251 |
-
answer_lang = None #
|
| 252 |
|
| 253 |
-
# Se detection fallisce o le lingue coincidono
|
| 254 |
if not question_lang or not answer_lang or question_lang == answer_lang:
|
| 255 |
logger.info("[Translate] Niente traduzione (lingue uguali o detection fallita).")
|
| 256 |
return model_answer_text
|
| 257 |
|
| 258 |
-
#
|
| 259 |
-
# (es: "Helsinki-NLP/opus-mt-en-it", "Helsinki-NLP/opus-mt-fr-en", ecc.)
|
| 260 |
translator_model = f"{TRANSLATOR_MODEL_PREFIX}-{answer_lang}-{question_lang}"
|
| 261 |
translator_client = InferenceClient(
|
| 262 |
token=HF_API_KEY,
|
| 263 |
-
model=translator_model
|
|
|
|
| 264 |
)
|
| 265 |
-
|
| 266 |
-
# Traduzione della risposta
|
| 267 |
try:
|
| 268 |
-
translation_result = translator_client.translation(model_answer_text) #
|
| 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 |
-
|
| 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
|
| 282 |
|
| 283 |
return translated_answer
|
| 284 |
|
| 285 |
|
|
|
|
| 286 |
def create_system_prompt_for_guide() -> str:
|
| 287 |
"""
|
| 288 |
Genera un testo di prompt che istruisce il modello a rispondere
|
|
|
|
| 201 |
return prompt
|
| 202 |
|
| 203 |
|
|
|
|
| 204 |
def classify_and_translate(question_text: str, model_answer_text: str):
|
| 205 |
"""
|
| 206 |
Classifica la lingua della domanda e della risposta, quindi traduce la risposta
|
| 207 |
se la lingua è diversa da quella della domanda. L'idea è di restituire una
|
| 208 |
risposta nella stessa lingua dell'utente.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
"""
|
| 210 |
def _extract_label(res):
|
| 211 |
+
# Compat: [ {label,score} ] | [ [ {label,score}, ... ] ] | oggetti con .label
|
| 212 |
try:
|
| 213 |
first = res[0]
|
| 214 |
if isinstance(first, list) and first:
|
|
|
|
| 219 |
except Exception:
|
| 220 |
return None
|
| 221 |
|
| 222 |
+
# Rilevamento lingua domanda
|
| 223 |
try:
|
| 224 |
+
question_lang_result = lang_detect_client.text_classification(question_text) # posizionale
|
| 225 |
question_lang = _extract_label(question_lang_result)
|
| 226 |
logger.info(f"[LangDetect] Lingua della domanda: {question_lang}")
|
| 227 |
except Exception:
|
| 228 |
logger.exception("Errore nel rilevamento della lingua della domanda")
|
| 229 |
+
question_lang = None # fallback prudente
|
| 230 |
|
| 231 |
+
# Rilevamento lingua risposta
|
| 232 |
try:
|
| 233 |
+
answer_lang_result = lang_detect_client.text_classification(model_answer_text) # posizionale
|
| 234 |
answer_lang = _extract_label(answer_lang_result)
|
| 235 |
logger.info(f"[LangDetect] Lingua della risposta: {answer_lang}")
|
| 236 |
except Exception:
|
| 237 |
logger.exception("Errore nel rilevamento della lingua della risposta")
|
| 238 |
+
answer_lang = None # fallback prudente
|
| 239 |
|
| 240 |
+
# Se detection fallisce o le lingue coincidono: non tradurre
|
| 241 |
if not question_lang or not answer_lang or question_lang == answer_lang:
|
| 242 |
logger.info("[Translate] Niente traduzione (lingue uguali o detection fallita).")
|
| 243 |
return model_answer_text
|
| 244 |
|
| 245 |
+
# Traduzione (provider esplicito)
|
|
|
|
| 246 |
translator_model = f"{TRANSLATOR_MODEL_PREFIX}-{answer_lang}-{question_lang}"
|
| 247 |
translator_client = InferenceClient(
|
| 248 |
token=HF_API_KEY,
|
| 249 |
+
model=translator_model,
|
| 250 |
+
provider="hf-inference",
|
| 251 |
)
|
|
|
|
|
|
|
| 252 |
try:
|
| 253 |
+
translation_result = translator_client.translation(model_answer_text) # posizionale
|
|
|
|
|
|
|
| 254 |
if isinstance(translation_result, dict):
|
| 255 |
+
translated_answer = translation_result.get("translation_text") or model_answer_text
|
| 256 |
elif hasattr(translation_result, "translation_text"):
|
| 257 |
+
translated_answer = translation_result.translation_text or model_answer_text
|
| 258 |
elif isinstance(translation_result, str):
|
| 259 |
translated_answer = translation_result
|
| 260 |
+
else:
|
| 261 |
translated_answer = model_answer_text
|
| 262 |
except Exception:
|
| 263 |
logger.exception(f"Errore nella traduzione {answer_lang} -> {question_lang}")
|
| 264 |
+
translated_answer = model_answer_text
|
| 265 |
|
| 266 |
return translated_answer
|
| 267 |
|
| 268 |
|
| 269 |
+
|
| 270 |
def create_system_prompt_for_guide() -> str:
|
| 271 |
"""
|
| 272 |
Genera un testo di prompt che istruisce il modello a rispondere
|