Spaces:
Sleeping
Sleeping
Update inference/predictor.py
Browse files- inference/predictor.py +19 -17
inference/predictor.py
CHANGED
|
@@ -7,11 +7,13 @@ from src.preprocessing import clean_and_engineer
|
|
| 7 |
|
| 8 |
MODEL_DIR = "models"
|
| 9 |
|
|
|
|
| 10 |
class CreditRiskPredictor:
|
| 11 |
def __init__(self):
|
| 12 |
self.model_path = get_latest_file(MODEL_DIR, "credit_model")
|
| 13 |
self.scaler_path = get_latest_file(MODEL_DIR, "scaler")
|
| 14 |
self.columns_path = get_latest_file(MODEL_DIR, "columns")
|
|
|
|
| 15 |
self.model = joblib.load(self.model_path)
|
| 16 |
self.scaler = joblib.load(self.scaler_path)
|
| 17 |
self.columns = joblib.load(self.columns_path)
|
|
@@ -47,33 +49,33 @@ class CreditRiskPredictor:
|
|
| 47 |
X_scaled = self.scaler.transform(df)
|
| 48 |
|
| 49 |
# -------------------------------
|
| 50 |
-
# 6.
|
| 51 |
# -------------------------------
|
| 52 |
probability, credit_score, rating = self._calculate_scorecard_output(X_scaled)
|
| 53 |
|
| 54 |
return probability, credit_score, rating
|
| 55 |
|
| 56 |
def _calculate_scorecard_output(self, X_scaled, base_score=300, scale_length=600):
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
rating = self._get_rating(credit_score)
|
| 70 |
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
-
print("🔥 PREDICTION ERROR:", repr(e))
|
| 75 |
-
raise
|
| 76 |
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
def _get_rating(self, score):
|
| 79 |
if 300 <= score < 500:
|
|
@@ -85,4 +87,4 @@ class CreditRiskPredictor:
|
|
| 85 |
elif 750 <= score <= 900:
|
| 86 |
return 'Excellent'
|
| 87 |
else:
|
| 88 |
-
return 'Undefined'
|
|
|
|
| 7 |
|
| 8 |
MODEL_DIR = "models"
|
| 9 |
|
| 10 |
+
|
| 11 |
class CreditRiskPredictor:
|
| 12 |
def __init__(self):
|
| 13 |
self.model_path = get_latest_file(MODEL_DIR, "credit_model")
|
| 14 |
self.scaler_path = get_latest_file(MODEL_DIR, "scaler")
|
| 15 |
self.columns_path = get_latest_file(MODEL_DIR, "columns")
|
| 16 |
+
|
| 17 |
self.model = joblib.load(self.model_path)
|
| 18 |
self.scaler = joblib.load(self.scaler_path)
|
| 19 |
self.columns = joblib.load(self.columns_path)
|
|
|
|
| 49 |
X_scaled = self.scaler.transform(df)
|
| 50 |
|
| 51 |
# -------------------------------
|
| 52 |
+
# 6. Scorecard logic
|
| 53 |
# -------------------------------
|
| 54 |
probability, credit_score, rating = self._calculate_scorecard_output(X_scaled)
|
| 55 |
|
| 56 |
return probability, credit_score, rating
|
| 57 |
|
| 58 |
def _calculate_scorecard_output(self, X_scaled, base_score=300, scale_length=600):
|
| 59 |
+
try:
|
| 60 |
+
print("DEBUG: Model type:", type(self.model))
|
| 61 |
+
print("DEBUG: Has predict_proba:", hasattr(self.model, "predict_proba"))
|
| 62 |
+
print("DEBUG: X_scaled shape:", X_scaled.shape)
|
| 63 |
+
print("DEBUG: Any NaN:", np.isnan(X_scaled).any())
|
| 64 |
+
print("DEBUG: Any Inf:", np.isinf(X_scaled).any())
|
| 65 |
|
| 66 |
+
default_probability = self.model.predict_proba(X_scaled)[:, 1]
|
| 67 |
|
| 68 |
+
non_default_probability = 1 - default_probability
|
| 69 |
+
credit_score = base_score + non_default_probability * scale_length
|
| 70 |
+
credit_score = int(credit_score[0])
|
|
|
|
| 71 |
|
| 72 |
+
rating = self._get_rating(credit_score)
|
| 73 |
|
| 74 |
+
return float(default_probability[0]), credit_score, rating
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print("🔥 PREDICTION ERROR:", repr(e))
|
| 78 |
+
raise
|
| 79 |
|
| 80 |
def _get_rating(self, score):
|
| 81 |
if 300 <= score < 500:
|
|
|
|
| 87 |
elif 750 <= score <= 900:
|
| 88 |
return 'Excellent'
|
| 89 |
else:
|
| 90 |
+
return 'Undefined'
|