File size: 6,575 Bytes
c1b2a5b
 
8e22233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1b2a5b
8e22233
048d980
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e22233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
license: cc-by-sa-4.0
language:
- en
- de
library_name: onnx
pipeline_tag: token-classification
tags:
- pii
- pii-detection
- privacy
- gdpr
- dsgvo
- anonymization
- redaction
- ner
- named-entity-recognition
- onnx
- cpu
- german
datasets:
- wikimedia/wikipedia
metrics:
- recall
- precision
---

# Tarnkappe: fast PII detection for German and English

**Find and mask personal data in German and English text for GDPR (DSGVO) work, on a plain CPU in about 1.5 ms per text.**

[![GitHub](https://img.shields.io/badge/GitHub-betterprivacy--tarnkappe-181717?logo=github)](https://github.com/ncserny/betterprivacy-tarnkappe)
[![Code: Apache 2.0](https://img.shields.io/badge/code-Apache%202.0-blue)](https://github.com/ncserny/betterprivacy-tarnkappe/blob/main/LICENSE)
[![Weights: CC BY-SA 4.0](https://img.shields.io/badge/weights-CC%20BY--SA%204.0-lightgrey)](https://creativecommons.org/licenses/by-sa/4.0/)

| Before | After |
| --- | --- |
| Hallo, ich bin Laura Becker von der Nordwind GmbH in Hamburg. Erreichbar unter 0171 3920045 oder laura.becker@nordwind.de. | Hallo, ich bin [FIRST_NAME] [LAST_NAME] von der [ORGANIZATION] in [LOCATION]. Erreichbar unter [PHONE] oder [EMAIL]. |
| Laura Becker works at Nordwind GmbH in Hamburg. Call her on +49 30 1234567 or mail laura.becker@nordwind.de. | [FIRST_NAME] [LAST_NAME] works at [ORGANIZATION] in [LOCATION]. Call her on [PHONE] or mail [EMAIL]. |

Many open PII models focus on English. Tarnkappe is trained and tested on German too, including German
company forms (GmbH, e.V., AG), German phone numbers, postcodes and IBANs.

It detects persons (split into first and last name), organisations, places, streets, postcodes, email addresses,
phone numbers, IBANs, card numbers and IP addresses.

- **Small:** a 2.5M-parameter BiGRU tagger, 5 MB as int8 ONNX, plus 25 MB of word vectors. No GPU, no PyTorch at runtime.
- **Hybrid:** the tagger handles names, organisations and places. Patterns, checksums and Google's libphonenumber handle structured data.
- **Built for scanned web pages:** a second check filters company detections, and names are shared across all pages of one person's scan.
- **Open:** code Apache 2.0, weights CC BY-SA 4.0, trained only on openly licensed data.

Made by [BetterPrivacy](https://betterprivacy.me), which uses it to find where a person's data is exposed on the web.
Code, training scripts and full results: **[github.com/ncserny/betterprivacy-tarnkappe](https://github.com/ncserny/betterprivacy-tarnkappe)**

## Quick start

```bash
pip install "git+https://github.com/ncserny/betterprivacy-tarnkappe" huggingface_hub
```

```python
from huggingface_hub import snapshot_download
from tarnkappe import Detector

det = Detector(snapshot_download("ndrcsr/tarnkappe"))   # the package also ships these files; this loads them from the Hub

text = "Laura Becker works at Nordwind GmbH in Hamburg. Call her on +49 30 1234567 or mail laura.becker@nordwind.de."
for e in det.detect(text):
    print(e["type"], e["text"])
# FIRST_NAME Laura / LAST_NAME Becker / ORGANIZATION Nordwind GmbH / LOCATION Hamburg
# PHONE +49 30 1234567 / EMAIL laura.becker@nordwind.de

print(det.redact(text))
# [FIRST_NAME] [LAST_NAME] works at [ORGANIZATION] in [LOCATION]. Call her on [PHONE] or mail [EMAIL].
```

For web pages, use `det.detect(text, profile="pages")`. For many pages about one person, use `detect_scan`
(see the GitHub README); it shares company names across pages.

## Results

**Hand-labelled real web pages** (`pages` profile). The largest test so far is 45 snippets from five people's scans,
never used for tuning: 141 organisations and 60 persons.

| | Found | Detections correct | Release bar |
| --- | --- | --- | --- |
| Persons | **100%** | **92.1%** | 95% found, 85% correct |
| Organisations | **87.9%** | **89.9%** | 80% found, 85% correct |

These pages are mostly business profiles and press releases. On a smaller test of political news and blogs,
organisations found was 71%.

**Public benchmarks** (share of items found, any overlap counts):

| Test | Found |
| --- | --- |
| Persons in English web text (UNER EWT test) | 96.2% |
| Persons in German Wikipedia/news (GermEval 2014 test) | 96.5% |
| Persons in tweets (Broad Twitter Corpus test) | 82.2% |
| Persons in social-media text (WNUT 2017 test) | 83.9% |
| All PII in Microsoft Presidio sentences (templates unseen in training) | 97.5% (96.3% of detections real) |
| European phone numbers, 40 countries (held-out synthetic test) | 99.2% |
| Companies in English Wikipedia excerpts, companies unseen in training | 82.6% |

**Speed:** 1.5 ms median per short text, about 600 texts per second on one CPU core, about 270 MB of memory.

All numbers, test conditions and what is and is not a clean test are in
[RESULTS.md](https://github.com/ncserny/betterprivacy-tarnkappe/blob/main/RESULTS.md).

## Files

| File | What |
| --- | --- |
| `tagger.int8.onnx` | Quantised tagger, used by default (5 MB) |
| `tagger.onnx` | Float32 tagger (10 MB) |
| `vectors.npz` | Frozen input word vectors (from spaCy's `en_core_web_md` / `de_core_news_md`) |
| `orgcheck.onnx`, `org_lexicon.json` | Second check on company detections |
| `institutions.txt` | Well-known organisations without a legal form (parties, media, public bodies), used on web pages |
| `thresholds.json`, `profiles.json` | Decision thresholds and the `pages` profile |
| `first_names.txt` | First names for splitting a person into first and last name |
| `metrics.json` | Test scores of the float32 tagger |

## Training data and licence

Trained on openly licensed data only: UNER English EWT (CC BY-SA 4.0), GermEval 2014, WNUT 2017 and the Broad Twitter
Corpus (CC BY 4.0), German and English Wikipedia paragraphs labelled by
[GLiNER2-PII](https://huggingface.co/fastino/gliner2-privacy-filter-PII-multi) (text CC BY-SA 4.0), company names
from CC0 sources, Presidio Research templates (MIT) and generated sentences. No real personal data was collected
for training. Hand-labelled real pages are used for testing only.

The weights are licensed **CC BY-SA 4.0** because the training data includes CC BY-SA text: if you publish a
modified version, keep the licence and the credits in `NOTICE`. The code is Apache 2.0 on GitHub.

## Limits

- English and German only.
- Small brand names with no context are sometimes missed or taken for persons.
- Weaker on informal text: about 82–84% of persons are found in tweets and social-media posts.
- It will miss things. Use it to reduce personal data, not as a guarantee of anonymisation or legal compliance.