bharathjanumpally commited on
Commit
f618ccc
·
verified ·
1 Parent(s): 83dc153

Improve model card, metadata, metrics, and usage example

Browse files
Files changed (2) hide show
  1. README.md +209 -62
  2. example_redaction.py +34 -0
README.md CHANGED
@@ -1,120 +1,264 @@
1
  ---
2
  language: en
3
  license: apache-2.0
 
 
 
4
  tags:
5
  - token-classification
6
  - ner
 
 
7
  - privacy
8
  - healthcare
9
  - deidentification
10
  - security
11
  - compliance
12
- pipeline_tag: token-classification
13
- library_name: transformers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  ---
15
 
16
- # PHI Span Detector (BIO NER) Synthetic
17
 
18
- This model detects **Protected Health Information (PHI)** spans in clinical-note-like text and log-like text using **BIO tagging** (token classification). It is intended to power **deterministic redaction** and **zero-trust logging guardrails**.
19
 
20
- ## PHI Types
21
 
22
- The model predicts spans for the following categories:
 
 
23
 
24
- - **NAME**
25
- - **DATE**
26
- - **AGE**
27
- - **PHONE**
28
- - **EMAIL**
29
- - **ADDRESS**
30
- - **ID** (e.g., MRN/account/record IDs)
31
- - **PROVIDER**
32
- - **FACILITY**
33
- - **LOCATION**
34
 
35
- Output is BIO-formatted per token (e.g., `B-NAME`, `I-NAME`, …).
 
 
36
 
37
- ---
 
38
 
39
- ## How it works
40
 
41
- This is a **token-classification** model trained on **synthetic** examples to keep the project openly shareable:
 
 
 
 
 
42
 
43
- 1. Synthetic clinical notes and log lines are generated using templates.
44
- 2. PHI-like fields are inserted (names, IDs, phone numbers, dates, addresses, etc.).
45
- 3. Gold labels are produced automatically as character spans and converted to BIO token labels.
46
 
47
- This produces clean supervision without using real patient data.
48
 
49
- ---
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- ## Intended Use
52
 
53
- **Appropriate uses**
54
- - PHI span detection for research prototypes
55
- - Pre-log / post-log redaction guardrails
56
- - De-identification pipelines when paired with deterministic redaction
57
 
58
- **Not intended for**
59
- - Medical diagnosis or treatment advice
60
- - Sole control for compliance (HIPAA/GDPR) decisions
61
- - High-stakes production usage without additional safeguards and evaluation
62
 
63
- **Recommended pipeline:** Detect spans deterministic redaction secondary leak-check gate.
64
 
65
- ---
66
 
67
- ## Limitations
 
 
68
 
69
- - Trained on **synthetic** text: real-world clinical documentation can include unseen formats and edge cases.
70
- - May over-redact (false positives) on numeric identifiers or location-like strings.
71
- - May miss rare PHI patterns not represented in synthetic templates.
72
 
73
- If using in a real system, evaluate on your organization’s internal test set and consider adding:
74
- - regex backstops (email/phone/date patterns)
75
- - human-in-the-loop review for flagged cases
76
- - a secondary “PHI leak checker” model
77
 
78
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  ## Usage
81
 
82
- ### 1) Transformers token-classification pipeline
 
83
  ```python
84
  from transformers import pipeline
85
 
86
  ner = pipeline(
87
  "token-classification",
88
- model="bharathja/phi-span-detector-deberta-v3",
89
- aggregation_strategy="simple"
 
 
 
 
 
90
  )
91
 
92
- text = "Patient John Smith (MRN: 001-23-4567) visited Boston Medical Center on 12/19/2025."
93
  print(ner(text))
94
  ```
95
- ### 2) Deterministic redaction (recommended)
96
 
97
- Use detected spans to redact with placeholders such as [NAME], [ID], [DATE], etc.
98
- (See companion project: PHI Guardrails.)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
- Output Schema (recommended)
101
  ```python
102
- A practical production-friendly span format:
 
 
 
 
 
 
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  [
105
  {"start": 8, "end": 18, "label": "NAME", "score": 0.97},
106
  {"start": 25, "end": 36, "label": "ID", "score": 0.94},
107
- {"start": 68, "end": 78, "label": "FACILITY", "score": 0.91},
108
- {"start": 82, "end": 92, "label": "DATE", "score": 0.89}
109
  ]
110
  ```
111
 
112
- ### Safety & Privacy
113
 
114
- This model is trained on synthetic data and is published for research and tooling purposes.
115
- Do not upload real PHI to public endpoints or demos. Use private infrastructure for real deployments.
116
- ```python
117
- Citation
 
118
  @misc{janumpally_phi_span_detector_2025,
119
  title = {PHI Span Detector (Synthetic)},
120
  author = {Bharath Kumar Reddy Janumpally},
@@ -122,5 +266,8 @@ Citation
122
  publisher = {Hugging Face},
123
  howpublished = {Model on Hugging Face}
124
  }
 
 
 
125
 
126
- ````
 
1
  ---
2
  language: en
3
  license: apache-2.0
4
+ library_name: transformers
5
+ pipeline_tag: token-classification
6
+ base_model: microsoft/deberta-v3-base
7
  tags:
8
  - token-classification
9
  - ner
10
+ - phi
11
+ - pii
12
  - privacy
13
  - healthcare
14
  - deidentification
15
  - security
16
  - compliance
17
+ - synthetic-data
18
+ - deberta-v3
19
+ widget:
20
+ - text: "Patient John Smith (MRN: 001-23-4567) visited Boston Medical Center on 12/19/2025."
21
+ - text: "Discharge summary reviewed by Dr. Emily Chen at Riverfront Clinic on 03/07/2025."
22
+ model-index:
23
+ - name: phi-span-detector-deberta-v3
24
+ results:
25
+ - task:
26
+ type: token-classification
27
+ name: Token Classification
28
+ dataset:
29
+ name: Synthetic PHI span test set
30
+ type: synthetic
31
+ metrics:
32
+ - name: Micro F1
33
+ type: f1
34
+ value: 0.6523
35
+ - name: Micro Precision
36
+ type: precision
37
+ value: 0.6657
38
+ - name: Micro Recall
39
+ type: recall
40
+ value: 0.6394
41
+ - name: Macro F1
42
+ type: f1
43
+ value: 0.6362
44
  ---
45
 
46
+ # PHI Span Detector (BIO NER) - Synthetic
47
 
48
+ `phi-span-detector-deberta-v3` is a DeBERTa v3 token-classification model for detecting Protected Health Information (PHI) spans in clinical-note-like text and log-like text using BIO tagging.
49
 
50
+ It is designed for privacy tooling workflows such as:
51
 
52
+ - deterministic redaction pipelines
53
+ - pre-log and post-log PHI guardrails
54
+ - research prototypes for de-identification
55
 
56
+ Recommended pipeline:
 
 
 
 
 
 
 
 
 
57
 
58
+ 1. detect PHI spans
59
+ 2. apply deterministic redaction
60
+ 3. run a secondary leak-check gate before downstream use
61
 
62
+ Companion model:
63
+ [`bharathjanumpally/phi-leak-checker-deberta-v3`](https://huggingface.co/bharathjanumpally/phi-leak-checker-deberta-v3)
64
 
65
+ ## Model at a glance
66
 
67
+ - Task: token classification
68
+ - Architecture: `DebertaV2ForTokenClassification`
69
+ - Base model: `microsoft/deberta-v3-base`
70
+ - Max sequence length: 512
71
+ - Labeling scheme: BIO
72
+ - Training data: synthetic text only
73
 
74
+ ## PHI label set
 
 
75
 
76
+ The model predicts the following entity families:
77
 
78
+ | Label | Meaning |
79
+ | --- | --- |
80
+ | `NAME` | patient or person names |
81
+ | `DATE` | visit dates, birth dates, service dates |
82
+ | `AGE` | age mentions that may be identifying in context |
83
+ | `PHONE` | phone and callback numbers |
84
+ | `EMAIL` | email addresses |
85
+ | `ADDRESS` | street or mailing addresses |
86
+ | `ID` | MRN, account, encounter, record, or similar identifiers |
87
+ | `PROVIDER` | clinician or provider names |
88
+ | `FACILITY` | hospitals, clinics, centers, departments |
89
+ | `LOCATION` | city, state, and other place references |
90
 
91
+ Token-level outputs use BIO labels from the model config:
92
 
93
+ `O`, `B-*`, and `I-*` across the ten PHI families above.
 
 
 
94
 
95
+ ## How the training data was built
 
 
 
96
 
97
+ This model was trained on synthetic examples to keep the project openly shareable.
98
 
99
+ High-level training recipe:
100
 
101
+ 1. Generate synthetic clinical notes and log-like text with templates.
102
+ 2. Insert PHI-like fields such as names, dates, IDs, facilities, phone numbers, and addresses.
103
+ 3. Convert gold character spans into BIO token labels for token classification.
104
 
105
+ This provides clean supervision without exposing real patient data, but it also means real-world formatting and writing styles may differ from training-time distributions.
 
 
106
 
107
+ ## Evaluation
 
 
 
108
 
109
+ The repository includes a full [`seqeval_report.txt`](./seqeval_report.txt). Key held-out results from that report are summarized below.
110
+
111
+ ### Overall metrics
112
+
113
+ | Metric | Value |
114
+ | --- | ---: |
115
+ | Micro precision | 0.6657 |
116
+ | Micro recall | 0.6394 |
117
+ | Micro F1 | 0.6523 |
118
+ | Macro precision | 0.6583 |
119
+ | Macro recall | 0.6224 |
120
+ | Macro F1 | 0.6362 |
121
+ | Weighted F1 | 0.6495 |
122
+
123
+ ### Per-label metrics
124
+
125
+ | Label | Precision | Recall | F1 | Support |
126
+ | --- | ---: | ---: | ---: | ---: |
127
+ | ADDRESS | 0.6652 | 0.6481 | 0.6565 | 233 |
128
+ | AGE | 0.6758 | 0.3834 | 0.4893 | 386 |
129
+ | DATE | 0.6553 | 0.6492 | 0.6522 | 1297 |
130
+ | EMAIL | 0.6474 | 0.6455 | 0.6465 | 347 |
131
+ | FACILITY | 0.6320 | 0.6494 | 0.6406 | 656 |
132
+ | ID | 0.6652 | 0.6519 | 0.6585 | 451 |
133
+ | LOCATION | 0.6600 | 0.6600 | 0.6600 | 350 |
134
+ | NAME | 0.7810 | 0.7802 | 0.7806 | 1001 |
135
+ | PHONE | 0.5358 | 0.5025 | 0.5186 | 595 |
136
+ | PROVIDER | 0.6652 | 0.6537 | 0.6594 | 231 |
137
+
138
+ ### Interpretation
139
+
140
+ - Strongest label in the current report: `NAME`
141
+ - Weakest labels in the current report: `PHONE` and `AGE`
142
+ - The model is usable as a PHI span detector for research and tooling, but it should be paired with deterministic rules and internal evaluation before higher-stakes deployment
143
+
144
+ ## Intended use
145
+
146
+ Appropriate uses:
147
+
148
+ - PHI span detection in research prototypes
149
+ - de-identification pipelines when paired with deterministic redaction
150
+ - zero-trust logging guardrails
151
+ - preprocessing before a secondary PHI leak checker
152
+
153
+ Not intended for:
154
+
155
+ - medical diagnosis or treatment advice
156
+ - sole control for HIPAA, GDPR, or other compliance decisions
157
+ - unsupervised high-stakes production usage without internal validation
158
+
159
+ ## Limitations and failure modes
160
+
161
+ - The model was trained on synthetic text, so real clinical documentation may include unseen abbreviations, formatting quirks, shorthand, OCR noise, and edge cases.
162
+ - Numeric strings may be over-flagged when they resemble IDs, dates, or phone numbers.
163
+ - Some rare PHI patterns may be missed if they were not well represented in the synthetic templates.
164
+ - Partial tokens and tokenizer boundary effects can require careful post-processing in downstream systems.
165
+ - Label performance is uneven; current metrics suggest extra caution around `PHONE` and `AGE`.
166
+
167
+ Recommended mitigations:
168
+
169
+ - add regex backstops for structured entities like email, phone, and date
170
+ - apply deterministic placeholder redaction after detection
171
+ - run a second PHI leak-check model before downstream release
172
+ - evaluate on an internal, policy-approved test set that matches your real document style
173
+ - keep a human-review path for ambiguous or high-risk content
174
 
175
  ## Usage
176
 
177
+ ### Transformers pipeline
178
+
179
  ```python
180
  from transformers import pipeline
181
 
182
  ner = pipeline(
183
  "token-classification",
184
+ model="bharathjanumpally/phi-span-detector-deberta-v3",
185
+ aggregation_strategy="simple",
186
+ )
187
+
188
+ text = (
189
+ "Patient John Smith (MRN: 001-23-4567) visited "
190
+ "Boston Medical Center on 12/19/2025."
191
  )
192
 
 
193
  print(ner(text))
194
  ```
 
195
 
196
+ ### AutoModel and AutoTokenizer
197
+
198
+ ```python
199
+ from transformers import AutoModelForTokenClassification, AutoTokenizer, pipeline
200
+
201
+ model_id = "bharathjanumpally/phi-span-detector-deberta-v3"
202
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
203
+ model = AutoModelForTokenClassification.from_pretrained(model_id)
204
+
205
+ ner = pipeline(
206
+ "token-classification",
207
+ model=model,
208
+ tokenizer=tokenizer,
209
+ aggregation_strategy="simple",
210
+ )
211
+
212
+ print(ner("Call Jane Doe at 617-555-0182 before 04/14/2025."))
213
+ ```
214
+
215
+ ### Deterministic redaction example
216
 
 
217
  ```python
218
+ from transformers import pipeline
219
+
220
+ ner = pipeline(
221
+ "token-classification",
222
+ model="bharathjanumpally/phi-span-detector-deberta-v3",
223
+ aggregation_strategy="simple",
224
+ )
225
 
226
+ text = (
227
+ "Patient John Smith (MRN: 001-23-4567) visited "
228
+ "Boston Medical Center on 12/19/2025."
229
+ )
230
+
231
+ spans = ner(text)
232
+
233
+ redacted = text
234
+ for item in sorted(spans, key=lambda x: x["start"], reverse=True):
235
+ label = item["entity_group"]
236
+ redacted = redacted[: item["start"]] + f"[{label}]" + redacted[item["end"] :]
237
+
238
+ print(spans)
239
+ print(redacted)
240
+ ```
241
+
242
+ ### Example output schema
243
+
244
+ For downstream systems, a practical span schema is:
245
+
246
+ ```json
247
  [
248
  {"start": 8, "end": 18, "label": "NAME", "score": 0.97},
249
  {"start": 25, "end": 36, "label": "ID", "score": 0.94},
250
+ {"start": 45, "end": 66, "label": "FACILITY", "score": 0.91},
251
+ {"start": 70, "end": 80, "label": "DATE", "score": 0.89}
252
  ]
253
  ```
254
 
255
+ ## Safety and privacy
256
 
257
+ This model was trained on synthetic data and is published for research and tooling purposes. Do not send real PHI to public demos or public inference endpoints. Use private infrastructure, access controls, and organization-approved evaluation workflows for real deployments.
258
+
259
+ ## Citation
260
+
261
+ ```bibtex
262
  @misc{janumpally_phi_span_detector_2025,
263
  title = {PHI Span Detector (Synthetic)},
264
  author = {Bharath Kumar Reddy Janumpally},
 
266
  publisher = {Hugging Face},
267
  howpublished = {Model on Hugging Face}
268
  }
269
+ ```
270
+
271
+ ## Contact
272
 
273
+ If you use this model in a serious workflow, validate it against your own internal test cases and document the operating policy around false positives, false negatives, and escalation paths.
example_redaction.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+
4
+ MODEL_ID = "bharathjanumpally/phi-span-detector-deberta-v3"
5
+
6
+
7
+ def redact_text(text: str) -> tuple[list[dict], str]:
8
+ ner = pipeline(
9
+ "token-classification",
10
+ model=MODEL_ID,
11
+ aggregation_strategy="simple",
12
+ )
13
+ spans = ner(text)
14
+
15
+ redacted = text
16
+ for item in sorted(spans, key=lambda x: x["start"], reverse=True):
17
+ label = item["entity_group"]
18
+ redacted = redacted[: item["start"]] + f"[{label}]" + redacted[item["end"] :]
19
+
20
+ return spans, redacted
21
+
22
+
23
+ if __name__ == "__main__":
24
+ sample = (
25
+ "Patient John Smith (MRN: 001-23-4567) visited "
26
+ "Boston Medical Center on 12/19/2025."
27
+ )
28
+ spans, redacted = redact_text(sample)
29
+ print("Spans:")
30
+ for span in spans:
31
+ print(span)
32
+ print()
33
+ print("Redacted:")
34
+ print(redacted)