""" Test script to diagnose and fix sklearn model loading issues """ import joblib import pickle import os import sys def test_model_loading(): """Test loading the sklearn models""" print("Testing sklearn model loading...") print("=" * 40) # Test scaler print("Testing StandardScaler...") try: scaler = joblib.load('face_recognition_scaler.sav') print("OK: Scaler loaded successfully") # Test with dummy data import numpy as np dummy_data = np.random.randn(1, 5) # Assuming 5 features scaled_data = scaler.transform(dummy_data) print(f"OK: Scaler transform works: {scaled_data.shape}") except Exception as e: print(f"ERROR: Scaler error: {e}") return False # Test classifier print("\nTesting KNeighborsClassifier...") try: classifier = joblib.load('decision_tree_model.sav') print("OK: Classifier loaded successfully") # Test prediction prediction = classifier.predict(scaled_data) print(f"OK: Classifier prediction works: {prediction[0]}") except Exception as e: print(f"ERROR: Classifier error: {e}") return False return True def try_compatibility_fixes(): """Try different compatibility approaches""" print("\nTrying compatibility fixes...") print("=" * 40) # Method 1: Try with different joblib versions print("Method 1: Trying with different joblib parameters...") try: scaler = joblib.load('face_recognition_scaler.sav', mmap_mode=None) classifier = joblib.load('decision_tree_model.sav', mmap_mode=None) print("OK: Loaded with mmap_mode=None") return True except Exception as e: print(f"ERROR: Method 1 failed: {e}") # Method 2: Try with pickle directly print("\nMethod 2: Trying with pickle...") try: with open('face_recognition_scaler.sav', 'rb') as f: scaler = pickle.load(f) with open('decision_tree_model.sav', 'rb') as f: classifier = pickle.load(f) print("OK: Loaded with pickle") return True except Exception as e: print(f"ERROR: Method 2 failed: {e}") # Method 3: Try with different sklearn version print("\nMethod 3: Checking sklearn version compatibility...") import sklearn print(f"Current sklearn version: {sklearn.__version__}") # Try to downgrade sklearn temporarily print("You may need to downgrade sklearn to match the training version") print("Try: pip install scikit-learn==1.6.1") return False def create_dummy_models(): """Create dummy models for testing""" print("\nCreating dummy models for testing...") print("=" * 40) try: from sklearn.neighbors import KNeighborsClassifier from sklearn.preprocessing import StandardScaler import numpy as np # Create dummy data n_samples = 50 n_features = 5 X = np.random.randn(n_samples, n_features) y = np.random.randint(0, 5, n_samples) # Create and fit scaler scaler = StandardScaler() scaler.fit(X) joblib.dump(scaler, 'face_recognition_scaler_dummy.sav') print("OK: Created dummy scaler") # Create and fit classifier classifier = KNeighborsClassifier(n_neighbors=3) classifier.fit(scaler.transform(X), y) joblib.dump(classifier, 'decision_tree_model_dummy.sav') print("OK: Created dummy classifier") # Test the dummy models test_data = np.random.randn(1, n_features) scaled_data = scaler.transform(test_data) prediction = classifier.predict(scaled_data) print(f"OK: Dummy model test: {prediction[0]}") return True except Exception as e: print(f"ERROR: Error creating dummy models: {e}") return False def main(): print("Sklearn Model Loading Diagnostic") print("=" * 50) # Check if model files exist model_files = ['decision_tree_model.sav', 'face_recognition_scaler.sav'] for file in model_files: if os.path.exists(file): print(f"OK: Found {file}") else: print(f"ERROR: Missing {file}") return # Test current models if test_model_loading(): print("\nSUCCESS! Your models are working fine!") return # Try compatibility fixes if try_compatibility_fixes(): print("\nSUCCESS! Fixed with compatibility approach!") return # Create dummy models as fallback if create_dummy_models(): print("\nWARNING: Created dummy models. You should retrain with current sklearn version.") print("To use dummy models, rename them:") print(" mv face_recognition_scaler_dummy.sav face_recognition_scaler.sav") print(" mv decision_tree_model_dummy.sav decision_tree_model.sav") print("\n" + "=" * 50) print("RECOMMENDATIONS:") print("1. Downgrade sklearn: pip install scikit-learn==1.6.1") print("2. Retrain your models with current sklearn version") print("3. Use the dummy models for testing") if __name__ == "__main__": main()