ayushsinha commited on
Commit
b8875f4
Β·
verified Β·
1 Parent(s): 0c2e5de

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +137 -0
README.md ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Named Entity Recognition (NER) with BERT
2
+
3
+ ## πŸ“Œ Overview
4
+ This repository hosts the quantized version of the `bert-base-cased` 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.
5
+
6
+ ## πŸ— Model Details
7
+ - **Model Architecture**: BERT Base Cased
8
+ - **Task**: Named Entity Recognition (NER)
9
+ - **Dataset**: Hugging Face's CoNLL-2003
10
+ - **Quantization**: BrainFloat16
11
+ - **Fine-tuning Framework**: Hugging Face Transformers
12
+
13
+ ---
14
+ ## πŸš€ Usage
15
+
16
+ ### Installation
17
+ ```bash
18
+ pip install transformers torch
19
+ ```
20
+
21
+ ### Loading the Model
22
+ ```python
23
+ from transformers import BertTokenizerFast, BertForTokenClassification
24
+ import torch
25
+
26
+ device = "cuda" if torch.cuda.is_available() else "cpu"
27
+
28
+ model_name = "AventIQ-AI/bert-named-entity-recognition"
29
+ model = BertForTokenClassification.from_pretrained(model_name).to(device)
30
+ tokenizer = BertTokenizerFast.from_pretrained(model_name)
31
+ ```
32
+
33
+ ### Named Entity Recognition Inference
34
+ ```python
35
+ label_list = ["O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "B-MISC", "I-MISC"]
36
+ ```
37
+ ### **πŸ”Ή Labeling Scheme (BIO Format)**
38
+
39
+ - **B-XYZ (Beginning)**: Indicates the beginning of an entity of type XYZ (e.g., B-PER for the beginning of a person’s name).
40
+ - **I-XYZ (Inside)**: Represents subsequent tokens inside an entity (e.g., I-PER for the second part of a person’s name).
41
+ - **O (Outside)**: Denotes tokens that are not part of any named entity.
42
+
43
+ ```
44
+ def predict_entities(text, model):
45
+ # βœ… Tokenize input text
46
+ tokens = tokenizer(text, return_tensors="pt", truncation=True)
47
+ tokens = {key: val.to(device) for key, val in tokens.items()} # Move to CUDA
48
+
49
+ # βœ… Run model inference
50
+ with torch.no_grad():
51
+ outputs = model(**tokens)
52
+
53
+ logits = outputs.logits # Extract logits
54
+ predictions = torch.argmax(logits, dim=2) # Get highest probability labels
55
+
56
+ # βœ… Convert token IDs back to words
57
+ tokens_list = tokenizer.convert_ids_to_tokens(tokens["input_ids"][0])
58
+ predicted_labels = [label_list[pred] for pred in predictions[0].cpu().numpy()]
59
+
60
+ # βœ… Group subword tokens into whole words
61
+ final_tokens = []
62
+ final_labels = []
63
+ for token, label in zip(tokens_list, predicted_labels):
64
+ if token.startswith("##"):
65
+ final_tokens[-1] += token[2:] # Merge subword
66
+ else:
67
+ final_tokens.append(token)
68
+ final_labels.append(label)
69
+
70
+ # βœ… Display results (ignore special tokens)
71
+ for token, label in zip(final_tokens, final_labels):
72
+ if token not in ["[CLS]", "[SEP]"]:
73
+ print(f"{token}: {label}")
74
+
75
+ # πŸ” Test Example
76
+ sample_text = "Elon Musk is the CEO of Tesla, which is based in California."
77
+ predict_entities(sample_text, model)
78
+ ```
79
+ ---
80
+ ## πŸ“Š Evaluation Results for Quantized Model
81
+
82
+ ### **πŸ”Ή Overall Performance**
83
+
84
+ - **Accuracy**: **97.10%** βœ…
85
+ - **Precision**: **89.52%**
86
+ - **Recall**: **90.67%**
87
+ - **F1 Score**: **90.09%**
88
+
89
+ ---
90
+
91
+ ### **πŸ”Ή Performance by Entity Type**
92
+
93
+ | Entity Type | Precision | Recall | F1 Score | Number of Entities |
94
+ |------------|-----------|--------|----------|--------------------|
95
+ | **LOC** (Location) | **91.46%** | **92.07%** | **91.76%** | 3,000 |
96
+ | **MISC** (Miscellaneous) | **71.25%** | **72.83%** | **72.03%** | 1,266 |
97
+ | **ORG** (Organization) | **89.83%** | **93.02%** | **91.40%** | 3,524 |
98
+ | **PER** (Person) | **95.16%** | **94.04%** | **94.60%** | 2,989 |
99
+
100
+ ---
101
+ #### ⏳ **Inference Speed Metrics**
102
+ - **Total Evaluation Time**: 15.89 sec
103
+ - **Samples Processed per Second**: 217.26
104
+ - **Steps per Second**: 27.18
105
+ - **Epochs Completed**: 3
106
+
107
+ ---
108
+ ## Fine-Tuning Details
109
+ ### Dataset
110
+ The Hugging Face's `CoNLL-2003` dataset was used, containing texts and their ner tags.
111
+
112
+ ## πŸ“Š Training Details
113
+ - **Number of epochs**: 3
114
+ - **Batch size**: 8
115
+ - **Evaluation strategy**: epoch
116
+
117
+ ### ⚑ Quantization
118
+ Post-training quantization was applied using PyTorch's built-in quantization framework to reduce the model size and improve inference efficiency.
119
+
120
+ ---
121
+ ## πŸ“‚ Repository Structure
122
+ ```
123
+ .
124
+ β”œβ”€β”€ model/ # Contains the quantized model files
125
+ β”œβ”€β”€ tokenizer_config/ # Tokenizer configuration and vocabulary files
126
+ β”œβ”€β”€ model.safetensors/ # Quantized Model
127
+ β”œβ”€β”€ README.md # Model documentation
128
+ ```
129
+
130
+ ---
131
+ ## ⚠️ Limitations
132
+ - The model may not generalize well to domains outside the fine-tuning dataset.
133
+ - Quantization may result in minor accuracy degradation compared to full-precision models.
134
+
135
+ ---
136
+ ## 🀝 Contributing
137
+ Contributions are welcome! Feel free to open an issue or submit a pull request if you have suggestions or improvements.