Token Classification
Safetensors
Tatar
bert
tatar
morphology
ArabovMK commited on
Commit
603a8ec
·
verified ·
1 Parent(s): d901598

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +7 -6
README.md CHANGED
@@ -56,17 +56,18 @@ inputs = tokenizer(tokens, is_split_into_words=True, return_tensors="pt", trunca
56
  outputs = model(**inputs)
57
  predictions = torch.argmax(outputs.logits, dim=2)
58
 
59
- # Load tag mappings
60
- import json
61
- with open("id2tag.json", "r") as f:
62
- id2tag = json.load(f)
63
 
64
- # Convert predictions to tags
65
  word_ids = inputs.word_ids()
66
  prev_word = None
67
  for idx, word_idx in enumerate(word_ids):
68
  if word_idx is not None and word_idx != prev_word:
69
- tag = id2tag[str(predictions[0][idx].item())]
 
 
 
 
70
  print(tokens[word_idx], "->", tag)
71
  prev_word = word_idx
72
  ```
 
56
  outputs = model(**inputs)
57
  predictions = torch.argmax(outputs.logits, dim=2)
58
 
59
+ # Get tag mapping from model config
60
+ id2tag = model.config.id2label
 
 
61
 
 
62
  word_ids = inputs.word_ids()
63
  prev_word = None
64
  for idx, word_idx in enumerate(word_ids):
65
  if word_idx is not None and word_idx != prev_word:
66
+ tag_id = predictions[0][idx].item()
67
+ if isinstance(id2tag, dict):
68
+ tag = id2tag.get(str(tag_id), id2tag.get(tag_id, "UNK"))
69
+ else:
70
+ tag = id2tag[tag_id] if tag_id < len(id2tag) else "UNK"
71
  print(tokens[word_idx], "->", tag)
72
  prev_word = word_idx
73
  ```