File size: 10,408 Bytes
1a7ee60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Lightweight Document Classifier using MiniLM

Fine-tuned for invoice, receipt, and form classification

Optimized for CPU inference

"""

import torch
import torch.nn as nn
from transformers import AutoTokenizer, AutoModel, AutoConfig
from typing import Dict, List, Tuple
import logging
import numpy as np

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class DocumentClassifier(nn.Module):
    """

    Lightweight document classifier based on MiniLM

    4 classes: INVOICE, RECEIPT, FORM, OTHER

    """
    
    def __init__(

        self,

        model_name: str = "nreimers/MiniLM-L6-H384-uncased",

        num_labels: int = 4,

        dropout_prob: float = 0.1

    ):
        super().__init__()
        
        self.num_labels = num_labels
        self.model_name = model_name
        
        # Load pre-trained MiniLM
        self.config = AutoConfig.from_pretrained(model_name)
        self.backbone = AutoModel.from_pretrained(model_name, config=self.config)
        
        # Classification head
        self.dropout = nn.Dropout(dropout_prob)
        self.classifier = nn.Linear(self.config.hidden_size, num_labels)
        
        logger.info(f"Initialized DocumentClassifier with {model_name}")
        logger.info(f"Model parameters: {sum(p.numel() for p in self.parameters()) / 1e6:.2f}M")
    
    def forward(

        self,

        input_ids: torch.Tensor,

        attention_mask: torch.Tensor,

        labels: torch.Tensor = None,

        **kwargs

    ) -> Dict[str, torch.Tensor]:
        """

        Forward pass

        

        Args:

            input_ids: Token IDs (batch_size, seq_len)

            attention_mask: Attention mask (batch_size, seq_len)

            labels: Ground truth labels (batch_size,)

            

        Returns:

            Dictionary with loss (if labels provided) and logits

        """
        # Get embeddings from backbone
        outputs = self.backbone(
            input_ids=input_ids,
            attention_mask=attention_mask
        )
        
        # Use [CLS] token representation
        pooled_output = outputs.last_hidden_state[:, 0, :]  # (batch_size, hidden_size)
        
        # Apply dropout and classification
        pooled_output = self.dropout(pooled_output)
        logits = self.classifier(pooled_output)  # (batch_size, num_labels)
        
        # Calculate loss if labels provided
        loss = None
        if labels is not None:
            loss_fct = nn.CrossEntropyLoss()
            loss = loss_fct(logits, labels)
        
        return {
            "loss": loss,
            "logits": logits,
        }


class DocumentClassifierInference:
    """

    Inference wrapper for document classification

    Handles tokenization and prediction

    """
    
    def __init__(

        self,

        model_path: str,

        model_name: str = "nreimers/MiniLM-L6-H384-uncased",

        device: str = None,

        use_fp16: bool = False

    ):
        """

        Args:

            model_path: Path to fine-tuned model weights

            model_name: Base model name (for tokenizer)

            device: Device to run on ('cpu', 'cuda', or None for auto)

            use_fp16: Use FP16 precision (faster on GPU)

        """
        self.device = device if device else ('cuda' if torch.cuda.is_available() else 'cpu')
        logger.info(f"Loading model on device: {self.device}")
        
        # Load tokenizer
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
        
        # Load model
        self.model = DocumentClassifier(model_name=model_name)
        
        # Load fine-tuned weights
        try:
            state_dict = torch.load(model_path, map_location=self.device)
            self.model.load_state_dict(state_dict)
            logger.info(f"Loaded fine-tuned weights from {model_path}")
        except Exception as e:
            logger.warning(f"Could not load weights from {model_path}: {str(e)}")
            logger.warning("Using pre-trained weights (not fine-tuned)")
        
        self.model.to(self.device)
        self.model.eval()
        
        # Apply FP16 if requested
        if use_fp16 and self.device == 'cuda':
            self.model.half()
            logger.info("Using FP16 precision")
        
        # Label mapping
        self.id2label = {
            0: 'INVOICE',
            1: 'RECEIPT',
            2: 'FORM',
            3: 'OTHER'
        }
        self.label2id = {v: k for k, v in self.id2label.items()}
    
    @torch.no_grad()
    def predict(

        self,

        text: str,

        max_length: int = 512,

        return_probabilities: bool = True

    ) -> Dict:
        """

        Predict document class from text

        

        Args:

            text: Input text (OCR extracted)

            max_length: Maximum sequence length

            return_probabilities: Return class probabilities

            

        Returns:

            Dictionary with predicted class and probabilities

        """
        # Tokenize
        inputs = self.tokenizer(
            text,
            max_length=max_length,
            padding='max_length',
            truncation=True,
            return_tensors='pt'
        )
        
        # Move to device
        inputs = {k: v.to(self.device) for k, v in inputs.items()}
        
        # Forward pass
        outputs = self.model(**inputs)
        logits = outputs['logits']
        
        # Get predictions
        probabilities = torch.softmax(logits, dim=-1)
        predicted_class_id = torch.argmax(probabilities, dim=-1).item()
        predicted_class = self.id2label[predicted_class_id]
        confidence = probabilities[0, predicted_class_id].item()
        
        result = {
            'predicted_class': predicted_class,
            'confidence': confidence,
        }
        
        if return_probabilities:
            result['probabilities'] = {
                self.id2label[i]: probabilities[0, i].item()
                for i in range(len(self.id2label))
            }
        
        return result
    
    @torch.no_grad()
    def predict_batch(

        self,

        texts: List[str],

        max_length: int = 512,

        batch_size: int = 8

    ) -> List[Dict]:
        """

        Predict document classes for multiple texts

        

        Args:

            texts: List of input texts

            max_length: Maximum sequence length

            batch_size: Batch size for processing

            

        Returns:

            List of prediction dictionaries

        """
        all_results = []
        
        for i in range(0, len(texts), batch_size):
            batch_texts = texts[i:i+batch_size]
            
            # Tokenize batch
            inputs = self.tokenizer(
                batch_texts,
                max_length=max_length,
                padding='max_length',
                truncation=True,
                return_tensors='pt'
            )
            
            # Move to device
            inputs = {k: v.to(self.device) for k, v in inputs.items()}
            
            # Forward pass
            outputs = self.model(**inputs)
            logits = outputs['logits']
            
            # Get predictions
            probabilities = torch.softmax(logits, dim=-1)
            predicted_classes = torch.argmax(probabilities, dim=-1)
            
            # Parse results
            for j in range(len(batch_texts)):
                pred_id = predicted_classes[j].item()
                result = {
                    'predicted_class': self.id2label[pred_id],
                    'confidence': probabilities[j, pred_id].item(),
                    'probabilities': {
                        self.id2label[k]: probabilities[j, k].item()
                        for k in range(len(self.id2label))
                    }
                }
                all_results.append(result)
        
        return all_results


def export_to_onnx(

    model_path: str,

    output_path: str,

    model_name: str = "nreimers/MiniLM-L6-H384-uncased"

):
    """

    Export model to ONNX format for faster inference

    

    Args:

        model_path: Path to PyTorch model weights

        output_path: Path to save ONNX model

        model_name: Base model name

    """
    import torch.onnx
    
    logger.info("Exporting model to ONNX...")
    
    # Load model
    model = DocumentClassifier(model_name=model_name)
    state_dict = torch.load(model_path, map_location='cpu')
    model.load_state_dict(state_dict)
    model.eval()
    
    # Load tokenizer
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    
    # Create dummy input
    dummy_text = "This is a sample invoice text for ONNX export"
    dummy_input = tokenizer(
        dummy_text,
        max_length=512,
        padding='max_length',
        truncation=True,
        return_tensors='pt'
    )
    
    # Export to ONNX
    torch.onnx.export(
        model,
        (dummy_input['input_ids'], dummy_input['attention_mask']),
        output_path,
        input_names=['input_ids', 'attention_mask'],
        output_names=['logits'],
        dynamic_axes={
            'input_ids': {0: 'batch_size'},
            'attention_mask': {0: 'batch_size'},
            'logits': {0: 'batch_size'}
        },
        opset_version=14,
    )
    
    logger.info(f"Model exported to {output_path}")


if __name__ == "__main__":
    # Example usage
    
    # Create a sample model (not trained)
    model = DocumentClassifier()
    
    print(f"\nModel architecture:")
    print(model)
    
    print(f"\nTotal parameters: {sum(p.numel() for p in model.parameters()) / 1e6:.2f}M")
    
    # Test forward pass
    batch_size = 2
    seq_len = 128
    dummy_input_ids = torch.randint(0, 1000, (batch_size, seq_len))
    dummy_attention_mask = torch.ones(batch_size, seq_len)
    dummy_labels = torch.tensor([0, 1])
    
    outputs = model(dummy_input_ids, dummy_attention_mask, dummy_labels)
    print(f"\nOutput logits shape: {outputs['logits'].shape}")
    print(f"Loss: {outputs['loss'].item():.4f}")