File size: 3,128 Bytes
503a897
 
 
 
f9c05ec
d43ac29
ed0d973
f9c05ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed0d973
 
 
 
f9c05ec
 
 
 
 
 
 
 
 
 
d43ac29
f9c05ec
 
 
d43ac29
f9c05ec
 
 
d43ac29
f9c05ec
 
 
d43ac29
f9c05ec
 
 
 
ed0d973
f9c05ec
 
 
19d864f
f9c05ec
19d864f
 
 
 
 
 
 
 
 
503a897
f9c05ec
503a897
ed0d973
19d864f
 
503a897
ed0d973
503a897
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
74
75
76
77
78
79
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()