-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
55 lines (40 loc) · 1.53 KB
/
streamlit_app.py
File metadata and controls
55 lines (40 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import streamlit as st
import pandas as pd
import numpy as np
import joblib
import os
st.title("🏦 Personal Loan Prediction")
# Load trained model
model_path = 'trained_pipeline.pkl'
if not os.path.exists(model_path):
st.error("🚫 trained_pipeline.pkl not found.")
st.stop()
model = joblib.load(model_path)
# Upload section
uploaded_file = st.file_uploader("📁 Upload a CSV file (raw input format)", type=["csv"])
if uploaded_file is not None:
try:
df = pd.read_csv(uploaded_file)
st.subheader("📄 Uploaded Data")
st.dataframe(df.head())
# ✅ Manual preprocessing to match training steps
# Drop unused columns
df.drop(['ID', 'ZIP Code', 'Experience'], axis=1, inplace=True)
# Log-transform skewed columns
for col in ['Income', 'CCAvg', 'Mortgage']:
df[col] = np.log1p(df[col])
# Create HasMortgage
df['HasMortgage'] = df['Mortgage'].apply(lambda x: 1 if x > 0 else 0)
# Drop original Mortgage
df.drop('Mortgage', axis=1, inplace=True)
# Make prediction
df['Prediction'] = model.predict(df)
st.subheader("📊 Prediction Results")
st.dataframe(df)
# Download option
csv = df.to_csv(index=False).encode('utf-8')
st.download_button("⬇️ Download CSV with Predictions", csv, "predictions.csv", "text/csv")
except Exception as e:
st.error(f"⚠️ Error while processing file: {str(e)}")
else:
st.info("Upload a raw data CSV file to see predictions.")