File size: 14,931 Bytes
3efedb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import streamlit as st
import requests
import pandas as pd
import plotly.express as px

# Function to create status box
def status_box(status, text):
    if status == "good":
        return f'<div class="status-box status-good">{text}</div>'
    elif status == "ok":
        return f'<div class="status-box status-ok">{text}</div>'
    else:
        return f'<div class="status-box status-bad">{text}</div>'

# Function to create a metric with progress bar
def metric_with_progress(label, value, description, status, progress_percent):
    progress_class = "progress-good" if status == "good" else "progress-ok" if status == "ok" else "progress-bad"
    return f"""

    <div class="metric-label">{label}</div>

    <div class="metric-value">{value}</div>

    <div class="metric-desc">{description}</div>

    <div class="progress-container">

        <div class="progress-bar {progress_class}" style="width: {progress_percent}%"></div>

    </div>

    """

# Custom CSS for styling
st.markdown("""

<style>

    .status-box {

        display: inline-block;

        padding: 6px 0px;

        border-radius: 4px;

        text-align: center;

        font-weight: bold;

        width: 100%;

        white-space: nowrap;

        font-size: 14px;

    }

    .status-good {

        background-color: #0e5814;

        color: white;

    }

    .status-ok {

        background-color: #9c6a00;

        color: white;

    }

    .status-bad {

        background-color: #8b0000;

        color: white;

    }

    .metric-label {

        font-size: 18px;

        font-weight: bold;

        margin-bottom: 5px;

    }

    .metric-value {

        font-size: 28px;

        font-weight: bold;

    }

    .metric-desc {

        font-size: 14px;

        color: #ccc;

        margin-top: 5px;

    }

    .progress-container {

        width: 100%;

        background-color: #333;

        border-radius: 5px;

        margin-top: 10px;

    }

    .progress-bar {

        height: 10px;

        border-radius: 5px;

    }

    .progress-good {

        background-color: #0e5814;

    }

    .progress-ok {

        background-color: #9c6a00;

    }

    .progress-bad {

        background-color: #8b0000;

    }

</style>

""", unsafe_allow_html=True)

def show_attrition_prediction():
    st.title("Employee Attrition Prediction")
    
    # Create sidebar for inputs
    with st.sidebar:
        st.header("Employee Details")
        
        # Personal Information
        st.subheader("Personal Information")
        age = st.slider("Age", 18, 65, 30)
        
        # Job Information
        st.subheader("Job Information")
        distance_from_home = st.slider("Distance From Home (km)", 1, 30, 5)
        job_level = st.slider("Job Level", 1, 5, 2)
        
        # Work Experience
        st.subheader("Work Experience")
        total_working_years = st.slider("Total Working Years", 0, 40, 5)
        years_at_company = st.slider("Years at Company", 0, 40, 3)
        
        # Compensation
        st.subheader("Compensation")
        monthly_income = st.number_input("Monthly Income", min_value=1000, max_value=20000, value=5000, step=500)
        
        # Work Environment & Satisfaction
        st.subheader("Work Environment & Satisfaction")
        environment_satisfaction = st.slider("Environment Satisfaction", 1, 4, 3, 
                                          help="1=Low, 2=Medium, 3=High, 4=Very High")
        job_satisfaction = st.slider("Job Satisfaction", 1, 4, 3,
                                   help="1=Low, 2=Medium, 3=High, 4=Very High")
        work_life_balance = st.slider("Work Life Balance", 1, 4, 3,
                                    help="1=Low, 2=Medium, 3=High, 4=Very High")
        overtime = st.selectbox("Overtime", ["Yes", "No"])
        
        # Submit button
        predict_button = st.button("Predict Attrition Risk")
    
    # Main content area
    if predict_button:
        # Prepare data for API
        data = {
            "model_type": "attrition",
            "features": {
                # Top 10 most important features
                "Age": age,
                "DistanceFromHome": distance_from_home,
                "EnvironmentSatisfaction": environment_satisfaction,
                "JobLevel": job_level,
                "JobSatisfaction": job_satisfaction,
                "MonthlyIncome": monthly_income,
                "OverTime": overtime,
                "TotalWorkingYears": total_working_years,
                "WorkLifeBalance": work_life_balance,
                "YearsAtCompany": years_at_company
            }
        }
        
        # Call API
        with st.spinner("Predicting..."):
            try:
                response = requests.post("http://127.0.0.1:8000/predict", json=data)
                
                if response.status_code == 200:
                    result = response.json()
                    
                    # Display results
                    col1, col2 = st.columns([3, 2])
                    
                    with col1:
                        # Prediction result
                        if result["prediction"] == 0:
                            st.success("### Low Attrition Risk ✅")
                            st.markdown("This employee is likely to stay with the company.")
                        else:
                            st.error("### High Attrition Risk ⚠️")
                            st.markdown("This employee may be at risk of leaving.")
                        
                        # Enhanced explanation
                        st.info("### Key Factors")
                        
                        # Feature importance visualization - Updated with actual top 10 features
                        st.subheader("Factors Affecting Attrition Risk")
                        feature_importance = {
                            "Monthly Income": 0.20,
                            "Years at Company": 0.15,
                            "Overtime": 0.12,
                            "Job Satisfaction": 0.11,
                            "Distance From Home": 0.10,
                            "Work Life Balance": 0.09,
                            "Age": 0.08,
                            "Job Level": 0.06,
                            "Total Working Years": 0.05,
                            "Environment Satisfaction": 0.04
                        }
                        
                        feature_df = pd.DataFrame({
                            "feature": list(feature_importance.keys()),
                            "importance": list(feature_importance.values())
                        })
                        
                        fig = px.bar(
                            feature_df,
                            x="importance",
                            y="feature",
                            orientation='h',
                            title="Impact of Different Factors on Attrition Risk",
                            labels={"importance": "Impact", "feature": "Factor"},
                            height=400
                        )
                        st.plotly_chart(fig, use_container_width=True)
                    
                    with col2:
                        # Add container for better styling
                        with st.container():
                            # Probability gauge
                            st.subheader("Attrition Probability")
                            attrition_prob = result["probability"]
                            st.metric(
                                label="Risk Level",
                                value=f"{attrition_prob:.1%}"
                            )
                            
                            st.markdown("---")
                            
                            # Employee Profile Summary
                            st.subheader("Employee Profile")
                            
                            # Job Metrics
                            metrics_col1, metrics_col2 = st.columns(2)
                            
                            with metrics_col1:
                                # Job Level
                                level_status = "good" if job_level >= 3 else "ok"
                                level_desc = "Senior Position" if job_level >= 3 else "Junior/Mid-level Position"
                                level_percent = (job_level / 5) * 100
                                
                                st.markdown(
                                    metric_with_progress("Job Level", str(job_level), level_desc, level_status, level_percent),
                                    unsafe_allow_html=True
                                )
                            
                            with metrics_col2:
                                # Years at Company
                                tenure_status = "good" if years_at_company >= 5 else "ok"
                                tenure_desc = "Experienced Employee" if years_at_company >= 5 else "Growing Experience"
                                tenure_percent = min((years_at_company / 10) * 100, 100)
                                
                                st.markdown(
                                    metric_with_progress("Tenure", f"{years_at_company} years", tenure_desc, tenure_status, tenure_percent),
                                    unsafe_allow_html=True
                                )
                            
                            # Satisfaction Metrics
                            st.markdown("### Satisfaction Levels")
                            
                            sat_col1, sat_col2 = st.columns(2)
                            
                            with sat_col1:
                                # Job Satisfaction
                                job_sat_status = "good" if job_satisfaction >= 3 else "ok" if job_satisfaction >= 2 else "bad"
                                job_sat_desc = "High Satisfaction" if job_satisfaction >= 3 else "Moderate Satisfaction" if job_satisfaction >= 2 else "Low Satisfaction"
                                job_sat_percent = (job_satisfaction / 4) * 100
                                
                                st.markdown(
                                    metric_with_progress("Job Satisfaction", f"{job_satisfaction}/4", job_sat_desc, job_sat_status, job_sat_percent),
                                    unsafe_allow_html=True
                                )
                            
                            with sat_col2:
                                # Work Life Balance
                                wlb_status = "good" if work_life_balance >= 3 else "ok" if work_life_balance >= 2 else "bad"
                                wlb_desc = "Good Balance" if work_life_balance >= 3 else "Moderate Balance" if work_life_balance >= 2 else "Poor Balance"
                                wlb_percent = (work_life_balance / 4) * 100
                                
                                st.markdown(
                                    metric_with_progress("Work-Life Balance", f"{work_life_balance}/4", wlb_desc, wlb_status, wlb_percent),
                                    unsafe_allow_html=True
                                )
                            
                            # Compensation Analysis
                            st.markdown("### Compensation Analysis")
                            
                            # Monthly Income vs Experience
                            income_per_year = monthly_income * 12
                            expected_income = 3000 * (1 + total_working_years * 0.1) * job_level  # Simple model for expected income
                            income_ratio = income_per_year / expected_income
                            
                            income_status = "good" if income_ratio >= 1 else "ok" if income_ratio >= 0.8 else "bad"
                            income_desc = f"{'Above' if income_ratio >= 1 else 'Near' if income_ratio >= 0.8 else 'Below'} expected for experience level"
                            income_percent = min(income_ratio * 100, 100)
                            
                            st.markdown(
                                metric_with_progress(
                                    "Annual Income",
                                    f"₹{income_per_year:,.0f}",
                                    income_desc,
                                    income_status,
                                    income_percent
                                ),
                                unsafe_allow_html=True
                            )
                            
                            # If high attrition risk, show recommendations
                            if result["prediction"] == 1:
                                st.markdown("### 🚨 Retention Recommendations")
                                
                                recommendations = []
                                
                                if monthly_income < 5000:
                                    recommendations.append("Consider a compensation review")
                                if job_satisfaction < 3:
                                    recommendations.append("Schedule a job satisfaction discussion")
                                if work_life_balance < 3:
                                    recommendations.append("Review workload and scheduling")
                                if overtime == "Yes":
                                    recommendations.append("Evaluate workload distribution and overtime requirements")
                                if environment_satisfaction < 3:
                                    recommendations.append("Assess work environment concerns")
                                
                                for rec in recommendations:
                                    st.markdown(f"• {rec}")
                
                else:
                    st.error(f"Error: API returned status code {response.status_code}")
                    try:
                        error_detail = response.json()
                        st.error(f"Error details: {error_detail}")
                    except:
                        st.code(response.text)
                        
            except Exception as e:
                st.error(f"Error connecting to API: {str(e)}")
                st.error("Make sure the FastAPI backend is running with: python -m uvicorn src.api.main:app --reload")