| import os
|
| import joblib
|
| import pandas as pd
|
| import numpy as np
|
| from feature_builder import prepare_input_features
|
|
|
|
|
|
|
|
|
| BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
| CLASSIFIER_PATH = os.path.join(BASE_DIR, "models", "emi_classifier_final.pkl")
|
| REGRESSOR_PATH = os.path.join(BASE_DIR, "models", "emi_model_optimized.pkl")
|
| LABEL_ENCODER_PATH = os.path.join(BASE_DIR, "models", "label_encoder.pkl")
|
|
|
|
|
|
|
|
|
| classifier = joblib.load(CLASSIFIER_PATH)
|
| regressor = joblib.load(REGRESSOR_PATH)
|
|
|
| label_encoder = joblib.load(LABEL_ENCODER_PATH)
|
|
|
|
|
|
|
|
|
| def predict_emi(raw_input: dict):
|
| """
|
| Returns:
|
| eligibility_label (str): Eligible | High Risk | Not Eligible
|
| max_emi (float): Predicted maximum EMI
|
| """
|
|
|
|
|
| input_df = prepare_input_features(raw_input)
|
|
|
|
|
|
|
| probs = classifier.predict_proba(input_df)[0]
|
|
|
|
|
|
|
| prob_map = {
|
| label_encoder.inverse_transform([i])[0]: prob
|
| for i, prob in enumerate(probs)
|
| }
|
|
|
|
|
|
|
|
|
| eligible_prob = prob_map.get("Eligible", 0)
|
| high_risk_prob = prob_map.get("High_Risk", 0)
|
|
|
| if eligible_prob > 0.35:
|
| ml_label = "Eligible"
|
| elif high_risk_prob > 0.40:
|
| ml_label = "High Risk"
|
| else:
|
| ml_label = "Not Eligible"
|
|
|
|
|
| max_emi = float(regressor.predict(input_df)[0])
|
| max_emi = max(max_emi, 0.0)
|
|
|
|
|
|
|
| credit_score = input_df["credit_score"].iloc[0]
|
| dti = input_df.get("debt_to_income", pd.Series([0])).iloc[0]
|
|
|
|
|
| is_hard_reject = (
|
| credit_score < 400 or
|
| dti > 0.85
|
| )
|
|
|
|
|
| if is_hard_reject:
|
| eligibility_label = "Not Eligible"
|
| max_emi = 0.0
|
| else:
|
| eligibility_label = ml_label
|
|
|
|
|
| if eligibility_label == "Not Eligible":
|
| max_emi = 0.0
|
|
|
| return eligibility_label, round(max_emi, 2) |