Privacy & content safety detection with heuristic and AI modes
Project description
Zero Harm AI Detectors
Privacy & content safety detection with heuristic default and optional AI config.
One API, Two Execution Paths
| Feature | detect(text) |
detect(text, ai_config=...) |
|---|---|---|
| Speed | โก 1โ5ms | ๐ข 50โ200ms |
| Email, Phone, SSN | โ 95โ99% | โ 95โ99% |
| Credit Card (Luhn) | โ 99% | โ 99% |
| Secrets / API Keys | โ 95% | โ 95% |
| Person Names | โ ๏ธ 20โ60% (tiered) | โ 85โ95% |
| Locations | โ | โ 80โ90% |
| Organisations | โ | โ 75โ85% |
| Harmful Content | โ Pattern-based | โ Contextual AI |
| Extra dependencies | None | transformers/torch and/or spacy |
Installation
# Heuristic mode only (fast, no ML dependencies)
pip install zero_harm_ai_detectors
# With AI mode (~2 GB model download on first use)
pip install 'zero_harm_ai_detectors[ai]'
On Python 3.9, the ai extra installs the spaCy 3.7 line for compatibility.
Quick Start
from zero_harm_ai_detectors import detect, AIConfig
# Heuristic mode (default) โ fast, great for structured data
result = detect("Email: john@example.com, SSN: 123-45-6789")
print(result.redacted_text)
# โ Email: [REDACTED_EMAIL], SSN: [REDACTED_SSN]
# AI mode โ enabled when ai_config is provided
result = detect("Contact John Smith at Microsoft in NYC", ai_config=AIConfig())
print(result.detections)
# โ [Detection(PERSON, ...), Detection(ORGANIZATION, ...), Detection(LOCATION, ...)]
Detection Result
Both modes return an identical DetectionResult:
result = detect(text) # heuristic
result_ai = detect(text, ai_config=AIConfig()) # ai
result.original_text # str โ original input
result.redacted_text # str โ sensitive content replaced
result.detections # list โ List[Detection]
result.mode # str โ "heuristic" or "ai"
result.harmful # bool โ harmful content found
result.severity # str โ "none" | "low" | "medium" | "high"
result.harmful_scores # dict โ per-category scores
result.to_dict() # full dict with all fields
result.get_pii() # List[Detection] โ PII only
result.get_secrets() # List[Detection] โ secrets only
Target Selection
from zero_harm_ai_detectors import detect, DetectTarget
# Default = all targets
detect(text)
# PII only
detect(text, targets=DetectTarget.PII)
# Secrets only
detect(text, targets=DetectTarget.SECRET)
# Harmful only
detect(text, targets=DetectTarget.HARMFUL)
# Combine
detect(text, targets=DetectTarget.PII | DetectTarget.SECRET)
PII Detection
Structured data (heuristic, 95โ99% accuracy)
from zero_harm_ai_detectors import (
detect_emails,
detect_phones,
detect_ssns,
detect_credit_cards,
detect_bank_accounts,
detect_dob,
detect_addresses,
)
emails = detect_emails("Contact alice@test.com or bob@example.org")
# โ [Detection(EMAIL, 'alice@test.com', ...), Detection(EMAIL, 'bob@example.org', ...)]
cards = detect_credit_cards("Card: 4532-0151-1283-0366")
# โ [Detection(CREDIT_CARD, '4532-0151-1283-0366', confidence=0.99)] # Luhn validated
Person names โ heuristic mode (tiered confidence)
Heuristic-mode person name detection uses three tiers to balance recall against
false positives. Every PERSON detection carries metadata["tier"] and a
confidence value from the documented set {0.20, 0.45, 0.60}.
| Tier | Confidence | Trigger | Precision |
|---|---|---|---|
| 1 โ titled | 0.60 |
Name preceded by a title: Mr, Mrs, Ms, Miss, Dr, Prof, Rev, Capt, Lt, Sgt, Cpl, Pvt |
High |
| 2 โ context | 0.45 |
Name follows a context keyword: Name:, Contact:, From:, To:, Cc:, Bcc:, Signed by:, Authored by: |
Medium |
| 3 โ bare | 0.20 |
Plain "First Last" capitalised pair with no surrounding signal โ high false-positive rate |
Low |
Tiers 1 and 2 are checked first; any span they cover is suppressed from Tier 3, so a titled name never appears as a duplicate bare match.
from zero_harm_ai_detectors import detect_person_names_heuristic
detections = detect_person_names_heuristic(
"Dr. Alice Brown arrived. Contact: Bob White. Meet James Green tomorrow."
)
for d in detections:
print(f"{d.text!r:20} tier={d.metadata['tier']:8} confidence={d.confidence}")
# 'Dr. Alice Brown' tier=titled confidence=0.6
# 'Bob White' tier=context confidence=0.45
# 'James Green' tier=bare confidence=0.2
Filtering by confidence threshold is the recommended way to tune precision vs. recall for your use case:
# High-precision only: titled and context names
precise = [d for d in detections if d.confidence >= 0.45]
# Maximum recall: include bare matches too
all_names = detections # confidence >= 0.20
# Inspect tier directly
titled_only = [d for d in detections if d.metadata.get("tier") == "titled"]
Tip: For reliable person-name detection (85โ95% accuracy) use
detect(text, ai_config=AIConfig())instead. The heuristic tiers are best suited to structured forms and templated text where titles or context keywords are consistently present.
Names, locations, organisations (AI mode only)
from zero_harm_ai_detectors import AIConfig
result = detect("Dr. Jane Wilson visited Stanford University in Palo Alto", ai_config=AIConfig())
for d in result.detections:
print(f"{d.type}: {d.text} ({d.confidence:.0%})")
# PERSON: Dr. Jane Wilson (92%)
# ORGANIZATION: Stanford University (88%)
# LOCATION: Palo Alto (85%)
Secrets Detection
Three-tier detection, always uses heuristic matching (95%+ accuracy):
from zero_harm_ai_detectors import detect_secrets_heuristic
secrets = detect_secrets_heuristic("""
OPENAI_KEY = "sk-1234567890abcdef1234567890abcdef"
AWS_KEY = "AKIAIOSFODNN7EXAMPLE"
GH_TOKEN = "ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcd1234"
""")
Supported patterns: OpenAI ยท AWS (access + secret) ยท GitHub ยท Stripe ยท Google ยท Slack ยท Twilio ยท SendGrid ยท npm ยท PyPI ยท Anthropic ยท Generic secrets (context + entropy)
Harmful Content Detection
from zero_harm_ai_detectors import DetectTarget
result = detect(text, targets=DetectTarget.HARMFUL)
print(result.harmful) # True / False
print(result.severity) # "none" | "low" | "medium" | "high"
print(result.harmful_scores) # {"insult": 0.6, "threat_phrases": 0.8, ...}
Severity rules:
| Condition | Severity |
|---|---|
identity_hate found |
high |
| Explicit threat phrase | high |
| 2+ threat words or 6+ total matches | high |
| 1 threat word or 4+ total matches | medium |
| 2+ obscene terms | medium |
| Any other match | low |
Redaction Strategies
text = "Email: john@example.com"
detect(text, redaction_strategy="token") # Email: [REDACTED_EMAIL]
detect(text, redaction_strategy="mask_all") # Email: ****************
detect(text, redaction_strategy="mask_last4")# Email: ************.com
detect(text, redaction_strategy="hash") # Email: [HASH:a1b2c3d4e5f6]
Selective Detection
from zero_harm_ai_detectors import DetectTarget
# PII only
result = detect(text, targets=DetectTarget.PII)
# Secrets only
result = detect(text, targets=DetectTarget.SECRET)
# PII + Secrets
result = detect(text, targets=DetectTarget.PII | DetectTarget.SECRET)
AI Configuration
from zero_harm_ai_detectors import detect, AIConfig
config = AIConfig(
backend="transformers", # higher NER accuracy
ner_model="dslim/bert-base-NER",
ner_threshold=0.8, # higher = fewer false positives
harmful_threshold=0.6,
device="cuda", # or "cpu"
)
result = detect(text, ai_config=config)
SpaCy NER option:
config = AIConfig(
backend="spacy",
spacy_model="en_core_web_sm",
)
result = detect(text, ai_config=config)
Harmful detection behavior in AI path:
- Uses transformer harmful model when available.
- If unavailable/fails, falls back to heuristic harmful detection.
- Non-default
harmful_modelvalues emit a warning.
Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ detect(text, targets=..., ai_config=...) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ default (no ai_config) โ ai_config provided โ
โ heuristic_detectors.py โ ai_detectors.py โ
โ โ - structured PII via heuristic โ
โ - structured PII โ - NER via transformers or spacy โ
โ - secrets via heuristic โ - harmful via transformer โ
โ - harmful via heuristic โ with heuristic fallback โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
DetectionResult
Integration Example โ GitHub App PR Scanner
from zero_harm_ai_detectors import detect, AI_AVAILABLE, AIConfig, DetectTarget
def scan_pr_diff(diff: str, is_paid_user: bool) -> dict:
ai_config = AIConfig() if (is_paid_user and AI_AVAILABLE) else None
result = detect(
diff,
targets=DetectTarget.PII | DetectTarget.SECRET,
ai_config=ai_config,
)
blocking_types = {"API_KEY", "SECRET", "SSN", "CREDIT_CARD"}
return {
"has_issues": len(result.detections) > 0,
"should_block": any(d.type in blocking_types for d in result.detections),
"detections": result.to_dict()["detections"],
}
Performance
| Text length | Default (heuristic) | AI (ai_config) |
|---|---|---|
| ~50 chars | 1โ2ms | 50โ100ms |
| ~500 chars | 2โ3ms | 100โ150ms |
| ~5 000 chars | 3โ5ms | 150โ200ms |
| Throughput | ~500/sec | ~5โ10/sec |
Testing
# All tests
pytest tests/ -v
# With coverage
pytest tests/ --cov=zero_harm_ai_detectors
# Skip AI tests (if dependencies not installed)
pytest tests/test_core_and_heuristic.py -v
Licensing
The open-source core of this project is licensed under MIT.
- Open-source license: https://scalapps.com/docs/LICENSE
- Commercial licensing terms: https://scalapps.com/docs/LICENSE-COMMERCIAL
Commercial licensing is available for organizations that need contractual terms
such as support/SLA, warranty, indemnity, or other enterprise conditions.
Contact: info@scalapps.com
Links
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zero_harm_ai_detectors-0.2.8.tar.gz.
File metadata
- Download URL: zero_harm_ai_detectors-0.2.8.tar.gz
- Upload date:
- Size: 63.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66ab7ee17cea911d0d0ab851ac8212469f9df09bc77ba19751d65747bef30ebb
|
|
| MD5 |
303685a73b539dad111d158e946e4c92
|
|
| BLAKE2b-256 |
9def9a163d86f2982f6d7f256732c2404e4e05cd12e1dfaa26c779f4873ec8f6
|
File details
Details for the file zero_harm_ai_detectors-0.2.8-py3-none-any.whl.
File metadata
- Download URL: zero_harm_ai_detectors-0.2.8-py3-none-any.whl
- Upload date:
- Size: 36.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13d213e1110c56fa1c80e434fb73e0dc2bf97033090bb95a26242939dcc84134
|
|
| MD5 |
42880e66e4a0b943d9a30bb7e7a94456
|
|
| BLAKE2b-256 |
3063d99a4bac3776339f8b3e74799f7afbf5cd85fe10c5f6c7303031a7ccfb66
|