grootveil 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. grootveil/__init__.py +42 -0
  2. grootveil/actions/__init__.py +0 -0
  3. grootveil/actions/base.py +33 -0
  4. grootveil/actions/encrypt.py +32 -0
  5. grootveil/actions/hash.py +12 -0
  6. grootveil/actions/keep.py +6 -0
  7. grootveil/actions/mask.py +24 -0
  8. grootveil/actions/pseudonymize.py +23 -0
  9. grootveil/actions/redact.py +6 -0
  10. grootveil/actions/registry.py +27 -0
  11. grootveil/actions/replace.py +8 -0
  12. grootveil/core/__init__.py +0 -0
  13. grootveil/core/errors.py +2 -0
  14. grootveil/core/labels.py +40 -0
  15. grootveil/detectors/__init__.py +0 -0
  16. grootveil/detectors/base.py +11 -0
  17. grootveil/detectors/deny_list.py +27 -0
  18. grootveil/detectors/model.py +129 -0
  19. grootveil/detectors/rules/__init__.py +14 -0
  20. grootveil/detectors/rules/address.py +50 -0
  21. grootveil/detectors/rules/checks.py +18 -0
  22. grootveil/detectors/rules/contact.py +114 -0
  23. grootveil/detectors/rules/dates.py +127 -0
  24. grootveil/detectors/rules/detector.py +77 -0
  25. grootveil/detectors/rules/financial.py +96 -0
  26. grootveil/detectors/rules/ids/__init__.py +20 -0
  27. grootveil/detectors/rules/ids/americas.py +50 -0
  28. grootveil/detectors/rules/ids/asia_pacific.py +148 -0
  29. grootveil/detectors/rules/ids/documents.py +48 -0
  30. grootveil/detectors/rules/ids/europe.py +219 -0
  31. grootveil/detectors/rules/ids/germany.py +84 -0
  32. grootveil/detectors/rules/ids/middle_east_africa.py +85 -0
  33. grootveil/detectors/rules/rule.py +37 -0
  34. grootveil/detectors/rules/secrets.py +74 -0
  35. grootveil/models/__init__.py +0 -0
  36. grootveil/models/action_config.py +24 -0
  37. grootveil/models/finding.py +14 -0
  38. grootveil/models/item.py +14 -0
  39. grootveil/models/protected.py +21 -0
  40. grootveil/py.typed +0 -0
  41. grootveil/resolvers/__init__.py +0 -0
  42. grootveil/resolvers/address_prefix.py +64 -0
  43. grootveil/resolvers/join.py +30 -0
  44. grootveil/resolvers/overlaps.py +42 -0
  45. grootveil/resolvers/register.py +33 -0
  46. grootveil/resolvers/words.py +14 -0
  47. grootveil/services/__init__.py +0 -0
  48. grootveil/services/applier.py +26 -0
  49. grootveil/services/resolver.py +35 -0
  50. grootveil/services/vault.py +40 -0
  51. grootveil/shield.py +257 -0
  52. grootveil-2.0.0.dist-info/METADATA +198 -0
  53. grootveil-2.0.0.dist-info/RECORD +54 -0
  54. grootveil-2.0.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,198 @@
1
+ Metadata-Version: 2.5
2
+ Name: grootveil
3
+ Version: 2.0.0
4
+ Summary: Find PII (scan), change it (protect) and put it back (restore), with the GrootVeil fine-tuned model and rules.
5
+ Requires-Python: <3.15,>=3.11
6
+ Requires-Dist: cryptography<51,>=50
7
+ Requires-Dist: regex>=2022.1.18
8
+ Provides-Extra: transformers
9
+ Requires-Dist: torch<3,>=2.4; extra == 'transformers'
10
+ Requires-Dist: transformers<6,>=4.48; extra == 'transformers'
11
+ Description-Content-Type: text/markdown
12
+
13
+ # grootveil
14
+
15
+ Finds, changes and restores PII (names, emails, card numbers, IDs...) in text, with the GrootVeil fine-tuned model plus
16
+ rules. A Python library: scan, protect and restore.
17
+
18
+ It has two sibling repos, `server` (the REST service around the plugin) and `web` (the internal Playground and
19
+ the Docs pages). Clone all three into one folder; each finds the others at `../<name>`.
20
+
21
+ Rules for changing this repo (folders, security, tests, releases): [CLAUDE.md](CLAUDE.md).
22
+ Every change: [CHANGELOG.md](CHANGELOG.md).
23
+
24
+ ## Install the plugin from GitLab
25
+
26
+ Every merge into master publishes the plugin to PyPI (`pip install grootveil`) and to this project's GitLab package
27
+ registry. Until the first release, or for a branch, install it straight from this GitLab repo with a token that can
28
+ read it.
29
+
30
+ **1. Get a token** with the `read_repository` scope, one of:
31
+
32
+ - a personal access token: GitLab, your avatar, **Preferences**, **Access tokens**;
33
+ - a project or group deploy token (for CI and servers): the project's **Settings**, **Repository**, **Deploy tokens**.
34
+
35
+ **2. Put it in the environment**, never in code, `requirements.txt` or a committed file:
36
+
37
+ ```bash
38
+ export GITLAB_USER=<your GitLab username, or the deploy token's username>
39
+ export GITLAB_TOKEN=<the token>
40
+ ```
41
+
42
+ **3. Install** (pick a tag or branch after `@`; `master` is the latest):
43
+
44
+ ```bash
45
+ # rules only: small, no torch
46
+ pip install "grootveil @ git+https://${GITLAB_USER}:${GITLAB_TOKEN}@gitlab.grootan.com/ezto/ai/pii/plugin.git@master"
47
+
48
+ # with our fine-tuned model: adds torch and transformers (the model's weights download on first use, not here)
49
+ pip install "grootveil[transformers] @ git+https://${GITLAB_USER}:${GITLAB_TOKEN}@gitlab.grootan.com/ezto/ai/pii/plugin.git@master"
50
+ ```
51
+
52
+ **Which one?** `[transformers]` is a pip *extra*: optional dependencies installed only when you ask for them, the
53
+ same way Presidio does it (`pip install "presidio_analyzer[transformers]"`). It installs the libraries that run the
54
+ model, not the model itself.
55
+
56
+ | Install | Adds | Size | Finds | Use |
57
+ |---|---|---|---|---|
58
+ | `grootveil` | Rules only | A few MB | Pattern PII: emails, phones, cards, IBANs, IDs, keys | `Shield(model=None)` |
59
+ | `grootveil[transformers]` | + `torch`, `transformers` | Hundreds of MB to a few GB (torch) | The above, plus names, addresses and other PII that needs context | `Shield()` |
60
+
61
+ Without the extra, `Shield()` stops with a clear message: `pip install 'grootveil[transformers]'`. The weights
62
+ (about 2.4 GB) download from Hugging Face on the first `Shield()` and are cached after that (step 4).
63
+
64
+ With uv: the same commands with `uv pip install` in place of `pip install`.
65
+
66
+ With an SSH key on GitLab, no token is needed:
67
+ `pip install "grootveil @ git+ssh://git@gitlab.grootan.com/ezto/ai/pii/plugin.git@master"`.
68
+
69
+ In GitLab CI, use the job token instead of a personal one:
70
+ `git+https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.grootan.com/ezto/ai/pii/plugin.git@master`
71
+ (the pii project must allow your project under **Settings**, **CI/CD**, **Job token permissions**).
72
+
73
+ **4. The model** downloads from Hugging Face on first use. It is private, so set `HF_TOKEN` (a read-only token with
74
+ access to the model) before the first `Shield()`. Rules only (`Shield(model=None)`) needs no `HF_TOKEN`.
75
+
76
+ ## How it works
77
+
78
+ ```mermaid
79
+ flowchart TD
80
+ A["Your text"] --> S
81
+ A --> R
82
+ A --> D
83
+ S["Long text? Cut it into pieces<br/>that overlap a little,<br/>so nothing at a cut is missed"] --> M
84
+ R["1. Rules<br/>know fixed shapes and check them:<br/>card, email, IBAN, phone, IDs"] --> C
85
+ M["1. Model<br/>reads the words around,<br/>finds names and addresses"] --> C
86
+ D["1. Deny list<br/>your words that are<br/>always secret"] --> C
87
+ C["2. Merge and clean up<br/>keep every find, then drop the bad ones:<br/>half words, unsure, allow list, double marks"] --> E
88
+ E["3. Link<br/>same name again later in the text?<br/>mark it too"] --> O
89
+ O["4. Result<br/>scan(): what was found, and where<br/>protect(): the hidden text, a key to undo,<br/>and a flag: does it point at a person?"]
90
+ O -.-> X["5. restore()<br/>put the real values back"]
91
+ ```
92
+
93
+ In plain words:
94
+
95
+ 1. **Find.** Three finders look at the text side by side, and their results are merged. The **rules** catch things
96
+ with a fixed shape and check them (a card number must pass its check digit). The **model** reads the words around
97
+ each item, so it can tell a name or an address from a normal word; a long text is first split into overlapping
98
+ chunks, so nothing at a cut is missed. Your **deny list** adds values you always want caught.
99
+ 2. **Merge and clean up.** Everything any finder found goes into one pile: a name only the model found is kept,
100
+ a card only the rules found is kept. Then the bad finds are removed:
101
+ - `Priya` inside `Priyanka`: dropped, a finding must cover whole words;
102
+ - a finding with score 0.3: dropped, it is below your threshold (0.5 by default);
103
+ - `support@yourcompany.com` on your allow list: dropped;
104
+ - the rules and the model both mark the same digits: the stronger one stays, so each character has one label.
105
+ 3. **Link (entity register).** A name or an ID found once is marked everywhere else it appears, so the person is
106
+ hidden everywhere, not just in one place:
107
+
108
+ ```text
109
+ I'm Priya Sharma. ... Call Priya Sharma tomorrow.
110
+ ^^^^^^^^^^^^ ^^^^^^^^^^^^
111
+ found by the model the model missed it: the register marks it, the same words again
112
+ ```
113
+
114
+ Step 2 only removes finds and step 3 only adds copies. Removing first means only good finds get copied.
115
+ 4. **Result.** `scan()` stops here and gives you the list. `protect()` changes each value with the action you picked.
116
+ 5. **Put back.** `pseudonymize` and `encrypt` can be undone: `restore()` uses the `ref` (sealed with your `secret`) to
117
+ put the real values back, for example into an LLM's answer.
118
+
119
+ The model runs on your own machine: the text never leaves it. Rules only (`Shield(model=None)`) skips the model in
120
+ step 1.
121
+
122
+ ## Use the plugin
123
+
124
+ ```python
125
+ from grootveil import Shield
126
+
127
+ shield = Shield(secret="...") # Shield(model=None): rules only. secret: needed for hash, encrypt, pseudonymize
128
+
129
+ # find
130
+ shield.scan("Mail Anna at anna@example.com")
131
+ # [Finding(label='PERSON_NAME', start=5, end=9, score=0.98, source='model', why='...'), Finding(label='EMAIL', ...)]
132
+
133
+ # change: every value becomes [LABEL] unless you say otherwise
134
+ shield.protect("Mail Anna at anna@example.com").text
135
+ # 'Mail [PERSON_NAME] at [EMAIL]'
136
+
137
+ # pick the de-identification for every value
138
+ shield.protect("Mail Anna at anna@example.com", action="mask").text
139
+ # 'Mail **** at ****************'
140
+
141
+ # a different one for some labels: label_actions (optional)
142
+ safe = shield.protect(
143
+ "Anna paid with 4111 1111 1111 1111",
144
+ action="pseudonymize", # names, emails, everything else
145
+ label_actions={"CARD_NUMBER": {"type": "mask", "keep_last": 4}}, # except card numbers
146
+ )
147
+ safe.text # '<PERSON_NAME_1> paid with ***************1111'
148
+
149
+ # put the real values back, e.g. into an LLM's answer to safe.text
150
+ shield.restore("Thanks <PERSON_NAME_1>!", safe.ref) # 'Thanks Anna!'
151
+ ```
152
+
153
+ An action is a name (`"mask"`) or, with settings, a dict (`{"type": "mask", "keep_last": 4}`).
154
+
155
+ | Action | What it does | Example | Settings |
156
+ |---|---|---|---|
157
+ | `replace` (default) | The label in brackets | `[EMAIL]` | `new_value` |
158
+ | `redact` | Removes the value | (nothing) | |
159
+ | `mask` | Hides characters | `***************1111` | `char`, `keep_last` |
160
+ | `pseudonymize` | Numbered placeholder, restorable | `<EMAIL_1>` | `format` |
161
+ | `hash` | Same value, same hash (needs `secret`) | `379cfb18e2024103` | `length` (8 to 64) |
162
+ | `encrypt` | AES-GCM, restorable (needs `secret`) | `YSb2UxNVXLbp...` | |
163
+ | `keep` | Leaves the value as it is | `anna@example.com` | |
164
+
165
+ Options on every `scan` and `protect` call: `labels=["EMAIL", "PHONE"]` (only these), `threshold=0.7` (the lowest
166
+ score kept, 0.5 by default), `allow_list=["support@acme.com"]` (values that are never PII),
167
+ `deny_list={"Project Falcon": "CUSTOMER_ID"}` (values that are always PII) and `extra_rules=[Rule(...)]` (rules for
168
+ that call only). A list of texts in gives a list out, and a batch shares one set of placeholders and one `ref`.
169
+
170
+ ### Errors
171
+
172
+ Wrong input stops before any text is read: `TypeError` for a wrong type (a text that is not a `str`, `labels` given
173
+ as one string), `ValueError` for a wrong value (an unknown label or action, a bad action setting, a threshold outside
174
+ 0 to 1). `restore()` raises `RefError` (a `ValueError`) for a ref that is not ours, was changed, was sealed with
175
+ another secret or has expired. No error message ever holds a text, a found value or a secret.
176
+
177
+ ### Model settings
178
+
179
+ `Shield(max_len=1024, stride=256, batch_size=16, device="auto", fp16=True)`: the window and its overlap in tokens,
180
+ windows per model call, `"cuda"`, `"mps"` or `"cpu"` (`"auto"` picks the first one there is), and half precision on
181
+ a GPU. A model trained with a 512-token limit needs `max_len=512, stride=128`. A text of any length is read whole,
182
+ in windows, so limit the size of outside text yourself: memory and time grow with the length.
183
+
184
+ ### The secret
185
+
186
+ `secret` protects `hash`, `encrypt` and every `ref`. Use a long random value (32 characters or more) from your secret
187
+ store, never a word: the plugin accepts any length and does not check its strength.
188
+
189
+ ## Checks
190
+
191
+ ```bash
192
+ uv run pytest -q && uv run ruff check . && uv run mypy src
193
+
194
+ # the plugin as a user installs it (fresh environment)
195
+ uv run --isolated --no-project --with ".[transformers]" python e2e/plugin_smoke.py
196
+ ```
197
+
198
+ CI (`.gitlab-ci.yml`) runs the lint and the tests on every merge request.
@@ -0,0 +1,54 @@
1
+ grootveil/__init__.py,sha256=MqyxQHRhLbXiG3JNbgApNvuFErXrFc3qXGQHwFBtFP8,1221
2
+ grootveil/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ grootveil/shield.py,sha256=a1RFWDgdTJGNjeVZbp8O7Fx9xWJ9wwMGvuR1VLYxykg,12941
4
+ grootveil/actions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ grootveil/actions/base.py,sha256=RjtnyVhePgY0IV2PwxDYMRTiqVkCJcRGJXnvtLFLKwY,1530
6
+ grootveil/actions/encrypt.py,sha256=_h3u4zz4lUEY0W46c0tKRV3ilUEjhzLJLd21dNrjQRg,1251
7
+ grootveil/actions/hash.py,sha256=UJHK7ZRwIzZW7Ayl-8PKj8LsPmd5zJ1ZvFtFRgbyL08,582
8
+ grootveil/actions/keep.py,sha256=bNAq6-HymEPCrlcqkx1ty7qrLZq4QomYm5_PfQ1x8BM,199
9
+ grootveil/actions/mask.py,sha256=ajVSU0wxcESvGbhRXtgGungybnty5vvNF9xjUYYPs88,903
10
+ grootveil/actions/pseudonymize.py,sha256=qZMDDm6RdEqxanKBcu87WliNt9E_goIXPuINgSjE1lQ,1262
11
+ grootveil/actions/redact.py,sha256=Jxu80AMrQSf189NL-WVhBgCL2POP0hKrsJroCiu2wrE,135
12
+ grootveil/actions/registry.py,sha256=aTt41BQy5Hupc5FYktKFZq1gj53cXXijZmtJYSkXvV0,902
13
+ grootveil/actions/replace.py,sha256=AfxuGcaUsWbdwZjPP3bDGD_cQiiAYKGYH3xoFxQm95c,374
14
+ grootveil/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
+ grootveil/core/errors.py,sha256=-UHp-yiBDzcHSr3Sg298EIDdl46_iTu5FKC7sbzroQ8,120
16
+ grootveil/core/labels.py,sha256=FwMjaAM7CvG-vIslhk5lWrEIjApLeFTIer4G0NCqo98,780
17
+ grootveil/detectors/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
+ grootveil/detectors/base.py,sha256=VRCV4IuUQwx6ky3gRIjhxClMKQCRqYCdUjZrbZhyRdc,353
19
+ grootveil/detectors/deny_list.py,sha256=OCsEGEoR6EbKmZMNkmHBktad6b0LzrBAGvXv1AxOG0g,923
20
+ grootveil/detectors/model.py,sha256=HGGatnFmyXsc9ct1LBydw30gW0zWkAa6Mw1DCL1mIkE,6320
21
+ grootveil/detectors/rules/__init__.py,sha256=JvrFliJo8ehoXfhTf36C3EXNww8WS5GazWkwBStHHNg,878
22
+ grootveil/detectors/rules/address.py,sha256=KV2H_9V5VdUKR15zIDSGm4VLEYB0xcyg68dm8EmzO2o,1740
23
+ grootveil/detectors/rules/checks.py,sha256=IHvVjtEm2pYyxGKkHUGbhr3zL0D37vrc98mJIVnL_sE,685
24
+ grootveil/detectors/rules/contact.py,sha256=jvrKFmlCKWDC2KdWCX81C2s7jGM2VrzyK4z42JR4EeQ,5413
25
+ grootveil/detectors/rules/dates.py,sha256=CMUDJRmfUqFuoRWBqLpLV9E0Ym14l0EE4QzxSWGj4ls,5683
26
+ grootveil/detectors/rules/detector.py,sha256=PKlhCTQ_MsXyzomf2DfbxeYJkbliO1xfQHuhYoVy4_8,4037
27
+ grootveil/detectors/rules/financial.py,sha256=OL6hkKoQborWs2Dnzakj5yIXFbo4oL6HdUMzIoTI_2I,4239
28
+ grootveil/detectors/rules/rule.py,sha256=POOt2j4qs0xlCzLEV0qk_BHd7FcXIN6k0X1m28ANx60,1838
29
+ grootveil/detectors/rules/secrets.py,sha256=Jf_RZDPq3rZ_zbIR6BZ0aL3RzEQckV5S945UxmGsWAk,3440
30
+ grootveil/detectors/rules/ids/__init__.py,sha256=iMDCSQk_lecmf4qGTZtwNjAlMbQvjE9x6JZjbRcgavA,934
31
+ grootveil/detectors/rules/ids/americas.py,sha256=MsS_HeP3JGG5sIfhGYqfV84AxcK1mEA4EN3uxVFIJdQ,1969
32
+ grootveil/detectors/rules/ids/asia_pacific.py,sha256=Zr-EBuhLtLscXAPLW0svuufvCpqnj1IK3mSzMO_NWgM,6169
33
+ grootveil/detectors/rules/ids/documents.py,sha256=wG1mQVZzVDhnIar_JxaHzxu_M0pvYV08lNsNaJxxPvU,2128
34
+ grootveil/detectors/rules/ids/europe.py,sha256=7Sizy9IcQQV4mN84D4hO6Yv59P3mnNeZUSDvQF7v6_A,7970
35
+ grootveil/detectors/rules/ids/germany.py,sha256=zN6f9biwwpfau0x_ojue7syjfxiIzYKv-cNuF4ift4E,3344
36
+ grootveil/detectors/rules/ids/middle_east_africa.py,sha256=GHlgKsASSgxKOCYr-YRMI2e9d8zvkiAw6mEFfDdw8Ws,3350
37
+ grootveil/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
38
+ grootveil/models/action_config.py,sha256=ed7CG5LKWYYHxHhQV-szB2YeacaG8h10jpsaMD_Csm4,878
39
+ grootveil/models/finding.py,sha256=teou8wAXc2h3aGEjbd2z0mmH0ocaq4D4HrzrDuc1VfE,414
40
+ grootveil/models/item.py,sha256=Q9AtZmf_Af6jCvr1tyWYZY6y_lmY9BvbpEmSr_yJLc0,267
41
+ grootveil/models/protected.py,sha256=7dW8lBzaVn2qvWbfg2I0bLU2imXBRAfAAfh8xeC6cdA,526
42
+ grootveil/resolvers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
43
+ grootveil/resolvers/address_prefix.py,sha256=DHk02EoHJksj_ufWdizf4K1kz4rg7yz7JBCDaZxgdZQ,3763
44
+ grootveil/resolvers/join.py,sha256=3Jqm_CYqzrrvUrzsSe0jyyHlsqrw72eJNAlD5RbnD2g,1055
45
+ grootveil/resolvers/overlaps.py,sha256=DZaRNYjTNwX4lFgaVuoNWfcVOXv24DCfYJfkF15S9Yo,1988
46
+ grootveil/resolvers/register.py,sha256=DdavU9LiTlD6cAxdz24KlbO4HXkJJJyhZS5mEBpC4NU,1676
47
+ grootveil/resolvers/words.py,sha256=AqnOxRDnLvB1MFUhz5kC6Ul6wpHSdxzcuCUm-3rFP7E,634
48
+ grootveil/services/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
49
+ grootveil/services/applier.py,sha256=Dy7nNLxrWosoP56_iFPEryr090zZfgqAdOjBphHJhuE,1157
50
+ grootveil/services/resolver.py,sha256=mkoQTusBN5BVkatM1yiWkbXSD1RZCPcvZNKPlCO3Ffc,1376
51
+ grootveil/services/vault.py,sha256=1OiBJGU_6miFDZtA95ZQF3Nyz1nloj3-WDLzF6cQEXA,1503
52
+ grootveil-2.0.0.dist-info/METADATA,sha256=GPcYSF-3tqxAeD8zrN_5RzNjKECZR71-WFmt7g4a5g0,10135
53
+ grootveil-2.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
54
+ grootveil-2.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any