somukandula commited on
Commit
3fee4af
·
verified ·
1 Parent(s): 83a6599

Upload folder using huggingface_hub

Browse files
README.md CHANGED
@@ -7,16 +7,20 @@ tags:
7
  - pii
8
  - token-classification
9
  - named-entity-recognition
10
- - security
 
11
  - synthetic-data
12
  pipeline_tag: token-classification
13
- library_name: maskara
 
14
  datasets:
15
- - somukandula/maskara-synthetic-pii
 
16
  metrics:
17
  - precision
18
  - recall
19
  - f1
 
20
  model-index:
21
  - name: maskara
22
  results:
@@ -24,185 +28,167 @@ model-index:
24
  type: token-classification
25
  name: PII span detection
26
  dataset:
27
- type: somukandula/maskara-synthetic-pii
28
- name: Maskara Synthetic PII
29
- split: validation
30
  metrics:
31
- - type: accuracy
32
- value: 0.997638167217761
33
- name: Token accuracy
34
  - type: precision
35
- value: 0.9712230215827338
36
  name: Span precision
37
  - type: recall
38
- value: 0.989010989010989
39
  name: Span recall
40
  - type: f1
41
- value: 0.9800362976406534
42
  name: Span F1
 
 
 
43
  ---
44
 
45
  # Maskara
46
 
47
- Maskara is a local PII span detector for privacy-preserving LLM middleware. It
48
- finds sensitive values in prompts before they are sent to cloud model providers,
49
- so a local SDK can replace them with stable fake twins and restore the original
50
- values after the model responds.
 
51
 
52
- This repository contains the first CPU-trained Maskara detector. It is a small
53
- custom token classifier, not a Transformers checkpoint. It is designed to run
54
- inside the `maskara` Python SDK alongside deterministic regex and validator
55
- detectors.
56
 
57
- ## What This Model Does
58
 
59
- The model predicts BIO token tags for common sensitive entities:
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- - `PERSON_NAME`
62
- - `EMAIL`
63
- - `PHONE`
64
  - `ADDRESS`
65
- - `LOCATION`
66
  - `CREDIT_CARD`
 
 
 
 
 
 
 
 
67
  - `SSN`
68
- - `API_KEY`
69
  - `USERNAME`
70
- - `PASSWORD`
71
-
72
- Maskara uses this model for fuzzy spans such as names, locations, addresses,
73
- and usernames. Structured and high-risk values such as credit cards, SSNs,
74
- emails, phone numbers, API keys, and passwords should still be protected first
75
- by deterministic rules and validators.
76
 
77
- ## How It Helps
78
-
79
- LLM applications often need cloud model quality, but prompts can contain names,
80
- emails, phone numbers, addresses, payment test cards, credentials, or internal
81
- identifiers. Maskara helps by keeping the privacy boundary local:
82
 
83
- 1. Detect sensitive spans on the user's machine.
84
- 2. Replace those spans with fake but plausible values.
85
- 3. Send only the protected prompt to the LLM provider.
86
- 4. Restore original values locally in the response.
87
 
88
- The model improves coverage where pure regex is brittle, especially for names
89
- and location-like text in normal prose.
 
90
 
91
- ## Training Data
92
 
93
- The model was trained on synthetic data generated by the Maskara repository:
 
 
 
94
 
95
- - Dataset: [`somukandula/maskara-synthetic-pii`](https://huggingface.co/datasets/somukandula/maskara-synthetic-pii)
96
- - Generator: `scripts/generate_synthetic_dataset.py`
97
- - Train split: 800 examples
98
- - Validation split: 120 examples
99
- - Test split: 120 examples
100
- - Locales: `en-US`, `en-IN`, `en-GB`
101
- - Domains: chat prompts, email drafts, support tickets, delivery prompts,
102
- finance prompts, JSON snippets, code/log snippets, calendar messages, and
103
- hard negatives
104
 
105
- The dataset uses fake identities, reserved example domains, and payment
106
- processor test-card-style values. It does not contain real user vault data.
107
 
108
- ## Training Procedure
 
 
109
 
110
- The checked-in model was trained locally on CPU with a simple online perceptron
111
- token classifier.
112
 
113
  ```bash
114
- python scripts/generate_synthetic_dataset.py --output-dir data/synthetic
115
- python scripts/train_token_perceptron.py \
116
- --data-dir data/synthetic \
117
- --output maskara/models/pii_token_perceptron.json \
118
- --epochs 10
 
 
 
 
 
 
 
 
119
  ```
120
 
121
- Training features include token text, lowercase form, prefixes, suffixes,
122
- token shape, neighboring tokens, title-case flags, digit flags, `@`, and dash
123
- signals.
124
-
125
  ## Evaluation
126
 
127
- Validation metrics from the local training run:
128
-
129
- | Metric | Value |
130
- |---|---:|
131
- | Token accuracy | 0.9976 |
132
- | Span precision | 0.9712 |
133
- | Span recall | 0.9890 |
134
- | Span F1 | 0.9800 |
135
-
136
- End-to-end Maskara leakage evaluation on the synthetic test split:
137
 
138
  | Metric | Value |
139
  |---|---:|
140
- | Examples | 120 |
141
- | Cloud leakage rate | 0.0 |
142
- | p95 protection latency | ~1.35 ms |
 
 
143
 
144
- These numbers are from synthetic data and should not be interpreted as
145
- production guarantees. The detector is an MVP baseline for local development.
 
 
146
 
147
  ## Usage
148
 
149
- Install the local SDK from the Maskara repository, then:
150
-
151
  ```python
152
- from maskara import Maskara
153
-
154
- maskara = Maskara()
155
-
156
- prompt = "Draft a reply to Maya Rao at maya.rao@example.org."
157
- protected, ctx = maskara.protect(prompt)
158
 
159
- # Send `protected` to your LLM provider.
160
- # Then restore the provider response locally:
161
- final = maskara.restore(protected, ctx)
162
- ```
163
 
164
- Direct model inference from this repo's artifact:
 
165
 
166
- ```python
167
- from maskara.ml import PerceptronPiiDetector
 
 
 
 
168
 
169
- detector = PerceptronPiiDetector("pii_token_perceptron.json")
170
- spans = detector.detect("Email Maya Rao at maya.rao@example.org.")
171
- print(spans)
172
  ```
173
 
174
- ## Files
175
 
176
- - `pii_token_perceptron.json`: trained perceptron weights and label list
177
- - `labels.json`: BIO labels emitted by the model
178
- - `model_metadata.json`: training, dataset, and evaluation metadata
179
- - `eval_results.json`: validation and leakage evaluation metrics
180
- - `inference.py`: minimal standalone inference helper for this artifact
181
- - `requirements.txt`: minimal runtime requirements for standalone use
182
 
183
  ## Limitations
184
 
185
- - This is a custom lightweight model, not a general-purpose NER model.
186
- - It was trained on synthetic data only.
187
- - It currently focuses on English examples.
188
- - It should be used together with deterministic detectors for structured PII.
189
- - It may miss real-world names, addresses, and secrets outside the synthetic
190
- distribution.
191
- - It should not be trained on real local vault data.
192
 
193
  ## Intended Use
194
 
195
- Use this model as part of local privacy middleware for LLM prompts. It is
196
- intended for local detection and pseudonymization workflows, not surveillance,
197
- identity resolution, or user profiling.
198
 
199
- ## Citation
200
-
201
- ```bibtex
202
- @software{maskara2026,
203
- title = {Maskara: Local PII Detection for Privacy-Preserving LLM Middleware},
204
- author = {Maskara Contributors},
205
- year = {2026},
206
- url = {https://huggingface.co/somukandula/maskara}
207
- }
208
- ```
 
7
  - pii
8
  - token-classification
9
  - named-entity-recognition
10
+ - transformers
11
+ - bert
12
  - synthetic-data
13
  pipeline_tag: token-classification
14
+ library_name: transformers
15
+ base_model: google/bert_uncased_L-2_H-128_A-2
16
  datasets:
17
+ - somukandula/maskara-extensive-pii
18
+ - ai4privacy/pii-masking-300k
19
  metrics:
20
  - precision
21
  - recall
22
  - f1
23
+ - accuracy
24
  model-index:
25
  - name: maskara
26
  results:
 
28
  type: token-classification
29
  name: PII span detection
30
  dataset:
31
+ type: somukandula/maskara-extensive-pii
32
+ name: Maskara Extensive PII
33
+ split: test
34
  metrics:
 
 
 
35
  - type: precision
36
+ value: 0.6761385753211366
37
  name: Span precision
38
  - type: recall
39
+ value: 0.6948
40
  name: Span recall
41
  - type: f1
42
+ value: 0.6853422765831524
43
  name: Span F1
44
+ - type: accuracy
45
+ value: 0.9303269147627125
46
+ name: Token accuracy
47
  ---
48
 
49
  # Maskara
50
 
51
+ Maskara is a real Hugging Face Transformers token-classification model for
52
+ local PII span detection in LLM prompts.
53
+
54
+ It is trained to identify sensitive spans before prompts are sent to cloud LLM
55
+ providers. The intended product flow is:
56
 
57
+ 1. Detect PII locally.
58
+ 2. Replace detected spans with stable fake twins.
59
+ 3. Send only the protected prompt to the LLM provider.
60
+ 4. Restore the original values locally in the response.
61
 
62
+ ## Model
63
 
64
+ - Architecture: `BertForTokenClassification`
65
+ - Base model: `google/bert_uncased_L-2_H-128_A-2`
66
+ - Framework: Hugging Face Transformers + PyTorch
67
+ - Output: BIO token tags
68
+ - Checkpoint format: `model.safetensors`
69
+
70
+ This replaces the earlier perceptron baseline with a standard ML checkpoint
71
+ that can be loaded with `AutoModelForTokenClassification`.
72
+
73
+ ## Labels
74
+
75
+ The model predicts BIO tags for:
76
 
 
 
 
77
  - `ADDRESS`
78
+ - `API_KEY`
79
  - `CREDIT_CARD`
80
+ - `DATE_OF_BIRTH`
81
+ - `DRIVER_LICENSE`
82
+ - `EMAIL`
83
+ - `IP_ADDRESS`
84
+ - `LOCATION`
85
+ - `PASSWORD`
86
+ - `PERSON_NAME`
87
+ - `PHONE`
88
  - `SSN`
 
89
  - `USERNAME`
 
 
 
 
 
 
90
 
91
+ ## Training Data
 
 
 
 
92
 
93
+ The training corpus contains 75,000 examples:
 
 
 
94
 
95
+ - 67,500 train
96
+ - 3,750 validation
97
+ - 3,750 test
98
 
99
+ The final local training run used:
100
 
101
+ - 30,000 training examples
102
+ - 1,000 validation examples
103
+ - 1,000 test examples
104
+ - 2 epochs
105
 
106
+ Sources:
 
 
 
 
 
 
 
 
107
 
108
+ - Maskara synthetic v2 prompt/data generator
109
+ - Public PII examples normalized from `ai4privacy/pii-masking-300k`
110
 
111
+ The generator covers chat prompts, email drafts, support tickets, delivery
112
+ messages, JSON snippets, code/log snippets, identity prompts, finance prompts,
113
+ calendar messages, travel/map requests, and hard negatives.
114
 
115
+ ## Training Command
 
116
 
117
  ```bash
118
+ python scripts/build_extensive_pii_dataset.py \
119
+ --output-dir data/extensive \
120
+ --synthetic-size 50000 \
121
+ --public-sample-size 50000
122
+
123
+ python scripts/train_transformer_pii_model.py \
124
+ --data-dir data/extensive \
125
+ --base-model google/bert_uncased_L-2_H-128_A-2 \
126
+ --output-dir outputs/maskara-transformer \
127
+ --epochs 2 \
128
+ --batch-size 64 \
129
+ --max-train-samples 30000 \
130
+ --max-eval-samples 1000
131
  ```
132
 
 
 
 
 
133
  ## Evaluation
134
 
135
+ Held-out test metrics from the final run:
 
 
 
 
 
 
 
 
 
136
 
137
  | Metric | Value |
138
  |---|---:|
139
+ | Span precision | 0.6761 |
140
+ | Span recall | 0.6948 |
141
+ | Span F1 | 0.6853 |
142
+ | Token accuracy | 0.9303 |
143
+ | Eval loss | 0.3250 |
144
 
145
+ These are honest first-checkpoint metrics, not inflated benchmark claims. This
146
+ model is now a real ML baseline, and the next quality step is to train longer
147
+ on the full 75K corpus or run paid HF Jobs/GPU training on the 1M+ OpenPII
148
+ datasets.
149
 
150
  ## Usage
151
 
 
 
152
  ```python
153
+ from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
 
 
 
 
 
154
 
155
+ model_id = "somukandula/maskara"
 
 
 
156
 
157
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
158
+ model = AutoModelForTokenClassification.from_pretrained(model_id)
159
 
160
+ detector = pipeline(
161
+ "token-classification",
162
+ model=model,
163
+ tokenizer=tokenizer,
164
+ aggregation_strategy="simple",
165
+ )
166
 
167
+ print(detector("Email Maya Rao at maya.rao@example.org before shipping."))
 
 
168
  ```
169
 
170
+ ## How It Helps
171
 
172
+ Maskara helps developers use cloud LLMs without casually sending raw personal
173
+ data to providers. This model handles fuzzy spans such as names, locations,
174
+ addresses, usernames, and mixed natural-language context. In the SDK, it should
175
+ be combined with deterministic detectors for structured high-risk values such
176
+ as credit cards, SSNs, emails, phone numbers, API keys, private keys, JWTs, and
177
+ connection strings.
178
 
179
  ## Limitations
180
 
181
+ - The model is trained mostly on synthetic and normalized public PII examples.
182
+ - It is English-focused.
183
+ - It is a first real checkpoint; recall is not yet production-grade.
184
+ - It should not be the only privacy layer.
185
+ - Structured PII and secrets should still be protected by deterministic
186
+ validators first.
187
+ - Do not train on real user vault data.
188
 
189
  ## Intended Use
190
 
191
+ Use this model locally inside privacy middleware, coding-agent wrappers, or LLM
192
+ SDKs where prompts are pseudonymized before outbound provider calls.
 
193
 
194
+ Do not use it for surveillance, identity resolution, or profiling.
 
 
 
 
 
 
 
 
 
config.json ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_cross_attention": false,
3
+ "architectures": [
4
+ "BertForTokenClassification"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "bos_token_id": null,
8
+ "classifier_dropout": null,
9
+ "dtype": "float32",
10
+ "eos_token_id": null,
11
+ "hidden_act": "gelu",
12
+ "hidden_dropout_prob": 0.1,
13
+ "hidden_size": 128,
14
+ "id2label": {
15
+ "0": "O",
16
+ "1": "B-ADDRESS",
17
+ "2": "I-ADDRESS",
18
+ "3": "B-API_KEY",
19
+ "4": "I-API_KEY",
20
+ "5": "B-CREDIT_CARD",
21
+ "6": "I-CREDIT_CARD",
22
+ "7": "B-DATE_OF_BIRTH",
23
+ "8": "I-DATE_OF_BIRTH",
24
+ "9": "B-DRIVER_LICENSE",
25
+ "10": "I-DRIVER_LICENSE",
26
+ "11": "B-EMAIL",
27
+ "12": "I-EMAIL",
28
+ "13": "B-IP_ADDRESS",
29
+ "14": "I-IP_ADDRESS",
30
+ "15": "B-LOCATION",
31
+ "16": "I-LOCATION",
32
+ "17": "B-PASSWORD",
33
+ "18": "I-PASSWORD",
34
+ "19": "B-PERSON_NAME",
35
+ "20": "I-PERSON_NAME",
36
+ "21": "B-PHONE",
37
+ "22": "I-PHONE",
38
+ "23": "B-SSN",
39
+ "24": "I-SSN",
40
+ "25": "B-USERNAME",
41
+ "26": "I-USERNAME"
42
+ },
43
+ "initializer_range": 0.02,
44
+ "intermediate_size": 512,
45
+ "is_decoder": false,
46
+ "label2id": {
47
+ "B-ADDRESS": 1,
48
+ "B-API_KEY": 3,
49
+ "B-CREDIT_CARD": 5,
50
+ "B-DATE_OF_BIRTH": 7,
51
+ "B-DRIVER_LICENSE": 9,
52
+ "B-EMAIL": 11,
53
+ "B-IP_ADDRESS": 13,
54
+ "B-LOCATION": 15,
55
+ "B-PASSWORD": 17,
56
+ "B-PERSON_NAME": 19,
57
+ "B-PHONE": 21,
58
+ "B-SSN": 23,
59
+ "B-USERNAME": 25,
60
+ "I-ADDRESS": 2,
61
+ "I-API_KEY": 4,
62
+ "I-CREDIT_CARD": 6,
63
+ "I-DATE_OF_BIRTH": 8,
64
+ "I-DRIVER_LICENSE": 10,
65
+ "I-EMAIL": 12,
66
+ "I-IP_ADDRESS": 14,
67
+ "I-LOCATION": 16,
68
+ "I-PASSWORD": 18,
69
+ "I-PERSON_NAME": 20,
70
+ "I-PHONE": 22,
71
+ "I-SSN": 24,
72
+ "I-USERNAME": 26,
73
+ "O": 0
74
+ },
75
+ "layer_norm_eps": 1e-12,
76
+ "max_position_embeddings": 512,
77
+ "model_type": "bert",
78
+ "num_attention_heads": 2,
79
+ "num_hidden_layers": 2,
80
+ "pad_token_id": 0,
81
+ "tie_word_embeddings": true,
82
+ "transformers_version": "5.10.2",
83
+ "type_vocab_size": 2,
84
+ "use_cache": false,
85
+ "vocab_size": 30522
86
+ }
dataset_summary.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "total": 75000,
3
+ "train": 67500,
4
+ "validation": 3750,
5
+ "test": 3750,
6
+ "labels": [
7
+ "ADDRESS",
8
+ "API_KEY",
9
+ "CREDIT_CARD",
10
+ "DATE_OF_BIRTH",
11
+ "DRIVER_LICENSE",
12
+ "EMAIL",
13
+ "IP_ADDRESS",
14
+ "LOCATION",
15
+ "PASSWORD",
16
+ "PERSON_NAME",
17
+ "PHONE",
18
+ "SSN",
19
+ "USERNAME"
20
+ ]
21
+ }
maskara_training_metadata.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "base_model": "google/bert_uncased_L-2_H-128_A-2",
3
+ "labels": [
4
+ "O",
5
+ "B-ADDRESS",
6
+ "I-ADDRESS",
7
+ "B-API_KEY",
8
+ "I-API_KEY",
9
+ "B-CREDIT_CARD",
10
+ "I-CREDIT_CARD",
11
+ "B-DATE_OF_BIRTH",
12
+ "I-DATE_OF_BIRTH",
13
+ "B-DRIVER_LICENSE",
14
+ "I-DRIVER_LICENSE",
15
+ "B-EMAIL",
16
+ "I-EMAIL",
17
+ "B-IP_ADDRESS",
18
+ "I-IP_ADDRESS",
19
+ "B-LOCATION",
20
+ "I-LOCATION",
21
+ "B-PASSWORD",
22
+ "I-PASSWORD",
23
+ "B-PERSON_NAME",
24
+ "I-PERSON_NAME",
25
+ "B-PHONE",
26
+ "I-PHONE",
27
+ "B-SSN",
28
+ "I-SSN",
29
+ "B-USERNAME",
30
+ "I-USERNAME"
31
+ ],
32
+ "train_examples": 30000,
33
+ "validation_examples": 1000,
34
+ "test_examples": 1000,
35
+ "metrics": {
36
+ "eval_loss": 0.32501307129859924,
37
+ "eval_precision": 0.6761385753211366,
38
+ "eval_recall": 0.6948,
39
+ "eval_f1": 0.6853422765831524,
40
+ "eval_accuracy": 0.9303269147627125,
41
+ "eval_runtime": 1.1923,
42
+ "eval_samples_per_second": 838.744,
43
+ "eval_steps_per_second": 13.42,
44
+ "epoch": 2.0
45
+ }
46
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:13e40e2a9eb1fd3acb8a48bc4d2f2cf1ef96d976cca1487cb7d9de0c209552ba
3
+ size 17495980
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- maskara
 
 
 
1
+ transformers
2
+ torch
3
+ safetensors
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "cls_token": "[CLS]",
4
+ "do_lower_case": true,
5
+ "is_local": false,
6
+ "local_files_only": false,
7
+ "mask_token": "[MASK]",
8
+ "model_max_length": 1000000000000000019884624838656,
9
+ "pad_token": "[PAD]",
10
+ "sep_token": "[SEP]",
11
+ "strip_accents": null,
12
+ "tokenize_chinese_chars": true,
13
+ "tokenizer_class": "BertTokenizer",
14
+ "unk_token": "[UNK]"
15
+ }
training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:43ec08ad7eef4313d7cdfe51cd602b688b733c856694e277ce686cc0fa3cc688
3
+ size 5201