Zeyadd-Mostaffa commited on
Commit
7a63bb7
Β·
verified Β·
1 Parent(s): 7c5d1d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -77
app.py CHANGED
@@ -1,93 +1,54 @@
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()
 
1
  import gradio as gr
 
 
2
  import joblib
3
+ import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Load your model
6
+ model = joblib.load('best_model.json')
7
+
8
+ def predict_retention(satisfaction_level, last_evaluation, number_project,
9
+ average_monthly_hours, time_spent_company,
10
+ work_accident, promotion_last_5years, salary, department):
11
+ # One-hot encode the department
12
+ departments = [
13
+ 'RandD', 'accounting', 'hr', 'management', 'marketing',
14
+ 'product_mng', 'sales', 'support', 'technical'
15
+ ]
16
+ department_encoded = [1 if dept == department else 0 for dept in departments]
17
+
18
+ # Prepare the input with all 18 features
19
+ input_data = np.array([
20
+ satisfaction_level, last_evaluation, number_project,
21
+ average_monthly_hours, time_spent_company, work_accident,
22
+ promotion_last_5years, salary
23
+ ] + department_encoded).reshape(1, -1)
24
+
25
+ # Predict using the model
26
+ try:
27
+ prediction = model.predict(input_data)
28
+ return "Employee is likely to quit." if prediction[0] == 1 else "Employee is likely to stay."
29
+ except Exception as e:
30
+ return f"Error: {str(e)}"
31
 
 
32
  interface = gr.Interface(
33
+ fn=predict_retention,
34
  inputs=[
35
+ gr.Number(label="Satisfaction Level (0.0 - 1.0)"),
36
+ gr.Number(label="Last Evaluation (0.0 - 1.0)"),
37
+ gr.Number(label="Number of Projects (1 - 10)"),
38
+ gr.Number(label="Average Monthly Hours (80 - 320)"),
39
+ gr.Number(label="Time Spent at Company (Years)"),
40
  gr.Radio([0, 1], label="Work Accident (0 = No, 1 = Yes)"),
41
  gr.Radio([0, 1], label="Promotion in Last 5 Years (0 = No, 1 = Yes)"),
42
+ gr.Radio([0, 1, 2], label="Salary (0 = Low, 1 = Medium, 2 = High)"),
43
  gr.Dropdown(
44
+ ['RandD', 'accounting', 'hr', 'management', 'marketing',
45
+ 'product_mng', 'sales', 'support', 'technical'],
46
  label="Department"
47
  )
48
  ],
49
  outputs="text",
50
+ title="Employee Retention Prediction System",
51
+ description="Predict whether an employee is likely to stay or quit based on their profile."
 
52
  )
53
 
 
54
  interface.launch()