File size: 11,554 Bytes
4d1bb75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
🧠 ESG Model Integration Module
Connects the trained model with the Gradio application

This module provides the bridge between the trained ESG classifier
and the web application interface.
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from typing import Dict, List, Optional, Tuple
from pathlib import Path
from dataclasses import dataclass
import warnings

warnings.filterwarnings('ignore')


@dataclass
class ModelConfig:
    """Configuration for ESG model"""
    embed_dim: int = 4096
    n_labels: int = 4
    hidden_dim: int = 512
    dropout: float = 0.1
    labels: List[str] = None
    thresholds: Dict[str, float] = None
    
    def __post_init__(self):
        self.labels = ['E', 'S', 'G', 'non_ESG']
        # Optimized thresholds from training
        self.thresholds = {
            'E': 0.352,
            'S': 0.456,
            'G': 0.398,
            'non_ESG': 0.512
        }


class MLPClassifier(nn.Module):
    """
    Shallow MLP classifier matching the training architecture.
    Architecture: embed_dim -> 512 -> n_labels
    """
    
    def __init__(self, config: ModelConfig):
        super().__init__()
        self.config = config
        
        self.net = nn.Sequential(
            nn.Linear(config.embed_dim, config.hidden_dim),
            nn.BatchNorm1d(config.hidden_dim),
            nn.ReLU(),
            nn.Dropout(config.dropout),
            nn.Linear(config.hidden_dim, config.n_labels),
        )
        
        self._init_weights()
    
    def _init_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Linear):
                nn.init.xavier_uniform_(m.weight)
                if m.bias is not None:
                    nn.init.zeros_(m.bias)
    
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.net(x)


class ESGModelInference:
    """
    Production-ready ESG model inference class.
    Handles embedding extraction and classification.
    """
    
    def __init__(
        self,
        model_path: Optional[str] = None,
        embedding_model_name: str = "Qwen/Qwen3-Embedding-8B",
        device: str = "auto",
        use_fp16: bool = True,
    ):
        self.config = ModelConfig()
        
        # Set device
        if device == "auto":
            self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        else:
            self.device = torch.device(device)
        
        self.use_fp16 = use_fp16 and self.device.type == "cuda"
        self.embedding_model = None
        self.tokenizer = None
        self.classifier = None
        self.scaler = None
        
        # Load models if path provided
        if model_path:
            self.load_models(model_path, embedding_model_name)
    
    def load_embedding_model(self, model_name: str):
        """Load the embedding model (Qwen3-Embedding-8B)"""
        try:
            from transformers import AutoTokenizer, AutoModel
            
            print(f"Loading embedding model: {model_name}")
            self.tokenizer = AutoTokenizer.from_pretrained(
                model_name,
                padding_side='left',
                trust_remote_code=True,
            )
            
            dtype = torch.float16 if self.use_fp16 else torch.float32
            self.embedding_model = AutoModel.from_pretrained(
                model_name,
                torch_dtype=dtype,
                trust_remote_code=True,
            ).to(self.device)
            self.embedding_model.eval()
            
            print(f"βœ… Embedding model loaded on {self.device}")
            
        except Exception as e:
            print(f"⚠️ Could not load embedding model: {e}")
            self.embedding_model = None
    
    def load_classifier(self, model_path: str):
        """Load the trained classifier weights"""
        try:
            self.classifier = MLPClassifier(self.config).to(self.device)
            state_dict = torch.load(model_path, map_location=self.device)
            self.classifier.load_state_dict(state_dict)
            self.classifier.eval()
            print(f"βœ… Classifier loaded from {model_path}")
        except Exception as e:
            print(f"⚠️ Could not load classifier: {e}")
            self.classifier = None
    
    def load_models(self, model_path: str, embedding_model_name: str):
        """Load all models"""
        self.load_embedding_model(embedding_model_name)
        self.load_classifier(model_path)
    
    @torch.no_grad()
    def extract_embedding(self, text: str, instruction: str = None) -> torch.Tensor:
        """Extract embedding for a single text"""
        if self.embedding_model is None or self.tokenizer is None:
            raise RuntimeError("Embedding model not loaded")
        
        if instruction is None:
            instruction = (
                "Instruct: Classify the following text into ESG categories: "
                "Environmental, Social, Governance, or non-ESG.\nQuery: "
            )
        
        full_text = instruction + text
        
        encoded = self.tokenizer(
            [full_text],
            padding=True,
            truncation=True,
            max_length=512,
            return_tensors='pt',
        ).to(self.device)
        
        outputs = self.embedding_model(**encoded)
        
        # Last token pooling (Qwen3-Embedding style)
        attention_mask = encoded['attention_mask']
        last_hidden_states = outputs.last_hidden_state
        
        left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
        if left_padding:
            embedding = last_hidden_states[:, -1]
        else:
            seq_lens = attention_mask.sum(dim=1) - 1
            batch_size = last_hidden_states.shape[0]
            embedding = last_hidden_states[
                torch.arange(batch_size, device=self.device), seq_lens
            ]
        
        # L2 normalize
        embedding = F.normalize(embedding, p=2, dim=1)
        
        return embedding.float().cpu()
    
    @torch.no_grad()
    def predict(self, embedding: torch.Tensor) -> Dict:
        """Run classification on embedding"""
        if self.classifier is None:
            raise RuntimeError("Classifier not loaded")
        
        embedding = embedding.to(self.device)
        logits = self.classifier(embedding)
        probs = torch.sigmoid(logits).cpu().numpy()[0]
        
        # Apply thresholds
        predictions = []
        scores = {}
        for i, label in enumerate(self.config.labels):
            scores[label] = float(probs[i])
            if probs[i] >= self.config.thresholds[label]:
                predictions.append(label)
        
        # Default to non_ESG if no predictions
        if not predictions:
            predictions = ['non_ESG']
        
        return {
            'scores': scores,
            'predictions': predictions,
            'confidence': np.mean([scores[p] for p in predictions]),
        }
    
    def classify(self, text: str) -> Dict:
        """Full pipeline: text -> embedding -> classification"""
        embedding = self.extract_embedding(text)
        return self.predict(embedding)
    
    def batch_classify(self, texts: List[str], batch_size: int = 8) -> List[Dict]:
        """Classify multiple texts efficiently"""
        results = []
        
        for i in range(0, len(texts), batch_size):
            batch_texts = texts[i:i + batch_size]
            for text in batch_texts:
                try:
                    result = self.classify(text)
                except Exception as e:
                    result = {
                        'scores': {l: 0.0 for l in self.config.labels},
                        'predictions': ['non_ESG'],
                        'confidence': 0.0,
                        'error': str(e),
                    }
                results.append(result)
        
        return results


class LogisticRegressionEnsemble:
    """
    Logistic Regression ensemble classifier (matches training approach).
    For use when the full embedding model isn't available.
    """
    
    def __init__(self, model_dir: Optional[str] = None):
        self.config = ModelConfig()
        self.models = {}
        self.scaler = None
        
        if model_dir:
            self.load(model_dir)
    
    def load(self, model_dir: str):
        """Load trained logistic regression models"""
        import joblib
        
        model_dir = Path(model_dir)
        
        # Load scaler
        scaler_path = model_dir / 'scaler.joblib'
        if scaler_path.exists():
            self.scaler = joblib.load(scaler_path)
        
        # Load per-class models
        for label in self.config.labels:
            model_path = model_dir / f'lr_{label}.joblib'
            if model_path.exists():
                self.models[label] = joblib.load(model_path)
    
    def predict(self, embedding: np.ndarray) -> Dict:
        """Predict on pre-computed embedding"""
        if self.scaler:
            embedding = self.scaler.transform(embedding.reshape(1, -1))
        
        scores = {}
        predictions = []
        
        for label in self.config.labels:
            if label in self.models:
                prob = self.models[label].predict_proba(embedding)[0, 1]
                scores[label] = float(prob)
                if prob >= self.config.thresholds[label]:
                    predictions.append(label)
            else:
                scores[label] = 0.0
        
        if not predictions:
            predictions = ['non_ESG']
        
        return {
            'scores': scores,
            'predictions': predictions,
            'confidence': np.mean([scores[p] for p in predictions]),
        }


# ═══════════════════════════════════════════════════════════════════════════════
# UTILITY FUNCTIONS
# ═══════════════════════════════════════════════════════════════════════════════

def save_models_for_deployment(
    classifier: nn.Module,
    scaler,
    lr_models: Dict,
    output_dir: str,
):
    """Save all models for deployment"""
    import joblib
    
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    
    # Save PyTorch classifier
    torch.save(
        classifier.state_dict(),
        output_dir / 'mlp_classifier.pt'
    )
    
    # Save scaler
    if scaler is not None:
        joblib.dump(scaler, output_dir / 'scaler.joblib')
    
    # Save LR models
    for label, model in lr_models.items():
        joblib.dump(model, output_dir / f'lr_{label}.joblib')
    
    # Save config
    config = ModelConfig()
    config_dict = {
        'embed_dim': config.embed_dim,
        'n_labels': config.n_labels,
        'hidden_dim': config.hidden_dim,
        'dropout': config.dropout,
        'labels': config.labels,
        'thresholds': config.thresholds,
    }
    
    import json
    with open(output_dir / 'config.json', 'w') as f:
        json.dump(config_dict, f, indent=2)
    
    print(f"βœ… Models saved to {output_dir}")


if __name__ == "__main__":
    # Test the module
    print("ESG Model Integration Module")
    print(f"Config: {ModelConfig()}")