Evaluated on
myanmar-ner-dataset test split using seqeval metrics:
1from transformers import pipeline
2
3ner = pipeline("token-classification", model="chuuhtetnaing/myanmar-ner-model", grouped_entities=True)
4result = ner("ကိုမောင်သည်ရန်ကုန်မြို့သို့သွားသည်။") # Ko Maung went to Yangon city
5print(result)
1!pip install seqeval
2
3from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer
4from datasets import load_dataset
5from tqdm import tqdm
6from seqeval.metrics import classification_report
7
8# Load model and tokenizer
9model = AutoModelForTokenClassification.from_pretrained("chuuhtetnaing/myanmar-ner-model")
10tokenizer = AutoTokenizer.from_pretrained("chuuhtetnaing/myanmar-ner-model")
11
12def tokenize_and_align_labels(examples):
13 tokenized_inputs = tokenizer(examples["tokens"], truncation=True, is_split_into_words=True)
14 labels = []
15 for i, label in enumerate(examples["ner_tags"]):
16 word_ids = tokenized_inputs.word_ids(batch_index=i)
17 previous_word_idx = None
18 label_ids = []
19 for word_idx in word_ids:
20 if word_idx is None:
21 label_ids.append(-100)
22 elif word_idx != previous_word_idx:
23 label_ids.append(label[word_idx])
24 else:
25 label_ids.append(-100)
26 previous_word_idx = word_idx
27 labels.append(label_ids)
28 tokenized_inputs["labels"] = labels
29 return tokenized_inputs
30
31# Load and tokenize dataset
32ner = pipeline("token-classification", model="chuuhtetnaing/myanmar-ner-model", aggregation_strategy=None)
33ds = load_dataset("chuuhtetnaing/myanmar-ner-dataset")
34tokenized_ds = ds.map(tokenize_and_align_labels, batched=True)
35test_ds = tokenized_ds["test"]
36
37# Get label mapping
38label_list = model.config.id2label
39
40y_true = []
41y_pred = []
42
43for example in tqdm(test_ds):
44 tokens = tokenizer.convert_ids_to_tokens(example["input_ids"])
45 true_labels = [label_list[l] if l != -100 else "O" for l in example["labels"]]
46
47 text = tokenizer.decode(example["input_ids"], skip_special_tokens=True)
48 preds = ner(text)
49
50 pred_labels = ["O"] * len(true_labels)
51 for pred in preds:
52 idx = pred["index"]
53 if idx < len(pred_labels):
54 pred_labels[idx] = pred["entity"]
55
56 y_true.append([label_list[l] for l in example["labels"] if l != -100])
57 y_pred.append([p for p, l in zip(pred_labels, example["labels"]) if l != -100])
58
59print(classification_report(y_true, y_pred))