Download backend/guardrails.py from C2MV/letxinet: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/C2MV/letxinet/resolve/main/backend/guardrails.py
- Command line
-
hf download hf://spaces/C2MV/letxinet/backend/guardrails.py
-
curl -L -o guardrails.py https://huggingface.co/spaces/C2MV/letxinet/resolve/main/backend/guardrails.py
1.09 kB
| import re | |
| XSS_PATTERNS = [r"<script", r"javascript:", r"on\w+\s*=", r"<iframe", r"<object", r"<embed"] | |
| PROFANITY_WORDS = [ | |
| "fuck", "shit", "damn", "hell", "ass", "bitch", "crap", "piss", | |
| "puta", "mierda", "pendejo", "cabr贸n", "est煤pido", "idiota", | |
| "marica", "gonorrea", "hijueputa", "malparido", "chucha", | |
| ] | |
| class Guardrails: | |
| def __init__(self, max_length: int = 500): | |
| self.max_length = max_length | |
| def validate(self, query: str) -> tuple[bool, str]: | |
| if not query or not query.strip(): | |
| return False, "La consulta est谩 vac铆a" | |
| if len(query) > self.max_length: | |
| return False, f"La consulta excede {self.max_length} caracteres (tiene {len(query)})" | |
| query_lower = query.lower() | |
| for pattern in XSS_PATTERNS: | |
| if re.search(pattern, query_lower): | |
| return False, "Se detect贸 contenido potencialmente inseguro (XSS)" | |
| for word in PROFANITY_WORDS: | |
| if word in query_lower: | |
| return False, "Se detect贸 lenguaje inapropiado" | |
| return True, "" | |