import streamlit as st import pandas as pd from joblib import load # Load the trained model and scaler model = load('loandefaulter.joblib') scaler = load('scaler.joblib') # Define numerical features for scaling (only those that were used during training) num_features = [ 'loan_amnt', 'int_rate', 'installment', 'annual_inc', 'dti', 'revol_bal', 'revol_util', 'total_acc', 'mort_acc' ] # Create the Streamlit app st.set_page_config(page_title='Loan Default Prediction', layout='wide') # App title and description st.markdown("""
Loan Default Prediction
Enter the loan details below to get a prediction on whether the loan will be defaulted.
""", unsafe_allow_html=True) # Input fields with sliders loan_amnt = st.slider('Loan Amount', min_value=0.0, max_value=1000000.0, step=1000.0, value=10000.0) int_rate = st.slider('Interest Rate (%)', min_value=0.0, max_value=30.0, step=0.1, value=5.0) installment = st.slider('EMI Amount', min_value=0.0, max_value=10000.0, step=10.0, value=200.0) annual_inc = st.slider('Annual Income', min_value=0.0, max_value=1000000.0, step=1000.0, value=50000.0) # CIBIL score input as a text field cibil_score = st.text_input('CIBIL Score (Enter a number between 300 and 900)', value='700') # Convert the CIBIL score input to a numeric value try: cibil_score = int(cibil_score) except ValueError: st.error("Please enter a valid number for CIBIL Score.") # Set default values for the missing features dti = 0.0 # Example default value for DTI revol_bal = 0.0 # Example default value for Revolving Balance revol_util = 0.0 # Example default value for Revolving Utilization total_acc = 0 # Example default value for Total Accounts mort_acc = 0 # Example default value for Mortgage Accounts loan_amnt_by_income = loan_amnt / (annual_inc + 1) # Create a DataFrame for the input input_data = pd.DataFrame({ 'loan_amnt': [loan_amnt], 'int_rate': [int_rate], 'installment': [installment], 'annual_inc': [annual_inc], 'dti': [dti], 'revol_bal': [revol_bal], 'revol_util': [revol_util], 'total_acc': [total_acc], 'mort_acc': [mort_acc] }) # Scale the numerical features that were used to fit the scaler input_data[num_features] = scaler.transform(input_data[num_features]) # Add the additional feature (not part of scaling) input_data['loan_amnt_by_income'] = [loan_amnt_by_income] input_data['cibil_score'] = cibil_score input_data = input_data[num_features + ['loan_amnt_by_income']] # Predict using the model if st.button('Predict'): if 300 <= cibil_score <= 900: # Ensure CIBIL score is within the valid range prediction = model.predict(input_data) result = "Defaulted" if prediction[0] == 1 else "Not Defaulted" color = "red" if prediction[0] == 1 else "green" st.markdown(f"""
Prediction: {result}
""", unsafe_allow_html=True) else: st.error("Please enter a CIBIL Score between 300 and 900.")