Metadata-Version: 2.4
Name: redactai
Version: 0.1.0
Summary: Make data safe before feeding it to AI
Project-URL: Homepage, https://github.com/jagreehal/redactai
Project-URL: Repository, https://github.com/jagreehal/redactai.git
Project-URL: Issues, https://github.com/jagreehal/redactai/issues
Author: Jag Reehal
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for describing the origin of the Work and
              reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Support. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or support.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright 2026 Jag Reehal
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
           implied. See the License for the specific language governing permissions
           and limitations under the License.
License-File: LICENSE
Keywords: anonymization,llm,pii,presidio,privacy,redaction
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.12
Requires-Dist: asyncpg>=0.29.0
Requires-Dist: faker>=33.0.0
Requires-Dist: fastapi>=0.115.0
Requires-Dist: httpx>=0.28.0
Requires-Dist: loguru>=0.7.0
Requires-Dist: presidio-analyzer>=2.2.0
Requires-Dist: presidio-anonymizer>=2.2.0
Requires-Dist: pydantic-settings>=2.7.0
Requires-Dist: pydantic>=2.10.0
Requires-Dist: pypdf>=5.0.0
Requires-Dist: python-docx>=1.1.0
Requires-Dist: python-multipart>=0.0.20
Requires-Dist: pyyaml>=6.0.0
Requires-Dist: rich>=13.0.0
Requires-Dist: spacy>=3.8.0
Requires-Dist: typer>=0.15.0
Requires-Dist: uvicorn[standard]>=0.34.0
Requires-Dist: watchfiles>=1.0.0
Provides-Extra: auth
Requires-Dist: workos>=5.0.0; extra == 'auth'
Provides-Extra: dev
Requires-Dist: mypy>=1.13.0; extra == 'dev'
Requires-Dist: pandas>=2.0.0; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.24.0; extra == 'dev'
Requires-Dist: pytest-cov>=6.0.0; extra == 'dev'
Requires-Dist: pytest>=8.3.0; extra == 'dev'
Requires-Dist: ruff>=0.8.0; extra == 'dev'
Requires-Dist: types-pyyaml>=6.0.0; extra == 'dev'
Provides-Extra: docs
Requires-Dist: scalar-fastapi>=1.0.0; extra == 'docs'
Provides-Extra: image
Requires-Dist: presidio-image-redactor>=0.0.50; extra == 'image'
Provides-Extra: mcp
Requires-Dist: fastmcp>=2.0.0; extra == 'mcp'
Provides-Extra: structured
Requires-Dist: pandas>=2.0.0; extra == 'structured'
Requires-Dist: presidio-structured>=0.0.3; extra == 'structured'
Provides-Extra: telemetry
Requires-Dist: opentelemetry-api>=1.29.0; extra == 'telemetry'
Requires-Dist: opentelemetry-exporter-otlp>=1.29.0; extra == 'telemetry'
Requires-Dist: opentelemetry-instrumentation-fastapi>=0.50b0; extra == 'telemetry'
Requires-Dist: opentelemetry-sdk>=1.29.0; extra == 'telemetry'
Description-Content-Type: text/markdown

# RedactAI

Strip PII from text, files, and pipelines before it reaches your AI.

## Install

```bash
pip install redactai
python -m spacy download en_core_web_sm
```

## Quick Start

**Clean a file:**

```bash
redactai clean data.csv -o data.clean.csv
```

**Clean text in Python:**

```python
from redactai import clean

safe = clean("Call John Smith at 555-0123")
# "Call Marcia Wells at 555-8912"  (faker replacements by default)
```

**Scan for PII in CI:**

```bash
redactai scan ./data --ci  # exits 1 if PII detected
```

## CLI Commands

| Command | Description |
|---------|-------------|
| `redactai clean [PATH]` | Anonymize a file, folder, or stdin |
| `redactai scan PATH` | Detect PII and report findings |
| `redactai analyze` | Analyze text or file and return entity details |
| `redactai decrypt` | Decrypt previously encrypted output |
| `redactai watch PATH` | Watch a folder and clean files on change |
| `redactai init` | Generate a `.redactai.yml` config file |
| `redactai entities` | List supported PII entity types |
| `redactai profiles` | List built-in profiles |
| `redactai profiles show NAME` | Show profile details |
| `redactai mcp` | Start MCP tool server for AI agents |
| `redactai server start` | Start the local API daemon |
| `redactai server stop` | Stop the daemon |
| `redactai server status` | Show daemon status |
| `redactai server restart` | Restart the daemon |
| `redactai login` | Authenticate with a remote API |
| `redactai logout` | Remove stored credentials |
| `redactai whoami` | Show current auth status |

## Python API

```python
from redactai import clean, scan
```

### `clean(text, *, profile, threshold, language, entities, operators) -> str`

```python
# Use a built-in profile
clean("Email me at john@acme.com", profile="llm_guardrail")
# "Email me at <EMAIL_ADDRESS>"

# Override operator for a specific entity
clean("Call 555-0123", operators={"PHONE_NUMBER": {"type": "mask", "masking_char": "*", "chars_to_mask": 6}})
# "Call ***-****"
```

### `scan(text, *, threshold, language, entities) -> list[dict]`

```python
hits = scan("My SSN is 123-45-6789")
# [{"entity_type": "US_SSN", "start": 10, "end": 21, "score": 0.85, "text": "123-45-6789"}]
```

### Other exports

```python
from redactai import entities, profiles, profile_detail

entities()          # ["CREDIT_CARD", "EMAIL_ADDRESS", "PERSON", ...]
profiles()          # [{"id": "llm_guardrail@1", "name": "llm_guardrail", ...}, ...]
profile_detail("llm_guardrail")  # full config including operators
```

## Profiles

| Profile | Description | Threshold |
|---------|-------------|-----------|
| `llm_guardrail` | Redact all PII before sending to LLMs | 0.3 |
| `app_logs_safe` | Mask PII in logs, keep structure for debugging | 0.7 |
| `analytics_pseudonymized` | Replace PII with consistent fakes for analytics | 0.5 |
| `customer_support_shareable` | Redact sensitive PII, keep names/locations for context | 0.5 |
| `strict_compliance_export` | Maximum redaction for GDPR/HIPAA/CCPA compliance | 0.3 |
| `dev_demo_readable` | Replace PII with realistic Faker data for demos | 0.5 |

## CI/CD

### Exit code

```bash
redactai scan ./data --ci  # exit 0 = clean, exit 1 = PII found
```

### GitHub Actions

```yaml
- uses: actions/setup-python@v5
  with:
    python-version: "3.12"
- run: pip install redactai && python -m spacy download en_core_web_sm
- run: redactai scan ./data --ci
```

### pre-commit

```yaml
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/yourorg/redactai
    rev: v0.1.0
    hooks:
      - id: redactai-scan
```

## Config (.redactai.yml)

Generate a starter config:

```bash
redactai init
```

Minimal example:

```yaml
profile: llm_guardrail
threshold: 0.4
entities:
  - PERSON
  - EMAIL_ADDRESS
  - CREDIT_CARD

operators:
  PERSON:
    type: faker
    locale: en_US
  EMAIL_ADDRESS:
    type: redact

allow_list:
  - "Acme Corp"

files:
  include:
    - "**/*.csv"
    - "**/*.txt"
  exclude:
    - "**/node_modules/**"
  output_dir: ./clean
```

## Hooks

Three hook layers fire on events: `pre_scan`, `on_pii_detected`, `post_clean`, `on_error`.

### Shell hooks (in .redactai.yml)

```yaml
hooks:
  post_clean:
    - shell: "echo 'Cleaned {{file}} -> {{output_file}}'"
  on_pii_detected:
    - shell: "notify-send 'PII found: {{entity_count}} entities in {{file}}'"
```

### Python plugins

```python
from redactai.hooks import on_pii_detected, HookEvent

@on_pii_detected
def alert(event: HookEvent):
    print(f"Found {event.entity_count} entities in {event.file}")
```

### Webhooks

```yaml
hooks:
  on_pii_detected:
    - url: "https://hooks.slack.com/services/..."
```

## MCP Server

Expose RedactAI as tools for Claude Desktop or other AI agents:

```bash
pip install redactai[mcp]
redactai mcp  # starts stdio transport
```

Add to Claude Desktop config (`claude_desktop_config.json`):

```json
{
  "mcpServers": {
    "redactai": {
      "command": "redactai",
      "args": ["mcp"]
    }
  }
}
```

## API Server

```bash
# Start as background daemon (auto-starts on first CLI call)
redactai server start

# Or run in foreground
redactai server start --foreground

# Manage
redactai server status
redactai server restart
redactai server stop
```

The daemon exposes a REST API at `http://localhost:8000` with endpoints:
- `POST /analyze` -- detect PII entities
- `POST /anonymize` -- anonymize text
- `POST /upload` -- process files (multipart)
- `GET /health` -- health check

## Persistence

Tokens and the audit log are stored in Postgres when `REDACTAI_DATABASE_URL` is set.
Without it, both fall back to in-process memory (convenient for local dev and tests,
but lost on restart and not safe across multiple instances).

```bash
# Supabase pooled connection (recommended for FastAPI)
export REDACTAI_DATABASE_URL="postgresql://postgres.PROJECT:PASSWORD@REGION.pooler.supabase.com:6543/postgres"
```

Schema is applied automatically on startup via `CREATE TABLE IF NOT EXISTS`.
Raw tokens are never stored — only SHA-256 hashes and the first 12-character
prefix. Revoked tokens are retained with a `revoked_at` timestamp for audit purposes.

### Testing

The default test suite runs entirely in-memory (no Postgres required).
Live-database smoke tests are marked `@pytest.mark.postgres` and auto-skip
unless `REDACTAI_DATABASE_URL` is set:

```bash
# Default — in-memory only
pytest

# Include Postgres smoke tests (requires live DB)
set -a; source .env; set +a
pytest -m postgres
```

## File Types

Supported: `.txt`, `.csv`, `.pdf`, `.docx`, `.png`, `.jpg`, `.jpeg`, `.bmp`, `.tiff`, `.json`

## Image Redaction

Redact PII from images using OCR:

```bash
# Single image
redactai redact-image screenshot.png -o screenshot.redacted.png

# Batch directory
redactai redact-image ./screenshots -o ./screenshots.redacted

# Custom fill color
redactai redact-image photo.jpg --fill "255,192,203"
```

## Structured Data

Anonymize PII in CSV and JSON files with column-aware detection:

```bash
# CSV
redactai structured data.csv -o data.clean.csv

# JSON
redactai structured data.json -o data.clean.json

# Custom strategy
redactai structured data.csv --strategy highest_confidence
```

## Pseudonymization

Consistent fake↔real mappings across files and sessions. Same input → same output always.

```bash
# Pseudonymize with deterministic seed
redactai pseudonymize data.txt --seed "project-alpha" --store mappings.json

# Restore originals
redactai pseudonymize data.pseudonymized.txt --restore --store mappings.json

# Show mapping stats
redactai pseudonymize data.txt --show-mapping --seed "project-alpha"
```

## Multi-Language Support

20+ languages with dedicated spaCy models:

```bash
# List all supported languages
redactai languages

# Use a specific language
redactai clean document.txt --language de   # German
redactai clean document.txt --language ja   # Japanese
redactai clean document.txt --language zh   # Chinese
```

## Custom NER Recognizers

Plug in Transformers, GLiNER, or Flair models for domain-specific detection:

```bash
# Add a Transformers recognizer
redactai add-recognizer transformers --model obi/deid_roberta_i2b2 --threshold 0.5

# Add GLiNER zero-shot recognizer
redactai add-recognizer gliner --model urchade/gliner_medium-v2.1 --labels "PERSON,EMAIL,PHONE"

# Add Flair recognizer
redactai add-recognizer flair --model ner-multi
```

## PDF Annotation

Highlight PII in PDFs without destroying the original. Perfect for legal review and audit trails.

```bash
# Annotate with highlights
redactai annotate-pdf document.pdf -o document.annotated.pdf

# Use underline instead of highlight
redactai annotate-pdf document.pdf --type underline --color "0.0,0.0,1.0"

# Generate a PII report (JSON, CSV, or text)
redactai annotate-pdf document.pdf --report --report-format json
```

## Evaluation

Benchmark detection quality against ground truth labels. Critical for audit evidence.

```bash
# Run evaluation against ground truth
redactai evaluate ground_truth.json -o report --format both

# Custom threshold and entities
redactai evaluate ground_truth.json --threshold 0.5 --entities PERSON,EMAIL_ADDRESS
```

**Ground truth format (`ground_truth.json`):**
```json
[
  {
    "text": "My name is John Smith and email is john@example.com",
    "entities": [
      {"entity_type": "PERSON", "start": 11, "end": 21},
      {"entity_type": "EMAIL_ADDRESS", "start": 39, "end": 54}
    ]
  }
]
```

## License

Apache 2.0 — see [LICENSE](LICENSE).

## Decision Trace

Explain exactly why each PII entity was detected — perfect for audit compliance and debugging.

```bash
# Show detailed decision trace
redactai trace --text "My name is John Smith and email is john@example.com"

# Trace from file
redactai trace --file document.txt --format json -o trace.json

# Show as markdown
redactai trace --file document.txt --format markdown
```

## DICOM Medical Redaction

HIPAA-compliant de-identification of medical images. Redacts both pixel text (OCR) and metadata tags.

```bash
# Single DICOM file
redactai redact-dicom scan.dcm -o scan.redacted.dcm

# Batch directory
redactai redact-dicom ./dicom_folder -o ./dicom_redacted

# Clean pixels only, keep metadata
redactai redact-dicom scan.dcm --no-clean-metadata
```

## K-Anonymity

Statistical anonymization guarantees — each record is indistinguishable from at least k-1 others.

```bash
# Apply k-anonymity (k=5)
redactai k-anonymity data.csv age,zip,gender -k 5 -o data.anonymous.csv

# With l-diversity check
redactai k-anonymity data.csv age,zip -k 5 --sensitive disease --l 3
```

## Streaming Processing

Real-time PII masking for logs, telemetry, and data pipelines.

```bash
# Process log file
redactai stream app.log -o app.masked.log

# Process stdin (pipe)
tail -f /var/log/app.log | redactai stream

# Custom entities
redactai stream app.log --entities EMAIL_ADDRESS,IP_ADDRESS
```

## Synthetic Data Generation

Generate realistic but fake datasets that preserve statistical patterns without exposing real PII.

```bash
# Generate synthetic CSV from real data
redactai synthetic real_data.csv -o synthetic.csv --num 1000

# Generate synthetic text
redactai synthetic "My name is John Smith, email john@test.com" -o synthetic.json

# Reproducible with seed
redactai synthetic data.csv --seed 42 --locale en_GB
```
