Token Classification
Transformers
PyTorch
Chinese
named-entity-recognition
ner
ernie
crf
chinese-nlp
person-name-extraction
financial-documents
Instructions to use warfbro/Human-Name-extraction with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use warfbro/Human-Name-extraction with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="warfbro/Human-Name-extraction")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("warfbro/Human-Name-extraction", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload model.py with huggingface_hub
Browse files
model.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ERINE + CRF 模型"""
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from torchcrf import CRF
|
| 5 |
+
from transformers import AutoModel, AutoTokenizer
|
| 6 |
+
|
| 7 |
+
from config import ERNIE_LOCAL, BIO_LABELS, CHECKPOINT, CHECKPOINT_FROZEN, CHECKPOINT_FC2
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class ErnieCRF(nn.Module):
|
| 11 |
+
"""ERNIE 3.0 encoder + Linear + CRF"""
|
| 12 |
+
|
| 13 |
+
def __init__(self, model_path, num_labels):
|
| 14 |
+
super().__init__()
|
| 15 |
+
self.ernie = AutoModel.from_pretrained(model_path)
|
| 16 |
+
self.fc = nn.Linear(self.ernie.config.hidden_size, num_labels)
|
| 17 |
+
self.crf = CRF(num_labels, batch_first=True)
|
| 18 |
+
|
| 19 |
+
def forward(self, input_ids, attention_mask, labels=None):
|
| 20 |
+
mask = attention_mask.bool()
|
| 21 |
+
hidden = self.ernie(input_ids, attention_mask=attention_mask).last_hidden_state
|
| 22 |
+
emissions = self.fc(hidden)
|
| 23 |
+
if labels is not None:
|
| 24 |
+
return -self.crf(emissions, labels, mask=mask, reduction="mean")
|
| 25 |
+
return self.crf.decode(emissions, mask=mask)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class ErnieCRF2(nn.Module):
|
| 29 |
+
"""ERNIE 3.0 encoder + 双层FC(hidden→3*hidden→3) + CRF"""
|
| 30 |
+
|
| 31 |
+
def __init__(self, model_path, num_labels, hidden_factor=3):
|
| 32 |
+
super().__init__()
|
| 33 |
+
self.ernie = AutoModel.from_pretrained(model_path)
|
| 34 |
+
self.hidden_size = self.ernie.config.hidden_size
|
| 35 |
+
mid_size = self.hidden_size * hidden_factor
|
| 36 |
+
self.fc1 = nn.Linear(self.hidden_size, mid_size)
|
| 37 |
+
self.fc2 = nn.Linear(mid_size, num_labels)
|
| 38 |
+
self.relu = nn.ReLU()
|
| 39 |
+
self.dropout = nn.Dropout(0.1)
|
| 40 |
+
self.crf = CRF(num_labels, batch_first=True)
|
| 41 |
+
|
| 42 |
+
def forward(self, input_ids, attention_mask, labels=None):
|
| 43 |
+
mask = attention_mask.bool()
|
| 44 |
+
hidden = self.ernie(input_ids, attention_mask=attention_mask).last_hidden_state
|
| 45 |
+
x = self.dropout(self.relu(self.fc1(hidden)))
|
| 46 |
+
emissions = self.fc2(x)
|
| 47 |
+
if labels is not None:
|
| 48 |
+
return -self.crf(emissions, labels, mask=mask, reduction="mean")
|
| 49 |
+
return self.crf.decode(emissions, mask=mask)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def load_model(device="cuda", frozen=False, fc2=False):
|
| 53 |
+
"""加载训练好的模型和 tokenizer"""
|
| 54 |
+
tokenizer = AutoTokenizer.from_pretrained(ERNIE_LOCAL)
|
| 55 |
+
if fc2:
|
| 56 |
+
model = ErnieCRF2(ERNIE_LOCAL, len(BIO_LABELS), hidden_factor=2).to(device)
|
| 57 |
+
ckpt = CHECKPOINT_FC2
|
| 58 |
+
else:
|
| 59 |
+
model = ErnieCRF(ERNIE_LOCAL, len(BIO_LABELS)).to(device)
|
| 60 |
+
ckpt = CHECKPOINT_FROZEN if frozen else CHECKPOINT
|
| 61 |
+
model.load_state_dict(torch.load(ckpt, map_location=device, weights_only=True))
|
| 62 |
+
model.eval()
|
| 63 |
+
return model, tokenizer
|