NicoGargano commited on
Commit
9965568
·
1 Parent(s): c70253a
Files changed (1) hide show
  1. app.py +220 -0
app.py ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import pickle
4
+ import os
5
+
6
+ # Define params names
7
+ PARAMS_NAME = [
8
+ "gender",
9
+ "age",
10
+ "hypertension",
11
+ "heart_disease",
12
+ "ever_married",
13
+ "work_type",
14
+ "Residence_type",
15
+ "avg_glucose_level",
16
+ "bmi",
17
+ "smoking_status"
18
+ ]
19
+
20
+
21
+ # Load model
22
+ with open("model/model1.pkl", "rb") as f:
23
+ model = pickle.load(f)
24
+
25
+
26
+ import os
27
+
28
+ # Hacking my own protocol
29
+ os.chmod('model/saved_bins_bmi.pkl', 0o777)
30
+
31
+ with open('model/saved_bins_bmi.pkl', 'rb') as handle:
32
+ saved_bins_bmi = pickle.load(handle)
33
+
34
+
35
+ def predict(*args):
36
+ answer_dict = {}
37
+
38
+ for i in range(len(PARAMS_NAME)):
39
+ answer_dict[PARAMS_NAME[i]] = [args[i]]
40
+
41
+ # Crear dataframe
42
+ single_instance = pd.DataFrame.from_dict(answer_dict)
43
+
44
+
45
+ single_instance["bmi"] = pd.cut(single_instance['bmi'],
46
+ bins=saved_bins_bmi,
47
+ include_lowest=True)
48
+ single_instance['bmi'] = single_instance['bmi'].cat.add_categories('null')
49
+
50
+ single_instance_numbers = single_instance
51
+
52
+ for columna in single_instance_numbers:
53
+ # Verificar si el tipo de dato es "object"
54
+ if single_instance_numbers[columna].dtype == 'object':
55
+ # Obtener los valores únicos de la columna
56
+ valores_unicos = single_instance_numbers[columna].unique()
57
+
58
+ # Crear un diccionario de reemplazo
59
+ diccionario_reemplazo = {valor: indice for indice, valor in enumerate(valores_unicos)}
60
+
61
+ # Reemplazar los valores en la columna
62
+ single_instance_numbers[columna] = single_instance_numbers[columna].map(diccionario_reemplazo)
63
+
64
+ dataEnd_ohe = pd.get_dummies(single_instance_numbers).fillna(0)
65
+
66
+
67
+ prediction = model.predict(dataEnd_ohe)
68
+
69
+
70
+ # Cast numpy.int64 to just a int
71
+ stroke = int(prediction[0])
72
+
73
+
74
+ # Adaptación respuesta
75
+ response = stroke
76
+ if stroke == 1:
77
+ response = "Keep rockin' babe!"
78
+ if stroke == 0:
79
+ response = "This brain will colapse in 3.. 2.. 1.. 🤯 "
80
+
81
+
82
+ return response
83
+
84
+
85
+ with gr.Blocks() as demo:
86
+ gr.Markdown(
87
+ """
88
+ # Stroke Prevention 🤯
89
+ """
90
+ )
91
+
92
+ with gr.Row():
93
+ with gr.Column():
94
+
95
+ gr.Markdown(
96
+ """
97
+ ## Insert your self data here please 🤓
98
+ """
99
+ )
100
+
101
+ gender = gr.Radio(
102
+ label='Gender',
103
+ choices=['Male', 'Female'],
104
+ value='Female',
105
+ )
106
+
107
+ age = gr.Slider(
108
+ label='Age',
109
+ minimum=35.0,
110
+ maximum=82.0,
111
+ step=1,
112
+ randomize=True
113
+ )
114
+
115
+ hypertension = gr.Radio(
116
+ label='Hypertension',
117
+ choices=['No', 'Yes'],
118
+ value='No',
119
+ )
120
+
121
+ heart_disease = gr.Radio(
122
+ label='Heart Disease',
123
+ choices=['Yes', 'No'],
124
+ value='No',
125
+ )
126
+
127
+ ever_married = gr.Radio(
128
+ label='Ever Married',
129
+ choices=['Yes', 'No'],
130
+ value='Yes',
131
+ )
132
+
133
+ work_type = gr.Radio(
134
+ label='Work Type',
135
+ choices=['Private', 'Self-employed', 'Govt-job'],
136
+ value='Private',
137
+ )
138
+
139
+ Residence_type = gr.Radio(
140
+ label='Residence Type',
141
+ choices=['Urban', 'Rural'],
142
+ value='Urban',
143
+ )
144
+
145
+ avg_glucose_level = gr.Slider(
146
+ label='Avg Glucose Level',
147
+ minimum=55.22,
148
+ maximum=271.74,
149
+ step=0.1,
150
+ randomize=True
151
+ )
152
+
153
+ bmi = gr.Slider(
154
+ label='Bmi',
155
+ minimum=11.3,
156
+ maximum=92.0,
157
+ step=0.1,
158
+ randomize=True
159
+ )
160
+
161
+ smoking_status = gr.Dropdown(
162
+ label='Smoking Status',
163
+ choices=['formerly smoked', 'never smoked', 'smokes', 'Unknown'],
164
+ multiselect=False,
165
+ value='never smoked',
166
+ )
167
+
168
+
169
+
170
+
171
+ with gr.Column():
172
+
173
+ gr.Markdown(
174
+ """
175
+ ## Look if your brain is in risk 🧠
176
+ """
177
+ )
178
+
179
+ label = gr.Label(label="Brain status")
180
+ predict_btn = gr.Button(value="Click me please!")
181
+ predict_btn.click(
182
+ predict,
183
+ inputs=[
184
+ gender,
185
+ age,
186
+ hypertension,
187
+ heart_disease,
188
+ ever_married,
189
+ work_type,
190
+ Residence_type,
191
+ avg_glucose_level,
192
+ bmi,
193
+ smoking_status,
194
+ ],
195
+ outputs=[label],
196
+ api_name="prediccion"
197
+ )
198
+
199
+ gr.Markdown(
200
+ """
201
+ ## <img src="https://media.giphy.com/media/ijb5ZE9zIQ2Nq/giphy.gif" alt="GIF">
202
+ """
203
+ )
204
+
205
+ gr.Markdown(
206
+ """
207
+ <p style='text-align: center'>
208
+ <a href='https://www.escueladedatosvivos.ai/cursos/bootcamp-de-data-science'
209
+ target='_blank'>Proyecto demo creado en el bootcamp de EDVAI 🤗
210
+ </a>
211
+ </p>
212
+ <p style='text-align: center'>
213
+ <a href='https://www.kaggle.com/datasets/fedesoriano/stroke-prediction-dataset'
214
+ target='_blank'>Data From IStroke Prediction Dataset update by Fede Soriano
215
+ </a>
216
+ </p>
217
+ """
218
+ )
219
+
220
+ demo.launch()