import joblib import pandas as pd import gradio as gr model = joblib.load("loan_risk_pipeline.joblib") def predict_loan_risk( branch_id, city, customer_age, address_length, has_valid_email, has_aadhar, has_pan, total_documents_uploaded, verified_documents_count, has_identity_doc, has_address_doc, has_income_doc, kyc_completeness_score, kyc_verification_ratio, past_loans_count, closed_loans_count, defaulted_loans_count, active_loans_count, avg_past_loan_amount, max_past_loan_amount, total_outstanding_amount, days_since_last_loan, loan_frequency_last_12_months, total_emi_expected_count, total_emi_paid_count, emi_payment_ratio, overdue_emi_count, default_emi_count, pending_emi_count, avg_days_between_emi_payments, late_payment_count, late_payment_ratio, total_amount_paid_so_far, avg_emi_paid_amount, successful_receipt_count, failed_receipt_count, cancelled_receipt_count, manual_payment_ratio, online_payment_ratio, payment_failure_ratio, duplicate_payment_attempt_flag, branch_default_rate, branch_avg_loan_amount, requested_loan_amount, requested_tenure, requested_gold_weight, requested_gold_price, requested_gold_purity, requested_gold_item_type, requested_ltv, requested_interest_rate ): df = pd.DataFrame([{ "branch_id": branch_id, "city": city, "customer_age": customer_age, "address_length": address_length, "has_valid_email": int(has_valid_email), "has_aadhar": int(has_aadhar), "has_pan": int(has_pan), "total_documents_uploaded": total_documents_uploaded, "verified_documents_count": verified_documents_count, "has_identity_doc": int(has_identity_doc), "has_address_doc": int(has_address_doc), "has_income_doc": int(has_income_doc), "kyc_completeness_score": kyc_completeness_score, "kyc_verification_ratio": kyc_verification_ratio, "past_loans_count": past_loans_count, "closed_loans_count": closed_loans_count, "defaulted_loans_count": defaulted_loans_count, "active_loans_count": active_loans_count, "avg_past_loan_amount": avg_past_loan_amount, "max_past_loan_amount": max_past_loan_amount, "total_outstanding_amount": total_outstanding_amount, "days_since_last_loan": days_since_last_loan, "loan_frequency_last_12_months": loan_frequency_last_12_months, "total_emi_expected_count": total_emi_expected_count, "total_emi_paid_count": total_emi_paid_count, "emi_payment_ratio": emi_payment_ratio, "overdue_emi_count": overdue_emi_count, "default_emi_count": default_emi_count, "pending_emi_count": pending_emi_count, "avg_days_between_emi_payments": avg_days_between_emi_payments, "late_payment_count": late_payment_count, "late_payment_ratio": late_payment_ratio, "total_amount_paid_so_far": total_amount_paid_so_far, "avg_emi_paid_amount": avg_emi_paid_amount, "successful_receipt_count": successful_receipt_count, "failed_receipt_count": failed_receipt_count, "cancelled_receipt_count": cancelled_receipt_count, "manual_payment_ratio": manual_payment_ratio, "online_payment_ratio": online_payment_ratio, "payment_failure_ratio": payment_failure_ratio, "duplicate_payment_attempt_flag": int(duplicate_payment_attempt_flag), "branch_default_rate": branch_default_rate, "branch_avg_loan_amount": branch_avg_loan_amount, "requested_loan_amount": requested_loan_amount, "requested_tenure": requested_tenure, "requested_gold_weight": requested_gold_weight, "requested_gold_price": requested_gold_price, "requested_gold_purity": requested_gold_purity, "requested_gold_item_type": requested_gold_item_type, "requested_ltv": requested_ltv, "requested_interest_rate": requested_interest_rate }]) prob = float(model.predict_proba(df)[0][1]) pred = int(model.predict(df)[0]) if prob >= 0.70: decision = "APPROVE" elif prob >= 0.45: decision = "MANUAL REVIEW" else: decision = "REJECT / HIGH RISK" return { "predicted_class": pred, "approval_probability": round(prob, 4), "recommended_decision": decision } demo = gr.Interface( fn=predict_loan_risk, inputs=[ gr.Dropdown(["BR001", "BR002", "BR003", "BR004", "BR005", "BR006"], label="Branch ID"), gr.Textbox(label="City"), gr.Number(label="Customer Age"), gr.Number(label="Address Length"), gr.Checkbox(label="Has Valid Email"), gr.Checkbox(label="Has Aadhar"), gr.Checkbox(label="Has PAN"), gr.Number(label="Total Documents Uploaded"), gr.Number(label="Verified Documents Count"), gr.Checkbox(label="Has Identity Doc"), gr.Checkbox(label="Has Address Doc"), gr.Checkbox(label="Has Income Doc"), gr.Number(label="KYC Completeness Score"), gr.Number(label="KYC Verification Ratio"), gr.Number(label="Past Loans Count"), gr.Number(label="Closed Loans Count"), gr.Number(label="Defaulted Loans Count"), gr.Number(label="Active Loans Count"), gr.Number(label="Avg Past Loan Amount"), gr.Number(label="Max Past Loan Amount"), gr.Number(label="Total Outstanding Amount"), gr.Number(label="Days Since Last Loan"), gr.Number(label="Loan Frequency Last 12 Months"), gr.Number(label="Total EMI Expected Count"), gr.Number(label="Total EMI Paid Count"), gr.Number(label="EMI Payment Ratio"), gr.Number(label="Overdue EMI Count"), gr.Number(label="Default EMI Count"), gr.Number(label="Pending EMI Count"), gr.Number(label="Avg Days Between EMI Payments"), gr.Number(label="Late Payment Count"), gr.Number(label="Late Payment Ratio"), gr.Number(label="Total Amount Paid So Far"), gr.Number(label="Avg EMI Paid Amount"), gr.Number(label="Successful Receipt Count"), gr.Number(label="Failed Receipt Count"), gr.Number(label="Cancelled Receipt Count"), gr.Number(label="Manual Payment Ratio"), gr.Number(label="Online Payment Ratio"), gr.Number(label="Payment Failure Ratio"), gr.Checkbox(label="Duplicate Payment Attempt Flag"), gr.Number(label="Branch Default Rate"), gr.Number(label="Branch Avg Loan Amount"), gr.Number(label="Requested Loan Amount"), gr.Number(label="Requested Tenure"), gr.Number(label="Requested Gold Weight"), gr.Number(label="Requested Gold Price"), gr.Dropdown(["22K", "23K", "24K"], label="Requested Gold Purity"), gr.Dropdown(["Ring", "Necklace", "Bracelet", "Chain", "Bangle", "Coin", "Earrings"], label="Requested Gold Item Type"), gr.Number(label="Requested LTV"), gr.Number(label="Requested Interest Rate"), ], outputs=gr.JSON(label="Prediction Result"), title="Loan Risk Predictor", description="Predict whether a loan applicant should be approved, reviewed, or rejected." ) demo.launch()