Views
No views yet
token-classification: https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.TokenClassificationPipeline| Label | Meaning |
|---|---|
PER | Persons |
ORG | Organizations |
LOC | Locations |
GEOPOLIT | Geopolitical entities (countries, regions) |
MEDIA | Media outlets and resources |
1from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
2
3label2id = {
4 'O': 0,
5 'B-GEOPOLIT': 1, 'I-GEOPOLIT': 2,
6 'B-MEDIA': 3, 'I-MEDIA': 4,
7 'B-LOC': 5, 'I-LOC': 6,
8 'B-ORG': 7, 'I-ORG': 8,
9 'B-PER': 9, 'I-PER': 10
10}
11id2label = {v: k for k, v in label2id.items()}
12
13model_id = "r1char9/ner-rubert-tiny-RuNews"
14
15tokenizer = AutoTokenizer.from_pretrained(model_id)
16model = AutoModelForTokenClassification.from_pretrained(
17 model_id,
18 num_labels=len(label2id),
19 id2label=id2label,
20 label2id=label2id
21)
22
23ner_pipeline = pipeline(
24 "ner",
25 model=model,
26 tokenizer=tokenizer,
27 aggregation_strategy="simple"
28)
29
30text = (
31 "Генеральный директор Сбербанка Герман Греф на конференции в Москве заявил, "
32 "что сотрудничество с Яндексом в области искусственного интеллекта выходит на новый уровень. "
33 "Он также отметил, что правительство Российской Федерации поддерживает развитие цифровой экономики, "
34 "особенно в рамках Евразийского экономического союза."
35)
36
37results = ner_pipeline(text)
38
39for entity in results:
40 print(entity)
41
42# {'entity_group': 'ORG', 'score': 0.951569, 'word': 'Сбербанка', 'start': 21, 'end': 30}
43# {'entity_group': 'PER', 'score': 0.9922959, 'word': 'Герман Греф', 'start': 31, 'end': 42}
44# {'entity_group': 'LOC', 'score': 0.60198957, 'word': 'Москве', 'start': 60, 'end': 66}
45# {'entity_group': 'ORG', 'score': 0.6973838, 'word': 'Яндексом', 'start': 96, 'end': 104}
46# {'entity_group': 'GEOPOLIT', 'score': 0.9631994, 'word': 'Российской Федерации', 'start': 203, 'end': 223}
47# {'entity_group': 'ORG', 'score': 0.85091865, 'word': 'Евразийского экономического союза.', 'start': 284, 'end': 318}