Metadata-Version: 2.4
Name: llm-mask
Version: 0.1.0
Summary: Mask sensitive data in documents using a local OpenAI-compatible LLM
Project-URL: Homepage, https://github.com/KodakV/llm-mask
Project-URL: Repository, https://github.com/KodakV/llm-mask
Project-URL: Issues, https://github.com/KodakV/llm-mask/issues
Project-URL: Changelog, https://github.com/KodakV/llm-mask/blob/main/CHANGELOG.md
Author-email: Vladislav Kodak <vladislavkodak@gmail.com>
License: MIT License
        
        Copyright (c) 2026
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: anonymization,llm,local-llm,masking,nlp,openai,pii,privacy,sensitive-data
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing :: Linguistic
Requires-Python: >=3.10
Requires-Dist: openai>=1.0.0
Provides-Extra: dev
Requires-Dist: mypy; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Description-Content-Type: text/markdown

# llm-mask

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
[![Tests](https://github.com/KodakV/llm-mask/actions/workflows/tests.yml/badge.svg)](https://github.com/KodakV/llm-mask/actions/workflows/tests.yml)

A Python library that masks sensitive data in documents (PII, tokens, URLs, company names, etc.) using a **local OpenAI-compatible LLM**, and restores the original text via a saved mapping — no data leaves your infrastructure.

## Installation

```bash
pip install llm-mask
```

## Requirements

A running local LLM server with an OpenAI-compatible API, e.g. [vLLM](https://github.com/vllm-project/vllm), [LM Studio](https://lmstudio.ai/), or [Ollama](https://ollama.com/).

## Quick start

```python
from llm_mask import MaskingClient

client = MaskingClient(
    base_url="http://localhost:8001/v1",   # your LLM server
    model="local-model",
    language="ru",   # "ru" or "en"
)

text = "Привет, меня зовут Иван, работаю в Apple. Email: ivan@apple.com"

# ── mask ──────────────────────────────────────────────────────────────
masked_text, mapping = client.mask(text)
# masked_text → "Привет, меня зовут <person_1>, работаю в <company_1>. Email: <email_1>"
# mapping     → {"Иван": "<person_1>", "Apple": "<company_1>", "ivan@apple.com": "<email_1>"}

# ── unmask (no LLM call) ───────────────────────────────────────────────
original = client.unmask(masked_text, mapping)
# original → original text restored exactly
```

Attribute-style access also works:

```python
result = client.mask(text)
result.masked_text
result.mapping
```

## File & directory helpers

```python
# Mask a file (nothing written to disk by default)
result = client.mask_file("document.md")

# Write masked file + mapping JSON to disk
result = client.mask_file(
    "document.md",
    save_masked=True,          # → document_masked.md
    save_mapping=True,         # → document_mapping.json
    mapping_dir="./mappings",
)

# Restore from files (no LLM)
original = client.unmask_file("document_masked.md", "document_mapping.json")

# Mask a whole directory
results = client.mask_directory(
    "./docs",
    pattern="*.md",
    overwrite_originals=False,          # writes *_masked.md next to originals
    mapping_store_path="./mappings.json",
)
```

## Configuration

| Parameter | Default | Description |
|-----------|---------|-------------|
| `base_url` | `http://localhost:8001/v1` | LLM server base URL |
| `model` | `local-model` | Model identifier |
| `api_key` | `EMPTY` | API key (ignored by most local servers) |
| `language` | `ru` | Built-in prompt language: `"ru"` or `"en"` |
| `chunk_size` | `6000` | Max characters per LLM call |
| `temperature` | `0.0` | Sampling temperature |
| `judge_model` | `None` | Optional second LLM pass to catch missed entities |

## Entity types

| Entity | Placeholder |
|--------|-------------|
| URLs / domains | `<url_1>` |
| Service names | `service_1` |
| Company / brand names | `<company_1>` |
| Person names / usernames | `<person_1>` |
| Email addresses | `<email_1>` |
| Phone numbers | `<phone_1>` |
| IP addresses | `<ip_1>` |
| Tokens / secrets / keys | `<secret_1>` |
| Numeric IDs | `<id_1>` |
| File paths | `<path_1>` |
| Project / code names | `project_1` |
| Infrastructure names | `<env_1>`, `<host_1>` |

## Mapping file format

```json
{
  "source_file": "document.md",
  "masked_at": "2026-03-05T14:22:00Z",
  "mapping": {
    "Apple": "<company_1>",
    "https://api.company.com": "<url_1>"
  }
}
```

## Development

```bash
git clone https://github.com/KodakV/llm-mask
cd llm-mask
pip install -e ".[dev]"
pytest
```

See [CONTRIBUTING.md](CONTRIBUTING.md) for contribution guidelines.

## License

MIT
