Views
No views yet
1from peft import PeftModel, PeftConfig
2from transformers import AutoModelForTokenClassification, AutoTokenizer
3
4label_names=['O','B-PERSON','B-GPE','B-ORG','B-LOC','B-DATE','B-EVENT']
5num_labels=len(label_names)
6id_to_label = {i: label for i, label in enumerate(label_names)}
7label_to_id = {label:i for i, label in enumerate(label_names)}
8
9peft_model_id = "ShakhzoDavronov/xlm-roberta-lora-ner-uz"
10config = PeftConfig.from_pretrained(peft_model_id)
11model = AutoModelForTokenClassification.from_pretrained(config.base_model_name_or_path, num_labels=len(label_names),
12 id2label=id_to_label, label2id=label_to_id)
13
14tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
15model = PeftModel.from_pretrained(model, peft_model_id)
16
17from transformers import pipeline
18ner_pipeline=pipeline('ner', model=model, tokenizer=tokenizer)
19
20text="""Toshkentda Shavkat Mirziyoyev Xalqaro dzyudo federatsiyasi rahbarini "Do'stlik" ordeni bilan mukofotladi."""
21ner=ner_pipeline(text)
22for entity in ner:
23 print(entity)1{'entity': 'B-LOC', 'score': 0.70824957, 'index': 1, 'word': '▁Toshkent', 'start': 0, 'end': 8}
2{'entity': 'B-LOC', 'score': 0.6332058, 'index': 2, 'word': 'da', 'start': 8, 'end': 10}
3{'entity': 'B-PERSON', 'score': 0.94302815, 'index': 3, 'word': '▁Shavkat', 'start': 11, 'end': 18}
4{'entity': 'B-PERSON', 'score': 0.9404429, 'index': 4, 'word': '▁Mirziyoyev', 'start': 19, 'end': 29}
5{'entity': 'B-ORG', 'score': 0.8909233, 'index': 5, 'word': '▁X', 'start': 30, 'end': 31}
6{'entity': 'B-ORG', 'score': 0.8900482, 'index': 6, 'word': 'alqaro', 'start': 31, 'end': 37}
7{'entity': 'B-ORG', 'score': 0.8870184, 'index': 7, 'word': '▁', 'start': 38, 'end': 39}
8{'entity': 'B-ORG', 'score': 0.84329146, 'index': 8, 'word': 'dzy', 'start': 38, 'end': 41}
9{'entity': 'B-ORG', 'score': 0.8615051, 'index': 9, 'word': 'udo', 'start': 41, 'end': 44}
10{'entity': 'B-ORG', 'score': 0.8913255, 'index': 10, 'word': '▁', 'start': 45, 'end': 46}
11{'entity': 'B-ORG', 'score': 0.8640603, 'index': 11, 'word': 'feder', 'start': 45, 'end': 50}
12{'entity': 'B-ORG', 'score': 0.8678666, 'index': 12, 'word': 'atsiyasi', 'start': 50, 'end': 58}| Epoch | Training Loss | Validation Loss | Precision | Recall | F1 Score | Accuracy |
|---|---|---|---|---|---|---|
| 1 | 0.214000 | 0.204440 | 0.745021 | 0.718837 | 0.731695 | 0.931259 |
| 2 | 0.197800 | 0.193916 | 0.783191 | 0.697650 | 0.737950 | 0.934701 |
| 3 | 0.182500 | 0.188210 | 0.765977 | 0.731226 | 0.748198 | 0.935774 |