关于
A Python package for zero-shot text anonymization using Transformer-based NER models.
人工智能与机器学习实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 0
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文Documentation · Tutorial · API reference · Contributing · Code of Conduct
ZINK (Zero-shot Ink)
Abstract
The proliferation of Large Language Models (LLMs) heightens challenges in protecting Personal Identifiable Information (PII), particularly Quasi-Identifiers (QIs), in unstructured text. QIs enable re-identification when combined and pose significant privacy risks, highlighted by their use in security verification. Current approaches face limitations: large LLMs offer flexibility for detecting diverse QIs but are often hindered by high computational costs, while traditional supervised NER models require domain-specific labeled data and fail to generalize to heterogeneous, unseen QI types. Furthermore, evaluating QI identification methods is hampered by the lack of diverse benchmarks. To address this need for evaluation resources, we present the Quasi-Identifier Benchmark (QIB), a new corpus with 1750 examples across 35 diverse QI categories (e.g., personal preferences, security answers) designed to assess model robustness against QI heterogeneity. To facilitate the application of flexible identification methods on such diverse data, we also introduce ZINK (Zero-shot INK), a Python package providing a unified framework for applying existing zero-shot NER models to QI identification and anonymization, simplifying model integration and offering configurable redaction and replacement.
Evaluation using ZINK on QIB shows strong performance, achieving an F4-score of 0.9206. This result outperforms both supervised models like BERT (0.6109) and paid large language models like GPT-4-Nano (0.9007), while remaining competitive with top-tier models like GPT-4 (0.9726). QIB and ZINK provide valuable resources enabling standardized evaluation and development of flexible, practical solutions for quasi-identifier anonymization in text.
Installation
Zink requires Python 3.11 or later. Install the package using uv or pip.
CPU Support (Recommended for most users):
uv add "zink[cpu]"
# or
pip install "zink[cpu]"
GPU Support:
pip install "zink[gpu]"
First inference: Your first redaction or replacement downloads approximately 2 GB of model files from Hugging Face if they are not already cached. Download and initialization can take several minutes. Run an example before working offline so the model is available locally. Importing Zink does not load the model. Installing bare
zinkemits a warning at import, and inference requires the CPU or GPU extra.
Quick Start
Get started with ZINK in just a few lines of code. The redact function replaces identified entities with [LABEL]_REDACTED.
import zink as zn
text = "John works as a doctor and plays football after work and drives a toyota."
labels = ("person", "profession", "sport", "car")
result = zn.redact(text, labels)
print(result.anonymized_text)
Output:
person_REDACTED works as a profession_REDACTED and plays sport_REDACTED after work and drives a car_REDACTED.
Key Features
1. Replacing Entities
Instead of simple redaction, you can replace entities with realistic synthetic data using the replace function. ZINK uses the Faker library to generate context-aware replacements.
import zink as zn
text = "John Doe dialled his mother at 992-234-3456 and then went out for a walk."
labels = ("person", "phone number", "relationship")
result = zn.replace(text, labels)
print(result.anonymized_text)
Possible Output:
Warren Buffet dialled his Uncle at 2347789287 and then went out for a walk.
2. Custom Replacements (Bring Your Own Data)
You can provide your own dictionary of replacements for specific labels. This is useful for consistent mapping or using domain-specific datasets.
import zink as zn
text = "Melissa works at Google and drives a Tesla."
labels = ("person", "company", "car")
custom_replacements = {
"person": "Alice",
"company": "OpenAI",
"car": ["Honda", "Toyota"]
}
result = zn.replace_with_my_data(text, labels, user_replacements=custom_replacements)
print(result.anonymized_text)
Possible Output:
Alice works at OpenAI and drives a Honda.
Consistency and version compatibility
ensure_consistency=True keeps the behavior of v0.7.1: repeated exact source
text shares one replacement across labels within the call. The first detected
occurrence chooses that replacement, even if later occurrences have a different
label and a different configured replacement. Upgrading to v0.7.2 keeps this
default.
New in v0.7.2: opt into label-specific reuse with
ensure_consistency=True, consistency_scope="label". A person named Jordan
and a location named Jordan can then become Alice and Venice independently.
Labels are case insensitive; source text matching is case sensitive.
For Jordan met Jordan., assuming the detections are person then location and
the replacement rules are {"person": "Alice", "location": "Venice"}:
| Version | Arguments | Result |
|---|---|---|
| v0.7.1 | ensure_consistency=True (default) |
Alice met Alice. |
| v0.7.2 | ensure_consistency=True, scope omitted or "text" |
Alice met Alice. |
| v0.7.2 | ensure_consistency=True, consistency_scope="label" |
Alice met Venice. |
| v0.7.1 / v0.7.2 | ensure_consistency=False |
Alice met Venice. |
With ensure_consistency=False, each detected occurrence chooses independently,
including repeated text within the same label. Lists and generated values may
therefore differ between mentions. In v0.7.2 the scope has no effect in this
mode. Older versions do not accept consistency_scope; passing it raises
TypeError.
import zink as zn
# Requires v0.7.2. Actual output depends on the detected labels.
result = zn.replace_with_my_data(
"Jordan met Jordan.",
categories=("person", "location"),
user_replacements={"person": "Alice", "location": "Venice"},
ensure_consistency=True,
consistency_scope="label",
)
print(result.anonymized_text)
For long text, chunking preserves whole words and adds overlapping context to
help recognize entities across boundaries. chunk_size is a soft character
target; inference windows can be longer. Both replacement APIs automatically
chunk text exceeding the target; redaction requires auto_parallel=True.
Model predictions can still miss sensitive text, so review the output.
3. Shielding LLM and API Calls
The @zink.shield decorator redacts detected spans in one argument before
passing it to a function. It restores those values in the returned string.
Review the redacted input before sharing it with an external service, since
model predictions can miss sensitive text. Treat the restored result and
local mapping file as sensitive.
import zink as zn
# This mock function simulates calling an external API (like OpenAI or Gemini)
@zn.shield(target_arg='prompt', labels=('person', 'company'))
def call_sensitive_api(prompt: str):
# Display the redacted argument passed to this mock function.
print(f"Mock API received: {prompt}")
return f"Analysis for {prompt} is complete."
# The original, sensitive text
sensitive_data = "Report for John Doe from Acme Inc."
# Call the function normally. The decorator handles anonymization and re-identification.
final_result = call_sensitive_api(prompt=sensitive_data)
print(final_result)
Possible output (placeholder IDs vary):
Mock API received: Report for person_1234_REDACTED from company_5678_REDACTED.
Analysis for Report for John Doe from Acme Inc. is complete.
4. Persistent Entity Mapping
Use numbered_entities=True to reuse placeholders for a detected label and
source text across calls and sessions. The ~/.zink/mapping.json file stores
the original detected text in plain JSON. Keep the file private.
On POSIX systems, Zink writes it atomically with owner-only permissions
(0600), including when updating an existing mapping. New mapping directories
use 0700. On Windows, restrict access through the directory's access controls.
Numbered placeholders support 9,000 distinct source strings per label across
calls and sessions. Existing IDs remain reusable at capacity; assigning a new
one raises ValueError. This limit applies to stored numbered placeholders;
detection and ordinary redaction do not have this limit.
import zink as zn
# First call: Generates IDs and saves them to default mapping file
text1 = "My name is Alice."
result1 = zn.redact(text1, categories=["person"], numbered_entities=True)
print(result1.anonymized_text)
# Output: person_1234_REDACTED ...
# Second call: Reuses the SAME ID for 'Alice'
text2 = "Alice is here again."
result2 = zn.redact(text2, categories=["person"], numbered_entities=True)
print(result2.anonymized_text)
# Output: person_1234_REDACTED ...
# Check where the mapping file is stored
print(zn.where_mapping_file())
Call zn.refresh_mapping_file() when you intend to discard the entire mapping.
For more uses, see the research text tutorial.
Docker Support
You can run zink easily using Docker. The image comes with the model pre-installed and exposes a REST API.
1. Build the Image
Run this command in the root of the repository:
docker build -t zink .
2. Run the Container
Start the API server on port 8000:
docker run --rm -p 8000:8000 zink
Tip: If port 8000 is already in use (e.g., you see a "Bind for 0.0.0.0:8000 failed" error), map it to a different port like 8001:
docker run --rm -p 8001:8000 zink
3. Use the API
Once the container is running, you can send requests to the /redact endpoint.
Example Request:
curl -X POST "http://localhost:8000/redact" \
-H "Content-Type: application/json" \
-d '{"text": "John Doe lives in New York.", "labels": ["person", "location"]}'
(Note: If you used port 8001, replace localhost:8000 with localhost:8001)
Example Response:
{
"original_text": "John Doe lives in New York.",
"anonymized_text": "person_REDACTED lives in location_REDACTED.",
"replacements": [
{
"label": "person",
"original": "John Doe",
"pseudonym": "person_REDACTED",
"start": 0,
"end": 8,
"score": 0.99
},
{
"label": "location",
"original": "New York",
"pseudonym": "location_REDACTED",
"start": 18,
"end": 26,
"score": 0.99
}
]
}
You can check the service at GET /health. The response from POST /redact
includes original text and matched values. Review the
server API documentation
before using it with sensitive inputs.
How It Works
GLiNER
GLiNER is a Named Entity Recognition (NER) model capable of identifying any entity type using a bidirectional transformer encoder (BERT-like). It provides a practical alternative to traditional NER models, which are limited to predefined entities, and Large Language Models (LLMs) that, despite their flexibility, are costly and large for resource-constrained scenarios.
NuNer
NuNerZero is a compact, zero-shot Named Entity Recognition model that leverages the robust GLiNER architecture for efficient token classification. It requires lower-cased labels and processes inputs as a concatenation of entity types and text, enabling it to detect arbitrarily long entities.
Faker & Training Data
Zink leverages both the Faker library and the extensive training data from the GLiNER model to generate realistic, synthetic replacements for sensitive information.
- GLiNER Training Data: Access to thousands of label categories from the model's training set for diverse and accurate replacements.
- Dynamic Data Generation: Faker generates context-aware values (e.g., names, addresses).
- Location Handling: Swaps countries/cities with valid alternatives.
- Date Replacement: Handles various date formats intelligently.
- Roles: Differentiates between roles (e.g., doctor vs. patient) for appropriate naming.
Related Tools
Zink combines GLiNER's inference-time entity labels with redaction, synthetic or custom replacements, and persistent numbered placeholders. PyRedactKit focuses on built-in patterns and regular expressions. scrubadub provides configurable text detectors and postprocessors. Microsoft Presidio combines extensible recognizers with anonymization operators, including a documented GLiNER integration. The related tools guide describes how these approaches differ.
Benchmarks
A reproducible QIB comparison evaluates Zink and the GLiNER model used in Presidio's official integration example with the same semantic label prompts. It reports micro-averaged F4, precision and recall, CPU latency, throughput and process memory. Scores use exact target boundaries: unmatched predictions count as false positives and missed targets as false negatives. The methodology includes setup and reproduction commands.
The following numbers are historical results on the Quasi-Identifier Benchmark (QIB).
These are the project's reported QIB results, not a head-to-head comparison with the redaction packages above. The new reproducible QIB comparison uses a separate exact-span scoring protocol and does not reproduce these historical numbers; verify their original conditions before using them as evidence in a submission.
| Model | Overall Recall | Overall Precision | Overall F4_SCORE | True Positives (TP) | False Negatives (FN) | Total Redaction Markers |
|---|---|---|---|---|---|---|
| gpt_41_nano | 0.8971 | 0.962 | 0.9007 | 1570 | 180 | 1632 |
| gpt_41 | 0.9726 | 0.9737 | 0.9726 | 1702 | 48 | 1748 |
| zink_single | 0.9 | 0.8858 | 0.8992 | 1575 | 175 | 1778 |
| zink_topic | 0.9126 | 0.6502 | 0.8914 | 1597 | 153 | 2456 |
| zink_human (run 1) | 0.9371 | 0.6597 | 0.9145 | 1640 | 110 | 2486 |
| zink_human (run 2) | 0.9446 | 0.6544 | 0.9206 | 1653 | 97 | 2526 |
| tars_topic | 0.5983 | 0.762 | 0.6059 | 1047 | 703 | 1374 |
| bert | 0.628 | 0.4255 | 0.6109 | 1099 | 651 | 2583 |
Development
To install the development version from a clone of this repository (Python 3.11+):
python -m pip install -e '.[cpu]'
python -m pip install pytest sphinx sphinx-rtd-theme
See CONTRIBUTING.md for a virtual environment setup and documentation checks.
Testing
To run the tests, navigate to the project directory and execute:
pytest
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues to suggest improvements or report bugs.
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature - Make your changes.
- Commit your changes:
git commit -m 'Add your feature' - Push to the branch:
git push origin feature/your-feature - Submit a pull request.
Citation
If you are using this package for your work/research, use the below citation:
Wadhwa, D. (2025). ZINK: Zero-shot anonymization in unstructured text. (v0.2.1). Zenodo. https://doi.org/10.5281/zenodo.15035072
License
This project is licensed under the Apache 2.0 License.
探索相关项目
加载中…