-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
369 lines (325 loc) · 13.2 KB
/
main.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# Importações necessárias
import base64
import io
import dash
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
from dash import dcc, html
from dash import dash_table
from EasyKerasMLP.KerasMLP_OPT import *
from EasyKerasMLP.KerasMLP import *
from EasyKerasMLP.KerasPredict import *
Input_Columns = None
Output_Columns = None
Dataset = None
# Inicializa o app Dash
app = dash.Dash(__name__,
suppress_callback_exceptions=True,
external_stylesheets=[dbc.themes.BOOTSTRAP],
meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1.0"}],)
app.title = "Easy Keras MLP Regression"
server = app.server
app.layout = html.Div([
html.Br(),
html.Br(),
html.Div([
html.Img(src='assets/logo.png', style={'height': '100px', 'margin-left': 'auto', 'margin-right': 'auto'}),
], style={'text-align': 'center', 'margin-bottom': '10px'}),
html.Div([
dcc.Tabs(id='tabs', value='tab1', children=[
dcc.Tab(label='Dataset Information', value='tab1'),
dcc.Tab(label='Simple Keras MLP', value='tab2'),
dcc.Tab(label='Optimized Keras MLP', value='tab3'),
dcc.Tab(label='Predict Values', value='tab4'),
], style={'align': 'center', 'width': '80%', 'margin-left': 'auto', 'margin-right': 'auto'}),
]),
dcc.Store(id='store', storage_type='memory'),
html.Div(id='tabs-content'),
])
dataset_layout = html.Div([
html.Br(),
dcc.Upload(
id='upload-data',
children=html.Div([
'Arraste e Solte ou ',
html.A('Selecione um Arquivo Excel ou CSV (Seu Dataset)')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
},
# Permite múltiplos arquivos a serem carregados
multiple=False
),
html.Br(),
html.Label('Selecione quais serão os Inputs da sua MLP:'),
dcc.Dropdown(
id='column-input-selector',
multi=True,
placeholder='Selecione as colunas após carregar um arquivo'
),
html.Br(),
dash_table.DataTable(
id='input-table',
page_size=3, # Número de linhas a mostrar por página
),
html.Br(),
html.Label('Selecione quais serão os Otputs da sua MLP:'),
dcc.Dropdown(
id='column-output-selector',
multi=True,
placeholder='Selecione as colunas após carregar um arquivo'
),
html.Br(),
dash_table.DataTable(
id='output-table',
page_size=3, # Número de linhas a mostrar por página
),
], style={'width': '80%', 'justifyContent': 'center', 'margin-left': 'auto', 'margin-right': 'auto', 'padding': '20px'})
simple_layout = html.Div([
html.Button('RUN PREDEFINED KERAS MLP!',
id='run-MLP-button',
disabled=False,
style={'display': 'flex', 'width': '500px', 'justifyContent': 'center',
'color': 'white', 'fontWeight': 'bold', 'background-color': 'green',
'margin-left': 'auto', 'margin-right': 'auto',
'margin-top': '10px', 'margin-bottom': '10px'}),
html.Br(),
dbc.Spinner(html.Div(id="loading-output1"), spinner_style={"width": "3rem", "height": "3rem"}),
html.H2("r² score:"),
dcc.Textarea(
id='r2-simple-mlp-textarea',
style={'width': '100%', 'height': 200, 'resize': 'none', 'color': 'white', 'fontWeight': 'bold'},
readOnly=True
),
html.Br(),
html.Div(id='button-output'),
], style={'width': '80%', 'justifyContent': 'center', 'margin-left': 'auto', 'margin-right': 'auto', 'padding': '20px'})
advanced_layout = html.Div([
html.Button('OPTIMIZE KERAS MLP!',
id='run-OPTMLP-button',
disabled=False,
style={'display': 'flex', 'width': '500px', 'justifyContent': 'center',
'color': 'white', 'fontWeight': 'bold', 'background-color': 'red',
'margin-left': 'auto', 'margin-right': 'auto',
'margin-top': '10px', 'margin-bottom': '10px'}),
html.Br(),
dbc.Spinner(html.Div(id="loading-output2"), spinner_style={"width": "3rem", "height": "3rem"}),
html.H2("r² score:"),
dcc.Textarea(
id='r2-opt-mlp-textarea',
style={'width': '100%', 'height': 200, 'resize': 'none', 'color': 'white', 'fontWeight': 'bold'},
readOnly=True
),
html.Br(),
html.H2("Best Hyperparameters:"),
dcc.Textarea(
id='best-hps-textarea',
style={'width': '100%', 'height': 200, 'resize': 'none', 'color': 'white', 'fontWeight': 'bold'},
readOnly=True
),
html.H2("Best Model Architecture:"),
dcc.Textarea(
id='model-summary-textarea',
style={'width': '100%', 'height': 200, 'resize': 'none', 'color': 'white', 'fontWeight': 'bold'},
readOnly=True
),
html.Br(),
html.Div(id='button-output-advanced'),
], style={'width': '80%', 'justifyContent': 'center', 'margin-left': 'auto', 'margin-right': 'auto', 'padding': '20px'})
def create_predict_layout():
predict_layout = html.Div([
html.Div([
html.H5("Input Values (coma separated):"),
dcc.Textarea(id='input-variables-textarea',
value=Input_Columns,
readOnly=True,
style={'width': '50%', 'height': 10, 'resize': 'none', 'color': 'white', 'fontWeight': 'bold'}),
]),
dcc.Textarea(
id='input-text',
value='',
style={'width': '50%', 'height': 10, 'resize': 'none', 'color': 'white', 'fontWeight': 'bold'},
),
html.Br(),
html.Br(),
html.Button('Predict Values!', id='predict-button', n_clicks=0,
style={'color': 'white', 'fontWeight': 'bold', 'background-color': 'green'}),
html.Br(),
html.Br(),
dcc.Textarea(
id='output-text',
readOnly=True,
style={'width': '50%', 'height': 300, 'resize': 'none', 'color': 'white', 'fontWeight': 'bold'},
),
], style={'width': '80%', 'justifyContent': 'center', 'margin-left': 'auto', 'margin-right': 'auto', 'padding': '20px'})
return predict_layout
predict_layout = create_predict_layout()
def parse_contents(contents, filename):
global Dataset
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'xlsx' in filename:
# Assume que é um arquivo Excel
df = pd.read_excel(io.BytesIO(decoded))
elif 'csv' in filename:
# Assume que é um arquivo CSV
df = pd.read_csv(io.BytesIO(decoded))
else:
return html.Div([
'Tipo de arquivo não suportado.'
])
except Exception as e:
print(e)
return html.Div([
'Houve um erro ao processar o arquivo.'
])
Dataset = df
return df
# Callback para atualizar o conteúdo da aba com base na seleção
@app.callback(Output('tabs-content', 'children'),
[Input('tabs', 'value')])
def update_tab_content(selected_tab):
if selected_tab == 'tab1':
return dataset_layout
elif selected_tab == 'tab2':
return simple_layout
elif selected_tab == 'tab3':
return advanced_layout
elif selected_tab == 'tab4':
predict_layout = create_predict_layout()
return predict_layout
# Define o callback para atualizar a caixa de texto de saída
@app.callback(
Output('output-text', 'value'),
[Input('predict-button', 'n_clicks')],
[dash.dependencies.State('input-text', 'value')]
)
def update_output(n_clicks, input_value):
try:
input_data = np.array([list(map(float, input_value.split(',')))])
ypred = PredictValues(input_data)
predicted_str = ""
for i in range(len(ypred)):
valor_formatado = f"{ypred[i]:.2f}"
predicted_str += f"{Output_Columns[i]}: {valor_formatado}\n"
except Exception as e:
predicted_str = ""
return predicted_str
@app.callback(
[Output("loading-output1", "children", allow_duplicate=True),
Output("button-output", "children", allow_duplicate=True),
Output('r2-simple-mlp-textarea', 'value')],
Input("run-MLP-button", "n_clicks"),
prevent_initial_call=True
)
def MLP(n_clicks):
r2_str = RunMLP(Dataset, Input_Columns, Output_Columns)
# Caminho do diretório contendo as imagens
directory_path = 'assets/images'
# Lista para armazenar os componentes de imagem
image_components = []
# Lista de extensões de arquivo para considerar como imagens
image_extensions = ['.jpg', '.jpeg', '.png', '.gif']
# Itera sobre todos os arquivos no diretório
for filename in os.listdir(directory_path):
# Verifica se o arquivo é uma imagem
if any(filename.lower().endswith(ext) for ext in image_extensions):
# Cria o caminho completo do arquivo
file_path = os.path.join(directory_path, filename)
# Cria um componente de imagem e adiciona à lista
image_components.append(html.Img(src=file_path, style={'width': '50%', 'height': 'auto'}))
loading_status = ""
return loading_status, image_components, r2_str
@app.callback(
[Output("loading-output2", "children", allow_duplicate=True),
Output("button-output-advanced", "children", allow_duplicate=True),
Output('best-hps-textarea', 'value'),
Output('model-summary-textarea', 'value'),
Output('r2-opt-mlp-textarea', 'value')],
Input("run-OPTMLP-button", "n_clicks"),
prevent_initial_call=True
)
def OPTMLP(n_clicks):
best_hps_str, model_summary_str, r2_str = RunOptimizedMLP(Dataset, Input_Columns, Output_Columns)
# Caminho do diretório contendo as imagens
directory_path = 'assets/images'
# Lista para armazenar os componentes de imagem
image_components = []
# Lista de extensões de arquivo para considerar como imagens
image_extensions = ['.jpg', '.jpeg', '.png', '.gif']
# Itera sobre todos os arquivos no diretório
for filename in os.listdir(directory_path):
# Verifica se o arquivo é uma imagem
if any(filename.lower().endswith(ext) for ext in image_extensions):
# Cria o caminho completo do arquivo
file_path = os.path.join(directory_path, filename)
# Cria um componente de imagem e adiciona à lista
image_components.append(html.Img(src=file_path, style={'width': '50%', 'height': 'auto'}))
loading_status = ""
return loading_status, image_components, best_hps_str, model_summary_str, r2_str
@app.callback(
Output('column-input-selector', 'options'),
Output('column-input-selector', 'value'),
Input('upload-data', 'contents'),
State('upload-data', 'filename')
)
def update_dropdown(list_of_contents, list_of_names):
if list_of_contents is not None:
df = parse_contents(list_of_contents, list_of_names)
return [{'label': col, 'value': col} for col in df.columns], df.columns.tolist()
return [], []
@app.callback(
Output('column-output-selector', 'options'),
Output('column-output-selector', 'value'),
Input('upload-data', 'contents'),
State('upload-data', 'filename')
)
def update_dropdown(list_of_contents, list_of_names):
if list_of_contents is not None:
df = parse_contents(list_of_contents, list_of_names)
return [{'label': col, 'value': col} for col in df.columns], df.columns.tolist()
return [], []
@app.callback(
Output('input-table', 'columns'),
Output('input-table', 'data'),
Input('column-input-selector', 'value'),
State('upload-data', 'contents'),
State('upload-data', 'filename')
)
def update_table(selected_columns, list_of_contents, list_of_names):
global Input_Columns
if list_of_contents is not None and selected_columns is not None:
df = parse_contents(list_of_contents, list_of_names)
filtered_df = df[selected_columns]
columns = [{"name": col, "id": col} for col in filtered_df.columns]
data = filtered_df.to_dict('records')
Input_Columns = selected_columns
return columns, data
return [], []
@app.callback(
Output('output-table', 'columns'),
Output('output-table', 'data'),
Input('column-output-selector', 'value'),
State('upload-data', 'contents'),
State('upload-data', 'filename')
)
def update_table(selected_columns, list_of_contents, list_of_names):
global Output_Columns
if list_of_contents is not None and selected_columns is not None:
df = parse_contents(list_of_contents, list_of_names)
filtered_df = df[selected_columns]
columns = [{"name": col, "id": col} for col in filtered_df.columns]
data = filtered_df.to_dict('records')
Output_Columns = selected_columns
return columns, data
return [], []
# Roda o app
if __name__ == '__main__':
app.run_server(host='127.0.0.2', port=8080, debug=False)