daviddrzik commited on
Commit
e1366ac
·
verified ·
1 Parent(s): 8b070de

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +148 -3
README.md CHANGED
@@ -1,3 +1,148 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - sk
5
+ pipeline_tag: token-classification
6
+ library_name: transformers
7
+ metrics:
8
+ - f1
9
+ base_model: daviddrzik/SK_BPE_BLM
10
+ tags:
11
+ - ner
12
+ ---
13
+
14
+ # Fine-Tuned Named Entity Recognition (NER) Model - SK_BPE_BLM (NER Tags)
15
+
16
+ ## Model Overview
17
+ This model is a fine-tuned version of the [SK_BPE_BLM model](https://huggingface.co/daviddrzik/SK_BPE_BLM) for tokenization and Named Entity Recognition (NER). For this task, we utilized the manually annotated [WikiGoldSK dataset]( https://github.com/NaiveNeuron/WikiGoldSK), which was created from 412 articles from the Slovak Wikipedia. The dataset contains annotations for four main categories of entities: Person (PER), Location (LOC), Organization (ORG), and Miscellaneous (MISC).
18
+
19
+ ## NER Tags
20
+ Each token in the dataset is annotated with one of the following NER tags:
21
+ - **O (0):** Regular text (not an entity)
22
+ - **B-PER (1):** Beginning of a person entity
23
+ - **I-PER (2):** Continuation of a person entity
24
+ - **B-LOC (3):** Beginning of a location entity
25
+ - **I-LOC (4):** Continuation of a location entity
26
+ - **B-ORG (5):** Beginning of an organization entity
27
+ - **I-ORG (6):** Continuation of an organization entity
28
+ - **B-MISC (7):** Beginning of a miscellaneous entity
29
+ - **I-MISC (8):** Continuation of a miscellaneous entity
30
+
31
+ ## Dataset Details
32
+ The WikiGoldSK dataset, which contains a total of **6,633** sequences, was adapted for this NER task. The dataset was originally split into training, validation, and test sets, but for our research, we combined all parts and evaluated the model using stratified 10-fold cross-validation. Each token in the text, including words and punctuation, was annotated with the appropriate NER tag.
33
+
34
+ ## Fine-Tuning Hyperparameters
35
+
36
+ The following hyperparameters were used during the fine-tuning process:
37
+
38
+ - **Learning Rate:** 3e-05
39
+ - **Training Batch Size:** 64
40
+ - **Evaluation Batch Size:** 64
41
+ - **Seed:** 42
42
+ - **Optimizer:** Adam (default)
43
+ - **Number of Epochs:** 10
44
+
45
+ ## Model Performance
46
+
47
+ The model was evaluated using stratified 10-fold cross-validation, achieving a weighted F1-score with a median value of <span style="font-size: 24px;">**0.9565**</span>.
48
+
49
+ ## Model Usage
50
+
51
+ This model is suitable for tokenization and NER tasks in Slovak text. It is specifically designed for applications requiring accurate identification and categorization of named entities in various Slovak texts.
52
+
53
+ ### Example Usage
54
+
55
+ Below is an example of how to use the fine-tuned `SK_Morph_BLM-ner ` model in a Python script:
56
+
57
+ ```python
58
+ import torch
59
+ from transformers import RobertaForTokenClassification, RobertaTokenizerFast
60
+ from huggingface_hub import hf_hub_download
61
+ import json
62
+
63
+ class TokenClassifier:
64
+ def __init__(self, model, tokenizer):
65
+ self.model = RobertaForTokenClassification.from_pretrained(model, num_labels=10)
66
+ self.tokenizer = RobertaTokenizerFast.from_pretrained(tokenizer, max_length=256)
67
+ byte_utf8_mapping_path = hf_hub_download(repo_id=tokenizer, filename="byte_utf8_mapping.json")
68
+ with open(byte_utf8_mapping_path, "r", encoding="utf-8") as f:
69
+ self.byte_utf8_mapping = json.load(f)
70
+
71
+ def decode(self, tokens):
72
+ decoded_tokens = []
73
+ for token in tokens:
74
+ for k, v in self.byte_utf8_mapping.items():
75
+ if k in token:
76
+ token = token.replace(k, v)
77
+ token = token.replace("Ġ"," ")
78
+ decoded_tokens.append(token)
79
+ return decoded_tokens
80
+
81
+ def tokenize_text(self, text):
82
+ encoded_text = self.tokenizer(text.lower(), max_length=256, padding='max_length', truncation=True, return_tensors='pt')
83
+ return encoded_text
84
+
85
+ def classify_tokens(self, text):
86
+ encoded_text = self.tokenize_text(text)
87
+ tokens = self.tokenizer.convert_ids_to_tokens(encoded_text['input_ids'].squeeze().tolist())
88
+
89
+ with torch.no_grad():
90
+ output = self.model(**encoded_text)
91
+ logits = output.logits
92
+ predictions = torch.argmax(logits, dim=-1)
93
+
94
+ active_loss = encoded_text['attention_mask'].view(-1) == 1
95
+ active_logits = logits.view(-1, self.model.config.num_labels)[active_loss]
96
+ active_predictions = predictions.view(-1)[active_loss]
97
+
98
+ probabilities = torch.softmax(active_logits, dim=-1)
99
+
100
+ results = []
101
+ for token, pred, prob in zip(self.decode(tokens), active_predictions.tolist(), probabilities.tolist()):
102
+ if token not in ['<s>', '</s>', '<pad>']:
103
+ result = f"Token: {token: <10} NER tag: ({self.model.config.id2label[pred]} = {max(prob):.4f})"
104
+ results.append(result)
105
+
106
+ return results
107
+
108
+ # Instantiate the NER classifier with the specified tokenizer and model
109
+ classifier = TokenClassifier(tokenizer="daviddrzik/SK_BPE_BLM", model="daviddrzik/SK_BPE_BLM-ner")
110
+
111
+ # Tokenize the input text
112
+ text_to_classify = "Dávid Držík je interný doktorand na Fakulte prírodných vied a informatiky UKF v Nitre na Slovensku."
113
+
114
+ # Classify the NER tags of the tokenized text
115
+ classification_results = classifier.classify_tokens(text_to_classify)
116
+ print(f"============= NER Token Classification =============")
117
+ print("Text to classify:", text_to_classify)
118
+ for classification_result in classification_results:
119
+ print(classification_result)
120
+ ```
121
+
122
+ Example Output
123
+ Here is the output when running the above example:
124
+ ```yaml
125
+ ============= NER Token Classification =============
126
+ Text to classify: Dávid Držík je interný doktorand na Fakulte prírodných vied a informatiky UKF v Nitre na Slovensku.
127
+ Token: dá NER tag: (B-PER = 0.9673)
128
+ Token: vid NER tag: (B-PER = 0.9816)
129
+ Token: drží NER tag: (I-PER = 0.6309)
130
+ Token: k NER tag: (I-PER = 0.6584)
131
+ Token: je NER tag: (O = 0.9970)
132
+ Token: inter NER tag: (O = 0.9005)
133
+ Token: ný NER tag: (O = 0.9833)
134
+ Token: doktorand NER tag: (O = 0.8623)
135
+ Token: na NER tag: (O = 0.9965)
136
+ Token: fakulte NER tag: (B-ORG = 0.9886)
137
+ Token: prírodných NER tag: (I-ORG = 0.8822)
138
+ Token: vied NER tag: (I-ORG = 0.9970)
139
+ Token: a NER tag: (I-ORG = 0.9908)
140
+ Token: informatiky NER tag: (I-ORG = 0.9849)
141
+ Token: ukf NER tag: (I-ORG = 0.9112)
142
+ Token: v NER tag: (I-ORG = 0.9969)
143
+ Token: nitre NER tag: (I-ORG = 0.9790)
144
+ Token: na NER tag: (O = 0.9744)
145
+ Token: slovensku NER tag: (B-LOC = 0.9944)
146
+ Token: . NER tag: (O = 0.9767)
147
+ ```
148
+