Atrittion / app.py
NicoGargano's picture
clean a coment that has no sense
8c19701
import gradio as gr
import pandas as pd
import pickle
# Define params names
PARAMS_NAME = [
"Age",
"BusinessTravel",
"DailyRate",
"Department",
"DistanceFromHome",
"Education",
"EducationField",
"EnvironmentSatisfaction",
"Gender",
"HourlyRate",
"JobInvolvement",
"JobLevel",
"JobRole",
"JobSatisfaction",
"MaritalStatus",
"MonthlyIncome",
"MonthlyRate",
"NumCompaniesWorked",
"OverTime",
"PercentSalaryHike",
"PerformanceRating",
"RelationshipSatisfaction",
"StockOptionLevel",
"TotalWorkingYears",
"TrainingTimesLastYear",
"WorkLifeBalance",
"YearsAtCompany",
"YearsInCurrentRole",
"YearsSinceLastPromotion",
"YearsWithCurrManager"
]
# Load model
with open("model/model1.pkl", "rb") as f:
model = pickle.load(f)
def predict(*args):
answer_dict = {}
for i in range(len(PARAMS_NAME)):
answer_dict[PARAMS_NAME[i]] = [args[i]]
# Crear dataframe
single_instance = pd.DataFrame.from_dict(answer_dict)
single_instance_numbers = single_instance
for columna in single_instance_numbers:
# Verificar si el tipo de dato es "object"
if single_instance_numbers[columna].dtype == 'object':
# Obtener los valores únicos de la columna
valores_unicos = single_instance_numbers[columna].unique()
# Crear un diccionario de reemplazo
diccionario_reemplazo = {valor: indice for indice, valor in enumerate(valores_unicos)}
# Reemplazar los valores en la columna
single_instance_numbers[columna] = single_instance_numbers[columna].map(diccionario_reemplazo)
prediction = model.predict(single_instance_numbers)
Attrition = int(prediction[0])
# Adaptación respuesta
response = Attrition
if Attrition == 1:
response = "Good idea, \n but not now, I am not atrittioned yet"
if Attrition == 0:
response = "🤯 \n OMG! PLEEEEEASE"
return response
with gr.Blocks() as demo:
gr.Markdown(
"""
# Attrition Prevention 🤯
"""
)
with gr.Row():
with gr.Column():
gr.Markdown(
"""
## Insert your job data here please 🤓
"""
)
Age = gr.Slider(
label='Age',
minimum=18,
maximum=60,
step=1,
value=41
)
BusinessTravel = gr.Radio(
label='Business Travel',
choices=['Travel Rarely', 'Travel Frequently', 'Non-Travel'],
value='Travel Rarely',
)
DailyRate = gr.Slider(
label='Daily Rate',
minimum=102,
maximum=1499,
step=1,
value=1102
)
Department = gr.Radio(
label='Department',
choices=['Sales', 'Research & Development', 'Human Resources'],
value='Sales',
)
DistanceFromHome = gr.Slider(
label='Distance From Home',
minimum=1,
maximum=29,
step=1,
value=1
)
Education = gr.Dropdown(
label='Education',
choices=['College', 'Below College', 'Master', 'Bachelor', 'Doctor'],
multiselect=False,
value='Bachelor',
)
EducationField = gr.Dropdown(
label='Education Field',
choices=['Life Sciences', 'Other', 'Medical', 'Marketing', 'Technical Degree', 'Human Resources'],
multiselect=False,
value='Life Sciences',
)
EnvironmentSatisfaction = gr.Dropdown(
label='Environment Satisfaction',
choices=['Medium', 'High', 'Very High', 'Low'],
multiselect=False,
value='Medium',
)
Gender = gr.Radio(
label='Gender',
choices=['Female', 'Male'],
value='Female',
)
HourlyRate = gr.Slider(
label='Hourly Rate',
minimum=30,
maximum=100,
step=1,
value=94
)
JobInvolvement = gr.Dropdown(
label='Job Involvement',
choices=['High', 'Medium', 'Very High', 'Low'],
multiselect=False,
value='High',
)
JobLevel = gr.Radio(
label='Job Level',
choices=[2, 1, 3, 4, 5],
value=2,
)
JobRole = gr.Dropdown(
label='Job Role',
choices=['Sales Executive', 'Research Scientist', 'Laboratory Technician', 'Manufacturing Director', 'Healthcare Representative', 'Manager', 'Sales Representative', 'Research Director', 'Human Resources'],
multiselect=False,
value='Sales Executive',
)
JobSatisfaction = gr.Dropdown(
label='Job Satisfaction',
choices=['Very High', 'Medium', 'High', 'Low'],
multiselect=False,
value='High',
)
MaritalStatus = gr.Radio(
label='Marital Status',
choices=['Single', 'Married', 'Divorced'],
value='Single',
)
MonthlyIncome = gr.Slider(
label='Monthly Income',
minimum=1009,
maximum=19999,
step=1,
value=5993
)
MonthlyRate = gr.Slider(
label='Monthly Rate',
minimum=2094,
maximum=26999,
step=1,
value=19479
)
NumCompaniesWorked = gr.Slider(
label='Num Companies Worked',
minimum=0,
maximum=9,
step=1,
value=8
)
OverTime = gr.Radio(
label='Overtime',
choices=['Yes', 'No'],
value='Yes',
)
PercentSalaryHike = gr.Slider(
label='Percent Salary Hike',
minimum=11,
maximum=25,
step=1,
value=11
)
PerformanceRating = gr.Radio(
label='Performance Rating',
choices=['Excellent', 'Outstanding'],
value='Excellent',
)
RelationshipSatisfaction = gr.Dropdown(
label='Relationship Satisfaction',
choices=['Low', 'Very High', 'Medium', 'High'],
multiselect=False,
value='Low',
)
StockOptionLevel = gr.Radio(
label='Stockoption Level',
choices=[0, 1, 3, 2],
value=0,
)
TotalWorkingYears = gr.Slider(
label='Total Working Years',
minimum=0,
maximum=40,
step=1,
value=8
)
TrainingTimesLastYear = gr.Slider(
label='Training Times Last Year',
minimum=0,
maximum=6,
step=1,
value=0
)
WorkLifeBalance = gr.Dropdown(
label='Work Life balance',
choices=['Bad', 'Better', 'Good', 'Best'],
multiselect=False,
value='Bad',
)
YearsAtCompany = gr.Slider(
label='Years At Company',
minimum=0,
maximum=40,
step=1,
value=6
)
YearsInCurrentRole = gr.Slider(
label='Years In Currentrole',
minimum=0,
maximum=18,
step=1,
value=41
)
YearsSinceLastPromotion = gr.Slider(
label='Years Since Last Promotion',
minimum=0,
maximum=15,
step=1,
value=0
)
YearsWithCurrManager = gr.Slider(
label='Years With Curr Manager',
minimum=0,
maximum=17,
step=1,
value=5
)
with gr.Column():
gr.Markdown(
"""
## Look if you need some Holy Days 🏝️
"""
)
label = gr.Label(label="Attrition Level")
predict_btn = gr.Button(value="Click Here Please")
predict_btn.click(
predict,
inputs=[
Age,
BusinessTravel,
DailyRate,
Department,
DistanceFromHome,
Education,
EducationField,
EnvironmentSatisfaction,
Gender,
HourlyRate,
JobInvolvement,
JobLevel,
JobRole,
JobSatisfaction,
MaritalStatus,
MonthlyIncome,
MonthlyRate,
NumCompaniesWorked,
OverTime,
PercentSalaryHike,
PerformanceRating,
RelationshipSatisfaction,
StockOptionLevel,
TotalWorkingYears,
TrainingTimesLastYear,
WorkLifeBalance,
YearsAtCompany,
YearsInCurrentRole,
YearsSinceLastPromotion,
YearsWithCurrManager,
],
outputs=[label],
api_name="prediccion"
)
gr.Markdown(
"""
<p style='text-align: center'>
<a href='https://www.escueladedatosvivos.ai/cursos/bootcamp-de-data-science'
target='_blank'>Proyecto demo creado en el bootcamp de EDVAI 🤗
</a>
</p>
<p style='text-align: center'>
<a href='https://www.kaggle.com/datasets/pavansubhasht/ibm-hr-analytics-attrition-dataset'
target='_blank'>Data From IBM HR Analytics Employee Attrition & Performance
</a>
</p>
"""
)
demo.launch()