File size: 14,253 Bytes
eb53bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""

Small Language Model (SLM) architecture for document text extraction.

Uses DistilBERT with transfer learning for Named Entity Recognition.

"""

import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from transformers import (
    DistilBertTokenizer, 
    DistilBertForTokenClassification,
    DistilBertConfig,
    get_linear_schedule_with_warmup
)
from typing import List, Dict, Tuple, Optional
import json
import numpy as np
from sklearn.model_selection import train_test_split
from dataclasses import dataclass


@dataclass
class ModelConfig:
    """Configuration for the SLM model."""
    model_name: str = "distilbert-base-uncased"
    max_length: int = 512
    batch_size: int = 16
    learning_rate: float = 2e-5
    num_epochs: int = 3
    warmup_steps: int = 500
    weight_decay: float = 0.01
    dropout_rate: float = 0.3
    
    # Entity labels
    entity_labels: List[str] = None
    
    def __post_init__(self):
        if self.entity_labels is None:
            self.entity_labels = [
                'O', 'B-NAME', 'I-NAME', 'B-DATE', 'I-DATE', 
                'B-INVOICE_NO', 'I-INVOICE_NO', 'B-AMOUNT', 'I-AMOUNT',
                'B-ADDRESS', 'I-ADDRESS', 'B-PHONE', 'I-PHONE',
                'B-EMAIL', 'I-EMAIL'
            ]
    
    @property
    def num_labels(self) -> int:
        return len(self.entity_labels)
    
    @property
    def label2id(self) -> Dict[str, int]:
        return {label: i for i, label in enumerate(self.entity_labels)}
    
    @property
    def id2label(self) -> Dict[int, str]:
        return {i: label for i, label in enumerate(self.entity_labels)}


class NERDataset(Dataset):
    """PyTorch Dataset for NER training."""
    
    def __init__(self, dataset: List[Dict], tokenizer: DistilBertTokenizer, 

                 config: ModelConfig, mode: str = 'train'):
        self.dataset = dataset
        self.tokenizer = tokenizer
        self.config = config
        self.mode = mode
        
        # Prepare tokenized data
        self.tokenized_data = self._tokenize_and_align_labels()
    
    def _tokenize_and_align_labels(self) -> List[Dict]:
        """Tokenize text and align labels with subword tokens."""
        tokenized_data = []
        
        for example in self.dataset:
            tokens = example['tokens']
            labels = example['labels']
            
            # Tokenize each word and track alignments
            tokenized_inputs = self.tokenizer(
                tokens,
                is_split_into_words=True,
                padding='max_length',
                truncation=True,
                max_length=self.config.max_length,
                return_tensors='pt'
            )
            
            # Align labels with subword tokens
            word_ids = tokenized_inputs.word_ids()
            aligned_labels = []
            previous_word_idx = None
            
            for word_idx in word_ids:
                if word_idx is None:
                    # Special tokens get -100 (ignored in loss computation)
                    aligned_labels.append(-100)
                elif word_idx != previous_word_idx:
                    # First subword of a word gets the original label
                    if word_idx < len(labels):
                        label = labels[word_idx]
                        aligned_labels.append(self.config.label2id.get(label, 0))
                    else:
                        aligned_labels.append(-100)
                else:
                    # Subsequent subwords of the same word
                    if word_idx < len(labels):
                        label = labels[word_idx]
                        if label.startswith('B-'):
                            # Convert B- to I- for subword tokens
                            i_label = label.replace('B-', 'I-')
                            aligned_labels.append(self.config.label2id.get(i_label, 0))
                        else:
                            aligned_labels.append(self.config.label2id.get(label, 0))
                    else:
                        aligned_labels.append(-100)
                
                previous_word_idx = word_idx
            
            tokenized_data.append({
                'input_ids': tokenized_inputs['input_ids'].squeeze(),
                'attention_mask': tokenized_inputs['attention_mask'].squeeze(),
                'labels': torch.tensor(aligned_labels, dtype=torch.long),
                'original_tokens': tokens,
                'original_labels': labels
            })
        
        return tokenized_data
    
    def __len__(self) -> int:
        return len(self.tokenized_data)
    
    def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]:
        return {
            'input_ids': self.tokenized_data[idx]['input_ids'],
            'attention_mask': self.tokenized_data[idx]['attention_mask'],
            'labels': self.tokenized_data[idx]['labels']
        }


class DocumentNERModel(nn.Module):
    """DistilBERT-based model for document NER."""
    
    def __init__(self, config: ModelConfig):
        super().__init__()
        self.config = config
        
        # Load pre-trained DistilBERT configuration
        bert_config = DistilBertConfig.from_pretrained(
            config.model_name,
            num_labels=config.num_labels,
            id2label=config.id2label,
            label2id=config.label2id,
            dropout=config.dropout_rate,
            attention_dropout=config.dropout_rate
        )
        
        # Initialize model with token classification head
        self.model = DistilBertForTokenClassification.from_pretrained(
            config.model_name,
            config=bert_config
        )
        
        # Additional dropout layer for regularization
        self.dropout = nn.Dropout(config.dropout_rate)
    
    def forward(self, input_ids, attention_mask=None, labels=None):
        """Forward pass through the model."""
        outputs = self.model(
            input_ids=input_ids,
            attention_mask=attention_mask,
            labels=labels
        )
        
        return outputs
    
    def predict(self, input_ids, attention_mask):
        """Make predictions without computing loss."""
        with torch.no_grad():
            outputs = self.model(
                input_ids=input_ids,
                attention_mask=attention_mask
            )
            predictions = torch.argmax(outputs.logits, dim=-1)
            probabilities = torch.softmax(outputs.logits, dim=-1)
        
        return predictions, probabilities


class NERTrainer:
    """Trainer class for the NER model."""
    
    def __init__(self, model: DocumentNERModel, config: ModelConfig):
        self.model = model
        self.config = config
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        self.model.to(self.device)
        
        # Initialize tokenizer
        self.tokenizer = DistilBertTokenizer.from_pretrained(config.model_name)
    
    def prepare_dataloaders(self, dataset: List[Dict], 

                          test_size: float = 0.2) -> Tuple[DataLoader, DataLoader]:
        """Prepare training and validation dataloaders."""
        # Split dataset
        train_data, val_data = train_test_split(
            dataset, test_size=test_size, random_state=42
        )
        
        # Create datasets
        train_dataset = NERDataset(train_data, self.tokenizer, self.config, 'train')
        val_dataset = NERDataset(val_data, self.tokenizer, self.config, 'val')
        
        # Create dataloaders
        train_dataloader = DataLoader(
            train_dataset, 
            batch_size=self.config.batch_size, 
            shuffle=True
        )
        val_dataloader = DataLoader(
            val_dataset, 
            batch_size=self.config.batch_size, 
            shuffle=False
        )
        
        return train_dataloader, val_dataloader
    
    def train(self, train_dataloader: DataLoader, 

              val_dataloader: DataLoader) -> Dict[str, List[float]]:
        """Train the NER model."""
        # Initialize optimizer and scheduler
        optimizer = torch.optim.AdamW(
            self.model.parameters(),
            lr=self.config.learning_rate,
            weight_decay=self.config.weight_decay
        )
        
        total_steps = len(train_dataloader) * self.config.num_epochs
        scheduler = get_linear_schedule_with_warmup(
            optimizer,
            num_warmup_steps=self.config.warmup_steps,
            num_training_steps=total_steps
        )
        
        # Training history
        history = {
            'train_loss': [],
            'val_loss': [],
            'val_accuracy': []
        }
        
        print(f"Training on device: {self.device}")
        print(f"Total training steps: {total_steps}")
        
        for epoch in range(self.config.num_epochs):
            print(f"\nEpoch {epoch + 1}/{self.config.num_epochs}")
            print("-" * 50)
            
            # Training phase
            train_loss = self._train_epoch(train_dataloader, optimizer, scheduler)
            history['train_loss'].append(train_loss)
            
            # Validation phase
            val_loss, val_accuracy = self._validate_epoch(val_dataloader)
            history['val_loss'].append(val_loss)
            history['val_accuracy'].append(val_accuracy)
            
            print(f"Train Loss: {train_loss:.4f}")
            print(f"Val Loss: {val_loss:.4f}")
            print(f"Val Accuracy: {val_accuracy:.4f}")
        
        return history
    
    def _train_epoch(self, dataloader: DataLoader, optimizer, scheduler) -> float:
        """Train for one epoch."""
        self.model.train()
        total_loss = 0
        
        for batch_idx, batch in enumerate(dataloader):
            # Move batch to device
            batch = {k: v.to(self.device) for k, v in batch.items()}
            
            # Forward pass
            outputs = self.model(**batch)
            loss = outputs.loss
            
            # Backward pass
            optimizer.zero_grad()
            loss.backward()
            
            # Gradient clipping
            torch.nn.utils.clip_grad_norm_(self.model.parameters(), 1.0)
            
            optimizer.step()
            scheduler.step()
            
            total_loss += loss.item()
            
            if batch_idx % 10 == 0:
                print(f"Batch {batch_idx}/{len(dataloader)}, Loss: {loss.item():.4f}")
        
        return total_loss / len(dataloader)
    
    def _validate_epoch(self, dataloader: DataLoader) -> Tuple[float, float]:
        """Validate for one epoch."""
        self.model.eval()
        total_loss = 0
        total_correct = 0
        total_tokens = 0
        
        with torch.no_grad():
            for batch in dataloader:
                batch = {k: v.to(self.device) for k, v in batch.items()}
                
                outputs = self.model(**batch)
                loss = outputs.loss
                
                total_loss += loss.item()
                
                # Calculate accuracy (ignoring -100 labels)
                predictions = torch.argmax(outputs.logits, dim=-1)
                labels = batch['labels']
                
                # Mask for valid labels (not -100)
                valid_mask = labels != -100
                
                correct = (predictions == labels) & valid_mask
                total_correct += correct.sum().item()
                total_tokens += valid_mask.sum().item()
        
        avg_loss = total_loss / len(dataloader)
        accuracy = total_correct / total_tokens if total_tokens > 0 else 0
        
        return avg_loss, accuracy
    
    def save_model(self, save_path: str):
        """Save the trained model and tokenizer."""
        self.model.model.save_pretrained(save_path)
        self.tokenizer.save_pretrained(save_path)
        
        # Save config
        config_path = f"{save_path}/training_config.json"
        with open(config_path, 'w') as f:
            json.dump(vars(self.config), f, indent=2)
        
        print(f"Model saved to {save_path}")
    
    def load_model(self, model_path: str):
        """Load a pre-trained model."""
        self.model.model = DistilBertForTokenClassification.from_pretrained(model_path)
        self.tokenizer = DistilBertTokenizer.from_pretrained(model_path)
        self.model.to(self.device)
        print(f"Model loaded from {model_path}")


def create_model_and_trainer(config: Optional[ModelConfig] = None) -> Tuple[DocumentNERModel, NERTrainer]:
    """Create model and trainer with configuration."""
    if config is None:
        config = ModelConfig()
    
    model = DocumentNERModel(config)
    trainer = NERTrainer(model, config)
    
    return model, trainer


def main():
    """Demonstrate model creation and setup."""
    # Create configuration
    config = ModelConfig(
        batch_size=8,  # Smaller batch size for demo
        num_epochs=2,
        learning_rate=3e-5
    )
    
    print("Model Configuration:")
    print(f"Model: {config.model_name}")
    print(f"Max Length: {config.max_length}")
    print(f"Batch Size: {config.batch_size}")
    print(f"Learning Rate: {config.learning_rate}")
    print(f"Number of Labels: {config.num_labels}")
    print(f"Entity Labels: {config.entity_labels}")
    
    # Create model and trainer
    model, trainer = create_model_and_trainer(config)
    
    print(f"\nModel created successfully!")
    print(f"Model parameters: {sum(p.numel() for p in model.parameters()):,}")
    print(f"Trainable parameters: {sum(p.numel() for p in model.parameters() if p.requires_grad):,}")
    
    return model, trainer


if __name__ == "__main__":
    main()