Zeyadd-Mostaffa commited on
Commit
4339fe4
Β·
verified Β·
1 Parent(s): abb49f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -5,10 +5,9 @@ import joblib
5
  import warnings
6
  from huggingface_hub import hf_hub_download
7
 
8
- # Suppress warnings
9
  warnings.filterwarnings("ignore")
10
 
11
- # Load ensemble model from Hugging Face Hub
12
  def load_model():
13
  model_path = hf_hub_download(
14
  repo_id="Zeyadd-Mostaffa/final_ensemble_model",
@@ -18,7 +17,18 @@ def load_model():
18
  print("βœ… Ensemble model loaded successfully.")
19
  return model
20
 
 
 
 
 
 
 
 
 
 
 
21
  model = load_model()
 
22
 
23
  # Define prediction function
24
  def predict_employee_status(
@@ -26,7 +36,6 @@ def predict_employee_status(
26
  average_monthly_hours, time_spend_company,
27
  work_accident, promotion_last_5years, salary, department, threshold=0.5
28
  ):
29
- # Expected columns from training
30
  expected_columns = [
31
  'satisfaction_level', 'last_evaluation', 'number_project', 'average_monthly_hours',
32
  'time_spend_company', 'Work_accident', 'promotion_last_5years', 'salary',
@@ -36,17 +45,14 @@ def predict_employee_status(
36
  'department_sales', 'department_support', 'department_technical'
37
  ]
38
 
39
- # Construct department one-hot features
40
  department_features = {col: 0 for col in expected_columns if col.startswith("department_")}
41
  dept_key = f"department_{department}"
42
  if dept_key in department_features:
43
  department_features[dept_key] = 1
44
 
45
- # Create interaction features
46
  satisfaction_evaluation = satisfaction_level * last_evaluation
47
  work_balance = average_monthly_hours / number_project
48
 
49
- # Create input dataframe
50
  input_data = {
51
  "satisfaction_level": [satisfaction_level],
52
  "last_evaluation": [last_evaluation],
@@ -63,12 +69,19 @@ def predict_employee_status(
63
 
64
  input_df = pd.DataFrame(input_data)
65
 
66
- # Ensure all expected columns are present and ordered
67
  for col in expected_columns:
68
  if col not in input_df.columns:
69
  input_df[col] = 0
70
  input_df = input_df[expected_columns]
71
 
 
 
 
 
 
 
 
72
  try:
73
  prob = model.predict_proba(input_df)[0][1]
74
  result = "βœ… Employee is likely to quit." if prob >= threshold else "βœ… Employee is likely to stay."
@@ -76,7 +89,7 @@ def predict_employee_status(
76
  except Exception as e:
77
  return f"❌ Prediction error: {str(e)}"
78
 
79
- # Gradio Interface
80
  def gradio_interface():
81
  interface = gr.Interface(
82
  fn=predict_employee_status,
@@ -104,5 +117,3 @@ def gradio_interface():
104
  interface.launch()
105
 
106
  gradio_interface()
107
-
108
-
 
5
  import warnings
6
  from huggingface_hub import hf_hub_download
7
 
 
8
  warnings.filterwarnings("ignore")
9
 
10
+ # Load ensemble model
11
  def load_model():
12
  model_path = hf_hub_download(
13
  repo_id="Zeyadd-Mostaffa/final_ensemble_model",
 
17
  print("βœ… Ensemble model loaded successfully.")
18
  return model
19
 
20
+ # Load scaler
21
+ def load_scaler():
22
+ scaler_path = hf_hub_download(
23
+ repo_id="Zeyadd-Mostaffa/final_ensemble_model",
24
+ filename="scaler.pkl"
25
+ )
26
+ scaler = joblib.load(scaler_path)
27
+ print("βœ… Scaler loaded successfully.")
28
+ return scaler
29
+
30
  model = load_model()
31
+ scaler = load_scaler()
32
 
33
  # Define prediction function
34
  def predict_employee_status(
 
36
  average_monthly_hours, time_spend_company,
37
  work_accident, promotion_last_5years, salary, department, threshold=0.5
38
  ):
 
39
  expected_columns = [
40
  'satisfaction_level', 'last_evaluation', 'number_project', 'average_monthly_hours',
41
  'time_spend_company', 'Work_accident', 'promotion_last_5years', 'salary',
 
45
  'department_sales', 'department_support', 'department_technical'
46
  ]
47
 
 
48
  department_features = {col: 0 for col in expected_columns if col.startswith("department_")}
49
  dept_key = f"department_{department}"
50
  if dept_key in department_features:
51
  department_features[dept_key] = 1
52
 
 
53
  satisfaction_evaluation = satisfaction_level * last_evaluation
54
  work_balance = average_monthly_hours / number_project
55
 
 
56
  input_data = {
57
  "satisfaction_level": [satisfaction_level],
58
  "last_evaluation": [last_evaluation],
 
69
 
70
  input_df = pd.DataFrame(input_data)
71
 
72
+ # Ensure all expected columns exist
73
  for col in expected_columns:
74
  if col not in input_df.columns:
75
  input_df[col] = 0
76
  input_df = input_df[expected_columns]
77
 
78
+ # Apply scaling to same numerical columns as training
79
+ numeric_cols = [
80
+ 'satisfaction_level', 'last_evaluation',
81
+ 'average_monthly_hours', 'number_project', 'work_balance'
82
+ ]
83
+ input_df[numeric_cols] = scaler.transform(input_df[numeric_cols])
84
+
85
  try:
86
  prob = model.predict_proba(input_df)[0][1]
87
  result = "βœ… Employee is likely to quit." if prob >= threshold else "βœ… Employee is likely to stay."
 
89
  except Exception as e:
90
  return f"❌ Prediction error: {str(e)}"
91
 
92
+ # Gradio UI
93
  def gradio_interface():
94
  interface = gr.Interface(
95
  fn=predict_employee_status,
 
117
  interface.launch()
118
 
119
  gradio_interface()