boltuix commited on
Commit
c23d33f
Β·
verified Β·
1 Parent(s): 39e6c9c

Update README.md

Browse files

Code example updated

Files changed (1) hide show
  1. README.md +38 -10
README.md CHANGED
@@ -78,19 +78,47 @@ tags:
78
 
79
  ```python
80
  from transformers import AutoTokenizer, AutoModelForTokenClassification
81
- tokenizer = AutoTokenizer.from_pretrained("boltuix/bert-ner-model") 🎨
82
- model = AutoModelForTokenClassification.from_pretrained("boltuix/bert-ner-model") πŸ€–
83
- text = "Big Managers on Campus" πŸ“
84
- inputs = tokenizer(text, return_tensors="pt") πŸš€
85
- outputs = model(**inputs) 🌟
86
- predictions = outputs.logits.argmax(dim=-1) 🎯
87
- tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) ✨
88
- labels = [label_list[p] for p in predictions[0]] 🎨
89
- print(list(zip(tokens, labels))) πŸ“Š
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  ```
91
 
92
  ### ✨ Output
93
- πŸ’₯ "Big Managers on Campus" β†’ πŸ”ΉπŸ”ΉπŸ”ΉπŸ”Ή
 
 
 
 
 
 
 
94
 
95
  ## πŸ‹οΈ Training Details
96
 
 
78
 
79
  ```python
80
  from transformers import AutoTokenizer, AutoModelForTokenClassification
81
+ import torch
82
+
83
+ # Load tokenizer and model from Hugging Face Hub
84
+ tokenizer = AutoTokenizer.from_pretrained("boltuix/bert-ner")
85
+ model = AutoModelForTokenClassification.from_pretrained("boltuix/bert-ner")
86
+
87
+ # Sample input text
88
+ text = "Barack Obama visited Microsoft headquarters in Seattle."
89
+
90
+ # Tokenize the input
91
+ inputs = tokenizer(text, return_tensors="pt")
92
+ outputs = model(**inputs)
93
+
94
+ # Get predictions
95
+ predictions = outputs.logits.argmax(dim=-1)
96
+
97
+ # Convert token IDs to tokens
98
+ tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
99
+
100
+ # Get the label map from model config
101
+ label_map = model.config.id2label
102
+
103
+ # Map predicted IDs to labels
104
+ labels = [label_map[p.item()] for p in predictions[0]]
105
+
106
+ # Print token-label pairs (skip special tokens like [CLS], [SEP])
107
+ for token, label in zip(tokens, labels):
108
+ if token not in tokenizer.all_special_tokens:
109
+ print(f"{token:15} β†’ {label}")
110
+
111
  ```
112
 
113
  ### ✨ Output
114
+ barack β†’ B-PERSON
115
+ obama β†’ I-PERSON
116
+ visited β†’ O
117
+ microsoft β†’ B-ORG
118
+ headquarters β†’ O
119
+ in β†’ O
120
+ seattle β†’ B-GPE
121
+ . β†’ O
122
 
123
  ## πŸ‹οΈ Training Details
124