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.
Перейти к файлу
Mel 4ea903148e make sure all span types are lower case when useing faker 2022-12-26 11:11:00 +02:00
.pipelines Update publish-to-pypi.yml for Azure Pipelines 2022-11-23 11:31:46 +02:00
data data 2022-02-24 13:34:22 +02:00
docs Presidio approach to HIPAA 2020-03-19 12:54:39 +02:00
notebooks Add quickstarted 2022-11-28 11:14:05 +01:00
presidio_evaluator make sure all span types are lower case when useing faker 2022-12-26 11:11:00 +02:00
tests remove option of square brackets 2022-12-26 11:02:11 +02:00
.gitignore updates to notebooks and some evaluation logic, experiment tracking 2022-01-15 00:42:14 +02:00
CODE_OF_CONDUCT.md Initial CODE_OF_CONDUCT.md commit 2020-01-05 08:35:18 -08:00
LICENSE Initial LICENSE commit 2020-01-05 08:35:19 -08:00
NOTICE Added plotly 2022-11-14 09:47:50 +02:00
README.md Update readme 2022-11-25 16:41:06 +01:00
SECURITY.md Initial SECURITY.md commit 2020-01-05 08:35:21 -08:00
VERSION black and flake8-ing the entire code 2022-01-20 00:04:18 +02:00
pytest.ini initial commit 2020-01-06 22:59:12 +02:00
requirements.txt Add azure-ai-textanalytics==5.2.0 2022-11-23 14:17:40 +01:00
requirements_all.txt removed sklearn crfsuite 2022-02-28 11:27:03 +02:00
setup.cfg updated sample 2021-10-20 11:17:13 +03:00
setup.py renaming organizations.csv to companies_and_organizations.csv 2022-11-15 09:51:32 +01:00

README.md

Presidio-research

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

From PyPI

conda create --name presidio python=3.9
conda activate presidio
pip install presidio-evaluator

# Download a spaCy model used by presidio-analyzer
python -m spacy download en_core_web_lg

From source

To install the package:

  1. Clone the repo
  2. 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?

  1. Fake data generator for PII recognizers and NER models
  2. Data representation layer for data generation, modeling and analysis
  3. Multiple Model/Recognizer evaluation files (e.g. for Spacy, Flair, CRF, Presidio API, Presidio Analyzer python package, specific Presidio recognizers)
  4. Training and modeling code for multiple models
  5. 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.

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:

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

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.