Instructions to use EasyOctopus/german-support-pii-model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use EasyOctopus/german-support-pii-model with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="EasyOctopus/german-support-pii-model")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("EasyOctopus/german-support-pii-model") model = AutoModelForTokenClassification.from_pretrained("EasyOctopus/german-support-pii-model", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Release German support experiment and held-out evaluation
Browse files- LICENSE +201 -0
- NOTICE +5 -0
- NOTICE-nobody-pii.txt +16 -0
- README.md +74 -0
- SHA256SUMS +26 -0
- classifier.py +80 -0
- config.json +90 -0
- eval_results.json +0 -0
- examples.json +45 -0
- infer.py +44 -0
- model.safetensors +3 -0
- provenance.json +467 -0
- runtime.py +60 -0
- special_tokens_map.json +37 -0
- tokenizer.json +0 -0
- tokenizer_config.json +60 -0
- training/initial-provenance.json +26 -0
- training/initial/config.json +90 -0
- training/initial/model.safetensors +3 -0
- training/initial/special_tokens_map.json +7 -0
- training/initial/tokenizer.json +0 -0
- training/initial/tokenizer_config.json +56 -0
- training/initial/vocab.txt +0 -0
- training/recipe.json +69 -0
- training/retrain.py +156 -0
- training/scoring.py +89 -0
- vocab.txt +0 -0
LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Apache License
|
| 2 |
+
Version 2.0, January 2004
|
| 3 |
+
http://www.apache.org/licenses/
|
| 4 |
+
|
| 5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 6 |
+
|
| 7 |
+
1. Definitions.
|
| 8 |
+
|
| 9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 11 |
+
|
| 12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 13 |
+
the copyright owner that is granting the License.
|
| 14 |
+
|
| 15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 16 |
+
other entities that control, are controlled by, or are under common
|
| 17 |
+
control with that entity. For the purposes of this definition,
|
| 18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 19 |
+
direction or management of such entity, whether by contract or
|
| 20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 22 |
+
|
| 23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 24 |
+
exercising permissions granted by this License.
|
| 25 |
+
|
| 26 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 27 |
+
including but not limited to software source code, documentation
|
| 28 |
+
source, and configuration files.
|
| 29 |
+
|
| 30 |
+
"Object" form shall mean any form resulting from mechanical
|
| 31 |
+
transformation or translation of a Source form, including but
|
| 32 |
+
not limited to compiled object code, generated documentation,
|
| 33 |
+
and conversions to other media types.
|
| 34 |
+
|
| 35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 36 |
+
Object form, made available under the License, as indicated by a
|
| 37 |
+
copyright notice that is included in or attached to the work
|
| 38 |
+
(an example is provided in the Appendix below).
|
| 39 |
+
|
| 40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 41 |
+
form, that is based on (or derived from) the Work and for which the
|
| 42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 44 |
+
of this License, Derivative Works shall not include works that remain
|
| 45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 46 |
+
the Work and Derivative Works thereof.
|
| 47 |
+
|
| 48 |
+
"Contribution" shall mean any work of authorship, including
|
| 49 |
+
the original version of the Work and any modifications or additions
|
| 50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 54 |
+
means any form of electronic, verbal, or written communication sent
|
| 55 |
+
to the Licensor or its representatives, including but not limited to
|
| 56 |
+
communication on electronic mailing lists, source code control systems,
|
| 57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 59 |
+
excluding communication that is conspicuously marked or otherwise
|
| 60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 61 |
+
|
| 62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 64 |
+
subsequently incorporated within the Work.
|
| 65 |
+
|
| 66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 71 |
+
Work and such Derivative Works in Source or Object form.
|
| 72 |
+
|
| 73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 76 |
+
(except as stated in this section) patent license to make, have made,
|
| 77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 78 |
+
where such license applies only to those patent claims licensable
|
| 79 |
+
by such Contributor that are necessarily infringed by their
|
| 80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 82 |
+
institute patent litigation against any entity (including a
|
| 83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 84 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 85 |
+
or contributory patent infringement, then any patent licenses
|
| 86 |
+
granted to You under this License for that Work shall terminate
|
| 87 |
+
as of the date such litigation is filed.
|
| 88 |
+
|
| 89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 90 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 91 |
+
modifications, and in Source or Object form, provided that You
|
| 92 |
+
meet the following conditions:
|
| 93 |
+
|
| 94 |
+
(a) You must give any other recipients of the Work or
|
| 95 |
+
Derivative Works a copy of this License; and
|
| 96 |
+
|
| 97 |
+
(b) You must cause any modified files to carry prominent notices
|
| 98 |
+
stating that You changed the files; and
|
| 99 |
+
|
| 100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 101 |
+
that You distribute, all copyright, patent, trademark, and
|
| 102 |
+
attribution notices from the Source form of the Work,
|
| 103 |
+
excluding those notices that do not pertain to any part of
|
| 104 |
+
the Derivative Works; and
|
| 105 |
+
|
| 106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 107 |
+
distribution, then any Derivative Works that You distribute must
|
| 108 |
+
include a readable copy of the attribution notices contained
|
| 109 |
+
within such NOTICE file, excluding those notices that do not
|
| 110 |
+
pertain to any part of the Derivative Works, in at least one
|
| 111 |
+
of the following places: within a NOTICE text file distributed
|
| 112 |
+
as part of the Derivative Works; within the Source form or
|
| 113 |
+
documentation, if provided along with the Derivative Works; or,
|
| 114 |
+
within a display generated by the Derivative Works, if and
|
| 115 |
+
wherever such third-party notices normally appear. The contents
|
| 116 |
+
of the NOTICE file are for informational purposes only and
|
| 117 |
+
do not modify the License. You may add Your own attribution
|
| 118 |
+
notices within Derivative Works that You distribute, alongside
|
| 119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 120 |
+
that such additional attribution notices cannot be construed
|
| 121 |
+
as modifying the License.
|
| 122 |
+
|
| 123 |
+
You may add Your own copyright statement to Your modifications and
|
| 124 |
+
may provide additional or different license terms and conditions
|
| 125 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 126 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 127 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 128 |
+
the conditions stated in this License.
|
| 129 |
+
|
| 130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 132 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 133 |
+
this License, without any additional terms or conditions.
|
| 134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 135 |
+
the terms of any separate license agreement you may have executed
|
| 136 |
+
with Licensor regarding such Contributions.
|
| 137 |
+
|
| 138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 140 |
+
except as required for reasonable and customary use in describing the
|
| 141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 142 |
+
|
| 143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 144 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 147 |
+
implied, including, without limitation, any warranties or conditions
|
| 148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 150 |
+
appropriateness of using or redistributing the Work and assume any
|
| 151 |
+
risks associated with Your exercise of permissions under this License.
|
| 152 |
+
|
| 153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 154 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 155 |
+
unless required by applicable law (such as deliberate and grossly
|
| 156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 157 |
+
liable to You for damages, including any direct, indirect, special,
|
| 158 |
+
incidental, or consequential damages of any character arising as a
|
| 159 |
+
result of this License or out of the use or inability to use the
|
| 160 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 161 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 162 |
+
other commercial damages or losses), even if such Contributor
|
| 163 |
+
has been advised of the possibility of such damages.
|
| 164 |
+
|
| 165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 168 |
+
or other liability obligations and/or rights consistent with this
|
| 169 |
+
License. However, in accepting such obligations, You may act only
|
| 170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 171 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 172 |
+
defend, and hold each Contributor harmless for any liability
|
| 173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 174 |
+
of your accepting any such warranty or additional liability.
|
| 175 |
+
|
| 176 |
+
END OF TERMS AND CONDITIONS
|
| 177 |
+
|
| 178 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 179 |
+
|
| 180 |
+
To apply the Apache License to your work, attach the following
|
| 181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 182 |
+
replaced with your own identifying information. (Don't include
|
| 183 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 184 |
+
comment syntax for the file format. We also recommend that a
|
| 185 |
+
file or class name and description of purpose be included on the
|
| 186 |
+
same "printed page" as the copyright notice for easier
|
| 187 |
+
identification within third-party archives.
|
| 188 |
+
|
| 189 |
+
Copyright [yyyy] [name of copyright owner]
|
| 190 |
+
|
| 191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 192 |
+
you may not use this file except in compliance with the License.
|
| 193 |
+
You may obtain a copy of the License at
|
| 194 |
+
|
| 195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 196 |
+
|
| 197 |
+
Unless required by applicable law or agreed to in writing, software
|
| 198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 200 |
+
See the License for the specific language governing permissions and
|
| 201 |
+
limitations under the License.
|
NOTICE
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
German support model lab, 2026. Experimental release, Apache License 2.0.
|
| 2 |
+
Scenario templates, generated messages, evaluation and example code are original synthetic work.
|
| 3 |
+
Derived from distilbert/distilbert-base-german-cased (Apache-2.0), revision 6f96562de9886779ce7d48810dff8f5c4afb6d0b.
|
| 4 |
+
Modified through supervised token classification fine-tuning. See provenance.json for checkpoints and settings.
|
| 5 |
+
Supplemental replay / prior classifier training uses naeyn/nobody-pii-synth-de (Apache-2.0), revision f78fcbcad638f5e7f69bfe55a8ebd508e32c3731. See NOTICE-nobody-pii.txt. Prior original support augmentation was Apache-2.0.
|
NOTICE-nobody-pii.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Nobody PII Synthetic dataset
|
| 2 |
+
Copyright (c) 2026 naeyn. Apache-2.0 (see LICENSE).
|
| 3 |
+
|
| 4 |
+
This repository contains procedurally generated rows and does not contain
|
| 5 |
+
records copied from ai4privacy/pii-masking-400k or
|
| 6 |
+
ai4privacy/pii-masking-openpii-1m.
|
| 7 |
+
|
| 8 |
+
The generation process used Faker locale providers. Faker is MIT-licensed:
|
| 9 |
+
https://github.com/joke2k/faker
|
| 10 |
+
|
| 11 |
+
The companion model has a separate training-data attribution for 25,909
|
| 12 |
+
German rows from "OpenPII 1M" (ai4privacy/pii-masking-openpii-1m),
|
| 13 |
+
(c) Ai4Privacy / Ai Suisse SA, used under CC-BY-4.0:
|
| 14 |
+
https://creativecommons.org/licenses/by/4.0/
|
| 15 |
+
Required attribution: Ai4Privacy / Ai Suisse SA. No OpenPII rows are included
|
| 16 |
+
in this dataset repository.
|
README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- de
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
library_name: transformers
|
| 6 |
+
pipeline_tag: token-classification
|
| 7 |
+
base_model: distilbert/distilbert-base-german-cased
|
| 8 |
+
datasets:
|
| 9 |
+
- EasyOctopus/german-support-pii-data
|
| 10 |
+
tags:
|
| 11 |
+
- experimental
|
| 12 |
+
- german-support
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# German support PII experimental checkpoint
|
| 16 |
+
|
| 17 |
+
Detect personally identifiable information (PII) with beginning, inside and outside token labels. The example returns Unicode offsets and redacted text. It processes overflow in non-overlapping 256-token windows. Boundary context and missed spans limit privacy guarantees.
|
| 18 |
+
|
| 19 |
+
Selected variant **scenarios-hard** by the frozen validation rule. All parameters were updated on one NVIDIA A100-SXM4-80GB. See provenance.json for initialization, label policy, data revisions, seed and controlled alternatives.
|
| 20 |
+
|
| 21 |
+
| Candidate | Complete spans | Collateral characters | Flagged negatives |
|
| 22 |
+
|---|---:|---:|---:|
|
| 23 |
+
| replay | 214/256 | 170/10,805 (1.57%) | 16/48 |
|
| 24 |
+
| scenarios | 255/256 | 292/10,805 (2.70%) | 17/48 |
|
| 25 |
+
| scenarios-hard | 255/256 | 55/10,805 (0.51%) | 2/48 |
|
| 26 |
+
| prior | 214/256 | 178/10,805 (1.65%) | 16/48 |
|
| 27 |
+
| live-adapted | 178/256 | 245/10,805 (2.27%) | 46/48 |
|
| 28 |
+
| pilot-union | 240/256 | 275/10,805 (2.55%) | 46/48 |
|
| 29 |
+
|
| 30 |
+
Replay continues training on previously seen synthetic data. Scenarios adds novel positive examples. Scenarios-hard also swaps in hard negatives. Prior is the unmodified starting checkpoint. Live-adapted combines a CPU model, rules and a small tagger. Pilot-union combines that baseline with the starting checkpoint.
|
| 31 |
+
|
| 32 |
+
On the earlier independent suite, the selected checkpoint fully protected 94/108 spans, compared with 86 for its initializer. On the earlier confirmation suite, the selected checkpoint fully protected 64/70 spans, compared with 63 for its initializer.
|
| 33 |
+
|
| 34 |
+
The training messages and newly generated test messages are fictional. Repeated slot draws within a template are correlated. Split families are disjoint but use the same authoring process. These results do not measure protection or dictation quality on customer messages.
|
| 35 |
+
|
| 36 |
+
## Download and use
|
| 37 |
+
|
| 38 |
+
Fine-tuned by **Mahir Isikli (EasyOctopus)**. The companion dataset is [EasyOctopus/german-support-pii-data](https://huggingface.co/datasets/EasyOctopus/german-support-pii-data).
|
| 39 |
+
|
| 40 |
+
Load the tokenizer and classification head with standard Transformers. The model runs locally on a CPU or GPU.
|
| 41 |
+
|
| 42 |
+
```python
|
| 43 |
+
from transformers import AutoModelForTokenClassification, AutoTokenizer
|
| 44 |
+
|
| 45 |
+
repo_id = "EasyOctopus/german-support-pii-model"
|
| 46 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 47 |
+
model = AutoModelForTokenClassification.from_pretrained(repo_id).eval()
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
For the exact decoder used in the evaluation, download the repository and run the included example. This needs Python 3.11 or later, PyTorch and Transformers. It sends no message text to the Hub.
|
| 51 |
+
|
| 52 |
+
```python
|
| 53 |
+
import subprocess
|
| 54 |
+
import sys
|
| 55 |
+
from pathlib import Path
|
| 56 |
+
from huggingface_hub import snapshot_download
|
| 57 |
+
|
| 58 |
+
folder = Path(snapshot_download("EasyOctopus/german-support-pii-model", local_dir="german-support-pii"))
|
| 59 |
+
subprocess.run([sys.executable, str(folder / "infer.py"), "Bitte schickt die Bestätigung an beispiel@example.org."], check=True)
|
| 60 |
+
subprocess.run([sys.executable, str(folder / "infer.py"), "--self-test"], check=True)
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
See LICENSE and NOTICE for source attribution. The test predictions and validation selection are documented in eval_results.json and provenance.json.
|
| 64 |
+
|
| 65 |
+
## Retrain the controlled variants
|
| 66 |
+
|
| 67 |
+
Download this repository with `snapshot_download` as shown above. From the downloaded directory, run the commands below. The script fetches only the training and validation files it needs from the companion dataset. It verifies the initializer and input fingerprints before fitting. Use `--data-dir` to use a local dataset checkout.
|
| 68 |
+
|
| 69 |
+
```sh
|
| 70 |
+
uv run --script training/retrain.py --prepare-only --output smoke-results
|
| 71 |
+
CUDA_VISIBLE_DEVICES=0 uv run --script training/retrain.py --variant all --output ablation-results
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
The PII initializer is included in `training/initial/`. Its Apache-2.0 provenance is in `training/initial-provenance.json`. Initial weight SHA-256: `a30303f35671526f97590f30206469ac286b014d11e74133123a758883e993dc`. Each variant uses seed 20261118, 3 epochs, batch size 24 and AdamW with a constant learning rate of 1.5e-05, weight decay 0.01 and gradient clipping at norm 1. Training uses BF16 autocast when the card supports it. The script retains every final-epoch checkpoint and records the validation-selected variant. CUDA, hardware and library differences can affect numerical results. The original run used PyTorch 2.6.0 with CUDA 12.4 and Transformers 4.49.0.
|
SHA256SUMS
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
43070e2d4e532684de521b885f385d0841030efa2b1a20bafb76133a5e1379c1 LICENSE
|
| 2 |
+
6c3aab2dea9cca4cbeb095b5014fadf0db96201d78ab2cec40c0a6b2f570a53d NOTICE
|
| 3 |
+
f8457642ae0f220653ca9d46be4d8f40744224b7281443155c65a345ddc7f243 NOTICE-nobody-pii.txt
|
| 4 |
+
51f1d75fc670b74a335625f909edc18d557244be4eb19e85ad76fccae74320bf README.md
|
| 5 |
+
e35161f26afed7a4693e1880f434e754ed608225819bc0e9d6099fc1365883d8 classifier.py
|
| 6 |
+
b34a89e171fdbae8dbfed5e9d115d8063771d52938d0d4f8e85f0f68a17adb9a config.json
|
| 7 |
+
81570c4f6d825a3004961bc863dccc7fa625233d548e062d0e723a97ebd7574f eval_results.json
|
| 8 |
+
8036f0dbaeac5b55abfcada8f463a16472b8bfdcf80490e5a867def8af26fd4c examples.json
|
| 9 |
+
0e049602afe66502ded41f85d244c93e2f647bce219988027cb19f8a829d3dc3 infer.py
|
| 10 |
+
70be52fb5e2cb5364aa070038b3560f9350a1c1615125d9618d7c8e4c0bb56d2 model.safetensors
|
| 11 |
+
399d46a6ae5388d24b00d965de9fa834fe4c11521bebd3c06ee0bdc2d40fed98 provenance.json
|
| 12 |
+
31da414ba9a9768660871a06abd03ce3ca896a75f862812806a641f4209879f0 runtime.py
|
| 13 |
+
5d5b662e421ea9fac075174bb0688ee0d9431699900b90662acd44b2a350503a special_tokens_map.json
|
| 14 |
+
4c74912a4ea7075eb1e02b89f2533ac1037e74344450f003daaa4f16f97df074 tokenizer.json
|
| 15 |
+
108c8ac0b4b1a7b4084b526cd36e9b46f0d90258e662aaade15a90da90b082c2 tokenizer_config.json
|
| 16 |
+
b34a89e171fdbae8dbfed5e9d115d8063771d52938d0d4f8e85f0f68a17adb9a training/initial/config.json
|
| 17 |
+
a30303f35671526f97590f30206469ac286b014d11e74133123a758883e993dc training/initial/model.safetensors
|
| 18 |
+
b6d346be366a7d1d48332dbc9fdf3bf8960b5d879522b7799ddba59e76237ee3 training/initial/special_tokens_map.json
|
| 19 |
+
4c74912a4ea7075eb1e02b89f2533ac1037e74344450f003daaa4f16f97df074 training/initial/tokenizer.json
|
| 20 |
+
d7ed459fc75813d0e50653ff952ca98376cd38c8a465cfc47760ed7497f86c8e training/initial/tokenizer_config.json
|
| 21 |
+
bf186f7fb9cb667d811c08b45ce64d1e8e262277bb053f05d1aef3e9f13039a3 training/initial/vocab.txt
|
| 22 |
+
6bcdc69c4b989123f0c6ea21d1a6d0df714d5ac10d6af0147f647f0a167117ed training/initial-provenance.json
|
| 23 |
+
698b7ba34716cfa3b630aad4bb9098abbec4cd22db2164e17909cea7e1e51781 training/recipe.json
|
| 24 |
+
ab45684106c7a0ea70e641dcce7a2f6436697419dc3c70b0678abad5397db44d training/retrain.py
|
| 25 |
+
da014bd909d663fa23e874740353450961eed82410303b4742e59b92610a65ed training/scoring.py
|
| 26 |
+
bf186f7fb9cb667d811c08b45ce64d1e8e262277bb053f05d1aef3e9f13039a3 vocab.txt
|
classifier.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Subword BIO alignment and decoding with original Unicode offsets."""
|
| 2 |
+
import json
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
ROOT = Path(__file__).resolve().parent
|
| 8 |
+
|
| 9 |
+
def rows(name):
|
| 10 |
+
return [json.loads(line) for line in (ROOT/"data"/(name+".jsonl")).read_text().splitlines()]
|
| 11 |
+
|
| 12 |
+
def encode(tokenizer, row, label_ids=None):
|
| 13 |
+
data = tokenizer(row["text"], return_offsets_mapping=True, truncation=True, max_length=256,
|
| 14 |
+
return_overflowing_tokens=True, stride=0)
|
| 15 |
+
gold = sorted(row.get("gold", []), key=lambda e:e["start"])
|
| 16 |
+
index = 0
|
| 17 |
+
seen = set()
|
| 18 |
+
for ids, offsets in zip(data["input_ids"],data["offset_mapping"]):
|
| 19 |
+
item = {"input_ids":ids,"offsets":offsets,"id":row["id"]}
|
| 20 |
+
if label_ids is not None:
|
| 21 |
+
labels = []
|
| 22 |
+
for start, end in offsets:
|
| 23 |
+
if start == end:
|
| 24 |
+
labels.append(-100)
|
| 25 |
+
continue
|
| 26 |
+
while index < len(gold) and gold[index]["end"] <= start:
|
| 27 |
+
index += 1
|
| 28 |
+
if index < len(gold) and gold[index]["start"] < end:
|
| 29 |
+
tag = ("I-" if index in seen else "B-")+gold[index]["label"]
|
| 30 |
+
seen.add(index)
|
| 31 |
+
else:
|
| 32 |
+
tag = "O"
|
| 33 |
+
labels.append(label_ids[tag])
|
| 34 |
+
item["labels"] = labels
|
| 35 |
+
yield item
|
| 36 |
+
|
| 37 |
+
def batch(items, pad, device):
|
| 38 |
+
length = ((max(len(i["input_ids"]) for i in items)+7)//8)*8
|
| 39 |
+
def field(key, fill):
|
| 40 |
+
values = [i[key]+[fill]*(length-len(i[key])) for i in items]
|
| 41 |
+
return torch.tensor(values,device=device)
|
| 42 |
+
ids = field("input_ids",pad)
|
| 43 |
+
inputs = {"input_ids":ids, "attention_mask":torch.tensor([
|
| 44 |
+
[1]*len(i["input_ids"])+[0]*(length-len(i["input_ids"])) for i in items],device=device)}
|
| 45 |
+
if "labels" in items[0]:
|
| 46 |
+
inputs["labels"] = field("labels",-100)
|
| 47 |
+
return inputs
|
| 48 |
+
|
| 49 |
+
@torch.inference_mode()
|
| 50 |
+
def predict(model, tokenizer, documents, batch_size=24):
|
| 51 |
+
windows = [w for r in documents for w in encode(tokenizer,r)]
|
| 52 |
+
groups = {r["id"]:[] for r in documents}
|
| 53 |
+
previous = {}
|
| 54 |
+
model.eval()
|
| 55 |
+
for begin in range(0,len(windows),batch_size):
|
| 56 |
+
items = windows[begin:begin+batch_size]
|
| 57 |
+
logits = model(**batch(items,tokenizer.pad_token_id,model.device)).logits
|
| 58 |
+
probs, indices = logits.float().softmax(dim=-1).max(dim=-1)
|
| 59 |
+
for w, confidence, tags in zip(items,probs.cpu().tolist(),indices.cpu().tolist()):
|
| 60 |
+
rid = w["id"]
|
| 61 |
+
for (start,end),score,idx in zip(w["offsets"],confidence,tags):
|
| 62 |
+
if start == end:
|
| 63 |
+
continue
|
| 64 |
+
tag = model.config.id2label[idx]
|
| 65 |
+
if tag == "O":
|
| 66 |
+
previous[rid] = None
|
| 67 |
+
continue
|
| 68 |
+
boundary,label = tag.split("-",1)
|
| 69 |
+
if boundary == "I" and previous.get(rid) == label:
|
| 70 |
+
group = groups[rid][-1]
|
| 71 |
+
group["end"] = end
|
| 72 |
+
group["scores"].append(score)
|
| 73 |
+
else:
|
| 74 |
+
groups[rid].append({"start":start,"end":end,"label":label,"scores":[score]})
|
| 75 |
+
previous[rid] = label
|
| 76 |
+
return groups
|
| 77 |
+
|
| 78 |
+
def apply_threshold(groups, threshold):
|
| 79 |
+
return {rid:[{k:g[k] for k in ("start","end","label")} for g in spans
|
| 80 |
+
if sum(g["scores"])/len(g["scores"]) >= threshold] for rid,spans in groups.items()}
|
config.json
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"activation": "gelu",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"DistilBertForTokenClassification"
|
| 5 |
+
],
|
| 6 |
+
"attention_dropout": 0.1,
|
| 7 |
+
"dim": 768,
|
| 8 |
+
"dropout": 0.1,
|
| 9 |
+
"hidden_dim": 3072,
|
| 10 |
+
"id2label": {
|
| 11 |
+
"0": "O",
|
| 12 |
+
"1": "B-ACCOUNT_ID",
|
| 13 |
+
"2": "I-ACCOUNT_ID",
|
| 14 |
+
"3": "B-ADDRESS",
|
| 15 |
+
"4": "I-ADDRESS",
|
| 16 |
+
"5": "B-BIC",
|
| 17 |
+
"6": "I-BIC",
|
| 18 |
+
"7": "B-CUSTOMER_ID",
|
| 19 |
+
"8": "I-CUSTOMER_ID",
|
| 20 |
+
"9": "B-DATE_OF_BIRTH",
|
| 21 |
+
"10": "I-DATE_OF_BIRTH",
|
| 22 |
+
"11": "B-EMAIL",
|
| 23 |
+
"12": "I-EMAIL",
|
| 24 |
+
"13": "B-IBAN",
|
| 25 |
+
"14": "I-IBAN",
|
| 26 |
+
"15": "B-INSURANCE_ID",
|
| 27 |
+
"16": "I-INSURANCE_ID",
|
| 28 |
+
"17": "B-IP",
|
| 29 |
+
"18": "I-IP",
|
| 30 |
+
"19": "B-ORDER_ID",
|
| 31 |
+
"20": "I-ORDER_ID",
|
| 32 |
+
"21": "B-ORGANIZATION",
|
| 33 |
+
"22": "I-ORGANIZATION",
|
| 34 |
+
"23": "B-PAYMENT_CARD",
|
| 35 |
+
"24": "I-PAYMENT_CARD",
|
| 36 |
+
"25": "B-PERSON",
|
| 37 |
+
"26": "I-PERSON",
|
| 38 |
+
"27": "B-PHONE",
|
| 39 |
+
"28": "I-PHONE",
|
| 40 |
+
"29": "B-TAX_ID",
|
| 41 |
+
"30": "I-TAX_ID"
|
| 42 |
+
},
|
| 43 |
+
"initializer_range": 0.02,
|
| 44 |
+
"label2id": {
|
| 45 |
+
"B-ACCOUNT_ID": 1,
|
| 46 |
+
"B-ADDRESS": 3,
|
| 47 |
+
"B-BIC": 5,
|
| 48 |
+
"B-CUSTOMER_ID": 7,
|
| 49 |
+
"B-DATE_OF_BIRTH": 9,
|
| 50 |
+
"B-EMAIL": 11,
|
| 51 |
+
"B-IBAN": 13,
|
| 52 |
+
"B-INSURANCE_ID": 15,
|
| 53 |
+
"B-IP": 17,
|
| 54 |
+
"B-ORDER_ID": 19,
|
| 55 |
+
"B-ORGANIZATION": 21,
|
| 56 |
+
"B-PAYMENT_CARD": 23,
|
| 57 |
+
"B-PERSON": 25,
|
| 58 |
+
"B-PHONE": 27,
|
| 59 |
+
"B-TAX_ID": 29,
|
| 60 |
+
"I-ACCOUNT_ID": 2,
|
| 61 |
+
"I-ADDRESS": 4,
|
| 62 |
+
"I-BIC": 6,
|
| 63 |
+
"I-CUSTOMER_ID": 8,
|
| 64 |
+
"I-DATE_OF_BIRTH": 10,
|
| 65 |
+
"I-EMAIL": 12,
|
| 66 |
+
"I-IBAN": 14,
|
| 67 |
+
"I-INSURANCE_ID": 16,
|
| 68 |
+
"I-IP": 18,
|
| 69 |
+
"I-ORDER_ID": 20,
|
| 70 |
+
"I-ORGANIZATION": 22,
|
| 71 |
+
"I-PAYMENT_CARD": 24,
|
| 72 |
+
"I-PERSON": 26,
|
| 73 |
+
"I-PHONE": 28,
|
| 74 |
+
"I-TAX_ID": 30,
|
| 75 |
+
"O": 0
|
| 76 |
+
},
|
| 77 |
+
"max_position_embeddings": 512,
|
| 78 |
+
"model_type": "distilbert",
|
| 79 |
+
"n_heads": 12,
|
| 80 |
+
"n_layers": 6,
|
| 81 |
+
"output_past": true,
|
| 82 |
+
"pad_token_id": 0,
|
| 83 |
+
"qa_dropout": 0.1,
|
| 84 |
+
"seq_classif_dropout": 0.2,
|
| 85 |
+
"sinusoidal_pos_embds": true,
|
| 86 |
+
"tie_weights_": true,
|
| 87 |
+
"torch_dtype": "float32",
|
| 88 |
+
"transformers_version": "4.49.0",
|
| 89 |
+
"vocab_size": 31102
|
| 90 |
+
}
|
eval_results.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
examples.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"input": "Im Fundbüro des Hotels liegt mein Beutel. Auf dem Schild steht Nino Đurić, reserviert hatte ich unter AB/754736-X.",
|
| 4 |
+
"output": {
|
| 5 |
+
"redacted_text": "Im Fundbüro des Hotels liegt mein Beutel. Auf dem Schild steht [PERSON], reserviert hatte ich unter [CUSTOMER_ID].",
|
| 6 |
+
"entities": [
|
| 7 |
+
{
|
| 8 |
+
"start": 63,
|
| 9 |
+
"end": 73,
|
| 10 |
+
"label": "PERSON"
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"start": 102,
|
| 14 |
+
"end": 113,
|
| 15 |
+
"label": "CUSTOMER_ID"
|
| 16 |
+
}
|
| 17 |
+
]
|
| 18 |
+
}
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"input": "Für die Kontenklärung soll ich den Geburtstag 13.07.1976 und die Versicherungskennung G766176000 nennen.",
|
| 22 |
+
"output": {
|
| 23 |
+
"redacted_text": "Für die Kontenklärung soll ich den Geburtstag [DATE_OF_BIRTH] und die Versicherungskennung [INSURANCE_ID] nennen.",
|
| 24 |
+
"entities": [
|
| 25 |
+
{
|
| 26 |
+
"start": 46,
|
| 27 |
+
"end": 56,
|
| 28 |
+
"label": "DATE_OF_BIRTH"
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"start": 86,
|
| 32 |
+
"end": 96,
|
| 33 |
+
"label": "INSURANCE_ID"
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"input": "Die Kompatibilitätsliste nennt für Baureihe MK-64708-L mindestens Firmware 4.3.6.",
|
| 40 |
+
"output": {
|
| 41 |
+
"redacted_text": "Die Kompatibilitätsliste nennt für Baureihe MK-64708-L mindestens Firmware 4.3.6.",
|
| 42 |
+
"entities": []
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
]
|
infer.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env -S uv run --script
|
| 2 |
+
# /// script
|
| 3 |
+
# requires-python = ">=3.11"
|
| 4 |
+
# dependencies = ["torch==2.6.0","transformers==4.49.0"]
|
| 5 |
+
# ///
|
| 6 |
+
"""Load the checkpoint beside this script. No Hub access or remote code."""
|
| 7 |
+
import argparse
|
| 8 |
+
import hashlib
|
| 9 |
+
import json
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
import torch
|
| 12 |
+
from transformers import AutoModelForTokenClassification,AutoTokenizer
|
| 13 |
+
from runtime import pii,punctuation,render_pii
|
| 14 |
+
|
| 15 |
+
HERE=Path(__file__).resolve().parent
|
| 16 |
+
|
| 17 |
+
def main():
|
| 18 |
+
parser=argparse.ArgumentParser()
|
| 19 |
+
parser.add_argument("text",nargs="?")
|
| 20 |
+
parser.add_argument("--self-test",action="store_true")
|
| 21 |
+
args=parser.parse_args()
|
| 22 |
+
torch.set_num_threads(2)
|
| 23 |
+
meta=json.loads((HERE/"provenance.json").read_text())
|
| 24 |
+
with (HERE/"model.safetensors").open("rb") as f:
|
| 25 |
+
assert hashlib.file_digest(f,"sha256").hexdigest()==meta["weights_sha256"]
|
| 26 |
+
tok=AutoTokenizer.from_pretrained(HERE,local_files_only=True)
|
| 27 |
+
model=AutoModelForTokenClassification.from_pretrained(HERE,local_files_only=True).eval()
|
| 28 |
+
def infer(text):
|
| 29 |
+
row={"id":"example","text":text}
|
| 30 |
+
if meta["task"]=="pii":
|
| 31 |
+
spans=pii(model,tok,[row])["example"]
|
| 32 |
+
return {"redacted_text":render_pii(text,spans),"entities":spans}
|
| 33 |
+
return {"text":punctuation(model,tok,[row])["example"]}
|
| 34 |
+
if args.self_test:
|
| 35 |
+
examples=json.loads((HERE/"examples.json").read_text())
|
| 36 |
+
for case in examples:
|
| 37 |
+
assert infer(case["input"])==case["output"]
|
| 38 |
+
print(f"Verified {len(examples)} frozen offline examples.")
|
| 39 |
+
elif args.text is not None:
|
| 40 |
+
print(json.dumps(infer(args.text),ensure_ascii=False,indent=2))
|
| 41 |
+
else:
|
| 42 |
+
parser.error("Provide text or --self-test")
|
| 43 |
+
if __name__=="__main__":
|
| 44 |
+
main()
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:70be52fb5e2cb5364aa070038b3560f9350a1c1615125d9618d7c8e4c0bb56d2
|
| 3 |
+
size 267340988
|
provenance.json
ADDED
|
@@ -0,0 +1,467 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task": "pii",
|
| 3 |
+
"status": "experimental",
|
| 4 |
+
"license": "Apache-2.0",
|
| 5 |
+
"variant": "scenarios-hard",
|
| 6 |
+
"weights_sha256": "70be52fb5e2cb5364aa070038b3560f9350a1c1615125d9618d7c8e4c0bb56d2",
|
| 7 |
+
"seed": 20261118,
|
| 8 |
+
"source_encoder": "distilbert/distilbert-base-german-cased",
|
| 9 |
+
"base_revision": "6f96562de9886779ce7d48810dff8f5c4afb6d0b",
|
| 10 |
+
"initial_weights_sha256": "a30303f35671526f97590f30206469ac286b014d11e74133123a758883e993dc",
|
| 11 |
+
"config": {
|
| 12 |
+
"version": 1,
|
| 13 |
+
"seed": 20261118,
|
| 14 |
+
"generation": {
|
| 15 |
+
"train": 28,
|
| 16 |
+
"validation": 8,
|
| 17 |
+
"test": 8
|
| 18 |
+
},
|
| 19 |
+
"epochs": 3,
|
| 20 |
+
"batch_size": 24,
|
| 21 |
+
"pii_learning_rate": 1.5e-05,
|
| 22 |
+
"punctuation_learning_rate": 5e-05,
|
| 23 |
+
"max_tokens": 256,
|
| 24 |
+
"pii_variants": [
|
| 25 |
+
"replay",
|
| 26 |
+
"scenarios",
|
| 27 |
+
"scenarios-hard"
|
| 28 |
+
],
|
| 29 |
+
"punctuation_variants": [
|
| 30 |
+
"final-only",
|
| 31 |
+
"all-marks"
|
| 32 |
+
],
|
| 33 |
+
"common_replay_rows": 384,
|
| 34 |
+
"positive_slots": 480,
|
| 35 |
+
"negative_slots": 160,
|
| 36 |
+
"pii_selection": "highest validation full-span protection minus non-gold character masking; ties prefer lower collateral, then earlier listed variant",
|
| 37 |
+
"punctuation_selection": "highest validation typed word-final mark micro F1; ties prefer fewer false marks, then earlier listed variant",
|
| 38 |
+
"pii_threshold": 0.0,
|
| 39 |
+
"checkpoint": "final epoch for every variant",
|
| 40 |
+
"base_revision": "6f96562de9886779ce7d48810dff8f5c4afb6d0b",
|
| 41 |
+
"base_sha256": "764a3ad5aa5f599cd1a93619eba403fb1cce7ad61b5abefd0e2bc129bc34ded5",
|
| 42 |
+
"prior_sha256": "a30303f35671526f97590f30206469ac286b014d11e74133123a758883e993dc"
|
| 43 |
+
},
|
| 44 |
+
"data": {
|
| 45 |
+
"seed": 20261118,
|
| 46 |
+
"task": "pii",
|
| 47 |
+
"license": "Apache-2.0",
|
| 48 |
+
"source": "original fictional scenario templates",
|
| 49 |
+
"templates_sha256": "ea1b57647c882a0380b8f64efc5d350b7ffc6270f935f1779ef5e167bcf5f782",
|
| 50 |
+
"generator_sha256": "5244cc340e5b4c29b15d50e48290ec77507df571c482546c51fc9277a70e2a68",
|
| 51 |
+
"splits": {
|
| 52 |
+
"train": {
|
| 53 |
+
"rows": 784,
|
| 54 |
+
"scenarios": [
|
| 55 |
+
"billing-address",
|
| 56 |
+
"card-dispute",
|
| 57 |
+
"catalog-size",
|
| 58 |
+
"change-of-name",
|
| 59 |
+
"clinic-intake",
|
| 60 |
+
"maintenance-calendar",
|
| 61 |
+
"parcel-locker",
|
| 62 |
+
"payroll-query",
|
| 63 |
+
"public-release",
|
| 64 |
+
"refund-routing",
|
| 65 |
+
"return-fees",
|
| 66 |
+
"sim-replacement",
|
| 67 |
+
"standing-order",
|
| 68 |
+
"vpn-session"
|
| 69 |
+
],
|
| 70 |
+
"templates": 28,
|
| 71 |
+
"annotations": 1120,
|
| 72 |
+
"negative_rows": 224,
|
| 73 |
+
"sha256": "6cfb2539bd697a5b190b67b386356108fb4da2797d9ccab05b0c499ae5e8dd28",
|
| 74 |
+
"prior_normalised_text_overlap": {
|
| 75 |
+
"v1-train": 0,
|
| 76 |
+
"v1-validation": 0,
|
| 77 |
+
"v1-support-dev": 0,
|
| 78 |
+
"independent": 0,
|
| 79 |
+
"confirmation": 0,
|
| 80 |
+
"old-support": 0,
|
| 81 |
+
"public-test": 0,
|
| 82 |
+
"public-validation": 0,
|
| 83 |
+
"public-train": 0
|
| 84 |
+
}
|
| 85 |
+
},
|
| 86 |
+
"validation": {
|
| 87 |
+
"rows": 112,
|
| 88 |
+
"scenarios": [
|
| 89 |
+
"disputed-invoice",
|
| 90 |
+
"error-manual",
|
| 91 |
+
"membership-transfer",
|
| 92 |
+
"prescription-pickup",
|
| 93 |
+
"privacy-export",
|
| 94 |
+
"public-opening",
|
| 95 |
+
"warehouse-call"
|
| 96 |
+
],
|
| 97 |
+
"templates": 14,
|
| 98 |
+
"annotations": 160,
|
| 99 |
+
"negative_rows": 32,
|
| 100 |
+
"sha256": "62b3c748035a3cefc3c52f37cb0f649688e0b580a5e43d196a9507f579c8ffef",
|
| 101 |
+
"prior_normalised_text_overlap": {
|
| 102 |
+
"v1-train": 0,
|
| 103 |
+
"v1-validation": 0,
|
| 104 |
+
"v1-support-dev": 0,
|
| 105 |
+
"independent": 0,
|
| 106 |
+
"confirmation": 0,
|
| 107 |
+
"old-support": 0,
|
| 108 |
+
"public-test": 0,
|
| 109 |
+
"public-validation": 0,
|
| 110 |
+
"public-train": 0
|
| 111 |
+
}
|
| 112 |
+
},
|
| 113 |
+
"test": {
|
| 114 |
+
"rows": 176,
|
| 115 |
+
"scenarios": [
|
| 116 |
+
"accessibility-interpreter",
|
| 117 |
+
"caregiver-contact",
|
| 118 |
+
"direct-debit-reversal",
|
| 119 |
+
"fare-machine",
|
| 120 |
+
"firmware-compatibility",
|
| 121 |
+
"hotel-lost-property",
|
| 122 |
+
"museum-price",
|
| 123 |
+
"pension-record",
|
| 124 |
+
"public-roadworks",
|
| 125 |
+
"scanner-audit",
|
| 126 |
+
"tax-adviser"
|
| 127 |
+
],
|
| 128 |
+
"templates": 22,
|
| 129 |
+
"annotations": 256,
|
| 130 |
+
"negative_rows": 48,
|
| 131 |
+
"sha256": "49bc3e4eabc5161c27198ea2902ddffc2dee2a38613fc3336d2f1d6576038815",
|
| 132 |
+
"prior_normalised_text_overlap": {
|
| 133 |
+
"v1-train": 0,
|
| 134 |
+
"v1-validation": 0,
|
| 135 |
+
"v1-support-dev": 0,
|
| 136 |
+
"independent": 0,
|
| 137 |
+
"confirmation": 0,
|
| 138 |
+
"old-support": 0,
|
| 139 |
+
"public-test": 0,
|
| 140 |
+
"public-validation": 0,
|
| 141 |
+
"public-train": 0
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
},
|
| 145 |
+
"replay": {
|
| 146 |
+
"rows": 1024,
|
| 147 |
+
"sha256": "d0cb30185cc8412cefdbc997f895a27d2a181d791a59d3d6118813809bdc90ae",
|
| 148 |
+
"source_sha256": "6ee4bba05aa8819f1cb2b2613ca4894fa92c5d2d9df7aee7a6f9a7016134e75e",
|
| 149 |
+
"source": "frozen v1 train only, intentionally shared by controlled variants; supplemental, outside new splits"
|
| 150 |
+
}
|
| 151 |
+
},
|
| 152 |
+
"training": {
|
| 153 |
+
"variants": {
|
| 154 |
+
"replay": {
|
| 155 |
+
"rows": 1024,
|
| 156 |
+
"tokens_per_epoch": 81210,
|
| 157 |
+
"steps": 129,
|
| 158 |
+
"losses": [
|
| 159 |
+
0.0076342183490132175,
|
| 160 |
+
0.00555266959722652,
|
| 161 |
+
0.004366781475932099
|
| 162 |
+
],
|
| 163 |
+
"validation": {
|
| 164 |
+
"documents": 112,
|
| 165 |
+
"gold_entities": 160,
|
| 166 |
+
"predicted_entities": 220,
|
| 167 |
+
"gold_chars": 2284,
|
| 168 |
+
"pred_chars": 2143,
|
| 169 |
+
"protected_chars": 1927,
|
| 170 |
+
"non_gold_chars": 5772,
|
| 171 |
+
"overmasked_chars": 216,
|
| 172 |
+
"exact_bounds": {
|
| 173 |
+
"precision": 0.372727,
|
| 174 |
+
"recall": 0.5125,
|
| 175 |
+
"f1": 0.431579
|
| 176 |
+
},
|
| 177 |
+
"exact_typed": {
|
| 178 |
+
"precision": 0.363636,
|
| 179 |
+
"recall": 0.5,
|
| 180 |
+
"f1": 0.421053
|
| 181 |
+
},
|
| 182 |
+
"only_punctuation_unmasked": 1,
|
| 183 |
+
"fully_masked": 102,
|
| 184 |
+
"positive_documents_with_any_leak": 43,
|
| 185 |
+
"negative_documents": 32,
|
| 186 |
+
"negative_documents_flagged": 16,
|
| 187 |
+
"full_entity_mask_rate": 0.6375,
|
| 188 |
+
"non_gold_mask_rate": 0.037422,
|
| 189 |
+
"negative_doc_mask_rate": 0.5,
|
| 190 |
+
"source_char": {
|
| 191 |
+
"precision": 0.899207,
|
| 192 |
+
"recall": 0.843695,
|
| 193 |
+
"f1": 0.870567
|
| 194 |
+
},
|
| 195 |
+
"per_gold_class": {
|
| 196 |
+
"ACCOUNT_ID": {
|
| 197 |
+
"total": 16,
|
| 198 |
+
"fully_masked": 0,
|
| 199 |
+
"all_alnum_masked": 0,
|
| 200 |
+
"completely_missed": 5
|
| 201 |
+
},
|
| 202 |
+
"ADDRESS": {
|
| 203 |
+
"total": 16,
|
| 204 |
+
"fully_masked": 16,
|
| 205 |
+
"all_alnum_masked": 16,
|
| 206 |
+
"completely_missed": 0
|
| 207 |
+
},
|
| 208 |
+
"CUSTOMER_ID": {
|
| 209 |
+
"total": 16,
|
| 210 |
+
"fully_masked": 0,
|
| 211 |
+
"all_alnum_masked": 0,
|
| 212 |
+
"completely_missed": 16
|
| 213 |
+
},
|
| 214 |
+
"EMAIL": {
|
| 215 |
+
"total": 16,
|
| 216 |
+
"fully_masked": 8,
|
| 217 |
+
"all_alnum_masked": 8,
|
| 218 |
+
"completely_missed": 0
|
| 219 |
+
},
|
| 220 |
+
"INSURANCE_ID": {
|
| 221 |
+
"total": 16,
|
| 222 |
+
"fully_masked": 16,
|
| 223 |
+
"all_alnum_masked": 16,
|
| 224 |
+
"completely_missed": 0
|
| 225 |
+
},
|
| 226 |
+
"IP": {
|
| 227 |
+
"total": 16,
|
| 228 |
+
"fully_masked": 5,
|
| 229 |
+
"all_alnum_masked": 5,
|
| 230 |
+
"completely_missed": 8
|
| 231 |
+
},
|
| 232 |
+
"ORDER_ID": {
|
| 233 |
+
"total": 16,
|
| 234 |
+
"fully_masked": 10,
|
| 235 |
+
"all_alnum_masked": 11,
|
| 236 |
+
"completely_missed": 0
|
| 237 |
+
},
|
| 238 |
+
"PERSON": {
|
| 239 |
+
"total": 32,
|
| 240 |
+
"fully_masked": 31,
|
| 241 |
+
"all_alnum_masked": 31,
|
| 242 |
+
"completely_missed": 0
|
| 243 |
+
},
|
| 244 |
+
"PHONE": {
|
| 245 |
+
"total": 16,
|
| 246 |
+
"fully_masked": 16,
|
| 247 |
+
"all_alnum_masked": 16,
|
| 248 |
+
"completely_missed": 0
|
| 249 |
+
}
|
| 250 |
+
}
|
| 251 |
+
},
|
| 252 |
+
"seconds": 5.347269667999996,
|
| 253 |
+
"weights_sha256": "d21ac1343824750aca301f6ef3f2c00813d178fb43e9551ab20d4bc87dc3e8bd"
|
| 254 |
+
},
|
| 255 |
+
"scenarios": {
|
| 256 |
+
"rows": 1024,
|
| 257 |
+
"tokens_per_epoch": 52734,
|
| 258 |
+
"steps": 129,
|
| 259 |
+
"losses": [
|
| 260 |
+
0.059628808221151665,
|
| 261 |
+
0.01283741967622624,
|
| 262 |
+
0.00793909610703934
|
| 263 |
+
],
|
| 264 |
+
"validation": {
|
| 265 |
+
"documents": 112,
|
| 266 |
+
"gold_entities": 160,
|
| 267 |
+
"predicted_entities": 193,
|
| 268 |
+
"gold_chars": 2284,
|
| 269 |
+
"pred_chars": 2490,
|
| 270 |
+
"protected_chars": 2250,
|
| 271 |
+
"non_gold_chars": 5772,
|
| 272 |
+
"overmasked_chars": 240,
|
| 273 |
+
"exact_bounds": {
|
| 274 |
+
"precision": 0.761658,
|
| 275 |
+
"recall": 0.91875,
|
| 276 |
+
"f1": 0.832861
|
| 277 |
+
},
|
| 278 |
+
"exact_typed": {
|
| 279 |
+
"precision": 0.694301,
|
| 280 |
+
"recall": 0.8375,
|
| 281 |
+
"f1": 0.759207
|
| 282 |
+
},
|
| 283 |
+
"only_punctuation_unmasked": 1,
|
| 284 |
+
"fully_masked": 154,
|
| 285 |
+
"positive_documents_with_any_leak": 6,
|
| 286 |
+
"negative_documents": 32,
|
| 287 |
+
"negative_documents_flagged": 16,
|
| 288 |
+
"full_entity_mask_rate": 0.9625,
|
| 289 |
+
"non_gold_mask_rate": 0.04158,
|
| 290 |
+
"negative_doc_mask_rate": 0.5,
|
| 291 |
+
"source_char": {
|
| 292 |
+
"precision": 0.903614,
|
| 293 |
+
"recall": 0.985114,
|
| 294 |
+
"f1": 0.942606
|
| 295 |
+
},
|
| 296 |
+
"per_gold_class": {
|
| 297 |
+
"ACCOUNT_ID": {
|
| 298 |
+
"total": 16,
|
| 299 |
+
"fully_masked": 16,
|
| 300 |
+
"all_alnum_masked": 16,
|
| 301 |
+
"completely_missed": 0
|
| 302 |
+
},
|
| 303 |
+
"ADDRESS": {
|
| 304 |
+
"total": 16,
|
| 305 |
+
"fully_masked": 16,
|
| 306 |
+
"all_alnum_masked": 16,
|
| 307 |
+
"completely_missed": 0
|
| 308 |
+
},
|
| 309 |
+
"CUSTOMER_ID": {
|
| 310 |
+
"total": 16,
|
| 311 |
+
"fully_masked": 16,
|
| 312 |
+
"all_alnum_masked": 16,
|
| 313 |
+
"completely_missed": 0
|
| 314 |
+
},
|
| 315 |
+
"EMAIL": {
|
| 316 |
+
"total": 16,
|
| 317 |
+
"fully_masked": 16,
|
| 318 |
+
"all_alnum_masked": 16,
|
| 319 |
+
"completely_missed": 0
|
| 320 |
+
},
|
| 321 |
+
"INSURANCE_ID": {
|
| 322 |
+
"total": 16,
|
| 323 |
+
"fully_masked": 16,
|
| 324 |
+
"all_alnum_masked": 16,
|
| 325 |
+
"completely_missed": 0
|
| 326 |
+
},
|
| 327 |
+
"IP": {
|
| 328 |
+
"total": 16,
|
| 329 |
+
"fully_masked": 10,
|
| 330 |
+
"all_alnum_masked": 11,
|
| 331 |
+
"completely_missed": 0
|
| 332 |
+
},
|
| 333 |
+
"ORDER_ID": {
|
| 334 |
+
"total": 16,
|
| 335 |
+
"fully_masked": 16,
|
| 336 |
+
"all_alnum_masked": 16,
|
| 337 |
+
"completely_missed": 0
|
| 338 |
+
},
|
| 339 |
+
"PERSON": {
|
| 340 |
+
"total": 32,
|
| 341 |
+
"fully_masked": 32,
|
| 342 |
+
"all_alnum_masked": 32,
|
| 343 |
+
"completely_missed": 0
|
| 344 |
+
},
|
| 345 |
+
"PHONE": {
|
| 346 |
+
"total": 16,
|
| 347 |
+
"fully_masked": 16,
|
| 348 |
+
"all_alnum_masked": 16,
|
| 349 |
+
"completely_missed": 0
|
| 350 |
+
}
|
| 351 |
+
}
|
| 352 |
+
},
|
| 353 |
+
"seconds": 4.343123457000004,
|
| 354 |
+
"weights_sha256": "cd0f234933a465e4f258ac89245904a2d50d8091eebd967b7e97e6905579752f"
|
| 355 |
+
},
|
| 356 |
+
"scenarios-hard": {
|
| 357 |
+
"rows": 1024,
|
| 358 |
+
"tokens_per_epoch": 52236,
|
| 359 |
+
"steps": 129,
|
| 360 |
+
"losses": [
|
| 361 |
+
0.08145274117935536,
|
| 362 |
+
0.01409973516020664,
|
| 363 |
+
0.008259040671725606
|
| 364 |
+
],
|
| 365 |
+
"validation": {
|
| 366 |
+
"documents": 112,
|
| 367 |
+
"gold_entities": 160,
|
| 368 |
+
"predicted_entities": 202,
|
| 369 |
+
"gold_chars": 2284,
|
| 370 |
+
"pred_chars": 2392,
|
| 371 |
+
"protected_chars": 2258,
|
| 372 |
+
"non_gold_chars": 5772,
|
| 373 |
+
"overmasked_chars": 134,
|
| 374 |
+
"exact_bounds": {
|
| 375 |
+
"precision": 0.717822,
|
| 376 |
+
"recall": 0.90625,
|
| 377 |
+
"f1": 0.801105
|
| 378 |
+
},
|
| 379 |
+
"exact_typed": {
|
| 380 |
+
"precision": 0.663366,
|
| 381 |
+
"recall": 0.8375,
|
| 382 |
+
"f1": 0.740331
|
| 383 |
+
},
|
| 384 |
+
"only_punctuation_unmasked": 0,
|
| 385 |
+
"fully_masked": 155,
|
| 386 |
+
"positive_documents_with_any_leak": 5,
|
| 387 |
+
"negative_documents": 32,
|
| 388 |
+
"negative_documents_flagged": 12,
|
| 389 |
+
"full_entity_mask_rate": 0.96875,
|
| 390 |
+
"non_gold_mask_rate": 0.023216,
|
| 391 |
+
"negative_doc_mask_rate": 0.375,
|
| 392 |
+
"source_char": {
|
| 393 |
+
"precision": 0.94398,
|
| 394 |
+
"recall": 0.988616,
|
| 395 |
+
"f1": 0.965783
|
| 396 |
+
},
|
| 397 |
+
"per_gold_class": {
|
| 398 |
+
"ACCOUNT_ID": {
|
| 399 |
+
"total": 16,
|
| 400 |
+
"fully_masked": 16,
|
| 401 |
+
"all_alnum_masked": 16,
|
| 402 |
+
"completely_missed": 0
|
| 403 |
+
},
|
| 404 |
+
"ADDRESS": {
|
| 405 |
+
"total": 16,
|
| 406 |
+
"fully_masked": 16,
|
| 407 |
+
"all_alnum_masked": 16,
|
| 408 |
+
"completely_missed": 0
|
| 409 |
+
},
|
| 410 |
+
"CUSTOMER_ID": {
|
| 411 |
+
"total": 16,
|
| 412 |
+
"fully_masked": 16,
|
| 413 |
+
"all_alnum_masked": 16,
|
| 414 |
+
"completely_missed": 0
|
| 415 |
+
},
|
| 416 |
+
"EMAIL": {
|
| 417 |
+
"total": 16,
|
| 418 |
+
"fully_masked": 16,
|
| 419 |
+
"all_alnum_masked": 16,
|
| 420 |
+
"completely_missed": 0
|
| 421 |
+
},
|
| 422 |
+
"INSURANCE_ID": {
|
| 423 |
+
"total": 16,
|
| 424 |
+
"fully_masked": 16,
|
| 425 |
+
"all_alnum_masked": 16,
|
| 426 |
+
"completely_missed": 0
|
| 427 |
+
},
|
| 428 |
+
"IP": {
|
| 429 |
+
"total": 16,
|
| 430 |
+
"fully_masked": 11,
|
| 431 |
+
"all_alnum_masked": 11,
|
| 432 |
+
"completely_missed": 0
|
| 433 |
+
},
|
| 434 |
+
"ORDER_ID": {
|
| 435 |
+
"total": 16,
|
| 436 |
+
"fully_masked": 16,
|
| 437 |
+
"all_alnum_masked": 16,
|
| 438 |
+
"completely_missed": 0
|
| 439 |
+
},
|
| 440 |
+
"PERSON": {
|
| 441 |
+
"total": 32,
|
| 442 |
+
"fully_masked": 32,
|
| 443 |
+
"all_alnum_masked": 32,
|
| 444 |
+
"completely_missed": 0
|
| 445 |
+
},
|
| 446 |
+
"PHONE": {
|
| 447 |
+
"total": 16,
|
| 448 |
+
"fully_masked": 16,
|
| 449 |
+
"all_alnum_masked": 16,
|
| 450 |
+
"completely_missed": 0
|
| 451 |
+
}
|
| 452 |
+
}
|
| 453 |
+
},
|
| 454 |
+
"seconds": 4.341576239999995,
|
| 455 |
+
"weights_sha256": "70be52fb5e2cb5364aa070038b3560f9350a1c1615125d9618d7c8e4c0bb56d2"
|
| 456 |
+
}
|
| 457 |
+
},
|
| 458 |
+
"selected": "scenarios-hard"
|
| 459 |
+
},
|
| 460 |
+
"gpu": "NVIDIA A100-SXM4-80GB, 81920 MiB, 580.178.04",
|
| 461 |
+
"torch": "2.6.0+cu124",
|
| 462 |
+
"transformers": "4.49.0",
|
| 463 |
+
"training_sha256": "7ea32cd4f576567e0479097c016036139fa7fea08b81bebdbd3afba78a1bb2e7",
|
| 464 |
+
"runtime_sha256": "31da414ba9a9768660871a06abd03ce3ca896a75f862812806a641f4209879f0",
|
| 465 |
+
"classifier_sha256": "e35161f26afed7a4693e1880f434e754ed608225819bc0e9d6099fc1365883d8",
|
| 466 |
+
"preregistration_sha256": "5bb271fd8793d3aa74b4a133d06ecb4a60c5a85d5441f1e62e87d4a87a50337a"
|
| 467 |
+
}
|
runtime.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Shared offline decoding for the standalone releases."""
|
| 2 |
+
import re
|
| 3 |
+
import torch
|
| 4 |
+
from classifier import apply_threshold, batch, predict
|
| 5 |
+
|
| 6 |
+
MARK_LABELS=["O","PERIOD","COMMA","QUESTION","COLON"]
|
| 7 |
+
SYMBOLS=dict(zip(MARK_LABELS,["",".",",","?",":"]))
|
| 8 |
+
|
| 9 |
+
def punct_encode(tokenizer,row,final_only=False):
|
| 10 |
+
text=row["text"]
|
| 11 |
+
words=list(re.finditer(r"\S+",text))
|
| 12 |
+
encoded=tokenizer(text,return_offsets_mapping=True,truncation=False,verbose=False)
|
| 13 |
+
if len(encoded["input_ids"])>256:
|
| 14 |
+
raise ValueError("Input exceeds 256 tokens including special tokens")
|
| 15 |
+
ends={end:i for i,(start,end) in enumerate(encoded["offset_mapping"]) if start<end}
|
| 16 |
+
gold={s["index"]:s["mark"] for s in row.get("gold",[])}
|
| 17 |
+
positions=[]
|
| 18 |
+
labels=[-100]*len(encoded["input_ids"])
|
| 19 |
+
for i,w in enumerate(words):
|
| 20 |
+
if w.end() not in ends:
|
| 21 |
+
raise ValueError("Tokenizer cannot align this word to its original end")
|
| 22 |
+
pos=ends[w.end()]
|
| 23 |
+
symbol=gold.get(i,"")
|
| 24 |
+
if final_only and i!=len(words)-1:
|
| 25 |
+
symbol=""
|
| 26 |
+
labels[pos]=list(SYMBOLS.values()).index(symbol)
|
| 27 |
+
positions.append((pos,w.end()))
|
| 28 |
+
return {"input_ids":encoded["input_ids"],"labels":labels,"positions":positions}
|
| 29 |
+
|
| 30 |
+
@torch.inference_mode()
|
| 31 |
+
def punctuation(model,tokenizer,rows):
|
| 32 |
+
output={}
|
| 33 |
+
model.eval()
|
| 34 |
+
for start in range(0,len(rows),24):
|
| 35 |
+
group=rows[start:start+24]
|
| 36 |
+
items=[punct_encode(tokenizer,{"text":r["text"]}) for r in group]
|
| 37 |
+
features=batch(items,tokenizer.pad_token_id,model.device)
|
| 38 |
+
del features["labels"]
|
| 39 |
+
indices=model(**features).logits.argmax(-1).cpu().tolist()
|
| 40 |
+
for row,item,ids in zip(group,items,indices):
|
| 41 |
+
text=row["text"]
|
| 42 |
+
pos=0
|
| 43 |
+
parts=[]
|
| 44 |
+
for index,end in item["positions"]:
|
| 45 |
+
symbol=SYMBOLS[model.config.id2label[ids[index]]]
|
| 46 |
+
parts.extend([text[pos:end],symbol if text[end-1] not in ".,?!:;" else ""])
|
| 47 |
+
pos=end
|
| 48 |
+
output[row["id"]]="".join(parts)+text[pos:]
|
| 49 |
+
return output
|
| 50 |
+
|
| 51 |
+
def pii(model,tokenizer,rows):
|
| 52 |
+
return apply_threshold(predict(model,tokenizer,rows),0.0)
|
| 53 |
+
|
| 54 |
+
def render_pii(text,spans):
|
| 55 |
+
parts=[]
|
| 56 |
+
end=0
|
| 57 |
+
for s in spans:
|
| 58 |
+
parts.extend([text[end:s["start"]],"["+s["label"]+"]"])
|
| 59 |
+
end=s["end"]
|
| 60 |
+
return "".join(parts)+text[end:]
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": {
|
| 3 |
+
"content": "[CLS]",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"mask_token": {
|
| 10 |
+
"content": "[MASK]",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"pad_token": {
|
| 17 |
+
"content": "[PAD]",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"sep_token": {
|
| 24 |
+
"content": "[SEP]",
|
| 25 |
+
"lstrip": false,
|
| 26 |
+
"normalized": false,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
},
|
| 30 |
+
"unk_token": {
|
| 31 |
+
"content": "[UNK]",
|
| 32 |
+
"lstrip": false,
|
| 33 |
+
"normalized": false,
|
| 34 |
+
"rstrip": false,
|
| 35 |
+
"single_word": false
|
| 36 |
+
}
|
| 37 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"101": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"102": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"103": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"104": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": false,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"do_lower_case": false,
|
| 47 |
+
"extra_special_tokens": {},
|
| 48 |
+
"mask_token": "[MASK]",
|
| 49 |
+
"max_length": 256,
|
| 50 |
+
"model_max_length": 512,
|
| 51 |
+
"pad_token": "[PAD]",
|
| 52 |
+
"sep_token": "[SEP]",
|
| 53 |
+
"stride": 0,
|
| 54 |
+
"strip_accents": null,
|
| 55 |
+
"tokenize_chinese_chars": true,
|
| 56 |
+
"tokenizer_class": "DistilBertTokenizer",
|
| 57 |
+
"truncation_side": "right",
|
| 58 |
+
"truncation_strategy": "longest_first",
|
| 59 |
+
"unk_token": "[UNK]"
|
| 60 |
+
}
|
training/initial-provenance.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"license": "Apache-2.0",
|
| 3 |
+
"architecture": "DistilBertForTokenClassification",
|
| 4 |
+
"base_model": "distilbert/distilbert-base-german-cased",
|
| 5 |
+
"base_revision": "6f96562de9886779ce7d48810dff8f5c4afb6d0b",
|
| 6 |
+
"upstream_training_corpus": "naeyn/nobody-pii-synth-de",
|
| 7 |
+
"upstream_revision": "f78fcbcad638f5e7f69bfe55a8ebd508e32c3731",
|
| 8 |
+
"original_training_rows": {
|
| 9 |
+
"public": 4962,
|
| 10 |
+
"fictional_augmentation": 1442
|
| 11 |
+
},
|
| 12 |
+
"seed": 181126,
|
| 13 |
+
"selected_epoch": 2,
|
| 14 |
+
"training_optimizer": "AdamW, learning rate 3e-5, 8% warmup, weight decay 0.01",
|
| 15 |
+
"weights_sha256": "a30303f35671526f97590f30206469ac286b014d11e74133123a758883e993dc",
|
| 16 |
+
"files": {
|
| 17 |
+
"model.safetensors": "a30303f35671526f97590f30206469ac286b014d11e74133123a758883e993dc",
|
| 18 |
+
"config.json": "b34a89e171fdbae8dbfed5e9d115d8063771d52938d0d4f8e85f0f68a17adb9a",
|
| 19 |
+
"tokenizer.json": "4c74912a4ea7075eb1e02b89f2533ac1037e74344450f003daaa4f16f97df074",
|
| 20 |
+
"tokenizer_config.json": "d7ed459fc75813d0e50653ff952ca98376cd38c8a465cfc47760ed7497f86c8e",
|
| 21 |
+
"vocab.txt": "bf186f7fb9cb667d811c08b45ce64d1e8e262277bb053f05d1aef3e9f13039a3",
|
| 22 |
+
"special_tokens_map.json": "b6d346be366a7d1d48332dbc9fdf3bf8960b5d879522b7799ddba59e76237ee3"
|
| 23 |
+
},
|
| 24 |
+
"modification": "Removed local path metadata from JSON configurations. Numerical weights and tokenizer assets retain the prior checkpoint.",
|
| 25 |
+
"attribution": "See the repository LICENSE, NOTICE and NOTICE-nobody-pii.txt."
|
| 26 |
+
}
|
training/initial/config.json
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"activation": "gelu",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"DistilBertForTokenClassification"
|
| 5 |
+
],
|
| 6 |
+
"attention_dropout": 0.1,
|
| 7 |
+
"dim": 768,
|
| 8 |
+
"dropout": 0.1,
|
| 9 |
+
"hidden_dim": 3072,
|
| 10 |
+
"id2label": {
|
| 11 |
+
"0": "O",
|
| 12 |
+
"1": "B-ACCOUNT_ID",
|
| 13 |
+
"2": "I-ACCOUNT_ID",
|
| 14 |
+
"3": "B-ADDRESS",
|
| 15 |
+
"4": "I-ADDRESS",
|
| 16 |
+
"5": "B-BIC",
|
| 17 |
+
"6": "I-BIC",
|
| 18 |
+
"7": "B-CUSTOMER_ID",
|
| 19 |
+
"8": "I-CUSTOMER_ID",
|
| 20 |
+
"9": "B-DATE_OF_BIRTH",
|
| 21 |
+
"10": "I-DATE_OF_BIRTH",
|
| 22 |
+
"11": "B-EMAIL",
|
| 23 |
+
"12": "I-EMAIL",
|
| 24 |
+
"13": "B-IBAN",
|
| 25 |
+
"14": "I-IBAN",
|
| 26 |
+
"15": "B-INSURANCE_ID",
|
| 27 |
+
"16": "I-INSURANCE_ID",
|
| 28 |
+
"17": "B-IP",
|
| 29 |
+
"18": "I-IP",
|
| 30 |
+
"19": "B-ORDER_ID",
|
| 31 |
+
"20": "I-ORDER_ID",
|
| 32 |
+
"21": "B-ORGANIZATION",
|
| 33 |
+
"22": "I-ORGANIZATION",
|
| 34 |
+
"23": "B-PAYMENT_CARD",
|
| 35 |
+
"24": "I-PAYMENT_CARD",
|
| 36 |
+
"25": "B-PERSON",
|
| 37 |
+
"26": "I-PERSON",
|
| 38 |
+
"27": "B-PHONE",
|
| 39 |
+
"28": "I-PHONE",
|
| 40 |
+
"29": "B-TAX_ID",
|
| 41 |
+
"30": "I-TAX_ID"
|
| 42 |
+
},
|
| 43 |
+
"initializer_range": 0.02,
|
| 44 |
+
"label2id": {
|
| 45 |
+
"B-ACCOUNT_ID": 1,
|
| 46 |
+
"B-ADDRESS": 3,
|
| 47 |
+
"B-BIC": 5,
|
| 48 |
+
"B-CUSTOMER_ID": 7,
|
| 49 |
+
"B-DATE_OF_BIRTH": 9,
|
| 50 |
+
"B-EMAIL": 11,
|
| 51 |
+
"B-IBAN": 13,
|
| 52 |
+
"B-INSURANCE_ID": 15,
|
| 53 |
+
"B-IP": 17,
|
| 54 |
+
"B-ORDER_ID": 19,
|
| 55 |
+
"B-ORGANIZATION": 21,
|
| 56 |
+
"B-PAYMENT_CARD": 23,
|
| 57 |
+
"B-PERSON": 25,
|
| 58 |
+
"B-PHONE": 27,
|
| 59 |
+
"B-TAX_ID": 29,
|
| 60 |
+
"I-ACCOUNT_ID": 2,
|
| 61 |
+
"I-ADDRESS": 4,
|
| 62 |
+
"I-BIC": 6,
|
| 63 |
+
"I-CUSTOMER_ID": 8,
|
| 64 |
+
"I-DATE_OF_BIRTH": 10,
|
| 65 |
+
"I-EMAIL": 12,
|
| 66 |
+
"I-IBAN": 14,
|
| 67 |
+
"I-INSURANCE_ID": 16,
|
| 68 |
+
"I-IP": 18,
|
| 69 |
+
"I-ORDER_ID": 20,
|
| 70 |
+
"I-ORGANIZATION": 22,
|
| 71 |
+
"I-PAYMENT_CARD": 24,
|
| 72 |
+
"I-PERSON": 26,
|
| 73 |
+
"I-PHONE": 28,
|
| 74 |
+
"I-TAX_ID": 30,
|
| 75 |
+
"O": 0
|
| 76 |
+
},
|
| 77 |
+
"max_position_embeddings": 512,
|
| 78 |
+
"model_type": "distilbert",
|
| 79 |
+
"n_heads": 12,
|
| 80 |
+
"n_layers": 6,
|
| 81 |
+
"output_past": true,
|
| 82 |
+
"pad_token_id": 0,
|
| 83 |
+
"qa_dropout": 0.1,
|
| 84 |
+
"seq_classif_dropout": 0.2,
|
| 85 |
+
"sinusoidal_pos_embds": true,
|
| 86 |
+
"tie_weights_": true,
|
| 87 |
+
"torch_dtype": "float32",
|
| 88 |
+
"transformers_version": "4.49.0",
|
| 89 |
+
"vocab_size": 31102
|
| 90 |
+
}
|
training/initial/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a30303f35671526f97590f30206469ac286b014d11e74133123a758883e993dc
|
| 3 |
+
size 267340988
|
training/initial/special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
training/initial/tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
training/initial/tokenizer_config.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"101": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"102": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"103": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"104": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": false,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"do_lower_case": false,
|
| 47 |
+
"extra_special_tokens": {},
|
| 48 |
+
"mask_token": "[MASK]",
|
| 49 |
+
"model_max_length": 512,
|
| 50 |
+
"pad_token": "[PAD]",
|
| 51 |
+
"sep_token": "[SEP]",
|
| 52 |
+
"strip_accents": null,
|
| 53 |
+
"tokenize_chinese_chars": true,
|
| 54 |
+
"tokenizer_class": "DistilBertTokenizer",
|
| 55 |
+
"unk_token": "[UNK]"
|
| 56 |
+
}
|
training/initial/vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
training/recipe.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task": "pii",
|
| 3 |
+
"dataset_id": "EasyOctopus/german-support-pii-data",
|
| 4 |
+
"config": {
|
| 5 |
+
"version": 1,
|
| 6 |
+
"seed": 20261118,
|
| 7 |
+
"generation": {
|
| 8 |
+
"train": 28,
|
| 9 |
+
"validation": 8,
|
| 10 |
+
"test": 8
|
| 11 |
+
},
|
| 12 |
+
"epochs": 3,
|
| 13 |
+
"batch_size": 24,
|
| 14 |
+
"pii_learning_rate": 1.5e-05,
|
| 15 |
+
"punctuation_learning_rate": 5e-05,
|
| 16 |
+
"max_tokens": 256,
|
| 17 |
+
"pii_variants": [
|
| 18 |
+
"replay",
|
| 19 |
+
"scenarios",
|
| 20 |
+
"scenarios-hard"
|
| 21 |
+
],
|
| 22 |
+
"punctuation_variants": [
|
| 23 |
+
"final-only",
|
| 24 |
+
"all-marks"
|
| 25 |
+
],
|
| 26 |
+
"common_replay_rows": 384,
|
| 27 |
+
"positive_slots": 480,
|
| 28 |
+
"negative_slots": 160,
|
| 29 |
+
"pii_selection": "highest validation full-span protection minus non-gold character masking; ties prefer lower collateral, then earlier listed variant",
|
| 30 |
+
"punctuation_selection": "highest validation typed word-final mark micro F1; ties prefer fewer false marks, then earlier listed variant",
|
| 31 |
+
"pii_threshold": 0.0,
|
| 32 |
+
"checkpoint": "final epoch for every variant",
|
| 33 |
+
"base_revision": "6f96562de9886779ce7d48810dff8f5c4afb6d0b",
|
| 34 |
+
"base_sha256": "764a3ad5aa5f599cd1a93619eba403fb1cce7ad61b5abefd0e2bc129bc34ded5",
|
| 35 |
+
"prior_sha256": "a30303f35671526f97590f30206469ac286b014d11e74133123a758883e993dc"
|
| 36 |
+
},
|
| 37 |
+
"optimizer": {
|
| 38 |
+
"name": "AdamW",
|
| 39 |
+
"weight_decay": 0.01,
|
| 40 |
+
"betas": [
|
| 41 |
+
0.9,
|
| 42 |
+
0.999
|
| 43 |
+
],
|
| 44 |
+
"epsilon": 1e-08,
|
| 45 |
+
"gradient_norm_limit": 1.0,
|
| 46 |
+
"schedule": "constant",
|
| 47 |
+
"learning_rate": 1.5e-05
|
| 48 |
+
},
|
| 49 |
+
"public_base": "distilbert/distilbert-base-german-cased",
|
| 50 |
+
"base_revision": "6f96562de9886779ce7d48810dff8f5c4afb6d0b",
|
| 51 |
+
"initial_files": {
|
| 52 |
+
"model.safetensors": "a30303f35671526f97590f30206469ac286b014d11e74133123a758883e993dc",
|
| 53 |
+
"config.json": "b34a89e171fdbae8dbfed5e9d115d8063771d52938d0d4f8e85f0f68a17adb9a",
|
| 54 |
+
"tokenizer.json": "4c74912a4ea7075eb1e02b89f2533ac1037e74344450f003daaa4f16f97df074",
|
| 55 |
+
"tokenizer_config.json": "d7ed459fc75813d0e50653ff952ca98376cd38c8a465cfc47760ed7497f86c8e",
|
| 56 |
+
"vocab.txt": "bf186f7fb9cb667d811c08b45ce64d1e8e262277bb053f05d1aef3e9f13039a3",
|
| 57 |
+
"special_tokens_map.json": "b6d346be366a7d1d48332dbc9fdf3bf8960b5d879522b7799ddba59e76237ee3"
|
| 58 |
+
},
|
| 59 |
+
"prepared_sha256": {
|
| 60 |
+
"replay": "1bb909b9474b7c8976195a83d045ea548f25e6026e5c34c14f632b50bd2ed647",
|
| 61 |
+
"scenarios": "4eb557d9f21254399fdf75ffb6535e9e30a9d98fb6d10f443c41e221cdd2c99e",
|
| 62 |
+
"scenarios-hard": "03fdcf9c3c0ac76f3b43f31530e5e50b5758ee069d6e57680e27be3b7b4bfc4e"
|
| 63 |
+
},
|
| 64 |
+
"split_hashes": {
|
| 65 |
+
"data/train.jsonl": "6cfb2539bd697a5b190b67b386356108fb4da2797d9ccab05b0c499ae5e8dd28",
|
| 66 |
+
"data/validation.jsonl": "62b3c748035a3cefc3c52f37cb0f649688e0b580a5e43d196a9507f579c8ffef",
|
| 67 |
+
"replay/replay.jsonl": "d0cb30185cc8412cefdbc997f895a27d2a181d791a59d3d6118813809bdc90ae"
|
| 68 |
+
}
|
| 69 |
+
}
|
training/retrain.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env -S uv run --script
|
| 2 |
+
# /// script
|
| 3 |
+
# requires-python = ">=3.11"
|
| 4 |
+
# dependencies = ["torch==2.6.0","transformers==4.49.0"]
|
| 5 |
+
# ///
|
| 6 |
+
"""Run the released controlled ablation using Hub files or offline local folders."""
|
| 7 |
+
import argparse
|
| 8 |
+
import hashlib
|
| 9 |
+
import json
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
import random
|
| 12 |
+
import sys
|
| 13 |
+
import time
|
| 14 |
+
|
| 15 |
+
import torch
|
| 16 |
+
from huggingface_hub import snapshot_download
|
| 17 |
+
from transformers import AutoModelForTokenClassification, AutoTokenizer
|
| 18 |
+
|
| 19 |
+
HERE=Path(__file__).resolve().parent
|
| 20 |
+
MODEL_REPO=HERE.parent
|
| 21 |
+
sys.path.insert(0,str(MODEL_REPO))
|
| 22 |
+
from classifier import batch,encode
|
| 23 |
+
from runtime import MARK_LABELS,pii,punct_encode,punctuation
|
| 24 |
+
from scoring import score,punctuation_score
|
| 25 |
+
|
| 26 |
+
def digest(path):
|
| 27 |
+
with path.open("rb") as stream:
|
| 28 |
+
return hashlib.file_digest(stream,"sha256").hexdigest()
|
| 29 |
+
|
| 30 |
+
def read_split(path,expected):
|
| 31 |
+
if digest(path)!=expected:
|
| 32 |
+
raise ValueError(f"Unexpected split SHA-256: {path.name}")
|
| 33 |
+
return [json.loads(line) for line in path.read_text().splitlines()]
|
| 34 |
+
|
| 35 |
+
def sources(args,recipe):
|
| 36 |
+
task=recipe["task"]
|
| 37 |
+
wanted=["data/train.jsonl","data/validation.jsonl"]
|
| 38 |
+
if task=="pii": wanted.append("replay/replay.jsonl")
|
| 39 |
+
data=args.data_dir or Path(snapshot_download(repo_id=recipe["dataset_id"],repo_type="dataset",allow_patterns=wanted))
|
| 40 |
+
if args.initial_dir:
|
| 41 |
+
initial=args.initial_dir
|
| 42 |
+
elif task=="pii":
|
| 43 |
+
initial=HERE/"initial"
|
| 44 |
+
else:
|
| 45 |
+
initial=Path(snapshot_download(repo_id=recipe["public_base"],revision=recipe["base_revision"],
|
| 46 |
+
allow_patterns=["model.safetensors","config.json","vocab.txt","tokenizer.json","tokenizer_config.json"]))
|
| 47 |
+
for name,expected in recipe["initial_files"].items():
|
| 48 |
+
if digest(initial/name)!=expected:
|
| 49 |
+
raise ValueError("Unexpected initializer file: "+name)
|
| 50 |
+
rows={name:read_split(data/name,recipe["split_hashes"][name]) for name in wanted}
|
| 51 |
+
return initial,rows
|
| 52 |
+
|
| 53 |
+
def variant_rows(task,variant,rows,cfg):
|
| 54 |
+
new=rows["data/train.jsonl"]
|
| 55 |
+
if task=="punctuation": return new
|
| 56 |
+
old=rows["replay/replay.jsonl"]
|
| 57 |
+
n,p=cfg["common_replay_rows"],cfg["positive_slots"]
|
| 58 |
+
sampler=random.Random(cfg["seed"])
|
| 59 |
+
positives=sampler.sample([r for r in new if r["gold"]],p)
|
| 60 |
+
negatives=sampler.sample([r for r in new if not r["gold"]],cfg["negative_slots"])
|
| 61 |
+
return old[:n]+(old[n:n+p] if variant=="replay" else positives)+(negatives if variant=="scenarios-hard" else old[n+p:])
|
| 62 |
+
|
| 63 |
+
def prepared_hash(docs,items):
|
| 64 |
+
digestor=hashlib.sha256()
|
| 65 |
+
for doc,w in zip(docs,items,strict=True):
|
| 66 |
+
digestor.update(json.dumps([doc["id"],w["input_ids"],w["labels"]],separators=(",",":")).encode()+b"\n")
|
| 67 |
+
return digestor.hexdigest()
|
| 68 |
+
|
| 69 |
+
def run_variant(task,variant,initial,rows,recipe,output,prepare_only):
|
| 70 |
+
cfg=recipe["config"]
|
| 71 |
+
torch.manual_seed(cfg["seed"])
|
| 72 |
+
rng=random.Random(cfg["seed"])
|
| 73 |
+
tok=AutoTokenizer.from_pretrained(initial,local_files_only=True)
|
| 74 |
+
kwargs={}
|
| 75 |
+
if task=="punctuation":
|
| 76 |
+
kwargs={"id2label":dict(enumerate(MARK_LABELS)), "label2id":{s:i for i,s in enumerate(MARK_LABELS)}}
|
| 77 |
+
model=AutoModelForTokenClassification.from_pretrained(initial,local_files_only=True,**kwargs)
|
| 78 |
+
docs=variant_rows(task,variant,rows,cfg)
|
| 79 |
+
if task=="pii":
|
| 80 |
+
items=[w for r in docs for w in encode(tok,r,model.config.label2id)]
|
| 81 |
+
else:
|
| 82 |
+
items=[punct_encode(tok,r,final_only=variant=="final-only") for r in docs]
|
| 83 |
+
fingerprint=prepared_hash(docs,items)
|
| 84 |
+
if fingerprint!=recipe["prepared_sha256"][variant]:
|
| 85 |
+
raise ValueError("Prepared rows differ from the frozen study: "+variant)
|
| 86 |
+
info={"rows":len(items),"prepared_sha256":fingerprint,"input_weights_sha256":recipe["initial_files"]["model.safetensors"]}
|
| 87 |
+
print(variant,len(items),fingerprint,flush=True)
|
| 88 |
+
if prepare_only:
|
| 89 |
+
# Include a real forward/backward pass on the first paired, shuffled training batch.
|
| 90 |
+
indices=list(range(len(items)))
|
| 91 |
+
rng.shuffle(indices)
|
| 92 |
+
inputs=batch([items[j] for j in indices[:cfg["batch_size"]]],tok.pad_token_id,"cpu")
|
| 93 |
+
model.train()
|
| 94 |
+
loss=model(**inputs).loss
|
| 95 |
+
loss.backward()
|
| 96 |
+
assert torch.isfinite(loss) and model.classifier.weight.grad is not None
|
| 97 |
+
info["smoke_batch_rows"]=len(inputs["input_ids"])
|
| 98 |
+
return info
|
| 99 |
+
start=time.monotonic()
|
| 100 |
+
model.to("cuda").train()
|
| 101 |
+
optimizer=torch.optim.AdamW(model.parameters(),lr=cfg[task+"_learning_rate"],weight_decay=0.01)
|
| 102 |
+
steps=0
|
| 103 |
+
for epoch in range(cfg["epochs"]):
|
| 104 |
+
indices=list(range(len(items)))
|
| 105 |
+
rng.shuffle(indices)
|
| 106 |
+
size=cfg["batch_size"]
|
| 107 |
+
for i in range(0,len(indices),size):
|
| 108 |
+
features=batch([items[j] for j in indices[i:i+size]],tok.pad_token_id,"cuda")
|
| 109 |
+
optimizer.zero_grad(set_to_none=True)
|
| 110 |
+
with torch.autocast("cuda",dtype=torch.bfloat16,enabled=torch.cuda.is_bf16_supported()):
|
| 111 |
+
loss=model(**features).loss
|
| 112 |
+
loss.backward()
|
| 113 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(),1.)
|
| 114 |
+
optimizer.step()
|
| 115 |
+
steps+=1
|
| 116 |
+
val=rows["data/validation.jsonl"]
|
| 117 |
+
metrics=score(val,pii(model,tok,val)) if task=="pii" else punctuation_score(val,punctuation(model,tok,val))
|
| 118 |
+
dst=output/variant
|
| 119 |
+
model.config._name_or_path=""
|
| 120 |
+
model.save_pretrained(dst,safe_serialization=True)
|
| 121 |
+
tok.save_pretrained(dst)
|
| 122 |
+
info.update(steps=steps,validation=metrics,weights_sha256=digest(dst/"model.safetensors"),seconds=time.monotonic()-start)
|
| 123 |
+
return info
|
| 124 |
+
|
| 125 |
+
def main():
|
| 126 |
+
parser=argparse.ArgumentParser()
|
| 127 |
+
parser.add_argument("--data-dir",type=Path,help="Local companion dataset repository (default: download frozen Hub files)")
|
| 128 |
+
parser.add_argument("--initial-dir",type=Path,help="Offline initializer folder override, verified by SHA-256")
|
| 129 |
+
parser.add_argument("--output",type=Path,required=True,help="New or empty directory")
|
| 130 |
+
parser.add_argument("--variant",default="all",help="One named variant, or all to reproduce the paired selection")
|
| 131 |
+
parser.add_argument("--prepare-only",action="store_true",help="Verify initialization, input ordering and labels on CPU, then test one batch")
|
| 132 |
+
args=parser.parse_args()
|
| 133 |
+
if not args.prepare_only and (not torch.cuda.is_available() or torch.cuda.device_count()!=1):
|
| 134 |
+
parser.error("Expose exactly one CUDA GPU to fit the variants")
|
| 135 |
+
if args.output.exists() and any(args.output.iterdir()): parser.error("Use an empty output directory")
|
| 136 |
+
args.output.mkdir(parents=True,exist_ok=True)
|
| 137 |
+
torch.set_num_threads(2)
|
| 138 |
+
recipe=json.loads((HERE/"recipe.json").read_text())
|
| 139 |
+
task=recipe["task"]
|
| 140 |
+
cfg=recipe["config"]
|
| 141 |
+
variants=cfg[task+"_variants"]
|
| 142 |
+
if args.variant!="all":
|
| 143 |
+
if args.variant not in variants: parser.error("Unknown variant")
|
| 144 |
+
variants=[args.variant]
|
| 145 |
+
initial,rows=sources(args,recipe)
|
| 146 |
+
results={v:run_variant(task,v,initial,rows,recipe,args.output,args.prepare_only) for v in variants}
|
| 147 |
+
report={"task":task,"prepare_only":args.prepare_only,"variants":results}
|
| 148 |
+
if not args.prepare_only:
|
| 149 |
+
def key(v):
|
| 150 |
+
m=results[v]["validation"]
|
| 151 |
+
if task=="pii": return (m["full_entity_mask_rate"]-m["non_gold_mask_rate"],-m["non_gold_mask_rate"])
|
| 152 |
+
return (m["mark_f1"],-m["extra_marks"])
|
| 153 |
+
report["selected"]=max(variants,key=key)
|
| 154 |
+
(args.output/"results.json").write_text(json.dumps(report,indent=2)+"\n")
|
| 155 |
+
if __name__=="__main__":
|
| 156 |
+
main()
|
training/scoring.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import Counter, defaultdict
|
| 2 |
+
|
| 3 |
+
def chars(text, spans):
|
| 4 |
+
return {i for s in spans for i in range(s["start"], s["end"]) if not text[i].isspace()}
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def safe_ratio(a, b):
|
| 8 |
+
return round(a/b, 6) if b else None
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def prf(tp, pred, gold):
|
| 12 |
+
return {"precision": safe_ratio(tp, pred), "recall": safe_ratio(tp, gold),
|
| 13 |
+
"f1": safe_ratio(2*tp, pred+gold)}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def score(rows, predictions):
|
| 17 |
+
total = Counter()
|
| 18 |
+
labels = defaultdict(Counter)
|
| 19 |
+
for row in rows:
|
| 20 |
+
text, gold = row["text"], row["gold"]
|
| 21 |
+
pred = predictions[row["id"]]
|
| 22 |
+
for source in [gold, pred]:
|
| 23 |
+
assert all(0 <= s["start"] < s["end"] <= len(text) for s in source)
|
| 24 |
+
true_chars, pred_chars = chars(text, gold), chars(text, pred)
|
| 25 |
+
protected = len(true_chars & pred_chars)
|
| 26 |
+
bounds = lambda values: {(e["start"],e["end"]) for e in values}
|
| 27 |
+
pb, gb = bounds(pred), bounds(gold)
|
| 28 |
+
typed = lambda values: {(e["start"],e["end"],e["label"]) for e in values}
|
| 29 |
+
total.update(documents=1, gold_entities=len(gold), predicted_entities=len(pred),
|
| 30 |
+
gold_chars=len(true_chars), pred_chars=len(pred_chars), protected_chars=protected,
|
| 31 |
+
non_gold_chars=sum(not c.isspace() for c in text)-len(true_chars),
|
| 32 |
+
overmasked_chars=len(pred_chars-true_chars),
|
| 33 |
+
exact_bounds=len(pb & gb), exact_typed=len(typed(gold) & typed(pred)))
|
| 34 |
+
if not gold:
|
| 35 |
+
total.update(negative_documents=1, negative_documents_flagged=int(bool(pred)))
|
| 36 |
+
missed = 0
|
| 37 |
+
for entity in gold:
|
| 38 |
+
truth = chars(text, [entity])
|
| 39 |
+
covered = int(truth <= pred_chars)
|
| 40 |
+
all_alnum = all(i in pred_chars for i in truth if text[i].isalnum())
|
| 41 |
+
total.update(only_punctuation_unmasked=int(bool(all_alnum) and not covered))
|
| 42 |
+
missed += 1 - covered
|
| 43 |
+
labels[entity["label"]].update(total=1, fully_masked=covered, all_alnum_masked=int(all_alnum), completely_missed=int(not truth & pred_chars))
|
| 44 |
+
total.update(fully_masked=len(gold)-missed, positive_documents_with_any_leak=int(bool(missed)))
|
| 45 |
+
out = dict(total)
|
| 46 |
+
out["full_entity_mask_rate"] = safe_ratio(total["fully_masked"], total["gold_entities"])
|
| 47 |
+
out["non_gold_mask_rate"] = safe_ratio(total["overmasked_chars"], total["non_gold_chars"])
|
| 48 |
+
out["negative_doc_mask_rate"] = safe_ratio(total["negative_documents_flagged"], total["negative_documents"])
|
| 49 |
+
out["source_char"] = prf(total["protected_chars"], total["pred_chars"], total["gold_chars"])
|
| 50 |
+
out["exact_bounds"] = prf(total["exact_bounds"], total["predicted_entities"], total["gold_entities"])
|
| 51 |
+
out["exact_typed"] = prf(total["exact_typed"], total["predicted_entities"], total["gold_entities"])
|
| 52 |
+
out["per_gold_class"] = {k:dict(v) for k,v in sorted(labels.items())}
|
| 53 |
+
return out
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def marks(text, word_list):
|
| 57 |
+
"""Recover word-indexed insertions; an unalignable lexical edit invalidates the row."""
|
| 58 |
+
observed=set()
|
| 59 |
+
i=0
|
| 60 |
+
for word in text.split():
|
| 61 |
+
if i and word=="-":
|
| 62 |
+
observed.add((i-1,"-"))
|
| 63 |
+
continue
|
| 64 |
+
if i==len(word_list) or not word.startswith(word_list[i]):
|
| 65 |
+
return observed,True
|
| 66 |
+
added=word[len(word_list[i]):]
|
| 67 |
+
if any(c not in ".,?:;!-" for c in added):
|
| 68 |
+
return observed,True
|
| 69 |
+
if added:
|
| 70 |
+
observed.add((i,added))
|
| 71 |
+
i+=1
|
| 72 |
+
return observed,i!=len(word_list)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def punctuation_score(rows,pred):
|
| 76 |
+
tp=fp=fn=changed=negative=flagged=exact=0
|
| 77 |
+
for r in rows:
|
| 78 |
+
expected={(s["index"],s["mark"]) for s in r["gold"]}
|
| 79 |
+
actual,edit=marks(pred[r["id"]],r["text"].split())
|
| 80 |
+
changed+=edit
|
| 81 |
+
tp+=len(actual & expected)
|
| 82 |
+
fp+=len(actual-expected)
|
| 83 |
+
fn+=len(expected-actual)
|
| 84 |
+
negative+=not expected
|
| 85 |
+
flagged+=int(not expected and (bool(actual) or edit))
|
| 86 |
+
exact+=int(pred[r["id"]]==r["reference"])
|
| 87 |
+
return {"documents":len(rows),"correct":tp,"extra_marks":fp,"missed_marks":fn,
|
| 88 |
+
"mark_f1":2*tp/max(1,2*tp+fp+fn),"word_changed_documents":changed,
|
| 89 |
+
"negative_documents":negative,"negative_documents_flagged":flagged,"exact_documents":exact}
|
vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|