Spaces:
Sleeping
Sleeping
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
# Step 1: Load the trained TensorFlow model | |
model = tf.keras.models.load_model("employee_attrition_model.h5") | |
# Step 2: Define the numerical training data used during model training | |
# Replace these rows with the actual numerical training data from your dataset | |
training_numerical_data = np.array([ | |
[25, 50000, 50, 5], # Example: Age, Salary, Workload, YearsAtCompany | |
[45, 70000, 70, 10], | |
[35, 60000, 60, 7], | |
[50, 80000, 75, 15], | |
[30, 55000, 55, 3] | |
]) | |
# Step 3: Reinitialize and adapt the normalizer to the training numerical data | |
normalizer = tf.keras.layers.Normalization(axis=-1) | |
normalizer.adapt(training_numerical_data) | |
# Step 4: Define the prediction function | |
def predict_attrition(age, salary, workload, years_at_company, job_satisfaction, promotions): | |
""" | |
Predict whether an employee will leave or stay based on input features. | |
""" | |
try: | |
# Step 4.1: Create input array | |
input_data = np.array([[age, salary, workload, years_at_company, job_satisfaction, promotions]]) | |
print("Raw Input Data:", input_data) | |
# Step 4.2: Split numerical and ordinal/binary features | |
input_numerical = input_data[:, :4] # Numerical: Age, Salary, Workload, YearsAtCompany | |
input_ordinal_binary = input_data[:, 4:] # Ordinal/Binary: JobSatisfaction, Promotions | |
print("Numerical Features:", input_numerical) | |
print("Ordinal/Binary Features:", input_ordinal_binary) | |
# Step 4.3: Normalize numerical features | |
normalized_numerical = normalizer(input_numerical) # Apply the normalizer | |
print("Normalized Numerical Features:", normalized_numerical.numpy()) | |
# Step 4.4: Combine normalized and raw features | |
final_input = np.hstack([normalized_numerical.numpy(), input_ordinal_binary]) | |
print("Final Input Data:", final_input) | |
# Step 4.5: Make predictions | |
prediction = model.predict(final_input) | |
print("Raw Model Prediction:", prediction) | |
# Step 4.6: Process prediction result | |
probability = float(prediction[0][0]) | |
result = "Will Leave" if probability > 0.5 else "Will Stay" | |
return f"{result} (Probability: {probability:.2%})" | |
except Exception as e: | |
print("Error during prediction:", e) | |
return "Error" | |
# Step 5: Define the Gradio interface | |
age = gr.Number(label="Age") | |
salary = gr.Number(label="Salary") | |
workload = gr.Number(label="Workload") | |
years_at_company = gr.Number(label="Years at Company") | |
job_satisfaction = gr.Slider(1, 4, step=1, label="Job Satisfaction (1=Low, 4=High)") | |
promotions = gr.Slider(0, 1, step=1, label="Promotions (0=No, 1=Yes)") | |
inputs = [age, salary, workload, years_at_company, job_satisfaction, promotions] | |
output = gr.Textbox(label="Prediction") | |
# Step 6: Create and launch the Gradio app | |
demo = gr.Interface( | |
fn=predict_attrition, | |
inputs=inputs, | |
outputs=output, | |
title="Employee Attrition Predictor", | |
description="Enter employee details to predict whether they will stay or leave the company." | |
) | |
demo.launch() |