Spaces:
Running
Running
Download core/privacy/redact.py from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/privacy/redact.py
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/core/privacy/redact.py
-
curl -L -o redact.py https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/privacy/redact.py
1.44 kB
| """Lightweight PII redaction for logs and audit rows. | |
| Covers Pakistani CNIC, Pakistani phone numbers, IBANs, emails and card-like numbers. | |
| Phase 15: ``core.privacy.pii`` wraps these as the default ``PIIAnalyzer`` (Presidio is an | |
| optional adapter); this module stays the deterministic baseline used by audit rows. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| from typing import Any | |
| PATTERNS: list[tuple[str, re.Pattern[str]]] = [ | |
| ("CNIC", re.compile(r"\b\d{5}-\d{7}-\d\b")), | |
| ("IBAN", re.compile(r"\bPK\d{2}[A-Z]{4}\d{16}\b", re.IGNORECASE)), | |
| ("EMAIL", re.compile(r"\b[\w.+-]+@[\w-]+\.[\w.-]+\b")), | |
| ("PHONE", re.compile(r"(?<!\d)(?:\+92|0092|0)3\d{2}[-\s]?\d{7}(?!\d)")), | |
| ("CARD", re.compile(r"\b(?:\d[ -]?){13,19}\b")), | |
| ] | |
| SENSITIVE_KEYS = {"password", "token", "secret", "api_key", "authorization", "cnic", "card"} | |
| def redact_text(text: str) -> str: | |
| for label, pattern in PATTERNS: | |
| text = pattern.sub(f"<{label}>", text) | |
| return text | |
| def redact_value(value: Any, key: str | None = None) -> Any: | |
| if key is not None and key.lower() in SENSITIVE_KEYS: | |
| return "<REDACTED>" | |
| if isinstance(value, str): | |
| return redact_text(value) | |
| if isinstance(value, dict): | |
| return {k: redact_value(v, str(k)) for k, v in value.items()} | |
| if isinstance(value, list | tuple): | |
| return [redact_value(v) for v in value] | |
| return value | |