Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +98 -99
src/streamlit_app.py
CHANGED
|
@@ -18,7 +18,7 @@ USER_CREDENTIALS = {
|
|
| 18 |
}
|
| 19 |
|
| 20 |
# --- Login Panel ---
|
| 21 |
-
st.set_page_config(page_title="Login
|
| 22 |
st.markdown("<h2 style='text-align: center;'>π Login Panel</h2>", unsafe_allow_html=True)
|
| 23 |
|
| 24 |
with st.form("login_form", clear_on_submit=False):
|
|
@@ -28,104 +28,103 @@ with st.form("login_form", clear_on_submit=False):
|
|
| 28 |
|
| 29 |
if login_btn:
|
| 30 |
if username in USER_CREDENTIALS and USER_CREDENTIALS[username] == password:
|
|
|
|
| 31 |
st.success(f"β
Welcome, {username}!")
|
| 32 |
st.write("π You are now logged in. Your app content goes here.")
|
| 33 |
-
else:
|
| 34 |
-
st.error("β Invalid username or password")
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
MODEL_FILENAME = "/tmp/insurance_churn_model.pkl"
|
| 39 |
-
st.title("Insurance Churn Prediction App")
|
| 40 |
-
|
| 41 |
-
menu = st.sidebar.radio("Navigation", ["Predict Churn","Train Model"])
|
| 42 |
-
|
| 43 |
-
if menu == "Train Model":
|
| 44 |
-
# st.header("Upload Dataset and Train Model")
|
| 45 |
-
# uploaded_file = st.file_uploader("Upload Insurance Churn Dataset (CSV)", type=["csv"])
|
| 46 |
-
# if uploaded_file is not None:
|
| 47 |
-
data = pd.read_csv("src/insurance.csv")
|
| 48 |
-
st.subheader("Dataset Preview")
|
| 49 |
-
st.dataframe(data.head())
|
| 50 |
-
|
| 51 |
-
st.subheader("Summary Statistics")
|
| 52 |
-
st.write(data.describe())
|
| 53 |
-
|
| 54 |
-
if 'churn' in data.columns:
|
| 55 |
-
st.subheader("Churn Distribution")
|
| 56 |
-
fig, ax = plt.subplots()
|
| 57 |
-
sns.countplot(x='churn', data=data, ax=ax)
|
| 58 |
-
st.pyplot(fig)
|
| 59 |
-
|
| 60 |
-
st.subheader("Model Training")
|
| 61 |
-
target_column = st.selectbox("Select Target Column", options=data.columns, index=data.columns.get_loc('churn') if 'churn' in data.columns else 0)
|
| 62 |
-
feature_columns = st.multiselect("Select Feature Columns", options=[col for col in data.columns if col != target_column])
|
| 63 |
-
|
| 64 |
-
#if feature_columns and target_column:
|
| 65 |
-
# X = pd.get_dummies(data[feature_columns])
|
| 66 |
-
# y = data[target_column]
|
| 67 |
-
|
| 68 |
-
# input features
|
| 69 |
-
# Automatically exclude identifier columns
|
| 70 |
-
# exclude_columns = ['Customer', 'Policy', 'Policy Number', 'Response']
|
| 71 |
-
# feature_columns = [col for col in data.columns if col not in exclude_columns]
|
| 72 |
-
# target_column = 'Response'
|
| 73 |
-
|
| 74 |
-
# feature_columns = data.drop(columns=[target_column]).select_dtypes(include=[np.number]).columns.tolist()
|
| 75 |
-
X= data[feature_columns]
|
| 76 |
-
y = data[target_column]
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
# --- Login Panel ---
|
| 21 |
+
st.set_page_config(page_title="Login to App", layout="centered")
|
| 22 |
st.markdown("<h2 style='text-align: center;'>π Login Panel</h2>", unsafe_allow_html=True)
|
| 23 |
|
| 24 |
with st.form("login_form", clear_on_submit=False):
|
|
|
|
| 28 |
|
| 29 |
if login_btn:
|
| 30 |
if username in USER_CREDENTIALS and USER_CREDENTIALS[username] == password:
|
| 31 |
+
|
| 32 |
st.success(f"β
Welcome, {username}!")
|
| 33 |
st.write("π You are now logged in. Your app content goes here.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
MODEL_FILENAME = "/tmp/insurance_churn_model.pkl"
|
| 36 |
+
st.title("Insurance Churn Prediction App")
|
| 37 |
+
|
| 38 |
+
menu = st.sidebar.radio("Navigation", ["Predict Churn","Train Model"])
|
| 39 |
+
|
| 40 |
+
if menu == "Train Model":
|
| 41 |
+
# st.header("Upload Dataset and Train Model")
|
| 42 |
+
# uploaded_file = st.file_uploader("Upload Insurance Churn Dataset (CSV)", type=["csv"])
|
| 43 |
+
# if uploaded_file is not None:
|
| 44 |
+
data = pd.read_csv("src/insurance.csv")
|
| 45 |
+
st.subheader("Dataset Preview")
|
| 46 |
+
st.dataframe(data.head())
|
| 47 |
+
|
| 48 |
+
st.subheader("Summary Statistics")
|
| 49 |
+
st.write(data.describe())
|
| 50 |
+
|
| 51 |
+
if 'churn' in data.columns:
|
| 52 |
+
st.subheader("Churn Distribution")
|
| 53 |
+
fig, ax = plt.subplots()
|
| 54 |
+
sns.countplot(x='churn', data=data, ax=ax)
|
| 55 |
+
st.pyplot(fig)
|
| 56 |
+
|
| 57 |
+
st.subheader("Model Training")
|
| 58 |
+
target_column = st.selectbox("Select Target Column", options=data.columns, index=data.columns.get_loc('churn') if 'churn' in data.columns else 0)
|
| 59 |
+
feature_columns = st.multiselect("Select Feature Columns", options=[col for col in data.columns if col != target_column])
|
| 60 |
+
|
| 61 |
+
#if feature_columns and target_column:
|
| 62 |
+
# X = pd.get_dummies(data[feature_columns])
|
| 63 |
+
# y = data[target_column]
|
| 64 |
+
|
| 65 |
+
# input features
|
| 66 |
+
# Automatically exclude identifier columns
|
| 67 |
+
# exclude_columns = ['Customer', 'Policy', 'Policy Number', 'Response']
|
| 68 |
+
# feature_columns = [col for col in data.columns if col not in exclude_columns]
|
| 69 |
+
# target_column = 'Response'
|
| 70 |
+
|
| 71 |
+
# feature_columns = data.drop(columns=[target_column]).select_dtypes(include=[np.number]).columns.tolist()
|
| 72 |
+
X= data[feature_columns]
|
| 73 |
+
y = data[target_column]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# X = data.drop('Response', axis = 1)
|
| 77 |
+
# # output labels
|
| 78 |
+
# y = data['Response']
|
| 79 |
+
|
| 80 |
+
agree = st.checkbox("Continue Training")
|
| 81 |
+
if agree:
|
| 82 |
+
st.write("Great! ML Model Training Started..")
|
| 83 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 84 |
+
|
| 85 |
+
model = RandomForestClassifier()
|
| 86 |
+
model.fit(X_train, y_train)
|
| 87 |
+
|
| 88 |
+
y_pred = model.predict(X_test)
|
| 89 |
+
st.subheader("Model Performance")
|
| 90 |
+
st.write("Accuracy:", accuracy_score(y_test, y_pred))
|
| 91 |
+
st.text("Classification Report:")
|
| 92 |
+
st.text(classification_report(y_test, y_pred))
|
| 93 |
+
|
| 94 |
+
joblib.dump((model, X.columns.tolist()), MODEL_FILENAME)
|
| 95 |
+
st.success(f"Model trained and saved as {MODEL_FILENAME}")
|
| 96 |
+
|
| 97 |
+
elif menu == "Predict Churn":
|
| 98 |
+
st.header("Insurance Churn Predictor")
|
| 99 |
+
st.markdown("To use Predictor, Please Train the ML Model if not done yet! ")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
model, feature_names = joblib.load(MODEL_FILENAME)
|
| 104 |
+
st.success("Model loaded successfully.")
|
| 105 |
+
except:
|
| 106 |
+
st.error("Model not found. Please train the model first.")
|
| 107 |
+
st.stop()
|
| 108 |
+
|
| 109 |
+
st.subheader("Enter Customer Details")
|
| 110 |
+
input_data = {}
|
| 111 |
+
for feature in feature_names:
|
| 112 |
+
input_data[feature] = st.text_input(f"{feature}", "")
|
| 113 |
+
|
| 114 |
+
if st.button("Predict Churn"):
|
| 115 |
+
try:
|
| 116 |
+
input_df = pd.DataFrame([input_data])
|
| 117 |
+
input_df = pd.get_dummies(input_df)
|
| 118 |
+
|
| 119 |
+
for col in feature_names:
|
| 120 |
+
if col not in input_df.columns:
|
| 121 |
+
input_df[col] = 0
|
| 122 |
+
input_df = input_df[feature_names]
|
| 123 |
+
|
| 124 |
+
prediction = model.predict(input_df)[0]
|
| 125 |
+
st.subheader("Prediction Result")
|
| 126 |
+
st.write(f"Churn: {'Yes' if prediction == 1 else 'No'}")
|
| 127 |
+
except Exception as e:
|
| 128 |
+
st.error(f"Error in prediction: {e}")
|
| 129 |
+
else:
|
| 130 |
+
st.error("β Invalid username or password")
|