import streamlit as st import pandas as pd import joblib from catboost import Pool import plotly.graph_objects as go import plotly.express as px from datetime import datetime import time # Page configuration st.set_page_config( page_title="Credit Score Predictor", page_icon="💳", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS for better styling st.markdown(""" """, unsafe_allow_html=True) # Load the model (with error handling) @st.cache_resource def load_model(): try: return joblib.load("best_catboost_model.pkl") except FileNotFoundError: st.error("⚠️ Model file not found. Please ensure 'best_catboost_model.pkl' is in the correct directory.") return None model = load_model() # Categorical feature list cat_features = ['Occupation', 'Credit_Mix', 'Payment_of_Min_Amount', 'Payment_Behaviour', 'Type_of_Loan'] # Header st.markdown('
Get instant insights into your creditworthiness with our AI-powered predictor
', unsafe_allow_html=True) # Sidebar for quick info with st.sidebar: st.markdown("### 📊 About Credit Scores") st.info(""" **Good Credit (750+)** ✅ Best loan rates \n ✅ Easy approvals \n ✅ Premium credit cards \n **Standard Credit (650-749)** ⚠️ Moderate rates \n ⚠️ Standard approvals \n **Poor Credit (<650)** ❌ High interest rates \n ❌ Limited options \n """) st.markdown("### 🎯 Tips for Better Credit") st.success(""" • Pay bills on time \n • Keep credit utilization low \n • Maintain old accounts \n • Monitor credit report \n • Diversify credit types \n """) # Main content in tabs tab1, tab2, tab3= st.tabs(["🔍 Predict Credit Score", "📈 Factors Affecting Credit Score", "💡 Recommendations"]) with tab1: if model is None: st.stop() # Personal Information Section st.markdown("### 👤 Personal Information") col1, col2, col3 = st.columns(3) with col1: name = st.text_input("📝 Full Name", placeholder="Enter your full name") age = st.slider("🎂 Age", min_value=18, max_value=100, value=30) with col2: month = st.selectbox("📅 Month", range(1, 13), index=0) occupation = st.selectbox("💼 Occupation", [ "Scientist", "Teacher", "Engineer", "Manager", "Doctor", "Lawyer", "Architect", "Developer", "Accountant", "Other" ]) with col3: annual_income = st.number_input("💰 Annual Income ($)", min_value=0.0, value=50000.0, step=1000.0) monthly_inhand_salary = st.number_input("💵 Monthly Salary ($)", min_value=0.0, value=4000.0, step=100.0) st.divider() # Banking Information Section st.markdown("### 🏦 Banking & Credit Information") col4, col5, col6 = st.columns(3) with col4: num_bank_accounts = st.slider("🏦 Bank Accounts", min_value=0, max_value=20, value=3) num_credit_card = st.slider("💳 Credit Cards", min_value=0, max_value=20, value=2) interest_rate = st.slider("📈 Interest Rate (%)", min_value=0.0, max_value=50.0, value=12.0, step=0.1) with col5: num_of_loan = st.slider("🏠 Number of Loans", min_value=0, max_value=20, value=2) type_of_loan = st.multiselect("🏷️ Loan Types", [ "Auto Loan", "Personal Loan", "Home Loan", "Student Loan", "Credit Card Loan", "Business Loan" ], default=["Auto Loan", "Personal Loan"]) # Convert multiselect to string type_of_loan_str = ", ".join(type_of_loan) if type_of_loan else "No Loan" with col6: credit_mix = st.selectbox("🎯 Credit Mix", ["Good", "Standard", "Poor"], index=0) payment_of_min_amount = st.selectbox("💸 Pay Minimum Amount", ["Yes", "No"], index=0) payment_behaviour = st.selectbox("📊 Payment Behaviour", [ "High_spent_Small_value_payments", "Low_spent_Large_value_payments", "High_spent_Medium_value_payments", "Low_spent_Medium_value_payments", "High_spent_Large_value_payments", "Low_spent_Small_value_payments" ]) st.divider() # Financial Details Section st.markdown("### 💹 Financial Details") col7, col8, col9 = st.columns(3) with col7: delay_from_due_date = st.slider("⏰ Days Late on Payments", min_value=0, max_value=60, value=5) num_of_delayed_payment = st.slider("⚠️ Delayed Payments Count", min_value=0, max_value=50, value=3) changed_credit_limit = st.number_input("📊 Credit Limit Changes", value=500.0, step=100.0) with col8: num_credit_inquiries = st.slider("🔍 Credit Inquiries", min_value=0, max_value=20, value=2) outstanding_debt = st.number_input("💳 Outstanding Debt ($)", min_value=0.0, value=1500.0, step=100.0) credit_utilization_ratio = st.slider("📈 Credit Utilization (%)", min_value=0.0, max_value=100.0, value=25.0, step=0.1) with col9: credit_history_age = st.slider("📅 Credit History (months)", min_value=0, max_value=600, value=200) total_emi_per_month = st.number_input("💰 Monthly EMI ($)", min_value=0.0, value=200.0, step=50.0) amount_invested_monthly = st.number_input("📈 Monthly Investment ($)", min_value=0.0, value=100.0, step=25.0) monthly_balance = st.number_input("💵 Monthly Balance ($)", value=800.0, step=100.0) st.divider() # Prediction Section col_pred1, col_pred2, col_pred3 = st.columns([1, 2, 1]) with col_pred2: if st.button("🔮 Predict My Credit Score", type="primary"): if not name: st.warning("⚠️ Please enter your name to continue.") else: # Show loading animation with st.spinner('🔄 Analyzing your credit profile...'): time.sleep(2) # Simulate processing time # Prepare input DataFrame data = pd.DataFrame([{ 'Month': month, 'Age': age, 'Occupation': occupation, 'Annual_Income': annual_income, 'Monthly_Inhand_Salary': monthly_inhand_salary, 'Num_Bank_Accounts': num_bank_accounts, 'Num_Credit_Card': num_credit_card, 'Interest_Rate': interest_rate, 'Num_of_Loan': num_of_loan, 'Type_of_Loan': type_of_loan_str, 'Delay_from_due_date': delay_from_due_date, 'Num_of_Delayed_Payment': num_of_delayed_payment, 'Changed_Credit_Limit': changed_credit_limit, 'Num_Credit_Inquiries': num_credit_inquiries, 'Credit_Mix': credit_mix, 'Outstanding_Debt': outstanding_debt, 'Credit_Utilization_Ratio': credit_utilization_ratio, 'Credit_History_Age': credit_history_age, 'Payment_of_Min_Amount': payment_of_min_amount, 'Total_EMI_per_month': total_emi_per_month, 'Amount_invested_monthly': amount_invested_monthly, 'Payment_Behaviour': payment_behaviour, 'Monthly_Balance': monthly_balance }]) # Make prediction try: pool = Pool(data, cat_features=cat_features) prediction = model.predict(pool) result = prediction[0] # Display result with appropriate styling if result == "Good": st.markdown(f'💳 Credit Score Predictor | Built with ❤️ using Streamlit & CatBoost
⚠️ This is a predictive tool and should not be considered as financial advice. Consult with financial professionals for important credit decisions.