Skip to main content

carnaval

CI codecov Python Version Checked with mypy License PyPI Version PyPI Downloads CodeQL Analysis

The art of masking: concealing identity, preserving the essentials.

carnaval is a reversible Python framework for text-document anonymization. It masks sensitive entities (people, organizations, emails, phone numbers, bank identifiers, etc.) before sending them to a cloud LLM, and restores the original values in the structured response (JSON or XML) on the way back.

Status: Stable (Beta) - v0.2.3

  • License: Apache 2.0
  • Stack: Python 3.11 / 3.12 / 3.13, GLiNER (zero-shot NER), regex, AES-256-GCM, PyMuPDF
  • No external PII framework (no Presidio, no spaCy NER)
  • 184 tests passing, ~95% coverage, mypy-checked, CI on every push
  • Used internally in production at one enterprise (anonymization of supplier acknowledgments before LLM extraction). Public API may evolve until v1.0.

Installation

Standard Installation (from PyPI)

pip install carnaval

Development / Local Source Installation

# 1. Clone the repository
git clone <repo>
cd carnaval

# 2. Set up virtual environment
python -m venv .venv
source .venv/bin/activate       # Linux/macOS
# or: .\.venv\Scripts\activate  # Windows

# 3. Install in editable mode
pip install -e .

Quick Start

1. Configuration

Create and edit your .env file to set your vault encryption password:

cp .env.example .env
# Edit .env and set CARNAVAL_VAULT_PASSWORD=<32+ characters>

2. Anonymization

Anonymize a document using one of the pre-configured business profiles:

python anonymize.py inbox/my_document.txt --profile acknowledge

3. Reinjection

Restore the original sensitive data back into the LLM's response (e.g. JSON/XML structure):

python reinject.py response_llm.json --vault outbox/vault/my_document_vault.enc

7-Stage Architecture

Raw TXT --> S1 Intake
        --> S2 Preprocess (language, normalization)
        --> S3 Detect (regex + denylist + GLiNER)
        --> S4 Resolve (dedup, arbitration)
        --> S5 Mask (placeholders + encrypted vault)
        --> S6 Output (6 formats: txt/json/jsonl/xml/conll/html)

JSON/XML --> S7 Reinject --> JSON/XML with original values

Out-of-the-box Business Profiles

Profile Document Type
acknowledge Supplier order acknowledgment
invoice Invoice / professional fee note
email B2B professional email

Private profiles (real client data) in profiles_private/ (git-ignored).

Documentation

Doc Topic
docs/00_overview.md Overview, principles
docs/01_architecture_etages.md The 7 stages in detail
docs/02_install.md Installation
docs/03_deploiement_production.md Production
docs/04_configuration.md YAML config + profiles
docs/05_extension_listes.md Adding entities to mask
docs/06_extension_recognizers.md Coding a new recognizer
docs/07_securite.md Vault, password, audit
docs/08_format_entree_sortie.md Supported formats
docs/09_troubleshooting.md Common errors
docs/10_api_reference.md Python API

Tests

pytest                          # all (except slow)
pytest -m slow                  # real AI tests (downloads GLiNER ~500 MB)
pytest --cov=src/carnaval       # coverage

Examples

You can find programmatic library usage examples in the examples/ directory:

  • examples/quickstart_api.py: A simple, commented python script that walks through using the library programmatically to anonymize data and reinject original values back into simulated LLM output.

Contributing

Contributions are welcome! Please read CONTRIBUTING.md and our CODE_OF_CONDUCT.md before getting started.

  • Issues and PRs: Welcome! Please ensure no personal or client data is included in public fixtures (use fictitious entities like Acme Corp, Globex, Initech, etc.).
  • Security Policy: For reporting security vulnerabilities, please check SECURITY.md to report responsibly via email.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Metadata

Release files for carnaval 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for carnaval 0.2.3
File Size Uploaded
carnaval-0.2.3.tar.gz 1.0 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for carnaval 0.2.3
File Interpreter ABI Platform
carnaval-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 2.0 MB

Release files / carnaval-0.2.3.tar.gz

Download URL carnaval-0.2.3.tar.gz
Size 1.0 MB
Tags Source
SHA-256 checksum
How to use checksums
0ec56aaebb79ba7b5024f22763e39144535b2592bff832f3f04fe5f0cc1bbc53
BLAKE2b-256 checksum
How to use checksums
34f74b56013f7725df8eab818d4fe768e54b1eb3b33c08103ab598699a03b835
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Release files / carnaval-0.2.3-py3-none-any.whl

Download URL carnaval-0.2.3-py3-none-any.whl
Size 1.0 MB
Tags Python 3
SHA-256 checksum
How to use checksums
b9eb1253cbcfbff108c638cbc09b55de260106beb2ebf3d3afb2bcfaf55a8b06
BLAKE2b-256 checksum
How to use checksums
99476b5f1e56d2ab85542a9c3fb0509da7643aaeeef88b9d1cce8a975bff00f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page