Transformers
Safetensors
flan-t5-classifier
File size: 4,240 Bytes
eef7067
 
142baea
 
 
 
 
 
 
 
 
 
 
 
eef7067
 
142baea
eef7067
142baea
eef7067
142baea
eef7067
 
 
 
 
142baea
eef7067
142baea
 
 
 
 
eef7067
 
 
 
 
142baea
eef7067
142baea
 
 
eef7067
 
 
142baea
 
eef7067
 
 
142baea
 
 
eef7067
142baea
 
 
eef7067
142baea
 
 
eef7067
142baea
 
 
eef7067
142baea
 
 
eef7067
142baea
 
 
eef7067
142baea
eef7067
142baea
eef7067
142baea
eef7067
142baea
 
 
eef7067
142baea
eef7067
142baea
 
 
 
 
 
eef7067
 
 
142baea
eef7067
142baea
eef7067
142baea
 
 
 
eef7067
 
 
142baea
 
 
 
 
eef7067
142baea
eef7067
 
 
 
 
 
 
 
142baea
eef7067
142baea
 
 
eef7067
 
 
142baea
 
 
 
 
 
 
 
eef7067
 
 
142baea
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
library_name: transformers
datasets:
- unimelb-nlp/wikiann
- ai4privacy/open-pii-masking-500k-ai4privacy
- custom
language:
- en
- de
- es
- it
- fr
base_model:
- google/flan-t5-base
---

# Model Card for Flan-T5 Base Token Classifier (NER: LOC, ORG, PER)

This model is a fine-tuned encoder-only version of `google/flan-t5-base` for **token-level Named Entity Recognition (NER)**. It predicts entity labels (e.g., LOC, ORG, PER) by classifying **individual tokens** in a prompting setup using `<TSTART>` and `<TEND>` markers.

---

## Model Details

### Model Description

This model is based on the encoder of the T5 architecture and has been fine-tuned for single-token classification using a prompt-driven approach. Given a sentence, one token is wrapped with `<TSTART>` and `<TEND>`, and the model predicts the corresponding entity class.

- **Developed by:** pepegiallo
- **Model type:** Encoder-only token classifier
- **Language(s) (NLP):** en, de, fr, it, es
- **License:** MIT
- **Finetuned from model:** google/flan-t5-base

## Uses

### Direct Use

You can use this model to classify named entities (PER, ORG, LOC, or O) one token at a time. This approach is suitable for tasks such as:

- PII detection
- Privacy-preserving document redaction
- Legal or medical text anonymization

### Out-of-Scope Use

- Full-sequence tagging (this model is optimized for classifying one token at a time)
- Multi-token entity recognition without aggregation logic

## How to Get Started with the Model

```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

# Load tokenizer and model
model = AutoModelForSequenceClassification.from_pretrained("pepegiallo/flan-t5-base_ner")
tokenizer = AutoTokenizer.from_pretrained("pepegiallo/flan-t5-base_ner")

# Helper: wrap a token with <TSTART> and <TEND>
def wrap_token(text, target_token, tstart="<TSTART>", tend="<TEND>"):
    return text.replace(target_token, f"{tstart} {target_token} {tend}")

text = "The headquarters of Microsoft is in Redmond."
target_token = "Microsoft"
prompt = "classify token in: " + wrap_token(text, target_token)

inputs = tokenizer(prompt, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
outputs = model(**inputs)
label_id = torch.argmax(outputs.logits, dim=-1).item()

id2label = {0: "LOC", 1: "ORG", 2: "PER", 3: "O"}
print("Predicted entity:", id2label[label_id])
```

---

## Training Details

The model was fine-tuned for 3 epochs on a multilingual, balanced dataset combining:

- `wikiann` (unimelb-nlp)
- `open-pii-masking-500k-ai4privacy`
- Custom annotated examples with `<TSTART>` / `<TEND>` tags

### Training Hyperparameters

- Model: google/flan-t5-base (encoder only)
- Batch size: 128
- Max input length: 128
- Optimizer: AdamW
- Learning rate: 3e-5
- Epochs: 3

## Evaluation

### Metrics

The model was evaluated using the following metrics:

- Accuracy
- Precision (Macro)
- Recall (Macro)
- F1 Score (Macro)

### Results

| Epoch | Training Loss | Validation Loss | Accuracy | Precision (Macro) | Recall (Macro) | F1 (Macro) |
|-------|---------------|-----------------|----------|-------------------|----------------|------------|
| 1     | 0.1702        | 0.1504          | 95.21%   | 0.9521            | 0.9521         | 0.9521     |
| 2     | 0.1444        | 0.1310          | 95.89%   | 0.9588            | 0.9589         | 0.9589     |
| 3     | 0.1290        | 0.1246          | 96.14%   | 0.9614            | 0.9614         | 0.9614     |

---

## Environmental Impact

- **Hardware Type:** [More Information Needed]
- **Hours used:** [More Information Needed]
- **Cloud Provider:** [More Information Needed]
- **Compute Region:** [More Information Needed]

## Technical Specifications

- **Architecture:** T5 Encoder + Dense Classification Head
- **Precision:** fp32
- **Framework:** PyTorch + Huggingface Transformers

## Citation [optional]

```bibtex
@misc{flan-t5-ner,
  title={Token Classification with Flan-T5 Encoder},
  author={pepegiallo},
  year={2025},
  howpublished={\url{https://huggingface.co/pepegiallo/flan-t5-base_ner}}
}
```

## Model Card Contact

For questions, contact: [https://huggingface.co/pepegiallo](https://huggingface.co/pepegiallo)