Views
No views yet
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.<TSTART> and <TEND>, and the model predicts the corresponding entity class.1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4# Load tokenizer and model
5model = AutoModelForSequenceClassification.from_pretrained("pepegiallo/flan-t5-base_ner")
6tokenizer = AutoTokenizer.from_pretrained("pepegiallo/flan-t5-base_ner")
7
8# Helper: wrap a token with <TSTART> and <TEND>
9def wrap_token(text, target_token, tstart="<TSTART>", tend="<TEND>"):
10 return text.replace(target_token, f"{tstart} {target_token} {tend}")
11
12text = "The headquarters of Microsoft is in Redmond."
13target_token = "Microsoft"
14prompt = "classify token in: " + wrap_token(text, target_token)
15
16inputs = tokenizer(prompt, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
17outputs = model(**inputs)
18label_id = torch.argmax(outputs.logits, dim=-1).item()
19
20id2label = {0: "LOC", 1: "ORG", 2: "PER", 3: "O"}
21print("Predicted entity:", id2label[label_id])wikiann (unimelb-nlp)open-pii-masking-500k-ai4privacy<TSTART> / <TEND> tags| 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 |
1@misc{flan-t5-ner,
2 title={Token Classification with Flan-T5 Encoder},
3 author={pepegiallo},
4 year={2025},
5 howpublished={\url{https://huggingface.co/pepegiallo/flan-t5-base_ner}}
6}