Token Classification
Transformers
ONNX
Safetensors
Transformers.js
modernbert
pii
pii-detection
ner
privacy
anonymization
redaction
de-identification
chinese
japanese
korean
cjk
mmbert
个人信息
脱敏
Eval Results (legacy)
Instructions to use CheezOne/pii-detector-cjk-small with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use CheezOne/pii-detector-cjk-small with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="CheezOne/pii-detector-cjk-small")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("CheezOne/pii-detector-cjk-small") model = AutoModelForTokenClassification.from_pretrained("CheezOne/pii-detector-cjk-small", device_map="auto") - Transformers.js
How to use CheezOne/pii-detector-cjk-small with Transformers.js:
// npm i @huggingface/transformers import { pipeline } from '@huggingface/transformers'; // Allocate pipeline const pipe = await pipeline('token-classification', 'CheezOne/pii-detector-cjk-small'); - Notebooks
- Google Colab
- Kaggle
Download scripts/quant_sweep.py from CheezOne/pii-detector-cjk-small: direct link, hf CLI and curl.
- Browser
- Download file 2.15 kB
-
https://huggingface.co/CheezOne/pii-detector-cjk-small/resolve/main/scripts/quant_sweep.py
- Command line
-
hf download hf://CheezOne/pii-detector-cjk-small/scripts/quant_sweep.py
-
curl -L -o quant_sweep.py https://huggingface.co/CheezOne/pii-detector-cjk-small/resolve/main/scripts/quant_sweep.py
2.15 kB
| """Try int8 dynamic-quantization variants and report F1 / redaction recall vs fp32.""" | |
| import sys, os, json, time, numpy as np, onnx | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| from transformers import AutoTokenizer | |
| import onnxruntime as ort | |
| from onnxruntime.quantization import quantize_dynamic, QuantType | |
| from common import read_jsonl, decode_spans, evaluate | |
| md, test = sys.argv[1], sys.argv[2] | |
| fp32 = f"{md}/onnx/model.onnx"; tok = AutoTokenizer.from_pretrained(md); ex = read_jsonl(test, 200) | |
| g = onnx.load(fp32, load_external_data=False) | |
| mm = [n.name for n in g.graph.node if n.op_type in ("MatMul", "Gemm")] | |
| head = [n for n in mm if "classifier" in n or "head" in n] | |
| print("matmuls", len(mm), "head nodes", head) | |
| def score(path): | |
| s = ort.InferenceSession(path, providers=["CPUExecutionProvider"]); preds = [] | |
| for e in ex: | |
| enc = tok(e["text"], return_offsets_mapping=True, return_special_tokens_mask=True) | |
| lg = s.run(None, {"input_ids": np.array([enc["input_ids"]]), "attention_mask": np.ones((1, len(enc["input_ids"])), dtype=np.int64)})[0][0] | |
| preds.append(decode_spans(e["text"], enc["offset_mapping"], lg.argmax(-1).tolist(), enc["special_tokens_mask"])) | |
| r = evaluate(ex, preds)["overall"]; return r["f1"], r["redact_recall"], round(os.path.getsize(path) / 1e6) | |
| # --- round 2: selective quantization + fp16 --- | |
| attn = [n for n in mm if "attn" in n]; mlp = [n for n in mm if "mlp" in n] | |
| print("attn", len(attn), "mlp", len(mlp), "example", (attn + mlp)[:3]) | |
| gathers = [n.name for n in g.graph.node if n.op_type == "Gather" and "embeddings" in n.name] | |
| for k, kw in { | |
| "emb_only": dict(op_types_to_quantize=["Gather"]), | |
| "emb_attn": dict(per_channel=True, op_types_to_quantize=["Gather", "MatMul"], nodes_to_exclude=head + mlp), | |
| }.items(): | |
| out = f"{md}/onnx/q_{k}.onnx"; quantize_dynamic(fp32, out, weight_type=QuantType.QInt8, **kw); print(k, score(out), flush=True) | |
| from onnxconverter_common import float16 | |
| m16 = float16.convert_float_to_float16(onnx.load(fp32), keep_io_types=True) | |
| onnx.save(m16, f"{md}/onnx/model_fp16.onnx"); print("fp16", score(f"{md}/onnx/model_fp16.onnx")) | |