Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import pickle | |
| # Define params names | |
| PARAMS_NAME = [ | |
| "BMI", | |
| "Smoking", | |
| "AlcoholDrinking", | |
| "Stroke", | |
| "PhysicalHealth", | |
| "MentalHealth", | |
| "DiffWalking", | |
| "Sex", | |
| "AgeCategory", | |
| "Race", | |
| "Diabetic", | |
| "PhysicalActivity", | |
| "GenHealth", | |
| "SleepTime", | |
| "Asthma", | |
| "KidneyDisease", | |
| "SkinCancer" | |
| ] | |
| # 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) | |
| data4 = single_instance | |
| data4=data4.replace({'80 or older': '80'}) | |
| def limpiar_valor(valor): | |
| return valor.replace('-', '').replace(' ', '') | |
| data4['AgeCategory'] = data4['AgeCategory'].apply(limpiar_valor).astype(int) | |
| data4=data4.replace({'Yes': 0, 'No': 1}) | |
| data4=data4.replace({'Female': 0, 'Male': 1}) | |
| data4=data4.replace({'Fair': 0, 'Good': 1, 'Very good': 2}) | |
| data4['Diabetic']=data4['Diabetic'].replace({'Yes (during pregnancy)': 1, 'No, borderline diabetes': 1}) | |
| data4.rename(columns={'Race': 'Etnic'}, inplace=True) | |
| data4['GenHealth']=data4['GenHealth'].replace({'Fair': 0, 'Poor': 1, 'Good': 2, 'Very good': 3, 'Excellent': 4}) | |
| data4['Etnic'] = data4['Etnic'].replace({'White': 'Gringo', 'Black': 'Afro'}) | |
| data4=data4.replace({'American Indian/Alaskan Native': 0, 'Asian': 1, 'Afro': 2, 'Hispanic': 3, 'Other': 4, 'Gringo': 5}) | |
| data4[['PhysicalHealth', 'MentalHealth', 'SleepTime']] = data4[['PhysicalHealth', 'MentalHealth', 'SleepTime']].astype(int) | |
| prediction = model.predict(data4) | |
| # Como sabemos el model nos devuelve los tipos de fraude 1, 2 y 3 en el response. Podemos devolver un response estilo semáforo. | |
| # Cast numpy.int64 to just a int | |
| result = int(prediction[0]) | |
| # Adaptación respuesta | |
| response = result | |
| if result == 1: | |
| response = "Still eating trash food and smoking, you're inmortal dude" | |
| if result == 0: | |
| response = "Leave the chips, the cakes and take walk everyday NOW!" | |
| return response | |
| with gr.Blocks() as demo: | |
| gr.Markdown( | |
| """ | |
| # Heart Status ❤️🩹 | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown( | |
| """ | |
| ## Insert your health data here please 😮💨 | |
| """ | |
| ) | |
| BMI = gr.Slider( | |
| label='Bmi - Body Mass Index', | |
| minimum=12.02, | |
| maximum=44.85, | |
| step=1, | |
| randomize=True | |
| ) | |
| Smoking = gr.Radio( | |
| label='Have you smoked at least 100 cigarettes in your entire life?', | |
| choices=['Yes', 'No'], | |
| value='No', | |
| ) | |
| AlcoholDrinking = gr.Radio( | |
| label='Heavy drinkers (adult men having more than 14 drinks per week and adult women having more than 7 drinks per week', | |
| choices=['No', 'Yes'], | |
| value='No', | |
| ) | |
| Stroke = gr.Radio( | |
| label='(Ever told) (you had) a stroke?', | |
| choices=['No', 'Yes'], | |
| value='No', | |
| ) | |
| PhysicalHealth = gr.Slider( | |
| label='Now thinking about your physical health, which includes physical illness and injury, for how many days during the past 30', | |
| minimum=0, | |
| maximum=30, | |
| step=1, | |
| randomize=True | |
| ) | |
| MentalHealth = gr.Slider( | |
| label='Thinking about your mental health, for how many days during the past 30 days was your mental health not good?', | |
| minimum=0, | |
| maximum=30, | |
| step=1, | |
| randomize=True | |
| ) | |
| DiffWalking = gr.Radio( | |
| label='Do you have serious difficulty walking or climbing stairs?', | |
| choices=['No', 'Yes'], | |
| value='No', | |
| ) | |
| Sex = gr.Radio( | |
| label='Are you male or female?', | |
| choices=['Female', 'Male'], | |
| value='Female', | |
| ) | |
| AgeCategory = gr.Dropdown( | |
| label='Age Category', | |
| choices=['55-59', '80 or older', '65-69', '75-79', '40-44', '70-74', '60-64', '50-54', '45-49', '18-24', '35-39', '30-34', '25-29'], | |
| multiselect=False, | |
| value='65-69', | |
| ) | |
| Race = gr.Dropdown( | |
| label='Etnic', | |
| choices=['Gringo', 'Afro', 'Asian', 'American Indian/Alaskan Native', 'Other', 'Hispanic'], | |
| multiselect=False, | |
| value='Gringo', | |
| ) | |
| Diabetic = gr.Dropdown( | |
| label='Diabetic', | |
| choices=['Yes', 'No', 'No, borderline diabetes', 'Yes (during pregnancy)'], | |
| multiselect=False, | |
| value='No', | |
| ) | |
| PhysicalActivity = gr.Radio( | |
| label='Physical activity', | |
| choices=['Yes', 'No'], | |
| value='Yes', | |
| ) | |
| GenHealth = gr.Dropdown( | |
| label='Gen health', | |
| choices=['Very good', 'Fair', 'Good', 'Poor', 'Excellent'], | |
| multiselect=False, | |
| value='Very good', | |
| ) | |
| SleepTime = gr.Slider( | |
| label='Sleeptime', | |
| minimum=3, | |
| maximum=12, | |
| step=1, | |
| randomize=True | |
| ) | |
| Asthma = gr.Radio( | |
| label='Asthma', | |
| choices=['Yes', 'No'], | |
| value='No', | |
| ) | |
| KidneyDisease = gr.Radio( | |
| label='Kidney disease', | |
| choices=['No', 'Yes'], | |
| value='No', | |
| ) | |
| SkinCancer = gr.Radio( | |
| label='Skin cancer', | |
| choices=['Yes', 'No'], | |
| value='No', | |
| ) | |
| with gr.Column(): | |
| gr.Markdown( | |
| """ | |
| ## Let see the medical board review | |
| ## <img src="https://media.tenor.com/ny2kroyiSI4AAAAC/hi-everybody-simpsons.gif" alt="GIF"> | |
| """ | |
| ) | |
| label = gr.Label(label="medical board") | |
| predict_btn = gr.Button(value="How is my heart Nick?") | |
| predict_btn.click( | |
| predict, | |
| inputs=[ | |
| BMI, | |
| Smoking, | |
| AlcoholDrinking, | |
| Stroke, | |
| PhysicalHealth, | |
| MentalHealth, | |
| DiffWalking, | |
| Sex, | |
| AgeCategory, | |
| Race, | |
| Diabetic, | |
| PhysicalActivity, | |
| GenHealth, | |
| SleepTime, | |
| Asthma, | |
| KidneyDisease, | |
| SkinCancer, | |
| ], | |
| 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/kamilpytlak/personal-key-indicators-of-heart-disease' | |
| target='_blank'>Data From Personal Key Indicators of Heart Disease in Kaggle | |
| </a> | |
| </p> | |
| <p style='text-align: center'> | |
| <a href='https://github.com/GarganoNicolas/heart' | |
| target='_blank'>GitHub Project 🛠️ | |
| </a> | |
| </p> | |
| """ | |
| ) | |
| demo.launch() | |