Spaces:
Sleeping
Sleeping
Guardrail: rifiuta lingua sbagliata e domande
Browse filesI modelli di riserva della cascata (qwen3.6-27b, gpt-oss-20b) ogni tanto rispondono in
inglese a caption portoghesi/spagnole anche con language=Italian, e fanno domande
(regola 9). Visto nel banco di prova del bot quando Groq limita gpt-oss-120b.
app.py
CHANGED
|
@@ -322,17 +322,23 @@ def _looks_truncated(text: str) -> bool:
|
|
| 322 |
return text[-1] not in ".?!\"”)…" and last in _FRAGMENT_TAIL
|
| 323 |
|
| 324 |
|
| 325 |
-
def _passes_guardrails(text: str) -> bool:
|
| 326 |
-
"""Hard guardrail: no #, no !, no
|
| 327 |
-
niente frammenti.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
if not text:
|
| 329 |
return False
|
| 330 |
-
if any(ch in text for ch in "#!"):
|
| 331 |
return False
|
| 332 |
if any(ord(ch) > 0x27BF for ch in text):
|
| 333 |
return False
|
| 334 |
if _looks_truncated(text):
|
| 335 |
return False
|
|
|
|
|
|
|
| 336 |
return True
|
| 337 |
|
| 338 |
|
|
@@ -449,12 +455,13 @@ def generate_comment_core(
|
|
| 449 |
"error": "GROQ_API_KEY non configurato nei Secrets dello Space.",
|
| 450 |
}
|
| 451 |
|
|
|
|
| 452 |
prompt = _build_prompt(
|
| 453 |
caption=caption or "",
|
| 454 |
target_username=target_username or None,
|
| 455 |
media_type=str(media_type or "photo").lower(),
|
| 456 |
hint=hint or None,
|
| 457 |
-
language=
|
| 458 |
)
|
| 459 |
|
| 460 |
started = time.time()
|
|
@@ -475,7 +482,7 @@ def generate_comment_core(
|
|
| 475 |
}
|
| 476 |
if text:
|
| 477 |
cleaned = _sanitize_output(text)
|
| 478 |
-
if _passes_guardrails(cleaned):
|
| 479 |
latency_ms = int((time.time() - started) * 1000)
|
| 480 |
if idx > 0:
|
| 481 |
logger.info(
|
|
|
|
| 322 |
return text[-1] not in ".?!\"”)…" and last in _FRAGMENT_TAIL
|
| 323 |
|
| 324 |
|
| 325 |
+
def _passes_guardrails(text: str, language: str = DEFAULT_LANGUAGE) -> bool:
|
| 326 |
+
"""Hard guardrail: no #, no !, no ? (regola 9: niente domande), no emoji
|
| 327 |
+
(heuristic su codepoint > U+27BF), niente frammenti, lingua giusta.
|
| 328 |
+
|
| 329 |
+
La lingua conta perche' i modelli di riserva della cascata (qwen, gpt-oss-20b)
|
| 330 |
+
ogni tanto rispondono in inglese a una caption portoghese/spagnola anche con
|
| 331 |
+
language=Italian: quel commento non deve uscire."""
|
| 332 |
if not text:
|
| 333 |
return False
|
| 334 |
+
if any(ch in text for ch in "#!?"):
|
| 335 |
return False
|
| 336 |
if any(ord(ch) > 0x27BF for ch in text):
|
| 337 |
return False
|
| 338 |
if _looks_truncated(text):
|
| 339 |
return False
|
| 340 |
+
if language == DEFAULT_LANGUAGE and _detect_language(text) == "English":
|
| 341 |
+
return False
|
| 342 |
return True
|
| 343 |
|
| 344 |
|
|
|
|
| 455 |
"error": "GROQ_API_KEY non configurato nei Secrets dello Space.",
|
| 456 |
}
|
| 457 |
|
| 458 |
+
resolved_language = _resolve_language(language, caption or "")
|
| 459 |
prompt = _build_prompt(
|
| 460 |
caption=caption or "",
|
| 461 |
target_username=target_username or None,
|
| 462 |
media_type=str(media_type or "photo").lower(),
|
| 463 |
hint=hint or None,
|
| 464 |
+
language=resolved_language,
|
| 465 |
)
|
| 466 |
|
| 467 |
started = time.time()
|
|
|
|
| 482 |
}
|
| 483 |
if text:
|
| 484 |
cleaned = _sanitize_output(text)
|
| 485 |
+
if _passes_guardrails(cleaned, resolved_language):
|
| 486 |
latency_ms = int((time.time() - started) * 1000)
|
| 487 |
if idx > 0:
|
| 488 |
logger.info(
|