Skip to main content

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.

PyPI version Python 3.8+ License: MIT

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_model values 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.

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

zero_harm_ai_detectors-0.2.9.tar.gz (63.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zero_harm_ai_detectors-0.2.9-py3-none-any.whl (36.2 kB view details)

Uploaded Python 3

File details

Details for the file zero_harm_ai_detectors-0.2.9.tar.gz.

File metadata

  • Download URL: zero_harm_ai_detectors-0.2.9.tar.gz
  • Upload date:
  • Size: 63.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for zero_harm_ai_detectors-0.2.9.tar.gz
Algorithm Hash digest
SHA256 e01d644159a38f323a3f6e1dfdf9a876b0011aaf36db2adb6cedc48a08eb17e8
MD5 8e575c2a95accecf835bd9e9283dc8a3
BLAKE2b-256 645c96c4eaeb16ef6a36ffa1c3a21438cb1c727636bd0ba543b9bd2fdf7741c9

See more details on using hashes here.

File details

Details for the file zero_harm_ai_detectors-0.2.9-py3-none-any.whl.

File metadata

File hashes

Hashes for zero_harm_ai_detectors-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 45931a27bc891c78116fb907f134e555c2077392c08c88a96178a29940efe91b
MD5 b0ef9a98559e2899424b01b1ee6f5f40
BLAKE2b-256 fcef192cefaf5ac7710ce4c32b1f456adcdeadd6dd58f88fd2795dcb9b5bf6e0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page