Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +76 -22
src/streamlit_app.py
CHANGED
|
@@ -1,39 +1,93 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
overtime = st.selectbox("OverTime", ["Yes", "No"])
|
| 19 |
|
| 20 |
-
# Convert
|
| 21 |
overtime_value = 1 if overtime == "Yes" else 0
|
| 22 |
|
| 23 |
-
# Prepare input
|
| 24 |
input_data = pd.DataFrame({
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
})
|
| 32 |
|
| 33 |
-
#
|
|
|
|
|
|
|
| 34 |
if st.button("Predict Attrition"):
|
| 35 |
prediction = model.predict(input_data)[0]
|
|
|
|
|
|
|
| 36 |
if prediction == 1:
|
| 37 |
-
st.error("โ ๏ธ This employee is likely to leave
|
| 38 |
else:
|
| 39 |
-
st.success("โ
This employee is likely to stay.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
+
from sklearn.metrics import accuracy_score
|
| 6 |
|
| 7 |
+
# -----------------------------
|
| 8 |
+
# Page Configuration
|
| 9 |
+
# -----------------------------
|
| 10 |
+
st.set_page_config(page_title="Employee Attrition Prediction", page_icon="๐ฉโ๐ผ", layout="centered")
|
| 11 |
|
| 12 |
+
st.title("๐ฉโ๐ผ Employee Attrition Prediction (No Pickle Version)")
|
| 13 |
+
st.write("Predict whether an employee is likely to leave the company โ safe for Hugging Face deployment!")
|
| 14 |
|
| 15 |
+
# -----------------------------
|
| 16 |
+
# Train Lightweight Model On Startup
|
| 17 |
+
# -----------------------------
|
| 18 |
+
@st.cache_resource
|
| 19 |
+
def train_lightweight_model():
|
| 20 |
+
"""Train a small dummy Random Forest model safely."""
|
| 21 |
+
data = pd.DataFrame({
|
| 22 |
+
"Age": [25, 35, 40, 50, 28, 45, 32, 38],
|
| 23 |
+
"MonthlyIncome": [4000, 6000, 8000, 12000, 3000, 10000, 7000, 5000],
|
| 24 |
+
"JobSatisfaction": [3, 4, 2, 1, 3, 2, 4, 3],
|
| 25 |
+
"WorkLifeBalance": [3, 2, 4, 3, 3, 2, 4, 3],
|
| 26 |
+
"YearsAtCompany": [2, 5, 10, 15, 1, 12, 6, 4],
|
| 27 |
+
"OverTime": [1, 0, 1, 0, 1, 0, 1, 0],
|
| 28 |
+
"Attrition": [1, 0, 0, 1, 1, 0, 0, 0]
|
| 29 |
+
})
|
| 30 |
|
| 31 |
+
X = data.drop("Attrition", axis=1)
|
| 32 |
+
y = data["Attrition"]
|
| 33 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
|
| 34 |
+
|
| 35 |
+
model = RandomForestClassifier(n_estimators=50, random_state=42)
|
| 36 |
+
model.fit(X_train, y_train)
|
| 37 |
+
|
| 38 |
+
acc = accuracy_score(y_test, model.predict(X_test))
|
| 39 |
+
return model, acc
|
| 40 |
+
|
| 41 |
+
model, acc = train_lightweight_model()
|
| 42 |
+
st.success(f"โ
Model trained successfully (Accuracy: {acc:.2f})")
|
| 43 |
+
|
| 44 |
+
# -----------------------------
|
| 45 |
+
# Input Form for Predictions
|
| 46 |
+
# -----------------------------
|
| 47 |
+
st.header("๐ฎ Enter Employee Details to Predict Attrition")
|
| 48 |
+
|
| 49 |
+
age = st.number_input("Age", 18, 60, 30)
|
| 50 |
+
monthly_income = st.number_input("Monthly Income", 1000, 20000, 5000)
|
| 51 |
+
job_satisfaction = st.slider("Job Satisfaction (1โ4)", 1, 4, 3)
|
| 52 |
+
work_life_balance = st.slider("Work-Life Balance (1โ4)", 1, 4, 3)
|
| 53 |
+
years_at_company = st.number_input("Years at Company", 0, 40, 5)
|
| 54 |
overtime = st.selectbox("OverTime", ["Yes", "No"])
|
| 55 |
|
| 56 |
+
# Convert categorical value
|
| 57 |
overtime_value = 1 if overtime == "Yes" else 0
|
| 58 |
|
| 59 |
+
# Prepare input data
|
| 60 |
input_data = pd.DataFrame({
|
| 61 |
+
"Age": [age],
|
| 62 |
+
"MonthlyIncome": [monthly_income],
|
| 63 |
+
"JobSatisfaction": [job_satisfaction],
|
| 64 |
+
"WorkLifeBalance": [work_life_balance],
|
| 65 |
+
"YearsAtCompany": [years_at_company],
|
| 66 |
+
"OverTime": [overtime_value]
|
| 67 |
})
|
| 68 |
|
| 69 |
+
# -----------------------------
|
| 70 |
+
# Predict Attrition
|
| 71 |
+
# -----------------------------
|
| 72 |
if st.button("Predict Attrition"):
|
| 73 |
prediction = model.predict(input_data)[0]
|
| 74 |
+
probability = model.predict_proba(input_data)[0][1]
|
| 75 |
+
|
| 76 |
if prediction == 1:
|
| 77 |
+
st.error(f"โ ๏ธ This employee is likely to leave. (Confidence: {probability:.2f})")
|
| 78 |
else:
|
| 79 |
+
st.success(f"โ
This employee is likely to stay. (Confidence: {1 - probability:.2f})")
|
| 80 |
+
|
| 81 |
+
# -----------------------------
|
| 82 |
+
# Info Section
|
| 83 |
+
# -----------------------------
|
| 84 |
+
with st.expander("โน๏ธ About This App"):
|
| 85 |
+
st.write("""
|
| 86 |
+
- This app predicts employee attrition using a simple **Random Forest** model.
|
| 87 |
+
- Itโs designed to run **without any .pkl files** (safe for Hugging Face Spaces).
|
| 88 |
+
- You can adjust parameters like age, income, and satisfaction to see how predictions change.
|
| 89 |
+
- To deploy it on Hugging Face, just include this file and `requirements.txt`.
|
| 90 |
+
""")
|
| 91 |
+
|
| 92 |
+
st.markdown("---")
|
| 93 |
+
st.caption("Built with โค๏ธ using Streamlit + Scikit-learn (Hugging Face Safe Version)")
|