Spaces:
Build error
Build error
| 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 | |
| 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(""" | |
| <style> | |
| .title { font-size: 36px; font-weight: bold; color: #2E86C1; } | |
| .description { font-size: 20px; color: #34495E; } | |
| .input-container { margin-top: 20px; } | |
| .slider-container { margin: 10px 0; } | |
| </style> | |
| <div class="title">Loan Default Prediction</div> | |
| <div class="description">Enter the loan details below to get a prediction on whether the loan will be defaulted.</div> | |
| """, 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('Installment', 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) | |
| dti = st.slider('Debt-to-Income Ratio', min_value=0.0, max_value=100.0, step=0.1, value=15.0) | |
| revol_bal = st.slider('Revolving Balance', min_value=0.0, max_value=500000.0, step=100.0, value=10000.0) | |
| revol_util = st.slider('Revolving Utilization (%)', min_value=0.0, max_value=100.0, step=0.1, value=30.0) | |
| total_acc = st.slider('Total Accounts', min_value=0, max_value=100, step=1, value=10) | |
| mort_acc = st.slider('Mortgage Accounts', min_value=0, max_value=10, step=1, value=1) | |
| 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 new feature | |
| input_data['loan_amnt_by_income'] = [loan_amnt_by_income] | |
| # Predict using the model | |
| if st.button('Predict'): | |
| 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""" | |
| <div style="font-size: 24px; color: {color}; font-weight: bold;">Prediction: {result}</div> | |
| """, unsafe_allow_html=True) | |