Skip to content

About

Open-source Python library to audit ML pipelines for privacy vulnerabilities - MIA, PII leakage, model inversion

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

38 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

57 Commits

Folders and files

Repository files navigation

πŸ” privacylens

Audit any ML model for privacy vulnerabilities β€” in 3 lines of code.

CI PyPI version Python Discussions License: MIT PRs Welcome


πŸ“¦ PyPI Installation Note: The PyPI package for privacylens is published as privacyaudit.
Install via pip: pip install privacyaudit (or pip install "privacyaudit[azure]").


privacylens 5-Point Privacy Audit Demo


πŸ’‘ Abstract (Executive Summary)

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: privacylens is 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.

🎯 What is privacylens?

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.

✨ 5-Point Privacy Audit Suite

  • πŸ•΅οΈ 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)

βš™οΈ Step-by-Step Installation Guide

Follow these steps to install privacylens (privacyaudit) for your environment:

Step 1: Create & Activate Virtual Environment (Recommended)

# macOS / Linux
python3 -m venv venv
source venv/bin/activate

# Windows PowerShell
python -m venv venv
.\venv\Scripts\Activate.ps1

Step 2: Install Package from PyPI

Choose 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 as from privacylens import audit.


Step 3: Verify Installation

Confirm your installation is working:

# Verify CLI command
privacylens --help

# Verify Python import
python -c "import privacylens; print(privacylens.__version__)"
# Output: 1.1.0

πŸš€ Quick Start: Run Your First Audit in 3 Lines

from 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())

☁️ Azure MLOps & Azure OpenAI Integration

privacylens provides first-class, plug-and-play components for Microsoft Azure MLOps and Responsible AI governance:

Enforce Privacy Gate in Azure ML Pipeline

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)

Audit Azure OpenAI Fine-Tuned Model Deployment

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)

πŸ–₯️ CLI Usage

# 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 --trust to explicitly confirm intent.


πŸ—οΈ Architecture

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

πŸ“– Risk Score Interpretation

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

🀝 Contributing

See CONTRIBUTING.md. All contributions welcome!

πŸ“„ License

MIT β€” see LICENSE.

About

Open-source Python library to audit ML pipelines for privacy vulnerabilities - MIA, PII leakage, model inversion

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

38 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages