Audit any ML model for privacy vulnerabilities β in 3 lines of code.
π¦ PyPI Installation Note: The PyPI package for
privacylensis published asprivacyaudit.
Install via pip:pip install privacyaudit(orpip install "privacyaudit[azure]").
The Problem: When Machine Learning models are trained on private data (like medical records, financial transactions, or customer emails), they can accidentally memorize that sensitive information. Attackers can then extract private data or determine if a specific person's record was in the training set.
The Solution:
privacylensis an open-source privacy auditing toolkit. In 3 lines of code, it runs 5 automated security checks against any ML model (scikit-learn, PyTorch, XGBoost, HuggingFace) to detect data leakage risks before deployment.Why it matters:
- π§βπ» For Developers: Catch privacy bugs automatically in CI/CD pipelines before pushing models to production.
- π’ For Enterprises: Generate standalone interactive HTML compliance reports for GDPR and HIPAA audits.
Most ML engineers don't know if their model is leaking private training data. privacylens audits it across 5 core privacy vulnerability vectors.
from privacylens import audit
report = audit(model, X_train, y_train, X_test)
report.summary()
# Export HTML Compliance Report for GDPR/HIPAA sharing
report.to_html("audit_report.html")+-------------------------------------------------------------------+
| privacylens β 5-Point Privacy Audit Report |
+------------------------------------+--------------+---------------+
| Check | Score | Risk |
+------------------------------------+--------------+---------------+
| Membership Inference Attack | 0.087 | LOW |
| PII Leakage Detection | 0.000 | LOW |
| Model Inversion Risk | 0.042 | LOW |
| Attribute Inference Risk | 0.015 | LOW |
| Differential Privacy (Ξ΅) | 0.038 | LOW |
+------------------------------------+--------------+---------------+
Model: RandomForestClassifier
Overall Risk: LOW
β’ MIA advantage score: 0.087 β model shows low Membership Inference vulnerability.
β’ PII leakage score: 0.000 β model shows low PII Leakage vulnerability.
β’ Model Inversion score: 0.042 β model shows low Model Inversion vulnerability.
β’ Attribute Inference score: 0.015 β model shows low Attribute Inference vulnerability.
β’ Differential Privacy score: 0.038 β model shows low Differential Privacy vulnerability.
- π΅οΈ 1. Membership Inference Attack (MIA) β Detect if an attacker can identify training records using shadow model estimation (Shokri et al., 2017)
- π 2. PII Leakage Detection β Scan predictions and samples for memorized PII (Emails, SSNs, Credit Cards, Phones, IPs)
- π 3. Model Inversion Risk Scorer β Evaluate feature reconstructability risk from confidence probabilities (Fredrikson et al., 2015)
- π― 4. Attribute Inference Attack β Measure sensitive secondary attribute predictability from confidence vectors (Yeom et al., 2018)
- π‘οΈ 5. Differential Privacy (Ξ΅) Estimator β Estimate empirical privacy loss (
$\epsilon$ ) under single-record modifications (Jagielski et al., 2020) - β‘ Azure Machine Learning Integration β Plug-and-play step (
AzureMLAuditStep) to enforce privacy audit gates inside Azure ML Pipelines - π€ Azure OpenAI Service Auditor β Evaluate fine-tuned Azure OpenAI deployments (
gpt-4,gpt-35-turbo) for prompt injection PII leakage - π Native Framework Adapters β Out-of-the-box support for scikit-learn, PyTorch (
nn.Module), XGBoost, and HuggingFace Transformers - π HTML Compliance Reports β Export standalone, interactive HTML reports (
--report audit.html) for security & GDPR/HIPAA compliance sharing - π€ CLI + Python API β Use in scripts or integrate into CI/CD pipelines (
privacylens audit) - π JSON output β Machine-readable results for dashboards and reporting (
--output json)
Follow these steps to install privacylens (privacyaudit) for your environment:
# macOS / Linux
python3 -m venv venv
source venv/bin/activate
# Windows PowerShell
python -m venv venv
.\venv\Scripts\Activate.ps1Choose the installation option tailored to your ML framework:
# π’ Option A: Core Installation (scikit-learn models)
pip install privacyaudit
# π¦ Option B: With Azure ML & Azure OpenAI Support
pip install "privacyaudit[azure]"
# π΄ Option C: With PyTorch Deep Learning Support
pip install "privacyaudit[torch]"
# β‘ Option D: With XGBoost Gradient Boosting Support
pip install "privacyaudit[xgboost]"
# π Option E: Full Enterprise Suite (Azure ML, PyTorch, XGBoost, Transformers)
pip install "privacyaudit[all]"Note: The PyPI package name is
privacyaudit. Import in Python asfrom privacylens import audit.
Confirm your installation is working:
# Verify CLI command
privacylens --help
# Verify Python import
python -c "import privacylens; print(privacylens.__version__)"
# Output: 1.1.0from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from privacylens import audit
# 1. Train model
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
model = RandomForestClassifier(n_estimators=100).fit(X[:700], y[:700])
# 2. Audit model across all 5 privacy vulnerability vectors
report = audit(model, X[:700], y[:700], X[700:], y[700:])
# 3. Display Rich summary table & export HTML compliance report
report.summary()
report.to_html("compliance_report.html")
# Optional: Get audit results as dict for JSON logging / API responses
print(report.to_dict())privacylens provides first-class, plug-and-play components for Microsoft Azure MLOps and Responsible AI governance:
from privacylens.integrations import AzureMLAuditStep
# Executes inside Azure ML Pipeline run, uploads report artifact, and logs workspace metrics
azure_step = AzureMLAuditStep(workspace_name="enterprise-azureml-ws")
report = azure_step.run_pipeline_audit(model, X_train, y_train, X_test, y_test)from privacylens.integrations import AzureOpenAIAuditor
# Audits fine-tuned Azure OpenAI endpoints (GPT-4 / GPT-3.5) for PII memorization
aoai_auditor = AzureOpenAIAuditor(
endpoint="https://my-aoai.openai.azure.com/",
deployment_name="gpt-4"
)
leakage_score, details = aoai_auditor.audit_deployment(prompts)# Audit a saved model file (pass --trust for trusted local models)
privacylens audit model.pkl train.csv test.csv --trust
# Export interactive HTML report
privacylens audit model.pkl train.csv test.csv --report compliance.html --trust
# Output JSON for CI/CD integration
privacylens audit model.pkl train.csv test.csv --output json --trust
# Skip MIA check in fast pipelines
privacylens audit model.pkl train.csv test.csv --no-mia --trustπ Security Notice: Deserializing model files (
.pkl,.joblib) can execute arbitrary code. Only audit models from verified, trusted pipelines, or specify--trustto explicitly confirm intent.
privacylens/
βββ src/privacylens/
β βββ __init__.py # Public API: audit(), AuditReport, Auditors, Adapters
β βββ auditor.py # Core 5-point orchestrator
β βββ adapters/
β β βββ base.py # BaseModelAdapter & get_adapter() factory
β β βββ sklearn_adapter.py
β β βββ pytorch_adapter.py
β β βββ xgboost_adapter.py
β β βββ hf_adapter.py # HuggingFace Transformers Adapter
β βββ integrations/
β β βββ azureml.py # Azure ML Pipeline Step & Azure OpenAI Auditor
β βββ attacks/
β β βββ membership.py # MIA engine (Shokri et al.)
β β βββ inversion.py # Model Inversion Risk Auditor (Fredrikson et al.)
β β βββ attribute.py # Attribute Inference Auditor (Yeom et al.)
β βββ leakage/
β β βββ pii.py # PII Leakage Auditor (Regex + Severity Weighting)
β β βββ dp.py # Empirical Differential Privacy Epsilon Estimator
β βββ report/
β β βββ html.py # HTML Compliance Report Generator (Jinja2)
β βββ cli.py # Click CLI
βββ examples/
β βββ benchmark_demo.py # Enterprise Privacy Audit Benchmark
βββ tests/
βββ test_auditor.py
βββ test_membership.py
βββ test_pii_leakage.py
βββ test_inversion.py
βββ test_adapters.py
βββ test_html_report.py
βββ test_hf_adapter.py
βββ test_5point_audits.py
| Check Score | Risk Level | Meaning |
|---|---|---|
0.0 β 0.10 |
π’ LOW | Model reveals minimal membership/PII/inversion information |
0.10 β 0.30 |
π‘ MEDIUM | Moderate risk β review training data exposure |
0.30 β 1.00 |
π΄ HIGH | Model likely memorising sensitive training data |
See CONTRIBUTING.md. All contributions welcome!
MIT β see LICENSE.
