Read the latest Real Python tutorials
Project description
Fork of Presidio-research, modifying some utility functions
This package features data-science related tasks for developing new recognizers for Presidio. It is used for the evaluation of the entire system, as well as for evaluating specific PII recognizers or PII detection models. In addition, it contains a fake data generator which creates fake sentences based on templates and fake PII.
Who should use it?
- Anyone interested in developing or evaluating PII detection models, an existing Presidio instance or a Presidio PII recognizer.
- Anyone interested in generating new data based on previous datasets or sentence templates (e.g. to increase the coverage of entity values) for Named Entity Recognition models.
Getting started
To install the package:
- Clone the repo
- Install all dependencies, preferably in a virtual environment:
# Create conda env (optional)
conda create --name presidio python=3.9
conda activate presidio
# Install package+dependencies
pip install -r requirements.txt
python setup.py install
# Download a spaCy model used by presidio-analyzer
python -m spacy download en_core_web_lg
# Verify installation
pytest
Note that some dependencies (such as Flair and Stanza) are not automatically installed to reduce installation complexity.
What's in this package?
- Fake data generator for PII recognizers and NER models
- Data representation layer for data generation, modeling and analysis
- Multiple Model/Recognizer evaluation files (e.g. for Spacy, Flair, CRF, Presidio API, Presidio Analyzer python package, specific Presidio recognizers)
- Training and modeling code for multiple models
- Helper functions for results analysis
1. Data generation
See Data Generator README for more details.
The data generation process receives a file with templates, e.g. My name is {{name}}
.
Then, it creates new synthetic sentences by sampling templates and PII values.
Furthermore, it tokenizes the data, creates tags (either IO/BIO/BILUO) and spans for the newly created samples.
- For information on data generation/augmentation, see the data generator README.
- For an example for running the generation process, see this notebook.
- For an understanding of the underlying fake PII data used, see this exploratory data analysis notebook.
Once data is generated, it could be split into train/test/validation sets while ensuring that each template only exists in one set. See this notebook for more details.
2. Data representation
In order to standardize the process, we use specific data objects that hold all the information needed for generating, analyzing, modeling and evaluating data and models. Specifically, see data_objects.py.
The standardized structure, List[InputSample]
could be translated into different formats:
- CONLL
from presidio_evaluator import InputSample
dataset = InputSample.read_dataset_json("data/synth_dataset_v2.json")
conll = InputSample.create_conll_dataset(dataset)
conll.to_csv("dataset.csv", sep="\t")
- spaCy v3
from presidio_evaluator import InputSample
dataset = InputSample.read_dataset_json("data/synth_dataset_v2.json")
InputSample.create_spacy_dataset(dataset, output_path="dataset.spacy")
- Flair
from presidio_evaluator import InputSample
dataset = InputSample.read_dataset_json("data/synth_dataset_v2.json")
flair = InputSample.create_flair_dataset(dataset)
- json
from presidio_evaluator import InputSample
dataset = InputSample.read_dataset_json("data/synth_dataset_v2.json")
InputSample.to_json(dataset, output_file="dataset_json")
3. PII models evaluation
The presidio-evaluator framework allows you to evaluate Presidio as a system, a NER model, or a specific PII recognizer for precision and recall and error-analysis.
Examples:
- Evaluate Presidio
- Evaluate spaCy models
- Evaluate Stanza models
- Evaluate CRF models
- Evaluate Flair models
4. Training PII detection models
CRF
To train a vanilla CRF on a new dataset, see this notebook. To evaluate, see this notebook.
spaCy
To train a new spaCy model, first save the dataset in a spaCy format:
# dataset is a List[InputSample]
InputSample.create_spacy_dataset(dataset ,output_path="dataset.spacy")
To evaluate, see this notebook
Flair
- To train Flair models, see this helper class or this snippet:
from presidio_evaluator.models import FlairTrainer
train_samples = "data/generated_train.json"
test_samples = "data/generated_test.json"
val_samples = "data/generated_validation.json"
trainer = FlairTrainer()
trainer.create_flair_corpus(train_samples, test_samples, val_samples)
corpus = trainer.read_corpus("")
trainer.train(corpus)
Note that the three json files are created using
InputSample.to_json
.
For more information
- Blog post on NLP approaches to data anonymization
- Conference talk about leveraging Presidio and utilizing NLP approaches for data anonymization
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Copyright notice:
Fake Name Generator identities by the Fake Name Generator are licensed under a Creative Commons Attribution-Share Alike 3.0 United States License. Fake Name Generator and the Fake Name Generator logo are trademarks of Corban Works, LLC.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file privy-presidio-utils-0.0.71.tar.gz
.
File metadata
- Download URL: privy-presidio-utils-0.0.71.tar.gz
- Upload date:
- Size: 484.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dac9dda83da7607f303ab058b10d69509bfec71cd19414d8aaa70ad89fba3ac9 |
|
MD5 | b2055da2669ad9c9f4746f3f1f75d451 |
|
BLAKE2b-256 | 66630b7b2462bf8f3b98937cf1c5e730d5731140b685bb8c7b0618bb45e134c4 |
File details
Details for the file privy_presidio_utils-0.0.71-py3-none-any.whl
.
File metadata
- Download URL: privy_presidio_utils-0.0.71-py3-none-any.whl
- Upload date:
- Size: 495.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2a675ad2e0506f72a14a2a80d1afd2c0402abb303616a931cd399d5e236b181 |
|
MD5 | dac9349ff07a2c7a2abd603c6fd2c4e2 |
|
BLAKE2b-256 | 5b92b9dd285d186a81af8a1a98dbf53235b08a00fb4fcbf40a1143e0de5f85d7 |