Spaces:
Sleeping
Sleeping
Commit ·
582b61d
1
Parent(s): a227197
Fix model loading issues and Dockerfile dependencies for Hugging Face
Browse files358 unmodified lines359360361362363364365366367368369370371372373133 unmodified lines507508509510511512513514515516517518519520521522523524525526527358 unmodified lineslogger.error(f"Model file not found at: {model_path}")return False# Load the modelwith open(model_path, 'rb') as f:model_data = pickle.load(f)# Extract componentsself.model = model_data['model']self.tfidf_vectorizer = model_data['tfidf_vectorizer']self.label_encoder = model_data['label_encoder']self.text_cleaner = model_data.get('text_cleaner', clean_text) # Use module-level functionself.model_accuracy = model_data.get('accuracy', 0.0)self.model_f1_score = model_data.get('f1_score', 0.0)self.classes = model_data.get('classes', [])133 unmodified lineslogger.error(f" - {features_path}")return False# Load model components with explicit binary mode and protocolprint(f" Loading stress_model.pkl...")with open(model_path, 'rb') as f:self.model = pickle.load(f, encoding='bytes')print(f" Loading stress_scaler.pkl...")with open(scaler_path, 'rb') as f:self.scaler = pickle.load(f, encoding='bytes')print(f" Loading feature_names.pkl...")with open(features_path, 'rb') as f:self.feature_names = pickle.load(f, encoding='bytes')# Convert bytes to string if neededif self.feature_names and isinstance(self.feature_names[0], bytes):self.feature_names = [f.decode('utf-8') for f in self.feature_names]self.is_initialized = Trueprint(f" ✅ Model loaded successfully!")358 unmodified lines359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406133 unmodified lines540541542543544545546547548549550551552553554555556557558559560561562563564565566358 unmodified lineslogger.error(f"Model file not found at: {model_path}")return Falsemodel_data = None# Try joblib first (more robust for sklearn models)try:import joblibmodel_data = joblib.load(model_path)logger.info("Model loaded with joblib")except Exception as e:logger.warning(f"Joblib load failed: {e}, trying pickle...")# Fallback to pickle with different approachesif model_data is None:try:with open(model_path, 'rb') as f:model_data = pickle.load(f)logger.info("Model loaded with pickle")except AttributeError as e:# Handle the case where text_cleaner function can't be foundlogger.warning(f"Pickle AttributeError (likely function reference): {e}")# Try loading with custom unpickler that ignores missing functionsimport ioclass CustomUnpickler(pickle.Unpickler):def find_class(self, module, name):# Replace any missing function with our module-level clean_textif name == 'clean_text' or 'clean' in name.lower():return clean_textreturn super().find_class(module, name)with open(model_path, 'rb') as f:model_data = CustomUnpickler(f).load()logger.info("Model loaded with custom unpickler")if model_data is None:logger.error("Failed to load model with all methods")return False# Extract componentsself.model = model_data['model']self.tfidf_vectorizer = model_data['tfidf_vectorizer']self.label_encoder = model_data.get('label_encoder')# Always use module-level clean_text to avoid pickle issuesself.text_cleaner = clean_textself.model_accuracy = model_data.get('accuracy', 0.0)self.model_f1_score = model_data.get('f1_score', 0.0)self.classes = model_data.get('classes', [])133 unmodified lineslogger.error(f" - {features_path}")return False# Helper function to load pkl with joblib fallbackdef load_pkl(path):try:import joblibreturn joblib.load(path)except Exception:with open(path, 'rb') as f:return pickle.load(f, encoding='bytes')# Load model componentsprint(f" Loading stress_model.pkl...")self.model = load_pkl(model_path)print(f" Loading stress_scaler.pkl...")self.scaler = load_pkl(scaler_path)print(f" Loading feature_names.pkl...")self.feature_names = load_pkl(features_path)# Convert bytes to string if neededif self.feature_names and isinstance(self.feature_names[0], bytes):self.feature_names = [f.decode('utf-8') for f in self.feature_names]self.is_initialized = Trueprint(f" ✅ Model loaded successfully!")
app.py+54-15