Views
No views yet
| 指标 | 数值 |
|---|---|
| 实体级别 F1 | 96.29% |
| Micro F1 | 96.29% |
| Macro F1 | 96.70% |
| 序列标注准确率 | 99.91% |
| 目标实体召回率 | 99.55% |
| 推理延迟 (GPU) | ~90ms / 512 tokens |
| 实体类型 | 说明 | Precision | Recall | F1 | 测试样本数 |
|---|---|---|---|---|---|
name | 姓名 | 99.33% | 99.66% | 99.50% | 1,194 |
id_card | 身份证号 | 98.85% | 99.23% | 99.04% | 1,039 |
mobile | 手机号 | 99.56% | 99.91% | 99.73% | 1,123 |
address | 地址 | 99.83% | 100.0% | 99.91% | 1,163 |
email | 邮箱 | 97.65% | 97.65% | 97.65% | 1,107 |
passport | 护照号 | 99.82% | 99.82% | 99.82% | 1,109 |
hkmtp_pass | 港澳通行证 | 99.74% | 99.74% | 99.74% | 1,153 |
social_security | 社会保障号 | 99.29% | 99.90% | 99.59% | 975 |
drivers_license | 驾驶证号 | 99.44% | 99.72% | 99.58% | 1,059 |
plate_number | 车牌号 | 99.47% | 99.82% | 99.64% | 1,118 |
bank_card | 银行卡号 | 99.57% | 99.78% | 99.67% | 1,377 |
credit_card | 信用卡号 | 99.58% | 99.92% | 99.75% | 1,193 |
bank_password | 银行密码 | 99.14% | 100.0% | 99.57% | 1,386 |
birth_date | 出生日期 | 95.86% | 98.01% | 96.92% | 1,206 |
insurance_policy | 保险单号 | 99.24% | 99.75% | 99.49% | 1,182 |
taobao_order | 淘宝订单号 | 98.94% | 99.56% | 99.25% | 1,128 |
jd_order | 京东订单号 | 99.43% | 99.76% | 99.59% | 1,228 |
pdd_order | 拼多多订单号 | 99.23% | 99.66% | 99.45% | 1,170 |
ems_tracking | EMS快递单号 | 100.0% | 100.0% | 100.0% | 1,257 |
sf_tracking | 顺丰快递单号 | 99.75% | 99.75% | 99.75% | 1,184 |
yto_tracking | 圆通快递单号 | 36.23% | 53.17% | 43.10% | 1,104 |
1"name": "汪豹锐",
2"id_card": "340203200210257432",
3"birth_date": "2002-10-25",
4"address": "安徽省芜湖市弋江区新华路17号楼1399室",
5"mobile": "13717627942",
6"email": "wang.uz02@sina.com",
7"passport": "K20138207",
8"hkmtp_pass": "W32765504",
9"social_security": "340203200210257432",
10"drivers_license": "340203200210257432",
11"plate_number": "皖C·44059",
12"bank_card": "6225880443899783447",
13"credit_card": "5588600092873914",
14"bank_password": "102574",
15"insurance_policy": "PAB202204089626802",
16"taobao_order": "26030304024271434099",
17"jd_order": "2952756430649672146",
18"pdd_order": "260607-8514645871356",
19"ems_tracking": "9895844345593",
20"sf_tracking": "SF0642806007386",
21"yto_tracking": "YT530178350300"⚠️ 注意:yto_tracking(圆通快递单号)因格式特征较弱,召回率较低,建议在实际使用中结合规则后处理进行增强。
pip install transformers torch1from transformers import AutoTokenizer, AutoModelForTokenClassification
2import torch
3
4model_name = "ZJUICSR/AIguard-pii-detection-fast"
5
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForTokenClassification.from_pretrained(model_name)
8
9# 自动获取标签映射
10id2label = model.config.id2label1def predict_pii(text, max_length=512):
2 inputs = tokenizer(
3 text,
4 return_tensors="pt",
5 truncation=True,
6 max_length=max_length,
7 return_offsets_mapping=True
8 )
9 offset_mapping = inputs.pop("offset_mapping")[0].tolist()
10
11 with torch.no_grad():
12 outputs = model(**inputs)
13
14 predictions = torch.argmax(outputs.logits, dim=-1)[0].tolist()
15 tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
16
17 # BIOE 解码
18 entities = []
19 current_entity = None
20
21 for idx, (pred_id, (start, end)) in enumerate(zip(predictions, offset_mapping)):
22 label = id2label[pred_id]
23
24 if label.startswith("B-"):
25 if current_entity:
26 entities.append(current_entity)
27 current_entity = {
28 "start": start,
29 "end": end,
30 "label": label[2:],
31 "text": text[start:end]
32 }
33 elif label.startswith("I-") or label.startswith("E-"):
34 if current_entity and current_entity["label"] == label[2:]:
35 current_entity["end"] = end
36 current_entity["text"] = text[current_entity["start"]:end]
37 if label.startswith("E-"):
38 entities.append(current_entity)
39 current_entity = None
40 else:
41 if current_entity:
42 entities.append(current_entity)
43 current_entity = None
44
45 if current_entity:
46 entities.append(current_entity)
47
48 return {"text": text, "entities": entities}
49
50# 示例
51text = "你好,我叫张三,身份证号是110101199001011234,手机号13800138000。"
52result = predict_pii(text)
53print(result)1{
2 "text": "你好,我叫张三,身份证号是110101199001011234,手机号13800138000。",
3 "entities": [
4 {"start": 6, "end": 8, "label": "name", "text": "张三"},
5 {"start": 14, "end": 32, "label": "id_card", "text": "110101199001011234"},
6 {"start": 37, "end": 48, "label": "mobile", "text": "13800138000"}
7 ]
8}DataLoader 进行批处理yto_tracking 等弱特征实体,可结合正则表达式进行二次校验yto_tracking 的识别能力较弱,建议生产环境结合规则引擎