Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +54 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,56 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import json
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
# Load lightweight JSON model
|
| 7 |
+
light_model = json.load(open("lightweight_model.json"))
|
| 8 |
+
means = light_model["feature_means"]
|
| 9 |
+
stds = light_model["feature_stds"]
|
| 10 |
+
|
| 11 |
+
st.set_page_config(page_title="Employee Attrition Predictor", page_icon="👩💼")
|
| 12 |
+
|
| 13 |
+
st.title("👩💼 Employee Attrition Prediction (HF-Safe Version)")
|
| 14 |
+
st.write("This version uses a *lightweight*, non-pickle model for safe deployment.")
|
| 15 |
+
|
| 16 |
+
# Input Fields
|
| 17 |
+
age = st.number_input("Age", 18, 60, 30)
|
| 18 |
+
monthly_income = st.number_input("Monthly Income", 1000, 20000, 5000)
|
| 19 |
+
job_satisfaction = st.slider("Job Satisfaction (1–4)", 1, 4, 3)
|
| 20 |
+
work_life_balance = st.slider("Work-Life Balance (1–4)", 1, 4, 3)
|
| 21 |
+
years_at_company = st.number_input("Years at Company", 0, 40, 5)
|
| 22 |
+
overtime = st.selectbox("OverTime", ["Yes", "No"])
|
| 23 |
+
|
| 24 |
+
overtime_value = 1 if overtime == "Yes" else 0
|
| 25 |
+
|
| 26 |
+
# Create DataFrame
|
| 27 |
+
input_data = pd.DataFrame([{
|
| 28 |
+
"Age": age,
|
| 29 |
+
"MonthlyIncome": monthly_income,
|
| 30 |
+
"JobSatisfaction": job_satisfaction,
|
| 31 |
+
"WorkLifeBalance": work_life_balance,
|
| 32 |
+
"YearsAtCompany": years_at_company,
|
| 33 |
+
"OverTime": overtime_value
|
| 34 |
+
}])
|
| 35 |
+
|
| 36 |
+
# Basic logistic-like probability calculation
|
| 37 |
+
def simple_predict(df):
|
| 38 |
+
# Normalize
|
| 39 |
+
for col in df.columns:
|
| 40 |
+
df[col] = (df[col] - means[col]) / (stds[col] + 1e-6)
|
| 41 |
+
|
| 42 |
+
score = df.sum(axis=1).values[0]
|
| 43 |
+
|
| 44 |
+
probability = 1 / (1 + np.exp(-score))
|
| 45 |
+
return probability
|
| 46 |
+
|
| 47 |
+
if st.button("Predict Attrition"):
|
| 48 |
+
prob = simple_predict(input_data)
|
| 49 |
+
|
| 50 |
+
if prob > 0.5:
|
| 51 |
+
st.error(f"⚠️ Employee likely to leave (confidence {prob:.2f})")
|
| 52 |
+
else:
|
| 53 |
+
st.success(f"✅ Employee likely to stay (confidence {1-prob:.2f})")
|
| 54 |
+
|
| 55 |
+
st.markdown("---")
|
| 56 |
+
st.caption("HF-Safe: No pickle, no joblib, lightweight model used.")
|