File size: 6,136 Bytes
9965568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1659eb
9965568
 
 
 
 
 
b420e1b
9965568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d90c463
 
 
 
 
9965568
 
 
 
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
import gradio as gr
import pandas as pd
import pickle
import os

# Define params names
PARAMS_NAME = [
            "gender",
            "age",
            "hypertension",
            "heart_disease",
            "ever_married",
            "work_type",
            "Residence_type",
            "avg_glucose_level",
            "bmi",
            "smoking_status"
]

           
# Load model
with open("model/model1.pkl", "rb") as f:
    model = pickle.load(f)


import os

# Hacking my own protocol
os.chmod('model/saved_bins_bmi.pkl', 0o777)

with open('model/saved_bins_bmi.pkl', 'rb') as handle:
    saved_bins_bmi = pickle.load(handle)


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["bmi"] = pd.cut(single_instance['bmi'],
                                     bins=saved_bins_bmi, 
                                     include_lowest=True)
    single_instance['bmi'] = single_instance['bmi'].cat.add_categories('null')

    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)

    dataEnd_ohe = pd.get_dummies(single_instance_numbers).fillna(0)

    
    prediction = model.predict(dataEnd_ohe)


    # Cast numpy.int64 to just a int
    stroke = int(prediction[0])


    # Adaptación respuesta
    response = stroke
    if stroke == 1:
        response = "Keep rockin' babe!"
    if stroke == 0:
        response = "This brain will colapse in 3.. 2.. 1.. 🤯 "


    return response


with gr.Blocks() as demo:
    gr.Markdown(
        """
        #   Stroke Prevention 🤯
        """
    )

    with gr.Row():
        with gr.Column():

            gr.Markdown(
                """
                ## Insert your self data here please 🤓
                """
            )
            
            gender = gr.Radio(
                label='Gender',
                choices=['Male', 'Female'],
                value='Female',
            )

            age = gr.Slider(
                label='Age',
                minimum=35.0,
                maximum=82.0,
                step=1,
                randomize=True
            )

            hypertension = gr.Radio(
                label='Hypertension',
                choices=['No', 'Yes'],
                value='No',
            )

            heart_disease = gr.Radio(
                label='Heart Disease',
                choices=['Yes', 'No'],
                value='No',
            )

            ever_married = gr.Radio(
                label='Ever Married',
                choices=['Yes', 'No'],
                value='Yes',
            )

            work_type = gr.Radio(
                label='Work Type',
                choices=['Private', 'Self-employed', 'Govt-job'],
                value='Private',
            )

            Residence_type = gr.Radio(
                label='Residence Type',
                choices=['Urban', 'Rural'],
                value='Urban',
            )

            avg_glucose_level = gr.Slider(
                label='Avg Glucose Level',
                minimum=55.22,
                maximum=271.74,
                step=0.1,
                randomize=True
            )

            bmi = gr.Slider(
                label='Bmi',
                minimum=11.3,
                maximum=42.0,
                step=0.1,
                randomize=True
            )

            smoking_status = gr.Dropdown(
                label='Smoking Status',
                choices=['formerly smoked', 'never smoked', 'smokes'],
                multiselect=False,
                value='never smoked',
            )         




        with gr.Column():

            gr.Markdown(
                """
                ## Look if your brain is in risk 🧠
                """
            )

            label = gr.Label(label="Brain status")
            predict_btn = gr.Button(value="Click me please!")
            predict_btn.click(
                predict,
                inputs=[
                    gender,
                    age,
                    hypertension,
                    heart_disease,
                    ever_married,
                    work_type,
                    Residence_type,
                    avg_glucose_level,
                    bmi,
                    smoking_status,
                ],
                outputs=[label],
                api_name="prediccion"
            )
            
            gr.Markdown(
                """
                ## <img src="https://media.giphy.com/media/ijb5ZE9zIQ2Nq/giphy.gif" alt="GIF">
                """
            )

    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/fedesoriano/stroke-prediction-dataset' 
                target='_blank'>Data From IStroke Prediction Dataset update by Fede Soriano
            </a>
        </p>
        <p style='text-align: center'>
            <a href='https://colab.research.google.com/drive/19Tnw9TnKqQEibaoXDgYwFLPSS7LM8Axv?usp=sharing' 
                target='_blank'>Colab Work of what I have done
            </a>
        </p>
        """
    )

demo.launch()