This model is a fine-tuned version of
FacebookAI/xlm-roberta-large on the Uzbek Ner dataset.
It achieves the following results on the evaluation set:
-
from transformers import AutoTokenizer, AutoModelForTokenClassification
-
import torch
-
custom_id2label = {
0: "O", 1: "B-CARDINAL", 2: "I-CARDINAL", 3: "B-DATE", 4: "I-DATE",
5: "B-EVENT", 6: "I-EVENT", 7: "B-GPE", 8: "I-GPE", 9: "B-LOC", 10: "I-LOC",
11: "B-MONEY", 12: "I-MONEY", 13: "B-ORDINAL", 14: "B-ORG", 15: "I-ORG",
16: "B-PERCENT", 17: "I-PERCENT", 18: "B-PERSON", 19: "I-PERSON",
20: "B-TIME", 21: "I-TIME"
}
-
custom_label2id = {v: k for k, v in custom_id2label.items()}
-
model_name = "mustafoyev202/roberta-uz"
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
-
model = AutoModelForTokenClassification.from_pretrained(model_name, num_labels=23)
-
model.config.id2label = custom_id2label
-
model.config.label2id = custom_label2id
-
text = "Tesla kompaniyasi AQSHda joylashgan."
-
tokens = tokenizer(text.split(), return_tensors="pt", is_split_into_words=True)
-
with torch.no_grad():
logits = model(**tokens).logits
-
predicted_token_class_ids = logits.argmax(-1).squeeze().tolist()
-
word_ids = tokens.word_ids()
-
previous_word_id = None
-
word_predictions = {}
-
for i, word_id in enumerate(word_ids):
if word_id is not None:
label = custom_id2label[predicted_token_class_ids[i]]
if word_id != previous_word_id: # New word
word_predictions[word_id] = label
previous_word_id = word_id
-
words = text.split() # Splitting for simplicity
-
final_predictions = [(word, word_predictions.get(i, "O")) for i, word in enumerate(words)]
-
print("Predictions:")
-
for word, label in final_predictions:
print(f"{word}: {label}")
-
labels = torch.tensor([predicted_token_class_ids]).unsqueeze(0) # Adjust dimensions
-
loss = model(**tokens, labels=labels).loss
-
print("\nLoss:", round(loss.item(), 2))