Update model/train.py
Browse filesKey Fixes Applied to train.py:
1. FIXED PATH MANAGEMENT (Critical Bug Fix):
- Removed hardcoded paths like `"/tmp/pipeline.pkl"`
- Added centralized `PathConfig` class that matches `fastapi_server.py`
- Fixed save paths in `save_model_artifacts()`:
- Pipeline: `/tmp/model/pipeline.pkl` (was `/tmp/pipeline.pkl`)
- Model: `/tmp/model/model.pkl` (was `/tmp/model.pkl`)
- Vectorizer: `/tmp/model/vectorizer.pkl` (was `/tmp/vectorizer.pkl`)
2. Enhanced Error Handling:
- Added comprehensive data validation with `DataValidator` class
- Better exception handling throughout the training pipeline
- Graceful fallbacks when components fail
3. Added Diagnostics & Testing:
- `TrainingDiagnostics` class for verifying training output
- Path verification functions to debug issues
- Model loading tests to ensure artifacts work correctly
- Command-line testing options (`python train.py test-paths`)
4. Improved Robustness:
- Directory auto-creation with proper permissions
- Enhanced metadata generation with comprehensive model info
- Better logging with status indicators (β
ββ οΈ)
5. Path Consistency Verification:
- Logs all paths during training for verification
- File existence checks after saving
- Size verification to ensure files aren't empty
The key problem was that:
- Before: `train.py` saved to `/tmp/pipeline.pkl` but `fastapi_server.py` looked in `/tmp/model/`
- After: Both use the same `PathConfig` and save/load from `/tmp/model/`
- model/train.py +711 -323
@@ -1,3 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
import numpy as np
|
3 |
from pathlib import Path
|
@@ -10,63 +27,162 @@ from typing import Dict, Tuple, Optional, Any
|
|
10 |
import warnings
|
11 |
warnings.filterwarnings('ignore')
|
12 |
|
13 |
-
# Scikit-learn imports
|
14 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
15 |
-
from sklearn.linear_model import LogisticRegression
|
16 |
-
from sklearn.ensemble import RandomForestClassifier
|
17 |
-
from sklearn.model_selection import (
|
18 |
-
train_test_split, cross_val_score, GridSearchCV,
|
19 |
-
StratifiedKFold, validation_curve
|
20 |
-
)
|
21 |
-
from sklearn.metrics import (
|
22 |
-
accuracy_score, precision_score, recall_score, f1_score,
|
23 |
-
roc_auc_score, confusion_matrix, classification_report,
|
24 |
-
precision_recall_curve, roc_curve
|
25 |
-
)
|
26 |
-
from sklearn.pipeline import Pipeline
|
27 |
-
from sklearn.preprocessing import FunctionTransformer
|
28 |
-
from sklearn.feature_selection import SelectKBest, chi2
|
29 |
-
import matplotlib.pyplot as plt
|
30 |
-
import seaborn as sns
|
31 |
|
32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
logging.basicConfig(
|
34 |
level=logging.INFO,
|
35 |
-
format='%(asctime)s - %(levelname)s - %(message)s',
|
36 |
handlers=[
|
37 |
-
logging.FileHandler(
|
38 |
logging.StreamHandler()
|
39 |
]
|
40 |
)
|
41 |
logger = logging.getLogger(__name__)
|
42 |
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
def __init__(self):
|
47 |
-
|
|
|
48 |
self.setup_training_config()
|
49 |
self.setup_models()
|
50 |
-
|
51 |
-
def setup_paths(self):
|
52 |
-
"""Setup all necessary paths"""
|
53 |
-
self.base_dir = Path("/tmp")
|
54 |
-
self.data_dir = self.base_dir / "data"
|
55 |
-
self.model_dir = self.base_dir / "model"
|
56 |
-
self.results_dir = self.base_dir / "results"
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
self.pipeline_path = self.model_dir / "pipeline.pkl"
|
67 |
-
self.metadata_path = Path("/tmp/metadata.json")
|
68 |
-
self.evaluation_path = self.results_dir / "evaluation_results.json"
|
69 |
-
|
70 |
def setup_training_config(self):
|
71 |
"""Setup training configuration"""
|
72 |
self.test_size = 0.2
|
@@ -80,7 +196,7 @@ class RobustModelTrainer:
|
|
80 |
self.max_iter = 1000
|
81 |
self.class_weight = 'balanced'
|
82 |
self.feature_selection_k = 5000
|
83 |
-
|
84 |
def setup_models(self):
|
85 |
"""Setup model configurations for comparison"""
|
86 |
self.models = {
|
@@ -109,95 +225,96 @@ class RobustModelTrainer:
|
|
109 |
}
|
110 |
}
|
111 |
}
|
112 |
-
|
113 |
def load_and_validate_data(self) -> Tuple[bool, Optional[pd.DataFrame], str]:
|
114 |
-
"""Load and validate training data"""
|
115 |
try:
|
116 |
-
logger.info("Loading training data...")
|
117 |
-
|
118 |
-
|
119 |
-
return False, None, f"Data file not found: {self.data_path}"
|
120 |
|
|
|
|
|
|
|
121 |
# Load data
|
122 |
-
df = pd.read_csv(
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
134 |
initial_count = len(df)
|
135 |
-
df = df.dropna(subset=required_columns)
|
136 |
-
if len(df) < initial_count:
|
137 |
-
logger.warning(f"Removed {initial_count - len(df)} rows with missing values")
|
138 |
-
|
139 |
-
# Validate text content
|
140 |
-
df = df[df['text'].astype(str).str.len() > 10]
|
141 |
|
142 |
-
#
|
143 |
-
|
144 |
-
if len(unique_labels) < 2:
|
145 |
-
return False, None, f"Need at least 2 classes, found: {unique_labels}"
|
146 |
|
147 |
-
#
|
148 |
-
|
149 |
-
return False, None, f"Insufficient samples for training: {len(df)}"
|
150 |
|
151 |
-
|
|
|
|
|
|
|
152 |
label_counts = df['label'].value_counts()
|
153 |
-
|
154 |
-
if min_class_ratio < 0.1:
|
155 |
-
logger.warning(f"Severe class imbalance detected: {min_class_ratio:.3f}")
|
156 |
-
|
157 |
-
logger.info(f"Data validation successful: {len(df)} samples, {len(unique_labels)} classes")
|
158 |
logger.info(f"Class distribution: {label_counts.to_dict()}")
|
159 |
-
|
160 |
-
return True, df, "Data loaded successfully"
|
161 |
-
|
162 |
except Exception as e:
|
163 |
error_msg = f"Error loading data: {str(e)}"
|
164 |
logger.error(error_msg)
|
165 |
return False, None, error_msg
|
166 |
-
|
167 |
def preprocess_text(self, text):
|
168 |
-
"""Advanced text preprocessing"""
|
169 |
import re
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
193 |
def create_preprocessing_pipeline(self) -> Pipeline:
|
194 |
-
"""Create advanced preprocessing pipeline"""
|
|
|
|
|
195 |
# Text preprocessing
|
196 |
text_preprocessor = FunctionTransformer(
|
197 |
func=lambda x: [self.preprocess_text(text) for text in x],
|
198 |
validate=False
|
199 |
)
|
200 |
-
|
201 |
# TF-IDF vectorization
|
202 |
vectorizer = TfidfVectorizer(
|
203 |
max_features=self.max_features,
|
@@ -208,13 +325,13 @@ class RobustModelTrainer:
|
|
208 |
sublinear_tf=True,
|
209 |
norm='l2'
|
210 |
)
|
211 |
-
|
212 |
# Feature selection
|
213 |
feature_selector = SelectKBest(
|
214 |
score_func=chi2,
|
215 |
k=self.feature_selection_k
|
216 |
)
|
217 |
-
|
218 |
# Create pipeline
|
219 |
pipeline = Pipeline([
|
220 |
('preprocess', text_preprocessor),
|
@@ -223,99 +340,205 @@ class RobustModelTrainer:
|
|
223 |
('model', None) # Will be set during training
|
224 |
])
|
225 |
|
226 |
-
|
227 |
-
joblib.dump(pipeline, "/tmp/pipeline.pkl") # Save complete pipeline
|
228 |
-
joblib.dump(pipeline.named_steps['model'], "/tmp/model.pkl") # Individual model
|
229 |
-
joblib.dump(pipeline.named_steps['vectorize'], "/tmp/vectorizer.pkl") # Individual vectorizer
|
230 |
-
|
231 |
return pipeline
|
232 |
-
|
233 |
-
def
|
234 |
-
"""
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
class_report = classification_report(y_test, y_pred, output_dict=True)
|
256 |
-
metrics['classification_report'] = class_report
|
257 |
-
|
258 |
-
# Cross-validation scores if training data provided
|
259 |
-
if X_train is not None and y_train is not None:
|
260 |
try:
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
'
|
269 |
-
|
270 |
-
|
|
|
|
|
|
|
271 |
except Exception as e:
|
272 |
-
logger.warning(f"
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
except Exception as e:
|
293 |
-
logger.
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
|
295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
# Training accuracy for overfitting detection
|
298 |
if X_train is not None and y_train is not None:
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
303 |
except Exception as e:
|
304 |
-
logger.
|
305 |
-
|
306 |
-
|
307 |
-
|
|
|
|
|
308 |
def hyperparameter_tuning(self, pipeline, X_train, y_train, model_name: str) -> Tuple[Any, Dict]:
|
309 |
"""Perform hyperparameter tuning with cross-validation"""
|
310 |
-
logger.info(f"Starting hyperparameter tuning for {model_name}...")
|
311 |
-
|
312 |
try:
|
313 |
# Set the model in the pipeline
|
314 |
pipeline.set_params(model=self.models[model_name]['model'])
|
315 |
-
|
316 |
# Get parameter grid
|
317 |
param_grid = self.models[model_name]['param_grid']
|
318 |
-
|
319 |
# Create GridSearchCV
|
320 |
grid_search = GridSearchCV(
|
321 |
pipeline,
|
@@ -325,10 +548,10 @@ class RobustModelTrainer:
|
|
325 |
n_jobs=-1,
|
326 |
verbose=1
|
327 |
)
|
328 |
-
|
329 |
# Fit grid search
|
330 |
grid_search.fit(X_train, y_train)
|
331 |
-
|
332 |
# Extract results
|
333 |
tuning_results = {
|
334 |
'best_params': grid_search.best_params_,
|
@@ -337,46 +560,50 @@ class RobustModelTrainer:
|
|
337 |
'cv_results': {
|
338 |
'mean_test_scores': grid_search.cv_results_['mean_test_score'].tolist(),
|
339 |
'std_test_scores': grid_search.cv_results_['std_test_score'].tolist(),
|
340 |
-
'params': grid_search.cv_results_['params']
|
341 |
}
|
342 |
}
|
343 |
-
|
344 |
logger.info(f"Hyperparameter tuning completed for {model_name}")
|
345 |
logger.info(f"Best score: {grid_search.best_score_:.4f}")
|
346 |
logger.info(f"Best params: {grid_search.best_params_}")
|
347 |
-
|
348 |
return grid_search.best_estimator_, tuning_results
|
349 |
-
|
350 |
except Exception as e:
|
351 |
-
logger.error(f"Hyperparameter tuning failed for {model_name}: {str(e)}")
|
352 |
# Return basic model if tuning fails
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
|
|
|
|
|
|
|
|
357 |
def train_and_evaluate_models(self, X_train, X_test, y_train, y_test) -> Dict:
|
358 |
"""Train and evaluate multiple models"""
|
359 |
-
logger.info("Starting model training and evaluation...")
|
360 |
-
|
361 |
results = {}
|
362 |
-
|
363 |
for model_name in self.models.keys():
|
364 |
logger.info(f"Training {model_name}...")
|
365 |
-
|
366 |
try:
|
367 |
-
# Create pipeline
|
368 |
pipeline = self.create_preprocessing_pipeline()
|
369 |
-
|
370 |
# Hyperparameter tuning
|
371 |
best_model, tuning_results = self.hyperparameter_tuning(
|
372 |
pipeline, X_train, y_train, model_name
|
373 |
)
|
374 |
-
|
375 |
# Comprehensive evaluation
|
376 |
evaluation_metrics = self.comprehensive_evaluation(
|
377 |
best_model, X_test, y_test, X_train, y_train
|
378 |
)
|
379 |
-
|
380 |
# Store results
|
381 |
results[model_name] = {
|
382 |
'model': best_model,
|
@@ -384,101 +611,45 @@ class RobustModelTrainer:
|
|
384 |
'evaluation_metrics': evaluation_metrics,
|
385 |
'training_time': datetime.now().isoformat()
|
386 |
}
|
387 |
-
|
388 |
-
logger.info(f"Model {model_name} - F1: {evaluation_metrics['f1']:.4f}, "
|
389 |
-
|
390 |
-
|
391 |
except Exception as e:
|
392 |
-
logger.error(f"Training failed for {model_name}: {str(e)}")
|
393 |
results[model_name] = {'error': str(e)}
|
394 |
-
|
395 |
return results
|
396 |
-
|
397 |
def select_best_model(self, results: Dict) -> Tuple[str, Any, Dict]:
|
398 |
"""Select the best performing model"""
|
399 |
-
logger.info("Selecting best model...")
|
400 |
-
|
401 |
best_model_name = None
|
402 |
best_model = None
|
403 |
best_score = -1
|
404 |
best_metrics = None
|
405 |
-
|
406 |
for model_name, result in results.items():
|
407 |
if 'error' in result:
|
|
|
408 |
continue
|
409 |
-
|
410 |
# Use F1 score as primary metric
|
411 |
f1_score = result['evaluation_metrics']['f1']
|
412 |
-
|
413 |
if f1_score > best_score:
|
414 |
best_score = f1_score
|
415 |
best_model_name = model_name
|
416 |
best_model = result['model']
|
417 |
best_metrics = result['evaluation_metrics']
|
418 |
-
|
419 |
if best_model_name is None:
|
420 |
-
raise ValueError("No models trained successfully")
|
421 |
-
|
422 |
-
logger.info(f"Best model: {best_model_name} with F1 score: {best_score:.4f}")
|
423 |
return best_model_name, best_model, best_metrics
|
424 |
-
|
425 |
-
def save_model_artifacts(self, model, model_name: str, metrics: Dict) -> bool:
|
426 |
-
"""Save model artifacts and metadata"""
|
427 |
-
try:
|
428 |
-
logger.info("Saving model artifacts...")
|
429 |
-
|
430 |
-
# Save the full pipeline
|
431 |
-
joblib.dump(model, self.pipeline_path)
|
432 |
-
|
433 |
-
# Save individual components for backward compatibility
|
434 |
-
joblib.dump(model.named_steps['model'], self.model_path)
|
435 |
-
joblib.dump(model.named_steps['vectorize'], self.vectorizer_path)
|
436 |
-
|
437 |
-
# Generate data hash
|
438 |
-
data_hash = hashlib.md5(str(datetime.now()).encode()).hexdigest()
|
439 |
-
|
440 |
-
# Create metadata
|
441 |
-
metadata = {
|
442 |
-
'model_version': f"v1.0_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
443 |
-
'model_type': model_name,
|
444 |
-
'data_version': data_hash,
|
445 |
-
'train_size': metrics.get('train_accuracy', 'Unknown'),
|
446 |
-
'test_size': len(metrics.get('confusion_matrix', [[0]])[0]) if 'confusion_matrix' in metrics else 'Unknown',
|
447 |
-
'test_accuracy': metrics['accuracy'],
|
448 |
-
'test_f1': metrics['f1'],
|
449 |
-
'test_precision': metrics['precision'],
|
450 |
-
'test_recall': metrics['recall'],
|
451 |
-
'test_roc_auc': metrics['roc_auc'],
|
452 |
-
'overfitting_score': metrics.get('overfitting_score', 'Unknown'),
|
453 |
-
'cv_score_mean': metrics.get('cv_scores', {}).get('mean', 'Unknown'),
|
454 |
-
'cv_score_std': metrics.get('cv_scores', {}).get('std', 'Unknown'),
|
455 |
-
'timestamp': datetime.now().isoformat(),
|
456 |
-
'training_config': {
|
457 |
-
'test_size': self.test_size,
|
458 |
-
'validation_size': self.validation_size,
|
459 |
-
'cv_folds': self.cv_folds,
|
460 |
-
'max_features': self.max_features,
|
461 |
-
'ngram_range': self.ngram_range,
|
462 |
-
'feature_selection_k': self.feature_selection_k
|
463 |
-
}
|
464 |
-
}
|
465 |
-
|
466 |
-
# Save metadata
|
467 |
-
with open(self.metadata_path, 'w') as f:
|
468 |
-
json.dump(metadata, f, indent=2)
|
469 |
-
|
470 |
-
logger.info(f"Model artifacts saved successfully")
|
471 |
-
logger.info(f"Model path: {self.model_path}")
|
472 |
-
logger.info(f"Vectorizer path: {self.vectorizer_path}")
|
473 |
-
logger.info(f"Pipeline path: {self.pipeline_path}")
|
474 |
-
logger.info(f"Metadata path: {self.metadata_path}")
|
475 |
-
|
476 |
-
return True
|
477 |
-
|
478 |
-
except Exception as e:
|
479 |
-
logger.error(f"Failed to save model artifacts: {str(e)}")
|
480 |
-
return False
|
481 |
-
|
482 |
def save_evaluation_results(self, results: Dict) -> bool:
|
483 |
"""Save comprehensive evaluation results"""
|
484 |
try:
|
@@ -490,89 +661,306 @@ class RobustModelTrainer:
|
|
490 |
else:
|
491 |
clean_results[model_name] = {
|
492 |
'tuning_results': {
|
493 |
-
k: v for k, v in result['tuning_results'].items()
|
494 |
-
if k != 'best_estimator'
|
495 |
},
|
496 |
'evaluation_metrics': result['evaluation_metrics'],
|
497 |
'training_time': result['training_time']
|
498 |
}
|
499 |
-
|
500 |
-
# Save results
|
501 |
-
|
|
|
502 |
json.dump(clean_results, f, indent=2, default=str)
|
503 |
-
|
504 |
-
logger.info(f"Evaluation results saved to {
|
505 |
return True
|
506 |
-
|
507 |
except Exception as e:
|
508 |
-
logger.error(f"Failed to save evaluation results: {str(e)}")
|
509 |
return False
|
510 |
-
|
511 |
def train_model(self, data_path: str = None) -> Tuple[bool, str]:
|
512 |
"""Main training function with comprehensive pipeline"""
|
513 |
try:
|
514 |
-
logger.info("Starting model training pipeline...")
|
515 |
-
|
516 |
-
# Override data path if provided
|
517 |
-
if data_path:
|
518 |
-
self.data_path = Path(data_path)
|
519 |
|
|
|
|
|
|
|
|
|
520 |
# Load and validate data
|
521 |
success, df, message = self.load_and_validate_data()
|
522 |
if not success:
|
523 |
return False, message
|
524 |
-
|
525 |
# Prepare data
|
526 |
X = df['text'].values
|
527 |
y = df['label'].values
|
528 |
-
|
529 |
# Train-test split
|
530 |
X_train, X_test, y_train, y_test = train_test_split(
|
531 |
-
X, y,
|
532 |
test_size=self.test_size,
|
533 |
stratify=y,
|
534 |
random_state=self.random_state
|
535 |
)
|
536 |
-
|
537 |
logger.info(f"Data split: {len(X_train)} train, {len(X_test)} test")
|
538 |
-
|
539 |
# Train and evaluate models
|
540 |
results = self.train_and_evaluate_models(X_train, X_test, y_train, y_test)
|
541 |
-
|
|
|
|
|
|
|
|
|
|
|
542 |
# Select best model
|
543 |
best_model_name, best_model, best_metrics = self.select_best_model(results)
|
544 |
-
|
545 |
-
# Save model artifacts
|
546 |
if not self.save_model_artifacts(best_model, best_model_name, best_metrics):
|
547 |
-
return False, "Failed to save model artifacts"
|
548 |
-
|
549 |
# Save evaluation results
|
550 |
self.save_evaluation_results(results)
|
551 |
-
|
552 |
success_message = (
|
553 |
-
f"Model training completed successfully
|
554 |
-
f"Best model: {best_model_name}
|
555 |
-
f"
|
|
|
556 |
)
|
557 |
-
|
558 |
logger.info(success_message)
|
559 |
return True, success_message
|
560 |
-
|
561 |
except Exception as e:
|
562 |
-
error_message = f"Model training failed: {str(e)}"
|
563 |
logger.error(error_message)
|
|
|
564 |
return False, error_message
|
565 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
566 |
def main():
|
567 |
-
"""
|
568 |
-
|
569 |
-
success, message = trainer.train_model()
|
570 |
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
575 |
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
576 |
|
577 |
if __name__ == "__main__":
|
578 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import seaborn as sns
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
from sklearn.feature_selection import SelectKBest, chi2
|
4 |
+
from sklearn.preprocessing import FunctionTransformer
|
5 |
+
from sklearn.pipeline import Pipeline
|
6 |
+
from sklearn.metrics import (
|
7 |
+
accuracy_score, precision_score, recall_score, f1_score,
|
8 |
+
roc_auc_score, confusion_matrix, classification_report,
|
9 |
+
precision_recall_curve, roc_curve
|
10 |
+
)
|
11 |
+
from sklearn.model_selection import (
|
12 |
+
train_test_split, cross_val_score, GridSearchCV,
|
13 |
+
StratifiedKFold, validation_curve
|
14 |
+
)
|
15 |
+
from sklearn.ensemble import RandomForestClassifier
|
16 |
+
from sklearn.linear_model import LogisticRegression
|
17 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
18 |
import pandas as pd
|
19 |
import numpy as np
|
20 |
from pathlib import Path
|
|
|
27 |
import warnings
|
28 |
warnings.filterwarnings('ignore')
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# =============================================================================
|
32 |
+
# CENTRALIZED PATH CONFIGURATION - MATCHES FASTAPI SERVER
|
33 |
+
# =============================================================================
|
34 |
+
class PathConfig:
|
35 |
+
"""Centralized path management to ensure consistency across all components"""
|
36 |
+
|
37 |
+
# Base directories
|
38 |
+
BASE_DIR = Path("/tmp")
|
39 |
+
DATA_DIR = BASE_DIR / "data"
|
40 |
+
MODEL_DIR = BASE_DIR / "model" # CONSISTENT: /tmp/model/
|
41 |
+
LOGS_DIR = BASE_DIR / "logs"
|
42 |
+
RESULTS_DIR = BASE_DIR / "results"
|
43 |
+
|
44 |
+
# Model files - CONSISTENT PATHS (matches fastapi_server.py)
|
45 |
+
MODEL_FILE = MODEL_DIR / "model.pkl" # /tmp/model/model.pkl
|
46 |
+
VECTORIZER_FILE = MODEL_DIR / "vectorizer.pkl" # /tmp/model/vectorizer.pkl
|
47 |
+
PIPELINE_FILE = MODEL_DIR / "pipeline.pkl" # /tmp/model/pipeline.pkl
|
48 |
+
METADATA_FILE = BASE_DIR / "metadata.json" # /tmp/metadata.json
|
49 |
+
|
50 |
+
# Data files
|
51 |
+
COMBINED_DATASET = DATA_DIR / "combined_dataset.csv"
|
52 |
+
SCRAPED_DATA = DATA_DIR / "scraped_real.csv"
|
53 |
+
GENERATED_DATA = DATA_DIR / "generated_fake.csv"
|
54 |
+
|
55 |
+
# Log and result files
|
56 |
+
TRAINING_LOG = LOGS_DIR / "model_training.log"
|
57 |
+
EVALUATION_RESULTS = RESULTS_DIR / "evaluation_results.json"
|
58 |
+
|
59 |
+
@classmethod
|
60 |
+
def ensure_directories(cls):
|
61 |
+
"""Create all required directories with proper permissions"""
|
62 |
+
for attr_name in dir(cls):
|
63 |
+
attr = getattr(cls, attr_name)
|
64 |
+
if isinstance(attr, Path) and attr_name.endswith('_DIR'):
|
65 |
+
attr.mkdir(parents=True, exist_ok=True, mode=0o755)
|
66 |
+
|
67 |
+
# Additional directory creation for safety
|
68 |
+
for directory in [cls.BASE_DIR, cls.DATA_DIR, cls.MODEL_DIR, cls.LOGS_DIR, cls.RESULTS_DIR]:
|
69 |
+
directory.mkdir(parents=True, exist_ok=True, mode=0o755)
|
70 |
+
|
71 |
+
|
72 |
+
# Initialize directories at startup
|
73 |
+
PathConfig.ensure_directories()
|
74 |
+
|
75 |
+
|
76 |
+
# =============================================================================
|
77 |
+
# ENHANCED LOGGING CONFIGURATION
|
78 |
+
# =============================================================================
|
79 |
logging.basicConfig(
|
80 |
level=logging.INFO,
|
81 |
+
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
|
82 |
handlers=[
|
83 |
+
logging.FileHandler(PathConfig.TRAINING_LOG),
|
84 |
logging.StreamHandler()
|
85 |
]
|
86 |
)
|
87 |
logger = logging.getLogger(__name__)
|
88 |
|
89 |
+
|
90 |
+
# =============================================================================
|
91 |
+
# DATA VALIDATION PIPELINE
|
92 |
+
# =============================================================================
|
93 |
+
class DataValidator:
|
94 |
+
"""Comprehensive data validation for training pipeline"""
|
95 |
|
96 |
+
def __init__(self, min_text_length: int = 10, max_null_ratio: float = 0.1):
|
97 |
+
self.min_text_length = min_text_length
|
98 |
+
self.max_null_ratio = max_null_ratio
|
99 |
+
|
100 |
+
def validate_schema(self, df: pd.DataFrame) -> Tuple[bool, list]:
|
101 |
+
"""Validate data schema"""
|
102 |
+
errors = []
|
103 |
+
required_columns = ['text', 'label']
|
104 |
+
|
105 |
+
missing_cols = set(required_columns) - set(df.columns)
|
106 |
+
if missing_cols:
|
107 |
+
errors.append(f"Missing required columns: {missing_cols}")
|
108 |
+
|
109 |
+
return len(errors) == 0, errors
|
110 |
+
|
111 |
+
def validate_quality(self, df: pd.DataFrame) -> Tuple[bool, list]:
|
112 |
+
"""Validate data quality"""
|
113 |
+
errors = []
|
114 |
+
|
115 |
+
# Check null ratio
|
116 |
+
null_ratio = df.isnull().sum().sum() / (len(df) * len(df.columns))
|
117 |
+
if null_ratio > self.max_null_ratio:
|
118 |
+
errors.append(f"Too many nulls: {null_ratio:.2%} > {self.max_null_ratio:.2%}")
|
119 |
+
|
120 |
+
# Check text quality
|
121 |
+
if 'text' in df.columns:
|
122 |
+
short_texts = (df['text'].astype(str).str.len() < self.min_text_length).sum()
|
123 |
+
if short_texts > 0:
|
124 |
+
errors.append(f"{short_texts} texts below minimum length ({self.min_text_length} chars)")
|
125 |
+
|
126 |
+
# Check minimum samples
|
127 |
+
if len(df) < 100:
|
128 |
+
errors.append(f"Insufficient samples for training: {len(df)} < 100")
|
129 |
+
|
130 |
+
# Check class distribution
|
131 |
+
if 'label' in df.columns:
|
132 |
+
unique_labels = df['label'].unique()
|
133 |
+
if len(unique_labels) < 2:
|
134 |
+
errors.append(f"Need at least 2 classes, found: {unique_labels}")
|
135 |
+
|
136 |
+
label_counts = df['label'].value_counts()
|
137 |
+
min_class_ratio = label_counts.min() / label_counts.max()
|
138 |
+
if min_class_ratio < 0.05:
|
139 |
+
errors.append(f"Severe class imbalance: {min_class_ratio:.3f}")
|
140 |
+
elif min_class_ratio < 0.1:
|
141 |
+
logger.warning(f"Class imbalance detected: {min_class_ratio:.3f}")
|
142 |
+
|
143 |
+
return len(errors) == 0, errors
|
144 |
+
|
145 |
+
def validate(self, df: pd.DataFrame) -> Tuple[bool, Dict[str, list]]:
|
146 |
+
"""Complete data validation"""
|
147 |
+
all_valid = True
|
148 |
+
all_errors = {}
|
149 |
+
|
150 |
+
# Schema validation
|
151 |
+
schema_valid, schema_errors = self.validate_schema(df)
|
152 |
+
if not schema_valid:
|
153 |
+
all_valid = False
|
154 |
+
all_errors['schema'] = schema_errors
|
155 |
+
|
156 |
+
# Quality validation
|
157 |
+
quality_valid, quality_errors = self.validate_quality(df)
|
158 |
+
if not quality_valid:
|
159 |
+
all_valid = False
|
160 |
+
all_errors['quality'] = quality_errors
|
161 |
+
|
162 |
+
return all_valid, all_errors
|
163 |
+
|
164 |
+
|
165 |
+
# =============================================================================
|
166 |
+
# ENHANCED MODEL TRAINER WITH FIXED PATHS
|
167 |
+
# =============================================================================
|
168 |
+
class RobustModelTrainer:
|
169 |
+
"""Production-ready model trainer with comprehensive evaluation and FIXED PATH MANAGEMENT"""
|
170 |
+
|
171 |
def __init__(self):
|
172 |
+
# Use centralized path configuration
|
173 |
+
PathConfig.ensure_directories()
|
174 |
self.setup_training_config()
|
175 |
self.setup_models()
|
176 |
+
self.data_validator = DataValidator()
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
+
# Log path configuration for verification
|
179 |
+
logger.info("π§ Path Configuration:")
|
180 |
+
logger.info(f"Model Directory: {PathConfig.MODEL_DIR}")
|
181 |
+
logger.info(f"Pipeline File: {PathConfig.PIPELINE_FILE}")
|
182 |
+
logger.info(f"Model File: {PathConfig.MODEL_FILE}")
|
183 |
+
logger.info(f"Vectorizer File: {PathConfig.VECTORIZER_FILE}")
|
184 |
+
logger.info(f"Metadata File: {PathConfig.METADATA_FILE}")
|
185 |
+
|
|
|
|
|
|
|
|
|
186 |
def setup_training_config(self):
|
187 |
"""Setup training configuration"""
|
188 |
self.test_size = 0.2
|
|
|
196 |
self.max_iter = 1000
|
197 |
self.class_weight = 'balanced'
|
198 |
self.feature_selection_k = 5000
|
199 |
+
|
200 |
def setup_models(self):
|
201 |
"""Setup model configurations for comparison"""
|
202 |
self.models = {
|
|
|
225 |
}
|
226 |
}
|
227 |
}
|
228 |
+
|
229 |
def load_and_validate_data(self) -> Tuple[bool, Optional[pd.DataFrame], str]:
|
230 |
+
"""Load and validate training data with enhanced validation"""
|
231 |
try:
|
232 |
+
logger.info("Loading and validating training data...")
|
233 |
+
|
234 |
+
data_path = PathConfig.COMBINED_DATASET
|
|
|
235 |
|
236 |
+
if not data_path.exists():
|
237 |
+
return False, None, f"Data file not found: {data_path}"
|
238 |
+
|
239 |
# Load data
|
240 |
+
df = pd.read_csv(data_path)
|
241 |
+
logger.info(f"Loaded dataset with {len(df)} samples")
|
242 |
+
|
243 |
+
# Enhanced validation using DataValidator
|
244 |
+
valid, validation_errors = self.data_validator.validate(df)
|
245 |
+
|
246 |
+
if not valid:
|
247 |
+
error_msg = "Data validation failed:\n" + "\n".join([
|
248 |
+
f" {category}: {errors}" for category, errors in validation_errors.items()
|
249 |
+
])
|
250 |
+
logger.error(error_msg)
|
251 |
+
return False, None, error_msg
|
252 |
+
|
253 |
+
# Clean data
|
254 |
initial_count = len(df)
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
+
# Remove missing values
|
257 |
+
df = df.dropna(subset=['text', 'label'])
|
|
|
|
|
258 |
|
259 |
+
# Remove short texts
|
260 |
+
df = df[df['text'].astype(str).str.len() >= self.data_validator.min_text_length]
|
|
|
261 |
|
262 |
+
if len(df) < initial_count:
|
263 |
+
logger.info(f"π§Ή Cleaned data: removed {initial_count - len(df)} invalid samples")
|
264 |
+
|
265 |
+
# Log final statistics
|
266 |
label_counts = df['label'].value_counts()
|
267 |
+
logger.info(f"Data validation successful: {len(df)} samples")
|
|
|
|
|
|
|
|
|
268 |
logger.info(f"Class distribution: {label_counts.to_dict()}")
|
269 |
+
|
270 |
+
return True, df, "Data loaded and validated successfully"
|
271 |
+
|
272 |
except Exception as e:
|
273 |
error_msg = f"Error loading data: {str(e)}"
|
274 |
logger.error(error_msg)
|
275 |
return False, None, error_msg
|
276 |
+
|
277 |
def preprocess_text(self, text):
|
278 |
+
"""Advanced text preprocessing with better error handling"""
|
279 |
import re
|
280 |
+
|
281 |
+
try:
|
282 |
+
# Convert to string
|
283 |
+
text = str(text)
|
284 |
+
|
285 |
+
# Remove URLs
|
286 |
+
text = re.sub(r'http\S+|www\S+|https\S+', '', text)
|
287 |
+
|
288 |
+
# Remove email addresses
|
289 |
+
text = re.sub(r'\S+@\S+', '', text)
|
290 |
+
|
291 |
+
# Remove excessive punctuation
|
292 |
+
text = re.sub(r'[!]{2,}', '!', text)
|
293 |
+
text = re.sub(r'[?]{2,}', '?', text)
|
294 |
+
text = re.sub(r'[.]{3,}', '...', text)
|
295 |
+
|
296 |
+
# Remove non-alphabetic characters except spaces and basic punctuation
|
297 |
+
text = re.sub(r'[^a-zA-Z\s.!?]', '', text)
|
298 |
+
|
299 |
+
# Remove excessive whitespace
|
300 |
+
text = re.sub(r'\s+', ' ', text)
|
301 |
+
|
302 |
+
return text.strip().lower()
|
303 |
+
|
304 |
+
except Exception as e:
|
305 |
+
logger.warning(f"Text preprocessing failed for text, returning original: {e}")
|
306 |
+
return str(text).lower()
|
307 |
+
|
308 |
def create_preprocessing_pipeline(self) -> Pipeline:
|
309 |
+
"""Create advanced preprocessing pipeline with FIXED saving"""
|
310 |
+
logger.info("π§ Creating preprocessing pipeline...")
|
311 |
+
|
312 |
# Text preprocessing
|
313 |
text_preprocessor = FunctionTransformer(
|
314 |
func=lambda x: [self.preprocess_text(text) for text in x],
|
315 |
validate=False
|
316 |
)
|
317 |
+
|
318 |
# TF-IDF vectorization
|
319 |
vectorizer = TfidfVectorizer(
|
320 |
max_features=self.max_features,
|
|
|
325 |
sublinear_tf=True,
|
326 |
norm='l2'
|
327 |
)
|
328 |
+
|
329 |
# Feature selection
|
330 |
feature_selector = SelectKBest(
|
331 |
score_func=chi2,
|
332 |
k=self.feature_selection_k
|
333 |
)
|
334 |
+
|
335 |
# Create pipeline
|
336 |
pipeline = Pipeline([
|
337 |
('preprocess', text_preprocessor),
|
|
|
340 |
('model', None) # Will be set during training
|
341 |
])
|
342 |
|
343 |
+
logger.info("Preprocessing pipeline created successfully")
|
|
|
|
|
|
|
|
|
344 |
return pipeline
|
345 |
+
|
346 |
+
def save_model_artifacts(self, model, model_name: str, metrics: Dict) -> bool:
|
347 |
+
"""Save model artifacts with FIXED PATHS and comprehensive error handling"""
|
348 |
+
try:
|
349 |
+
logger.info("πΎ Saving model artifacts with corrected paths...")
|
350 |
+
|
351 |
+
# FIXED: Use centralized path configuration
|
352 |
+
pipeline_path = PathConfig.PIPELINE_FILE # /tmp/model/pipeline.pkl
|
353 |
+
model_path = PathConfig.MODEL_FILE # /tmp/model/model.pkl
|
354 |
+
vectorizer_path = PathConfig.VECTORIZER_FILE # /tmp/model/vectorizer.pkl
|
355 |
+
metadata_path = PathConfig.METADATA_FILE # /tmp/metadata.json
|
356 |
+
|
357 |
+
logger.info(f"Saving to paths:")
|
358 |
+
logger.info(f" Pipeline: {pipeline_path}")
|
359 |
+
logger.info(f" Model: {model_path}")
|
360 |
+
logger.info(f" Vectorizer: {vectorizer_path}")
|
361 |
+
logger.info(f" Metadata: {metadata_path}")
|
362 |
+
|
363 |
+
# Save the complete pipeline (FIXED PATH)
|
364 |
+
joblib.dump(model, pipeline_path)
|
365 |
+
logger.info("Saved complete pipeline")
|
366 |
+
|
367 |
+
# Save individual components for backward compatibility (FIXED PATHS)
|
|
|
|
|
|
|
|
|
|
|
368 |
try:
|
369 |
+
if hasattr(model, 'named_steps'):
|
370 |
+
# Save individual model
|
371 |
+
if 'model' in model.named_steps and model.named_steps['model'] is not None:
|
372 |
+
joblib.dump(model.named_steps['model'], model_path)
|
373 |
+
logger.info("Saved individual model component")
|
374 |
+
|
375 |
+
# Save individual vectorizer
|
376 |
+
if 'vectorize' in model.named_steps and model.named_steps['vectorize'] is not None:
|
377 |
+
joblib.dump(model.named_steps['vectorize'], vectorizer_path)
|
378 |
+
logger.info("Saved individual vectorizer component")
|
379 |
+
else:
|
380 |
+
logger.warning("Model doesn't have named_steps, skipping individual component saves")
|
381 |
+
|
382 |
except Exception as e:
|
383 |
+
logger.warning(f"Could not save individual components: {e}")
|
384 |
+
|
385 |
+
# Generate comprehensive metadata
|
386 |
+
metadata = self.generate_metadata(model_name, metrics)
|
387 |
+
|
388 |
+
# Save metadata (FIXED PATH)
|
389 |
+
with open(metadata_path, 'w') as f:
|
390 |
+
json.dump(metadata, f, indent=2)
|
391 |
+
logger.info("Saved model metadata")
|
392 |
+
|
393 |
+
# Verify all files were created
|
394 |
+
verification_results = {
|
395 |
+
'pipeline': pipeline_path.exists(),
|
396 |
+
'model': model_path.exists(),
|
397 |
+
'vectorizer': vectorizer_path.exists(),
|
398 |
+
'metadata': metadata_path.exists()
|
399 |
+
}
|
400 |
+
|
401 |
+
logger.info("π File verification results:")
|
402 |
+
for file_type, exists in verification_results.items():
|
403 |
+
status = "β
" if exists else "β"
|
404 |
+
logger.info(f" {status} {file_type}: {exists}")
|
405 |
+
|
406 |
+
# Check if at least the pipeline was saved
|
407 |
+
if not verification_results['pipeline']:
|
408 |
+
raise Exception("Critical: Pipeline file was not created")
|
409 |
+
|
410 |
+
logger.info("π Model artifacts saved successfully!")
|
411 |
+
return True
|
412 |
+
|
413 |
except Exception as e:
|
414 |
+
logger.error(f"β Failed to save model artifacts: {str(e)}")
|
415 |
+
return False
|
416 |
+
|
417 |
+
def generate_metadata(self, model_name: str, metrics: Dict) -> Dict:
|
418 |
+
"""Generate comprehensive metadata"""
|
419 |
+
# Generate data hash for versioning
|
420 |
+
data_hash = hashlib.md5(str(datetime.now()).encode()).hexdigest()[:8]
|
421 |
|
422 |
+
metadata = {
|
423 |
+
'model_version': f"v1.0_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
424 |
+
'model_type': model_name,
|
425 |
+
'data_version': data_hash,
|
426 |
+
'training_metrics': {
|
427 |
+
'test_accuracy': metrics.get('accuracy', 'Unknown'),
|
428 |
+
'test_f1': metrics.get('f1', 'Unknown'),
|
429 |
+
'test_precision': metrics.get('precision', 'Unknown'),
|
430 |
+
'test_recall': metrics.get('recall', 'Unknown'),
|
431 |
+
'test_roc_auc': metrics.get('roc_auc', 'Unknown'),
|
432 |
+
'overfitting_score': metrics.get('overfitting_score', 'Unknown'),
|
433 |
+
'cv_score_mean': metrics.get('cv_scores', {}).get('mean', 'Unknown'),
|
434 |
+
'cv_score_std': metrics.get('cv_scores', {}).get('std', 'Unknown')
|
435 |
+
},
|
436 |
+
'training_config': {
|
437 |
+
'test_size': self.test_size,
|
438 |
+
'validation_size': self.validation_size,
|
439 |
+
'cv_folds': self.cv_folds,
|
440 |
+
'max_features': self.max_features,
|
441 |
+
'ngram_range': self.ngram_range,
|
442 |
+
'feature_selection_k': self.feature_selection_k,
|
443 |
+
'class_weight': self.class_weight
|
444 |
+
},
|
445 |
+
'paths': {
|
446 |
+
'pipeline_file': str(PathConfig.PIPELINE_FILE),
|
447 |
+
'model_file': str(PathConfig.MODEL_FILE),
|
448 |
+
'vectorizer_file': str(PathConfig.VECTORIZER_FILE)
|
449 |
+
},
|
450 |
+
'timestamp': datetime.now().isoformat(),
|
451 |
+
'training_completed': True
|
452 |
+
}
|
453 |
+
|
454 |
+
return metadata
|
455 |
+
|
456 |
+
def comprehensive_evaluation(self, model, X_test, y_test, X_train=None, y_train=None) -> Dict:
|
457 |
+
"""Comprehensive model evaluation with multiple metrics"""
|
458 |
+
logger.info("Starting comprehensive model evaluation...")
|
459 |
+
|
460 |
try:
|
461 |
+
# Predictions
|
462 |
+
y_pred = model.predict(X_test)
|
463 |
+
y_pred_proba = model.predict_proba(X_test)[:, 1] if hasattr(model, 'predict_proba') else None
|
464 |
+
|
465 |
+
# Basic metrics
|
466 |
+
metrics = {
|
467 |
+
'accuracy': float(accuracy_score(y_test, y_pred)),
|
468 |
+
'precision': float(precision_score(y_test, y_pred, average='weighted', zero_division=0)),
|
469 |
+
'recall': float(recall_score(y_test, y_pred, average='weighted', zero_division=0)),
|
470 |
+
'f1': float(f1_score(y_test, y_pred, average='weighted', zero_division=0))
|
471 |
+
}
|
472 |
+
|
473 |
+
# ROC AUC if probabilities available
|
474 |
+
if y_pred_proba is not None:
|
475 |
+
try:
|
476 |
+
metrics['roc_auc'] = float(roc_auc_score(y_test, y_pred_proba))
|
477 |
+
except Exception as e:
|
478 |
+
logger.warning(f"Could not calculate ROC AUC: {e}")
|
479 |
+
metrics['roc_auc'] = 0.0
|
480 |
+
else:
|
481 |
+
metrics['roc_auc'] = 0.0
|
482 |
+
|
483 |
+
# Confusion matrix
|
484 |
+
cm = confusion_matrix(y_test, y_pred)
|
485 |
+
metrics['confusion_matrix'] = cm.tolist()
|
486 |
+
|
487 |
+
# Classification report
|
488 |
+
try:
|
489 |
+
class_report = classification_report(y_test, y_pred, output_dict=True, zero_division=0)
|
490 |
+
metrics['classification_report'] = class_report
|
491 |
+
except Exception as e:
|
492 |
+
logger.warning(f"Could not generate classification report: {e}")
|
493 |
+
|
494 |
+
# Cross-validation scores if training data provided
|
495 |
+
if X_train is not None and y_train is not None:
|
496 |
+
try:
|
497 |
+
cv_scores = cross_val_score(
|
498 |
+
model, X_train, y_train,
|
499 |
+
cv=StratifiedKFold(n_splits=self.cv_folds, shuffle=True, random_state=self.random_state),
|
500 |
+
scoring='f1_weighted'
|
501 |
+
)
|
502 |
+
metrics['cv_scores'] = {
|
503 |
+
'mean': float(cv_scores.mean()),
|
504 |
+
'std': float(cv_scores.std()),
|
505 |
+
'scores': cv_scores.tolist()
|
506 |
+
}
|
507 |
+
except Exception as e:
|
508 |
+
logger.warning(f"Cross-validation failed: {e}")
|
509 |
+
metrics['cv_scores'] = {'mean': 0.0, 'std': 0.0, 'scores': []}
|
510 |
+
|
511 |
# Training accuracy for overfitting detection
|
512 |
if X_train is not None and y_train is not None:
|
513 |
+
try:
|
514 |
+
y_train_pred = model.predict(X_train)
|
515 |
+
train_accuracy = accuracy_score(y_train, y_train_pred)
|
516 |
+
metrics['train_accuracy'] = float(train_accuracy)
|
517 |
+
metrics['overfitting_score'] = float(train_accuracy - metrics['accuracy'])
|
518 |
+
except Exception as e:
|
519 |
+
logger.warning(f"Overfitting detection failed: {e}")
|
520 |
+
|
521 |
+
logger.info(f"π Evaluation completed - F1: {metrics['f1']:.4f}, Accuracy: {metrics['accuracy']:.4f}")
|
522 |
+
return metrics
|
523 |
+
|
524 |
except Exception as e:
|
525 |
+
logger.error(f"β Evaluation failed: {e}")
|
526 |
+
return {
|
527 |
+
'accuracy': 0.0, 'precision': 0.0, 'recall': 0.0,
|
528 |
+
'f1': 0.0, 'roc_auc': 0.0, 'error': str(e)
|
529 |
+
}
|
530 |
+
|
531 |
def hyperparameter_tuning(self, pipeline, X_train, y_train, model_name: str) -> Tuple[Any, Dict]:
|
532 |
"""Perform hyperparameter tuning with cross-validation"""
|
533 |
+
logger.info(f"π§ Starting hyperparameter tuning for {model_name}...")
|
534 |
+
|
535 |
try:
|
536 |
# Set the model in the pipeline
|
537 |
pipeline.set_params(model=self.models[model_name]['model'])
|
538 |
+
|
539 |
# Get parameter grid
|
540 |
param_grid = self.models[model_name]['param_grid']
|
541 |
+
|
542 |
# Create GridSearchCV
|
543 |
grid_search = GridSearchCV(
|
544 |
pipeline,
|
|
|
548 |
n_jobs=-1,
|
549 |
verbose=1
|
550 |
)
|
551 |
+
|
552 |
# Fit grid search
|
553 |
grid_search.fit(X_train, y_train)
|
554 |
+
|
555 |
# Extract results
|
556 |
tuning_results = {
|
557 |
'best_params': grid_search.best_params_,
|
|
|
560 |
'cv_results': {
|
561 |
'mean_test_scores': grid_search.cv_results_['mean_test_score'].tolist(),
|
562 |
'std_test_scores': grid_search.cv_results_['std_test_score'].tolist(),
|
563 |
+
'params': [dict(p) for p in grid_search.cv_results_['params']]
|
564 |
}
|
565 |
}
|
566 |
+
|
567 |
logger.info(f"Hyperparameter tuning completed for {model_name}")
|
568 |
logger.info(f"Best score: {grid_search.best_score_:.4f}")
|
569 |
logger.info(f"Best params: {grid_search.best_params_}")
|
570 |
+
|
571 |
return grid_search.best_estimator_, tuning_results
|
572 |
+
|
573 |
except Exception as e:
|
574 |
+
logger.error(f"β Hyperparameter tuning failed for {model_name}: {str(e)}")
|
575 |
# Return basic model if tuning fails
|
576 |
+
try:
|
577 |
+
pipeline.set_params(model=self.models[model_name]['model'])
|
578 |
+
pipeline.fit(X_train, y_train)
|
579 |
+
return pipeline, {'error': str(e), 'used_default_params': True}
|
580 |
+
except Exception as e2:
|
581 |
+
logger.error(f"β Even basic model training failed: {str(e2)}")
|
582 |
+
raise e2
|
583 |
+
|
584 |
def train_and_evaluate_models(self, X_train, X_test, y_train, y_test) -> Dict:
|
585 |
"""Train and evaluate multiple models"""
|
586 |
+
logger.info("π Starting model training and evaluation...")
|
587 |
+
|
588 |
results = {}
|
589 |
+
|
590 |
for model_name in self.models.keys():
|
591 |
logger.info(f"Training {model_name}...")
|
592 |
+
|
593 |
try:
|
594 |
+
# Create fresh pipeline for each model
|
595 |
pipeline = self.create_preprocessing_pipeline()
|
596 |
+
|
597 |
# Hyperparameter tuning
|
598 |
best_model, tuning_results = self.hyperparameter_tuning(
|
599 |
pipeline, X_train, y_train, model_name
|
600 |
)
|
601 |
+
|
602 |
# Comprehensive evaluation
|
603 |
evaluation_metrics = self.comprehensive_evaluation(
|
604 |
best_model, X_test, y_test, X_train, y_train
|
605 |
)
|
606 |
+
|
607 |
# Store results
|
608 |
results[model_name] = {
|
609 |
'model': best_model,
|
|
|
611 |
'evaluation_metrics': evaluation_metrics,
|
612 |
'training_time': datetime.now().isoformat()
|
613 |
}
|
614 |
+
|
615 |
+
logger.info(f"β
Model {model_name} - F1: {evaluation_metrics['f1']:.4f}, "
|
616 |
+
f"Accuracy: {evaluation_metrics['accuracy']:.4f}")
|
617 |
+
|
618 |
except Exception as e:
|
619 |
+
logger.error(f"β Training failed for {model_name}: {str(e)}")
|
620 |
results[model_name] = {'error': str(e)}
|
621 |
+
|
622 |
return results
|
623 |
+
|
624 |
def select_best_model(self, results: Dict) -> Tuple[str, Any, Dict]:
|
625 |
"""Select the best performing model"""
|
626 |
+
logger.info("π Selecting best model...")
|
627 |
+
|
628 |
best_model_name = None
|
629 |
best_model = None
|
630 |
best_score = -1
|
631 |
best_metrics = None
|
632 |
+
|
633 |
for model_name, result in results.items():
|
634 |
if 'error' in result:
|
635 |
+
logger.warning(f"Skipping {model_name} due to error: {result['error']}")
|
636 |
continue
|
637 |
+
|
638 |
# Use F1 score as primary metric
|
639 |
f1_score = result['evaluation_metrics']['f1']
|
640 |
+
|
641 |
if f1_score > best_score:
|
642 |
best_score = f1_score
|
643 |
best_model_name = model_name
|
644 |
best_model = result['model']
|
645 |
best_metrics = result['evaluation_metrics']
|
646 |
+
|
647 |
if best_model_name is None:
|
648 |
+
raise ValueError("β No models trained successfully")
|
649 |
+
|
650 |
+
logger.info(f"π Best model: {best_model_name} with F1 score: {best_score:.4f}")
|
651 |
return best_model_name, best_model, best_metrics
|
652 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
653 |
def save_evaluation_results(self, results: Dict) -> bool:
|
654 |
"""Save comprehensive evaluation results"""
|
655 |
try:
|
|
|
661 |
else:
|
662 |
clean_results[model_name] = {
|
663 |
'tuning_results': {
|
664 |
+
k: v for k, v in result['tuning_results'].items()
|
665 |
+
if k != 'best_estimator' # Can't serialize sklearn objects
|
666 |
},
|
667 |
'evaluation_metrics': result['evaluation_metrics'],
|
668 |
'training_time': result['training_time']
|
669 |
}
|
670 |
+
|
671 |
+
# Save results to centralized path
|
672 |
+
evaluation_path = PathConfig.EVALUATION_RESULTS
|
673 |
+
with open(evaluation_path, 'w') as f:
|
674 |
json.dump(clean_results, f, indent=2, default=str)
|
675 |
+
|
676 |
+
logger.info(f"π Evaluation results saved to {evaluation_path}")
|
677 |
return True
|
678 |
+
|
679 |
except Exception as e:
|
680 |
+
logger.error(f"β Failed to save evaluation results: {str(e)}")
|
681 |
return False
|
682 |
+
|
683 |
def train_model(self, data_path: str = None) -> Tuple[bool, str]:
|
684 |
"""Main training function with comprehensive pipeline"""
|
685 |
try:
|
686 |
+
logger.info("π Starting model training pipeline...")
|
|
|
|
|
|
|
|
|
687 |
|
688 |
+
# Log system information
|
689 |
+
logger.info(f"Training environment: {PathConfig.BASE_DIR}")
|
690 |
+
PathConfig.ensure_directories()
|
691 |
+
|
692 |
# Load and validate data
|
693 |
success, df, message = self.load_and_validate_data()
|
694 |
if not success:
|
695 |
return False, message
|
696 |
+
|
697 |
# Prepare data
|
698 |
X = df['text'].values
|
699 |
y = df['label'].values
|
700 |
+
|
701 |
# Train-test split
|
702 |
X_train, X_test, y_train, y_test = train_test_split(
|
703 |
+
X, y,
|
704 |
test_size=self.test_size,
|
705 |
stratify=y,
|
706 |
random_state=self.random_state
|
707 |
)
|
708 |
+
|
709 |
logger.info(f"Data split: {len(X_train)} train, {len(X_test)} test")
|
710 |
+
|
711 |
# Train and evaluate models
|
712 |
results = self.train_and_evaluate_models(X_train, X_test, y_train, y_test)
|
713 |
+
|
714 |
+
# Check if any models were trained successfully
|
715 |
+
successful_models = [name for name, result in results.items() if 'error' not in result]
|
716 |
+
if not successful_models:
|
717 |
+
return False, "β All model training attempts failed"
|
718 |
+
|
719 |
# Select best model
|
720 |
best_model_name, best_model, best_metrics = self.select_best_model(results)
|
721 |
+
|
722 |
+
# Save model artifacts with FIXED paths
|
723 |
if not self.save_model_artifacts(best_model, best_model_name, best_metrics):
|
724 |
+
return False, "β Failed to save model artifacts"
|
725 |
+
|
726 |
# Save evaluation results
|
727 |
self.save_evaluation_results(results)
|
728 |
+
|
729 |
success_message = (
|
730 |
+
f"Model training completed successfully!\n"
|
731 |
+
f"Best model: {best_model_name}\n"
|
732 |
+
f"Performance: F1={best_metrics['f1']:.4f}, Accuracy={best_metrics['accuracy']:.4f}\n"
|
733 |
+
f"Artifacts saved to: {PathConfig.MODEL_DIR}"
|
734 |
)
|
735 |
+
|
736 |
logger.info(success_message)
|
737 |
return True, success_message
|
738 |
+
|
739 |
except Exception as e:
|
740 |
+
error_message = f"β Model training failed: {str(e)}"
|
741 |
logger.error(error_message)
|
742 |
+
logger.error(f"π Full traceback: {traceback.format_exc()}")
|
743 |
return False, error_message
|
744 |
|
745 |
+
|
746 |
+
# =============================================================================
|
747 |
+
# TRAINING UTILITIES AND DIAGNOSTICS
|
748 |
+
# =============================================================================
|
749 |
+
class TrainingDiagnostics:
|
750 |
+
"""Diagnostic utilities for training pipeline"""
|
751 |
+
|
752 |
+
@staticmethod
|
753 |
+
def check_data_availability():
|
754 |
+
"""Check if training data is available"""
|
755 |
+
data_path = PathConfig.COMBINED_DATASET
|
756 |
+
|
757 |
+
if not data_path.exists():
|
758 |
+
logger.error(f"β Training data not found at: {data_path}")
|
759 |
+
|
760 |
+
# Check what files are available
|
761 |
+
if PathConfig.DATA_DIR.exists():
|
762 |
+
available_files = list(PathConfig.DATA_DIR.iterdir())
|
763 |
+
logger.info(f"Available files in data directory: {[f.name for f in available_files]}")
|
764 |
+
else:
|
765 |
+
logger.error(f"β Data directory doesn't exist: {PathConfig.DATA_DIR}")
|
766 |
+
|
767 |
+
return False
|
768 |
+
|
769 |
+
logger.info(f"β
Training data found at: {data_path}")
|
770 |
+
return True
|
771 |
+
|
772 |
+
@staticmethod
|
773 |
+
def verify_model_output():
|
774 |
+
"""Verify that model files were created correctly"""
|
775 |
+
files_to_check = {
|
776 |
+
'Pipeline': PathConfig.PIPELINE_FILE,
|
777 |
+
'Model': PathConfig.MODEL_FILE,
|
778 |
+
'Vectorizer': PathConfig.VECTORIZER_FILE,
|
779 |
+
'Metadata': PathConfig.METADATA_FILE
|
780 |
+
}
|
781 |
+
|
782 |
+
logger.info("π Verifying model output files:")
|
783 |
+
all_exist = True
|
784 |
+
|
785 |
+
for file_type, file_path in files_to_check.items():
|
786 |
+
exists = file_path.exists()
|
787 |
+
size = file_path.stat().st_size if exists else 0
|
788 |
+
|
789 |
+
status = "β
" if exists else "β"
|
790 |
+
logger.info(f" {status} {file_type}: {file_path} ({size} bytes)")
|
791 |
+
|
792 |
+
if not exists:
|
793 |
+
all_exist = False
|
794 |
+
|
795 |
+
return all_exist
|
796 |
+
|
797 |
+
@staticmethod
|
798 |
+
def test_model_loading():
|
799 |
+
"""Test if the saved model can be loaded correctly"""
|
800 |
+
try:
|
801 |
+
logger.info("π§ͺ Testing model loading...")
|
802 |
+
|
803 |
+
# Try loading pipeline
|
804 |
+
if PathConfig.PIPELINE_FILE.exists():
|
805 |
+
pipeline = joblib.load(PathConfig.PIPELINE_FILE)
|
806 |
+
logger.info("β
Pipeline loaded successfully")
|
807 |
+
|
808 |
+
# Test prediction
|
809 |
+
test_text = ["This is a test article for verification."]
|
810 |
+
prediction = pipeline.predict(test_text)
|
811 |
+
logger.info(f"β
Test prediction successful: {prediction}")
|
812 |
+
|
813 |
+
return True
|
814 |
+
else:
|
815 |
+
logger.error("β Pipeline file not found")
|
816 |
+
return False
|
817 |
+
|
818 |
+
except Exception as e:
|
819 |
+
logger.error(f"β Model loading test failed: {e}")
|
820 |
+
return False
|
821 |
+
|
822 |
+
|
823 |
+
# ================================
|
824 |
+
# ENHANCED MAIN EXECUTION FUNCTION
|
825 |
+
# ================================
|
826 |
def main():
|
827 |
+
"""Enhanced main execution function with comprehensive diagnostics"""
|
828 |
+
import traceback
|
|
|
829 |
|
830 |
+
logger.info("π Starting Enhanced Model Training Pipeline")
|
831 |
+
logger.info("=" * 60)
|
832 |
+
|
833 |
+
try:
|
834 |
+
# Step 1: Check data availability
|
835 |
+
logger.info("π Step 1: Checking data availability...")
|
836 |
+
if not TrainingDiagnostics.check_data_availability():
|
837 |
+
logger.error("β Training aborted: No data available")
|
838 |
+
print("β Training failed: Training data not found")
|
839 |
+
print(f"π Expected data location: {PathConfig.COMBINED_DATASET}")
|
840 |
+
print("π‘ Please ensure the data preparation step has been completed")
|
841 |
+
exit(1)
|
842 |
+
|
843 |
+
# Step 2: Initialize trainer
|
844 |
+
logger.info("π Step 2: Initializing trainer...")
|
845 |
+
trainer = RobustModelTrainer()
|
846 |
+
|
847 |
+
# Step 3: Train model
|
848 |
+
logger.info("π Step 3: Training model...")
|
849 |
+
success, message = trainer.train_model()
|
850 |
+
|
851 |
+
if success:
|
852 |
+
# Step 4: Verify output
|
853 |
+
logger.info("π Step 4: Verifying model output...")
|
854 |
+
if TrainingDiagnostics.verify_model_output():
|
855 |
+
logger.info("β
All model files created successfully")
|
856 |
+
else:
|
857 |
+
logger.warning("β οΈ Some model files may be missing")
|
858 |
+
|
859 |
+
# Step 5: Test model loading
|
860 |
+
logger.info("π Step 5: Testing model loading...")
|
861 |
+
if TrainingDiagnostics.test_model_loading():
|
862 |
+
logger.info("β
Model loading verification successful")
|
863 |
+
else:
|
864 |
+
logger.warning("β οΈ Model loading verification failed")
|
865 |
+
|
866 |
+
# Success summary
|
867 |
+
logger.info("=" * 60)
|
868 |
+
logger.info("TRAINING COMPLETED SUCCESSFULLY!")
|
869 |
+
logger.info("=" * 60)
|
870 |
+
print("β
Training completed successfully!")
|
871 |
+
print(f"{message}")
|
872 |
+
print(f"Model files saved to: {PathConfig.MODEL_DIR}")
|
873 |
+
print("Next steps:")
|
874 |
+
print(" 1. Start the FastAPI server to test predictions")
|
875 |
+
print(" 2. Run the monitoring dashboard")
|
876 |
+
print(" 3. Perform model validation tests")
|
877 |
+
|
878 |
+
else:
|
879 |
+
logger.error("=" * 60)
|
880 |
+
logger.error("β TRAINING FAILED!")
|
881 |
+
logger.error("=" * 60)
|
882 |
+
print("β Training failed!")
|
883 |
+
print(f"π Error: {message}")
|
884 |
+
print("\nπ§ Troubleshooting steps:")
|
885 |
+
print(" 1. Check if training data exists and is properly formatted")
|
886 |
+
print(" 2. Verify sufficient disk space and memory")
|
887 |
+
print(" 3. Review the training logs for detailed error information")
|
888 |
+
exit(1)
|
889 |
+
|
890 |
+
except KeyboardInterrupt:
|
891 |
+
logger.info("βΉοΈ Training interrupted by user")
|
892 |
+
print("\nβΉοΈ Training interrupted by user")
|
893 |
exit(1)
|
894 |
+
|
895 |
+
except Exception as e:
|
896 |
+
logger.error(f"Unexpected error during training: {str(e)}")
|
897 |
+
logger.error(f"Full traceback: {traceback.format_exc()}")
|
898 |
+
print(f"Unexpected error: {str(e)}")
|
899 |
+
print("Check the training logs for more details")
|
900 |
+
exit(1)
|
901 |
+
|
902 |
+
|
903 |
+
# ============================
|
904 |
+
# STANDALONE TESTING FUNCTIONS
|
905 |
+
# ============================
|
906 |
+
def test_path_configuration():
|
907 |
+
"""Test path configuration and directory creation"""
|
908 |
+
print("π§ͺ Testing path configuration...")
|
909 |
+
|
910 |
+
PathConfig.ensure_directories()
|
911 |
+
|
912 |
+
directories = [
|
913 |
+
PathConfig.BASE_DIR, PathConfig.DATA_DIR,
|
914 |
+
PathConfig.MODEL_DIR, PathConfig.LOGS_DIR, PathConfig.RESULTS_DIR
|
915 |
+
]
|
916 |
+
|
917 |
+
for directory in directories:
|
918 |
+
if directory.exists():
|
919 |
+
print(f"β
{directory}")
|
920 |
+
else:
|
921 |
+
print(f"β {directory}")
|
922 |
+
|
923 |
+
print("\n Expected file locations:")
|
924 |
+
print(f" Pipeline: {PathConfig.PIPELINE_FILE}")
|
925 |
+
print(f" Model: {PathConfig.MODEL_FILE}")
|
926 |
+
print(f" Vectorizer: {PathConfig.VECTORIZER_FILE}")
|
927 |
+
print(f" Metadata: {PathConfig.METADATA_FILE}")
|
928 |
+
|
929 |
+
|
930 |
+
def quick_data_check():
|
931 |
+
"""Quick check of training data"""
|
932 |
+
print("Quick data check...")
|
933 |
+
|
934 |
+
data_path = PathConfig.COMBINED_DATASET
|
935 |
+
if data_path.exists():
|
936 |
+
try:
|
937 |
+
df = pd.read_csv(data_path)
|
938 |
+
print(f"Data loaded: {len(df)} rows, {len(df.columns)} columns")
|
939 |
+
print(f"Columns: {list(df.columns)}")
|
940 |
+
if 'label' in df.columns:
|
941 |
+
print(f"Label distribution: {df['label'].value_counts().to_dict()}")
|
942 |
+
except Exception as e:
|
943 |
+
print(f"β Error reading data: {e}")
|
944 |
+
else:
|
945 |
+
print(f"β Data file not found: {data_path}")
|
946 |
+
|
947 |
|
948 |
if __name__ == "__main__":
|
949 |
+
import sys
|
950 |
+
|
951 |
+
# Handle command line arguments for testing
|
952 |
+
if len(sys.argv) > 1:
|
953 |
+
if sys.argv[1] == "test-paths":
|
954 |
+
test_path_configuration()
|
955 |
+
elif sys.argv[1] == "test-data":
|
956 |
+
quick_data_check()
|
957 |
+
elif sys.argv[1] == "test-loading":
|
958 |
+
TrainingDiagnostics.test_model_loading()
|
959 |
+
else:
|
960 |
+
print("Available test commands:")
|
961 |
+
print(" python train.py test-paths # Test path configuration")
|
962 |
+
print(" python train.py test-data # Quick data check")
|
963 |
+
print(" python train.py test-loading # Test model loading")
|
964 |
+
else:
|
965 |
+
# Run main training
|
966 |
+
main()
|