Bibek Mukherjee commited on
Commit
e05e897
·
verified ·
1 Parent(s): 80d5e09

Update src/api/loan_model.py

Browse files
Files changed (1) hide show
  1. src/api/loan_model.py +35 -12
src/api/loan_model.py CHANGED
@@ -41,29 +41,52 @@ class LoanApprovalModel:
41
  if load_model:
42
  self.load_components()
43
 
44
- # Add this to your load_components method
45
  def load_components(self):
 
46
  try:
47
- # Original loading code
48
- self.model = joblib.load(self.model_path)
49
- self.scaler = joblib.load(self.scaler_path)
50
-
51
- # Try to load the explainer with error handling
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  try:
53
- explainer_path = os.path.join(self.model_dir, 'loan_explainer.pkl')
54
  if os.path.exists(explainer_path):
55
  self.explainer = joblib.load(explainer_path)
56
- else:
57
- self.explainer = None
58
- logger.warning("Explainer file not found. Explanations will be limited.")
59
  except Exception as explainer_error:
60
- logger.error(f"Error loading explainer: {str(explainer_error)}")
61
  self.explainer = None
62
- logger.warning("Continuing without explainer. Explanations will be limited.")
63
 
64
  logger.info("Model components loaded successfully")
 
65
  except Exception as e:
66
  logger.error(f"Error loading model components: {str(e)}")
 
 
 
67
  raise ValueError(f"Failed to load model components: {str(e)}")
68
 
69
  def save(self, output_dir: str = "models") -> None:
 
41
  if load_model:
42
  self.load_components()
43
 
 
44
  def load_components(self):
45
+ """Load the trained model and preprocessing components."""
46
  try:
47
+ logger.info("Loading model components...")
48
+
49
+ # Load model
50
+ model_path = os.path.join(self.model_dir, 'loan_model.joblib')
51
+ if not os.path.exists(model_path):
52
+ raise FileNotFoundError(f"Model file not found at {model_path}")
53
+ self.model = joblib.load(model_path)
54
+
55
+ # Load scaler
56
+ scaler_path = os.path.join(self.model_dir, 'loan_scaler.joblib')
57
+ if not os.path.exists(scaler_path):
58
+ raise FileNotFoundError(f"Scaler file not found at {scaler_path}")
59
+ self.scaler = joblib.load(scaler_path)
60
+
61
+ # Load label encoders
62
+ encoders_path = os.path.join(self.model_dir, 'loan_label_encoders.joblib')
63
+ if not os.path.exists(encoders_path):
64
+ raise FileNotFoundError(f"Label encoders file not found at {encoders_path}")
65
+ self.label_encoders = joblib.load(encoders_path)
66
+
67
+ # Load feature names
68
+ features_path = os.path.join(self.model_dir, 'loan_feature_names.joblib')
69
+ if not os.path.exists(features_path):
70
+ raise FileNotFoundError(f"Feature names file not found at {features_path}")
71
+ self.feature_names = joblib.load(features_path)
72
+
73
+ # Try to load explainer if available
74
+ explainer_path = os.path.join(self.model_dir, 'loan_explainer.joblib')
75
  try:
 
76
  if os.path.exists(explainer_path):
77
  self.explainer = joblib.load(explainer_path)
 
 
 
78
  except Exception as explainer_error:
79
+ logger.warning(f"Error loading explainer: {str(explainer_error)}")
80
  self.explainer = None
81
+ logger.info("Continuing without explainer. Explanations will be limited.")
82
 
83
  logger.info("Model components loaded successfully")
84
+
85
  except Exception as e:
86
  logger.error(f"Error loading model components: {str(e)}")
87
+ # Add fallback mechanism for model loading errors
88
+ if "'LoanApprovalModel' object has no attribute 'model_path'" in str(e):
89
+ logger.error("Incorrect attribute reference in load_components method")
90
  raise ValueError(f"Failed to load model components: {str(e)}")
91
 
92
  def save(self, output_dir: str = "models") -> None: