Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,93 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import xgboost as xgb
|
3 |
-
import numpy as np
|
4 |
-
import joblib
|
5 |
-
import os
|
6 |
-
import warnings
|
7 |
-
|
8 |
-
# Suppress XGBoost warnings
|
9 |
-
warnings.filterwarnings("ignore", category=UserWarning, message=".*WARNING.*")
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
model
|
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 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import xgboost as xgb
|
3 |
+
import numpy as np
|
4 |
+
import joblib
|
5 |
+
import os
|
6 |
+
import warnings
|
7 |
+
|
8 |
+
# Suppress XGBoost warnings
|
9 |
+
warnings.filterwarnings("ignore", category=UserWarning, message=".*WARNING.*")
|
10 |
+
|
11 |
+
# Load your model (automatically detect XGBoost or joblib model)
|
12 |
+
def load_model():
|
13 |
+
if os.path.exists("best_model.json"): # Model in root directory
|
14 |
+
model = xgb.Booster()
|
15 |
+
model.load_model("best_model.json")
|
16 |
+
print("β
Model loaded using XGBoost's native method.")
|
17 |
+
return model
|
18 |
+
elif os.path.exists("best_model.pkl"): # Joblib model in root directory
|
19 |
+
model = joblib.load("best_model.pkl")
|
20 |
+
print("β
Model loaded using Joblib.")
|
21 |
+
return model
|
22 |
+
else:
|
23 |
+
print("β No model file found.")
|
24 |
+
return None
|
25 |
+
|
26 |
+
model = load_model()
|
27 |
+
|
28 |
+
# Prediction function
|
29 |
+
def predict_employee_status(satisfaction_level, last_evaluation, number_project,
|
30 |
+
average_monthly_hours, time_spend_company,
|
31 |
+
work_accident, promotion_last_5years, salary, department):
|
32 |
+
|
33 |
+
# Encode the department as numeric (One-Hot Encoding or Label Encoding)
|
34 |
+
department_mapping = {
|
35 |
+
"Sales": 0,
|
36 |
+
"Technical": 1,
|
37 |
+
"Support": 2,
|
38 |
+
"IT": 3,
|
39 |
+
"Management": 4,
|
40 |
+
"Product Management": 5,
|
41 |
+
"Marketing": 6,
|
42 |
+
"HR": 7,
|
43 |
+
"Accounting": 8,
|
44 |
+
"R&D": 9
|
45 |
+
}
|
46 |
+
|
47 |
+
# Convert the department to a numeric value
|
48 |
+
department_encoded = department_mapping.get(department, 0)
|
49 |
+
|
50 |
+
# Prepare input data including the department
|
51 |
+
input_data = np.array([[satisfaction_level, last_evaluation, number_project,
|
52 |
+
average_monthly_hours, time_spend_company,
|
53 |
+
work_accident, promotion_last_5years, salary, department_encoded]])
|
54 |
+
|
55 |
+
if model is None:
|
56 |
+
return "β No model found. Please upload the model file."
|
57 |
+
|
58 |
+
if isinstance(model, xgb.Booster):
|
59 |
+
dmatrix = xgb.DMatrix(input_data)
|
60 |
+
prediction = model.predict(dmatrix)[0]
|
61 |
+
result = "β
The employee is likely to Quit." if prediction > 0.5 else "β
The employee is likely to Stay."
|
62 |
+
else:
|
63 |
+
prediction = model.predict(input_data)[0]
|
64 |
+
result = "β
The employee is likely to Quit." if prediction == 1 else "β
The employee is likely to Stay."
|
65 |
+
|
66 |
+
return result
|
67 |
+
|
68 |
+
# Gradio interface with enhanced UI including Department
|
69 |
+
interface = gr.Interface(
|
70 |
+
fn=predict_employee_status,
|
71 |
+
inputs=[
|
72 |
+
gr.Number(label="Satisfaction Level (0.0 - 1.0)", value=0.5),
|
73 |
+
gr.Number(label="Last Evaluation (0.0 - 1.0)", value=0.6),
|
74 |
+
gr.Number(label="Number of Projects (1 - 10)", value=3),
|
75 |
+
gr.Number(label="Average Monthly Hours (80 - 320)", value=150),
|
76 |
+
gr.Number(label="Time Spent at Company (Years)", value=3),
|
77 |
+
gr.Radio([0, 1], label="Work Accident (0 = No, 1 = Yes)"),
|
78 |
+
gr.Radio([0, 1], label="Promotion in Last 5 Years (0 = No, 1 = Yes)"),
|
79 |
+
gr.Dropdown(choices=[0, 1, 2], label="Salary Level (0 = Low, 1 = Medium, 2 = High)"),
|
80 |
+
gr.Dropdown(
|
81 |
+
choices=["Sales", "Technical", "Support", "IT", "Management",
|
82 |
+
"Product Management", "Marketing", "HR", "Accounting", "R&D"],
|
83 |
+
label="Department"
|
84 |
+
)
|
85 |
+
],
|
86 |
+
outputs="text",
|
87 |
+
title="π Employee Retention Prediction System",
|
88 |
+
description="Predict whether an employee is likely to stay or quit based on their profile.",
|
89 |
+
live=False
|
90 |
+
)
|
91 |
+
|
92 |
+
# Launch Gradio app
|
93 |
+
interface.launch()
|