Views
No views yet
roberta-base model for Named Entity Recognition (NER) using the CoNLL-2003 dataset. The model is specifically designed to recognize entities related to Person (PER), Organization (ORG), and Location (LOC). The model has been optimized for efficient deployment while maintaining high accuracy, making it suitable for resource-constrained environments.pip install transformers torch1from transformers import RobertaTokenizerFast, RobertaForTokenClassification
2import torch
3
4device = "cuda" if torch.cuda.is_available() else "cpu"
5
6model_name = "AventIQ-AI/roberta-named-entity-recognition"
7model = RobertaForTokenClassification.from_pretrained(model_name).to(device)
8tokenizer = RobertaTokenizerFast.from_pretrained(model_name)label_list = ["O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "B-MISC", "I-MISC"]def predict_entities(text, model):
tokens = tokenizer(text, return_tensors="pt", truncation=True)
tokens = {key: val.to(device) for key, val in tokens.items()} # Move to CUDA
with torch.no_grad():
outputs = model(**tokens)
logits = outputs.logits # Extract logits
predictions = torch.argmax(logits, dim=2) # Get highest probability labels
tokens_list = tokenizer.convert_ids_to_tokens(tokens["input_ids"][0])
predicted_labels = [label_list[pred] for pred in predictions[0].cpu().numpy()]
final_tokens = []
final_labels = []
for token, label in zip(tokens_list, predicted_labels):
if token.startswith("##"):
final_tokens[-1] += token[2:] # Merge subword
else:
final_tokens.append(token)
final_labels.append(label)
for token, label in zip(final_tokens, final_labels):
if token not in ["[CLS]", "[SEP]"]:
print(f"{token}: {label}")
# 🔍 Test Example
sample_text = "Elon Musk is the CEO of Tesla, which is based in California."
predict_entities(sample_text, model)| Entity Type | Precision | Recall | F1 Score | Number of Entities |
|---|---|---|---|---|
| LOC (Location) | 91.46% | 92.07% | 91.76% | 3,000 |
| MISC (Miscellaneous) | 71.25% | 72.83% | 72.03% | 1,266 |
| ORG (Organization) | 89.83% | 93.02% | 91.40% | 3,524 |
| PER (Person) | 95.16% | 94.04% | 94.60% | 2,989 |
CoNLL-2003 dataset was used, containing texts and their ner tags..
├── model/ # Contains the quantized model files
├── tokenizer_config/ # Tokenizer configuration and vocabulary files
├── model.safetensors/ # Quantized Model
├── README.md # Model documentation