Abdulabin commited on
Commit
7487a04
·
verified ·
1 Parent(s): e7cf3a9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +376 -0
app.py ADDED
@@ -0,0 +1,376 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import evalml
3
+ import pandas as pd
4
+ import streamlit as st
5
+ import plotly.graph_objects as go
6
+ from evalml.automl import AutoMLSearch
7
+ from evalml.data_checks import DefaultDataChecks
8
+ from streamlit_option_menu import option_menu
9
+ from evalml.problem_types import detect_problem_type
10
+ from evalml.automl import get_default_primary_search_objective
11
+ from evalml.objectives import get_optimization_objectives
12
+ from time_series_splitter import split_time_series_data
13
+ from evalml.automl import get_default_primary_search_objective
14
+ from evalml.objectives import get_optimization_objectives
15
+ from evalml.model_understanding.metrics import confusion_matrix
16
+ from evalml.model_understanding.prediction_explanations import explain_predictions
17
+ from evalml.model_understanding import graph_prediction_vs_actual_over_time
18
+
19
+
20
+ st.set_page_config(None,layout="wide")
21
+ st.title("All_in_one_Model")
22
+ problem_types = ['BINARY', 'MULTICLASS', 'REGRESSION',]
23
+ time_problems = ['TIME SERIES REGRESSION','TIME SERIES BINARY', 'TIME SERIES MULTICLASS']
24
+
25
+
26
+
27
+
28
+ prob = option_menu(None, options=["General Problem","Time Series Problem"],
29
+ menu_icon="cast", default_index=0, orientation="horizontal",)
30
+
31
+
32
+ uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
33
+
34
+ if prob =="General Problem":
35
+ if uploaded_file is not None:
36
+ df = pd.read_csv(uploaded_file)
37
+ with st.container():
38
+ col1 ,col2 ,col3 = st.columns(3)
39
+
40
+ with col1:
41
+ y_col = st.selectbox("Select Output Columns",options=df.columns,index= (df.shape[1])-1,)
42
+ X = df.drop(y_col,axis=1)
43
+ y= df[y_col]
44
+
45
+ with col2:
46
+ detect_prob_type = detect_problem_type(y).name
47
+ problem_type = st.selectbox("Select Problem Type",options=problem_types,index= problem_types.index(detect_prob_type),)
48
+
49
+ with col3:
50
+ objective_auto = get_default_primary_search_objective(problem_type).name
51
+
52
+ objectives = []
53
+ for objective in get_optimization_objectives(problem_type):
54
+ objectives.append(objective.name)
55
+
56
+ objective_type = st.selectbox("Select Objective Type",options=objectives,index=objectives.index(objective_auto))
57
+
58
+
59
+
60
+ data_checks = DefaultDataChecks(problem_type, objective_type)
61
+ check = data_checks.validate(X,y)
62
+
63
+ if uploaded_file is not None:
64
+
65
+ if len(check) == 0:
66
+ st.success("Data Check Successful")
67
+ else:
68
+ st.error("Data Check Unsuccessful")
69
+
70
+
71
+ X_train, X_test, y_train, y_test = evalml.preprocessing.split_data(X, y,
72
+ problem_type=problem_type,
73
+ test_size=0.2,)
74
+
75
+
76
+ automl = AutoMLSearch(X_train=X_train,
77
+ y_train=y_train,
78
+ X_holdout=X_test,
79
+ y_holdout=y_test,
80
+ problem_type=problem_type,
81
+ allow_long_running_models=False,
82
+ objective=objective_type,
83
+ max_batches=3,
84
+ ensembling=True,
85
+ verbose=True
86
+ )
87
+
88
+
89
+ selected = option_menu(None, options=["Home", "Predict","Model_info","Graphs","XAI"],
90
+ icons=['house', 'person',"card-text", "list-task", 'chat-left-text-fill'], menu_icon="cast", default_index=0, orientation="horizontal",)
91
+
92
+
93
+ if selected == "Home":
94
+ train = st.button("Train Model ")
95
+
96
+
97
+ if train:
98
+ st.write("Training Model ....")
99
+ automl.search()
100
+ best_pipeline = automl.best_pipeline
101
+ best_pipeline.save("best_model.pkl")
102
+ load_model=automl.load('best_model.pkl')
103
+ st.dataframe(automl.rankings)
104
+
105
+
106
+ y_pred = load_model.predict(X_test)
107
+ st.write(load_model.score(X_test, y_test, objectives=objectives))
108
+ try:
109
+ y_pred = load_model.predict(X_test)
110
+ st.dataframe(confusion_matrix(y_test, y_pred))
111
+ except:
112
+ pass
113
+
114
+ if selected =="Predict":
115
+ try:
116
+ load_model=automl.load('best_model.pkl')
117
+ df1= df.drop(y_col,axis=1)
118
+ column_names = df1.columns
119
+ column_data_types = df1.dtypes
120
+
121
+ st.subheader("Enter Data:")
122
+ col1, col2 = st.columns(2)
123
+ new_row_data = {}
124
+ for col_name, col_type in zip(column_names, column_data_types):
125
+ if col_type == 'object':
126
+
127
+ with col1:
128
+ input_data = st.text_input(f"Enter {col_name}")
129
+ elif col_type == 'int64' or col_type == 'float64':
130
+
131
+ with col2:
132
+ input_data = st.number_input(f"Enter {col_name}")
133
+
134
+ else:
135
+
136
+ input_data = None
137
+ st.write(f"Data type {col_type} not supported for column {col_name}")
138
+
139
+
140
+ new_row_data[col_name] = input_data
141
+
142
+ if st.button("Predict"):
143
+ load_model=automl.load('best_model.pkl')
144
+ new_df = pd.DataFrame([new_row_data],columns=column_names)
145
+ st.dataframe(new_df,hide_index=True,width=1200)
146
+ pred = load_model.predict(new_df)
147
+ st.success(f"Predicted Value : {pred.values[0]}")
148
+
149
+
150
+ except:
151
+ st.write("Please Train Model first ..")
152
+
153
+ if selected == "Model_info":
154
+ try:
155
+ load_model=automl.load('best_model.pkl')
156
+ st.write(load_model.describe(return_dict=True))
157
+ except:
158
+ st.write("Please Train Model first ..")
159
+
160
+
161
+ if selected == "Graphs":
162
+ try:
163
+ pass
164
+ except:
165
+ st.write("Please Train Model first ..")
166
+
167
+ if selected == "XAI":
168
+ try:
169
+ with st.container():
170
+ col1 ,col2 = st.columns(2)
171
+ with col1:
172
+ load_model=automl.load('best_model.pkl')
173
+ st.dataframe(load_model.feature_importance)
174
+ with col2:
175
+
176
+ table = explain_predictions(
177
+ pipeline=load_model,
178
+ input_features=X_test,
179
+ y=None,
180
+ output_format="dataframe",
181
+ indices_to_explain=[3],
182
+ top_k_features=8,
183
+ include_explainer_values=True,
184
+ algorithm='shap'
185
+ )
186
+ st.subheader("Positive and Negative contributing features")
187
+ st.dataframe(table)
188
+
189
+
190
+
191
+ except:
192
+ st.write("Please Train Model first ..")
193
+
194
+ if prob == "Time Series Problem":
195
+ if uploaded_file is not None:
196
+ df = pd.read_csv(uploaded_file)
197
+ with st.container():
198
+ col1 ,col4,col2 ,col3 = st.columns(4)
199
+
200
+ with col1:
201
+ y_col = st.selectbox("Select Output Column",options=df.columns,index= (df.shape[1])-1,)
202
+ X = df.drop(y_col,axis=1)
203
+ y= df[y_col]
204
+
205
+ with col4:
206
+ def date_column_name(X):
207
+ date_column = None
208
+ for column in X.columns:
209
+ if pd.api.types.is_datetime64_any_dtype(X[column]):
210
+ date_column = column
211
+ break
212
+ if date_column is None:
213
+ raise ValueError("No datetime column found in the DataFrame.")
214
+ return date_column
215
+ try:
216
+ pred_date = date_column_name(df)
217
+ except:
218
+ pred_date = df.columns[0]
219
+
220
+ date_col = st.selectbox("Select Date Column",options=X.columns,index=X.columns.get_loc(pred_date))
221
+ df[date_col] = pd.to_datetime(df[date_col])
222
+
223
+ with col2:
224
+ detect_prob_type = detect_problem_type(y).name
225
+ try:
226
+ problem_type = st.selectbox("Select Problem Type",options=time_problems,index= time_problems.index(detect_prob_type),)
227
+ except:
228
+ problem_type = st.selectbox("Select Problem Type",options=time_problems,index= 0,)
229
+
230
+
231
+ with col3:
232
+ objective_auto = get_default_primary_search_objective(problem_type).name
233
+
234
+ objectives = []
235
+ for objective in get_optimization_objectives(problem_type):
236
+ objectives.append(objective.name)
237
+
238
+ objective_type = st.selectbox("Select Objective Type",options=objectives,index=objectives.index(objective_auto))
239
+
240
+
241
+
242
+ X_train, X_test, y_train, y_test = split_time_series_data(date_col,X,y,test_size=0.1)
243
+
244
+
245
+ with st.container():
246
+ col1 ,col2 ,col3= st.columns(3)
247
+ with col1:
248
+ gap = st.number_input('Select Gap in Data ',value= 0,min_value=0,max_value=10)
249
+ with col2:
250
+ max_delay = st.number_input('Select Max Delay',value=7,min_value=3,)
251
+ with col3:
252
+ forecast_horizon = st.number_input('Select forecast horizon ',value=7)
253
+
254
+
255
+ if problem_type=='TIME SERIES REGRESSION':
256
+ algo_families =["All Algorithm",'RANDOM_FOREST','XGBOOST','LIGHTGBM','LINEAR_MODEL','EXPONENTIAL_SMOOTHING','ARIMA','PROPHET',]
257
+
258
+ elif problem_type=='TIME SERIES BINARY':
259
+ algo_families =["All Algorithm",'RANDOM_FOREST','XGBOOST','LIGHTGBM','LINEAR_MODEL',"EXTRA_TREES"]
260
+
261
+
262
+ elif problem_type=='TIME SERIES MULTICLASS':
263
+ algo_families =["All Algorithm",'RANDOM_FOREST','XGBOOST','LIGHTGBM','LINEAR_MODEL',"EXTRA_TREES"]
264
+
265
+
266
+ algo_select = st.multiselect(
267
+ 'Select Algorithm',
268
+ algo_families,
269
+ default=["All Algorithm"],
270
+ )
271
+ if "All Algorithm" in algo_select or len(algo_select)==0:
272
+ algo_select=None
273
+
274
+
275
+
276
+ problem_config = {"gap": gap, "max_delay": max_delay, "forecast_horizon": forecast_horizon, "time_index":date_col }
277
+
278
+ automl_time = AutoMLSearch(
279
+ X_train,
280
+ y_train,
281
+ problem_type = problem_type,
282
+ objective=objective_type,
283
+ max_batches=1,
284
+ ensembling=True,
285
+ problem_configuration=problem_config,
286
+ automl_algorithm="iterative",
287
+ allowed_model_families=algo_select
288
+ )
289
+
290
+ with st.container():
291
+ col1 ,col2 ,col3= st.columns([2,2,6])
292
+ with col1:
293
+ train_time = st.button("Train Time Series Model....")
294
+ with col2:
295
+ load_time = st.button("Load Best Time Series Model....")
296
+
297
+ if train_time:
298
+ st.write("Model Training.....")
299
+ automl_time.search()
300
+ st.dataframe(automl_time.rankings)
301
+
302
+ best_pipeline_time = automl_time.best_pipeline
303
+ best_pipeline_time.save("best_model_time.pkl")
304
+
305
+
306
+
307
+ if load_time:
308
+ try:
309
+ load_model_time=automl_time.load('best_model_time.pkl')
310
+
311
+ st.write(load_model_time.describe(return_dict=True))
312
+
313
+ load_model_time.fit(X_train, y_train)
314
+
315
+ best_pipeline_score = load_model_time.score(X_test, y_test, [objective_type], X_train, y_train)[
316
+ objective_type]
317
+
318
+ st.write(f"{objective_type} : {best_pipeline_score}")
319
+
320
+ fig = graph_prediction_vs_actual_over_time(load_model_time, X_test, y_test, X_train, y_train, dates=X_test[date_col])
321
+ fig.update_layout(height=700)
322
+ st.plotly_chart(fig,use_container_width=True,height=200)
323
+
324
+ st.header("Predict Next 7 Days")
325
+
326
+ load_model_time.fit(X, y)
327
+ X_forecast_dates = load_model_time.get_forecast_period(X=X).to_frame()
328
+ y_forecast = load_model_time.get_forecast_predictions(X=X, y=y)
329
+ res = load_model_time.get_prediction_intervals(X=pd.DataFrame(X_forecast_dates), y=y_forecast, X_train=X, y_train=y)
330
+ y_lower = res["0.95_lower"]
331
+ y_upper = res["0.95_upper"]
332
+ X_before = X[-31:]
333
+ y_before = y[-31:]
334
+
335
+
336
+
337
+ fig2 = go.Figure(
338
+ [
339
+ go.Scatter(x=X_before[date_col], y=y_before, name="Training Data", mode="lines"),
340
+ go.Scatter(
341
+ x=X_forecast_dates[date_col], y=y_forecast, name="Forecast Data", mode="lines"
342
+ ),
343
+ go.Scatter(
344
+ x=pd.concat([X_forecast_dates[date_col], X_forecast_dates[date_col][::-1]]),
345
+ y=pd.concat([y_upper, y_lower[::-1]]),
346
+ fill="toself",
347
+ fillcolor="rgba(255,0,0,0.2)",
348
+ line=dict(color="rgba(255,0,0,0.2)"),
349
+ name="Forecast Prediction Intervals",
350
+ showlegend=True,
351
+ ),
352
+ ],
353
+ layout={
354
+ "title": "Plot of Last Two Weeks of Data + Forecast Data With Prediction Intervals",
355
+ "xaxis": dict(title=date_col),
356
+ "yaxis": dict(title=y_col),
357
+ },
358
+ )
359
+
360
+ fig2.update_layout(height=600)
361
+ st.plotly_chart(fig2,use_container_width=True)
362
+ except:
363
+ st.write("Please Train Model First....")
364
+
365
+
366
+
367
+ file_path = os.path.join('best_model.pkl')
368
+ file_path2 = os.path.join('best_model_time.pkl')
369
+ if uploaded_file == None:
370
+ try:
371
+ if prob == "General Problem":
372
+ os.remove(file_path)
373
+ elif prob == "Time Series Problem":
374
+ os.remove(file_path2)
375
+ except:
376
+ pass