Skip to main content

A core library to anonymize PDF, Markdown, and plain text files using LLMs.

Project description

🦉🫥 PDF Anonymizer Core

This package provides the core functionality for the PDF/Text anonymizer, including text extraction, LLM-driven anonymization, and deanonymization logic. It is used by pdf-anonymizer-cli.

Installation

Install the base package with your favorite package manager:

pip install pdf-anonymizer-core

To use a specific LLM provider, you must install the corresponding extra. This helps to keep the installation lightweight by only downloading the libraries you need.

  • Google: pip install "pdf-anonymizer-core[google]"
  • Ollama: pip install "pdf-anonymizer-core[ollama]"
  • Hugging Face: pip install "pdf-anonymizer-core[huggingface]"
  • OpenRouter: pip install "pdf-anonymizer-core[openrouter]"
  • OpenAI: pip install "pdf-anonymizer-core[openai]"
  • Anthropic: pip install "pdf-anonymizer-core[anthropic]"

You can also install multiple extras at once:

pip install "pdf-anonymizer-core[google,ollama]"

Full documentation, including the Python SDK guide, CLI reference, recipes, and architecture, lives at leo-gan.github.io/anonymizer/ (especially the Recipes & Common Workflows section).

Environment Variables

The core library itself does not load .env files. Environment variables must be loaded by the application that uses this library (e.g., pdf-anonymizer-cli) or set in your shell.

  • GOOGLE_API_KEY: Required when using Google models.
  • HUGGING_FACE_TOKEN: Required when using Hugging Face models.
  • OPENROUTER_API_KEY: Required when using OpenRouter models.
  • OPENAI_API_KEY: Required when using OpenAI models.
  • ANTHROPIC_API_KEY: Required when using Anthropic models.
  • OLLAMA_HOST: Optional, defaults to http://localhost:11434 when using Ollama models.

API Usage

anonymize_file()

Anonymizes a single file and returns the anonymized text and a mapping of original entities to their placeholders.

from pdf_anonymizer_core.core import anonymize_file
from pdf_anonymizer_core.prompts import detailed

# Example of programmatic usage
text, mapping = anonymize_file(
    file_path="/path/to/file.pdf",
    prompt_template=detailed.prompt_template,
    model_name="gemini-2.5-pro"  # Can also be a new model like "google/gemini-flash-latest"
)

if text and mapping:
    print("Anonymized Text:", text)
    print("Mapping:", mapping)

deanonymize_file()

Reverts anonymization using a mapping file. The function writes the restored document and a stats file to conventional locations and returns their paths.

from pdf_anonymizer_core.utils import deanonymize_file

# Assumes you have an anonymized file and a mapping file
deanonymized_file_path, stats_file_path = deanonymize_file(
    "path/to/anonymized.md",
    "path/to/mapping.json",
)

print("Deanonymized file:", deanonymized_file_path)
print("Stats file:", stats_file_path)

Configuration

You can import default configurations and available models from the conf module.

from pdf_anonymizer_core.conf import (
    DEFAULT_MODEL_NAME,
    ModelName,
    PromptEnum,
)

print(f"Default model: {DEFAULT_MODEL_NAME}")
print(f"Available Google models: {[m.value for m in ModelName if m.provider == 'google']}")

See Also

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

pdf_anonymizer_core-0.3.3.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

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

pdf_anonymizer_core-0.3.3-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file pdf_anonymizer_core-0.3.3.tar.gz.

File metadata

  • Download URL: pdf_anonymizer_core-0.3.3.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for pdf_anonymizer_core-0.3.3.tar.gz
Algorithm Hash digest
SHA256 c387babee51aff29b549e2b78f8af2478b812d19c46631728f5b3dd72b229424
MD5 3e34b360efedaf85813d31937e80b638
BLAKE2b-256 446c83b210595c81a133fcec65b24fbaf355892c69187df504081695006e05ef

See more details on using hashes here.

File details

Details for the file pdf_anonymizer_core-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pdf_anonymizer_core-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5151d0818bd8a304f041d5a04e1683344d508091f68624517fddcfa34a298bd4
MD5 a72e45f53e302a31ee16893331be41b1
BLAKE2b-256 236c836c2c5be7e5cdc4f4559a0291c4fe340a1c6f1a2507a8d3d2d7fa732308

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