Download app.py from rasmodev/Employee_Attrition_Prediction_App: direct link, hf CLI and curl.
- Browser
- Download file 9.78 kB
-
https://huggingface.co/spaces/rasmodev/Employee_Attrition_Prediction_App/resolve/main/app.py
- Command line
-
hf download hf://spaces/rasmodev/Employee_Attrition_Prediction_App/app.py
-
curl -L -o app.py https://huggingface.co/spaces/rasmodev/Employee_Attrition_Prediction_App/resolve/main/app.py
9.78 kB
| # Import Relevant Libraries | |
| import streamlit as st | |
| import pickle | |
| import pandas as pd | |
| from catboost import CatBoostClassifier | |
| # Load the trained model and unique values from the pickle file | |
| with open('model_and_key_components.pkl', 'rb') as file: | |
| saved_components = pickle.load(file) | |
| model = saved_components['model'] | |
| unique_values = saved_components['unique_values'] | |
| # Page Title with Style | |
| st.markdown( | |
| f""" | |
| <div style="text-align: center;"> | |
| <h2 style="color: #3a2a71;">π¨βπΌπ©βπΌ Employee Attrition Prediction App</h2> | |
| </div> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| st.markdown("---") | |
| # Attrition Information | |
| st.markdown( | |
| """ | |
| **Employee attrition** refers to the phenomenon of employees leaving their jobs for various reasons. It's crucial for organizations to predict attrition to retain valuable talent. | |
| """ | |
| ) | |
| # Main content | |
| st.image("https://www.aihr.com/wp-content/uploads/Reasons-for-Employee-Attrition.png") | |
| # Link to Detailed Article on Employee Attrition | |
| st.markdown("π **Learn more about employee attrition from [Academy to Innovate HR (AIHR)](https://www.aihr.com/wp-content/uploads/Reasons-for-Employee-Attrition.png)**") | |
| st.markdown("---") | |
| # Additional Information for Sample Prediction | |
| st.write("π To make a prediction, input the information of the employee whose attrition you want to predict.") | |
| st.write("Please provide the following information to make a prediction:") | |
| # About Section with Style | |
| st.sidebar.title("About") | |
| st.sidebar.info( | |
| "This app predicts employee attrition using machine learning on HR data, aiding HR professionals in retention strategies. " | |
| "It utilizes a CatBoost machine learning model trained on an employee attrition dataset." | |
| ) | |
| # Auto-expand sidebar code | |
| st.markdown( | |
| """ | |
| <style> | |
| [data-testid="stSidebar"][aria-expanded="false"] > div:first-child { | |
| width: 100%; | |
| } | |
| </style> | |
| """, | |
| unsafe_allow_html=True | |
| ) | |
| # Input Descriptions in Sidebar | |
| st.sidebar.title("Input Descriptions") | |
| st.sidebar.markdown("**Age:** Age of the employee.") | |
| st.sidebar.markdown("**Monthly Income:** Monthly income of the employee.") | |
| st.sidebar.markdown("**Number of Companies Worked:** Number of companies the employee has worked for.") | |
| st.sidebar.markdown("**Percent Salary Hike:** Percentage increase in salary for the employee.") | |
| st.sidebar.markdown("**Training Times Last Year:** Number of training sessions attended by the employee last year.") | |
| st.sidebar.markdown("**Department:** Department the employee belongs to (Sales, Research & Development, Human Resources).") | |
| st.sidebar.markdown("**Environment Satisfaction:** Level of satisfaction with the work environment (1: Low, 2: Medium, 3: High, 4: Very High).") | |
| st.sidebar.markdown("**Job Role:** Job role of the employee.") | |
| st.sidebar.markdown("**Job Satisfaction:** Level of job satisfaction (1: Low, 2: Medium, 3: High, 4: Very High).") | |
| st.sidebar.markdown("**Work Life Balance:** Level of satisfaction with work-life balance (1: Low, 2: Medium, 3: High, 4: Very High).") | |
| st.sidebar.markdown("**Over Time:** Whether the employee works overtime.") | |
| st.sidebar.markdown("**Relationship Satisfaction:** Level of satisfaction with work relationships (1: Low, 2: Medium, 3: High, 4: Very High).") | |
| st.sidebar.markdown("**Years Since Last Promotion:** Number of years since the employee's last promotion.") | |
| st.sidebar.markdown("**Years With Current Manager:** Number of years the employee has been with the current manager.") | |
| # Define the Streamlit app | |
| def main(): | |
| # Define layout with three columns | |
| col1, col2, col3 = st.columns(3) | |
| # Column 1 | |
| with col1: | |
| age = st.number_input("Age", min_value=18, max_value=70) | |
| monthly_income = st.number_input("Monthly Income") | |
| num_companies_worked = st.number_input("Number of Companies Worked") | |
| percent_salary_hike = st.number_input("Percent Salary Hike", min_value=0, max_value=25) | |
| training_times_last_year = st.number_input("Training Times Last Year", min_value=0, max_value=6) | |
| # Column 2 | |
| with col2: | |
| department = st.selectbox("Department", ['Sales', 'Research & Development', 'Human Resources']) | |
| environment_satisfaction = st.selectbox("Environment Satisfaction", [1, 2, 3, 4]) | |
| job_role = st.selectbox("Job Role", ['Sales Executive', 'Research Scientist', 'Laboratory Technician', | |
| 'Manufacturing Director', 'Healthcare Representative', 'Manager', | |
| 'Sales Representative', 'Research Director', 'Human Resources']) | |
| job_satisfaction = st.selectbox("Job Satisfaction", [1, 2, 3, 4]) | |
| work_life_balance = st.selectbox("Work Life Balance", [1, 2, 3, 4]) | |
| # Column 3 | |
| with col3: | |
| over_time = st.checkbox("Over Time") | |
| relationship_satisfaction = st.selectbox("Relationship Satisfaction", [1, 2, 3, 4]) | |
| years_since_last_promotion = st.number_input("Years Since Last Promotion") | |
| years_with_curr_manager = st.number_input("Years With Current Manager") | |
| # Predict button | |
| if st.button("Predict Attrition π"): | |
| # Create a DataFrame to hold the user input data | |
| input_data = pd.DataFrame({ | |
| 'Age': [age], | |
| 'Department': [department], | |
| 'EnvironmentSatisfaction': [environment_satisfaction], | |
| 'JobRole': [job_role], | |
| 'JobSatisfaction': [job_satisfaction], | |
| 'MonthlyIncome': [monthly_income], | |
| 'NumCompaniesWorked': [num_companies_worked], | |
| 'OverTime': [over_time], | |
| 'PercentSalaryHike': [percent_salary_hike], | |
| 'RelationshipSatisfaction': [relationship_satisfaction], | |
| 'TrainingTimesLastYear': [training_times_last_year], | |
| 'WorkLifeBalance': [work_life_balance], | |
| 'YearsSinceLastPromotion': [years_since_last_promotion], | |
| 'YearsWithCurrManager': [years_with_curr_manager] | |
| }) | |
| # Reorder columns to match the expected order | |
| input_data = input_data[['Age', 'Department', 'EnvironmentSatisfaction', 'JobRole', 'JobSatisfaction', | |
| 'MonthlyIncome', 'NumCompaniesWorked', 'OverTime', 'PercentSalaryHike', | |
| 'RelationshipSatisfaction', 'TrainingTimesLastYear', 'WorkLifeBalance', | |
| 'YearsSinceLastPromotion', 'YearsWithCurrManager']] | |
| # Make predictions | |
| prediction = model.predict(input_data) | |
| probability = model.predict_proba(input_data)[:, 1] | |
| # Display predicted attrition status | |
| st.subheader("Predicted Attrition Status π―") | |
| if prediction[0] == 1: | |
| st.error("The employee is at higher risk of leaving the organization") | |
| else: | |
| st.success("The employee is predicted to stay in the organization") | |
| # Display prediction probability | |
| if prediction[0] == 1: | |
| st.subheader("Prediction Probability π") | |
| st.write(f"The probability of the employee leaving is: <span style='color:red; font-weight:bold;'>{probability[0]*100:.2f}%</span>", unsafe_allow_html=True) | |
| # Display characteristic-based recommendations | |
| st.subheader("Recommendations for Retaining The Employee π‘:") | |
| st.markdown("---") | |
| if job_satisfaction == 1 or environment_satisfaction == 1: | |
| st.markdown("- **Job and Environment Satisfaction**: Enhance job and environment satisfaction through initiatives such as recognition programs and improving workplace conditions.") | |
| if years_since_last_promotion > 5: | |
| st.markdown("- **Opportunities for Advancement**: Implement a transparent promotion policy and provide opportunities for career advancement.") | |
| if years_with_curr_manager > 5: | |
| st.markdown("- **Change in Reporting Structure**: Offer opportunities for a change in reporting structure to prevent stagnation and promote growth.") | |
| if percent_salary_hike < 5: | |
| st.markdown("- **Salary and Benefits Adjustment**: Consider adjusting salary and benefits packages to remain competitive and reward employee loyalty.") | |
| if training_times_last_year < 2: | |
| st.markdown("- **Employee Development**: Invest in employee development through training programs and continuous learning opportunities.") | |
| if over_time: | |
| st.markdown("- **Workload Evaluation**: Evaluate workload distribution and consider implementing measures to prevent overwork, such as workload balancing and flexible scheduling.") | |
| if relationship_satisfaction == 1: | |
| st.markdown("- **Positive Work Environment**: Foster positive relationships and a supportive work environment through team-building activities and open communication channels.") | |
| if monthly_income < 5000: | |
| st.markdown("- **Compensation Review**: Review compensation structures and adjust salaries to align with industry standards and employee expectations.") | |
| if num_companies_worked > 5: | |
| st.markdown("- **Address High Turnover**: Identify reasons for high turnover and address issues related to job stability, career progression, and organizational culture.") | |
| if work_life_balance == 1: | |
| st.markdown("- **Work-Life Balance Initiatives**: Promote work-life balance initiatives, such as flexible work arrangements and wellness programs, to support employee well-being.") | |
| # General recommendation for all negative predictions | |
| st.markdown("- **Exit Interviews**: Conduct exit interviews to gather feedback and identify areas for improvement in retention strategies.") | |
| if __name__ == "__main__": | |
| main() | |