Token Classification
Transformers
Safetensors
English
bert
ner
named-entity-recognition
text-classification
transformer
pretrained-model
huggingface
real-time-inference
efficient-nlp
micro-nlp
chatbot
information-extraction
document-understanding
search-enhancement
medical-nlp
financial-nlp
legal-nlp
general-purpose-nlp
on-device-nlp
Update README.md
Browse files
README.md
CHANGED
|
@@ -103,23 +103,36 @@ Use the model for NER with the following Python code:
|
|
| 103 |
|
| 104 |
```python
|
| 105 |
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
|
|
|
|
|
|
| 106 |
|
| 107 |
# Load model and tokenizer
|
| 108 |
tokenizer = AutoTokenizer.from_pretrained("boltuix/EntityBERT")
|
| 109 |
model = AutoModelForTokenClassification.from_pretrained("boltuix/EntityBERT")
|
| 110 |
|
| 111 |
-
# Create NER pipeline
|
| 112 |
nlp = pipeline("token-classification", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
|
| 113 |
|
| 114 |
# Input text
|
| 115 |
-
text =
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# Run inference
|
| 118 |
ner_results = nlp(text)
|
| 119 |
|
| 120 |
-
#
|
|
|
|
| 121 |
for entity in ner_results:
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
```
|
| 124 |
|
| 125 |
### ✨ Example Output
|
|
|
|
| 103 |
|
| 104 |
```python
|
| 105 |
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
| 106 |
+
import json
|
| 107 |
+
from collections import defaultdict
|
| 108 |
|
| 109 |
# Load model and tokenizer
|
| 110 |
tokenizer = AutoTokenizer.from_pretrained("boltuix/EntityBERT")
|
| 111 |
model = AutoModelForTokenClassification.from_pretrained("boltuix/EntityBERT")
|
| 112 |
|
| 113 |
+
# Create NER pipeline with aggregation
|
| 114 |
nlp = pipeline("token-classification", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
|
| 115 |
|
| 116 |
# Input text
|
| 117 |
+
text = (
|
| 118 |
+
"Plan a trip to Miami from Orlando"
|
| 119 |
+
)
|
| 120 |
|
| 121 |
# Run inference
|
| 122 |
ner_results = nlp(text)
|
| 123 |
|
| 124 |
+
# Organize into dictionary by entity_group
|
| 125 |
+
entities = defaultdict(list)
|
| 126 |
for entity in ner_results:
|
| 127 |
+
group = entity["entity_group"]
|
| 128 |
+
word = entity["word"]
|
| 129 |
+
entities[group].append(word)
|
| 130 |
+
|
| 131 |
+
# Format results into final JSON structure
|
| 132 |
+
formatted_output = {k: " ".join(v) for k, v in entities.items()}
|
| 133 |
+
|
| 134 |
+
# Pretty-print as JSON
|
| 135 |
+
print(json.dumps(formatted_output, indent=2))
|
| 136 |
```
|
| 137 |
|
| 138 |
### ✨ Example Output
|