Zeyadd-Mostaffa commited on
Commit
a178285
Β·
verified Β·
1 Parent(s): 2bd9872

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -51
app.py CHANGED
@@ -4,26 +4,20 @@ import pandas as pd
4
  import joblib
5
  import os
6
  import warnings
7
- import zipfile
8
 
 
9
  warnings.filterwarnings("ignore")
10
 
 
11
  def load_model():
12
- zip_path = "final_ensemble_model.zip"
13
- pkl_path = "final_ensemble_model.pkl"
14
-
15
- if not os.path.exists(pkl_path):
16
- print("πŸ“¦ Extracting model from zip...")
17
- with zipfile.ZipFile(zip_path, 'r') as zip_ref:
18
- zip_ref.extractall(".")
19
-
20
- try:
21
- model = joblib.load(pkl_path)
22
- print("βœ… Ensemble model loaded.")
23
- return model
24
- except Exception as e:
25
- print(f"❌ Failed to load model: {e}")
26
- return None
27
 
28
  model = load_model()
29
 
@@ -32,19 +26,15 @@ def predict_employee_status(satisfaction_level, last_evaluation, number_project,
32
  average_monthly_hours, time_spend_company,
33
  work_accident, promotion_last_5years, salary, department, threshold=0.5):
34
 
35
- departments = [
36
- 'RandD', 'accounting', 'hr', 'management', 'marketing',
37
- 'product_mng', 'sales', 'support', 'technical'
38
- ]
39
  department_features = {f"department_{dept}": 0 for dept in departments}
40
  if department in departments:
41
  department_features[f"department_{department}"] = 1
42
 
43
- # Feature engineering
44
  satisfaction_evaluation = satisfaction_level * last_evaluation
45
  work_balance = average_monthly_hours / number_project
46
 
47
- # Construct DataFrame
48
  input_data = {
49
  "satisfaction_level": [satisfaction_level],
50
  "last_evaluation": [last_evaluation],
@@ -60,36 +50,33 @@ def predict_employee_status(satisfaction_level, last_evaluation, number_project,
60
  }
61
 
62
  input_df = pd.DataFrame(input_data)
 
 
 
63
 
64
- # Prediction
65
- if model is None:
66
- return "❌ No model loaded."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- try:
69
- prob = model.predict_proba(input_df)[0][1]
70
- label = "βœ… Employee is likely to quit." if prob >= threshold else "βœ… Employee is likely to stay."
71
- return f"{label} (Probability: {prob:.2%})"
72
- except Exception as e:
73
- return f"❌ Error during prediction: {str(e)}"
74
 
75
- # Launch Gradio Interface
76
- gr.Interface(
77
- fn=predict_employee_status,
78
- inputs=[
79
- gr.Number(label="Satisfaction Level (0.0 - 1.0)"),
80
- gr.Number(label="Last Evaluation (0.0 - 1.0)"),
81
- gr.Number(label="Number of Projects (1 - 10)"),
82
- gr.Number(label="Average Monthly Hours (80 - 320)"),
83
- gr.Number(label="Time Spend at Company (Years)"),
84
- gr.Radio([0, 1], label="Work Accident (0 = No, 1 = Yes)"),
85
- gr.Radio([0, 1], label="Promotion in Last 5 Years (0 = No, 1 = Yes)"),
86
- gr.Radio([0, 1, 2], label="Salary (0 = Low, 1 = Medium, 2 = High)"),
87
- gr.Dropdown(departments, label="Department"),
88
- gr.Slider(0.1, 0.9, value=0.5, step=0.05, label="Prediction Threshold")
89
- ],
90
- outputs="text",
91
- title="Employee Retention Prediction System (Voting Ensemble)",
92
- description="Predict whether an employee will stay or quit. Adjust threshold for sensitivity.",
93
- theme="dark"
94
- ).launch()
95
 
 
4
  import joblib
5
  import os
6
  import warnings
7
+ from huggingface_hub import hf_hub_download
8
 
9
+ # Suppress warnings
10
  warnings.filterwarnings("ignore")
11
 
12
+ # Load ensemble model from Hugging Face Hub
13
  def load_model():
14
+ model_path = hf_hub_download(
15
+ repo_id="Zeyadd-Mostaffa/final_ensemble_model",
16
+ filename="final_ensemble_model.pkl"
17
+ )
18
+ model = joblib.load(model_path)
19
+ print("βœ… Ensemble model loaded successfully.")
20
+ return model
 
 
 
 
 
 
 
 
21
 
22
  model = load_model()
23
 
 
26
  average_monthly_hours, time_spend_company,
27
  work_accident, promotion_last_5years, salary, department, threshold=0.5):
28
 
29
+ departments = ['RandD', 'accounting', 'hr', 'management', 'marketing',
30
+ 'product_mng', 'sales', 'support', 'technical']
 
 
31
  department_features = {f"department_{dept}": 0 for dept in departments}
32
  if department in departments:
33
  department_features[f"department_{department}"] = 1
34
 
 
35
  satisfaction_evaluation = satisfaction_level * last_evaluation
36
  work_balance = average_monthly_hours / number_project
37
 
 
38
  input_data = {
39
  "satisfaction_level": [satisfaction_level],
40
  "last_evaluation": [last_evaluation],
 
50
  }
51
 
52
  input_df = pd.DataFrame(input_data)
53
+ prediction_prob = model.predict_proba(input_df)[0][1]
54
+ result = "βœ… Employee is likely to quit." if prediction_prob >= threshold else "βœ… Employee is likely to stay."
55
+ return f"{result} (Probability: {prediction_prob:.2%})"
56
 
57
+ # Launch Gradio UI
58
+ def gradio_interface():
59
+ gr.Interface(
60
+ fn=predict_employee_status,
61
+ inputs=[
62
+ gr.Number(label="Satisfaction Level (0.0 - 1.0)"),
63
+ gr.Number(label="Last Evaluation (0.0 - 1.0)"),
64
+ gr.Number(label="Number of Projects (1 - 10)"),
65
+ gr.Number(label="Average Monthly Hours (80 - 320)"),
66
+ gr.Number(label="Time Spend at Company (Years)"),
67
+ gr.Radio([0, 1], label="Work Accident (0 = No, 1 = Yes)"),
68
+ gr.Radio([0, 1], label="Promotion in Last 5 Years (0 = No, 1 = Yes)"),
69
+ gr.Radio([0, 1, 2], label="Salary (0 = Low, 1 = Medium, 2 = High)"),
70
+ gr.Dropdown(['RandD', 'accounting', 'hr', 'management', 'marketing',
71
+ 'product_mng', 'sales', 'support', 'technical'], label="Department"),
72
+ gr.Slider(0.1, 0.9, value=0.5, step=0.05, label="Prediction Threshold")
73
+ ],
74
+ outputs="text",
75
+ title="Employee Retention Prediction System (Ensemble from Hugging Face Hub)",
76
+ description="Predict whether an employee is likely to stay or quit based on their profile. Adjust the threshold for accurate predictions.",
77
+ theme="dark"
78
+ ).launch()
79
 
80
+ gradio_interface()
 
 
 
 
 
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82