-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.py
352 lines (289 loc) · 11.2 KB
/
application.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
from flask import Flask
from flask import render_template
from flask import request, redirect, url_for
from flask import send_file
import os
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import r2_score
from sklearn.metrics import accuracy_score
"""""
_ _ ____ _
| | | | ___ _ __ ___ ___ | _ \ ___ _ _| |_ ___
| |_| |/ _ \| '_ ` _ \ / _ \ | |_) / _ \| | | | __/ _ \
| _ | (_) | | | | | | __/ | _ < (_) | |_| | || __/
|_| |_|\___/|_| |_| |_|\___| |_| \_\___/ \__,_|\__\___|
"""""
application = Flask(__name__)
filepath=""
orig_name=""
@application.route('/', methods=['GET', 'POST'])
def index():
global orig_name
filepath = "NOT FOUND"
df = pd.DataFrame()
accuracy=0
final=''
Keymax=''
if request.method == 'POST':
file = request.files['csvfile']
orig_name=file.filename
if not os.path.isdir('static'):
os.mkdir('static')
if os.path.isfile("static/data.csv"):
os.remove("static/data.csv")
filepath = os.path.join('static', file.filename)
newName = "static/data.csv"
file.save(filepath)
fp = os.rename(filepath, newName)
return redirect(url_for('model'))
return render_template('index.html', filepath=filepath, df = df)
fp = os.path.join("static","data.csv")
"""""
__ __ _ _ ____ _
| \/ | ___ __| | ___| | | _ \ ___ _ _| |_ ___
| |\/| |/ _ \ / _` |/ _ \ | | |_) / _ \| | | | __/ _ \
| | | | (_) | (_| | __/ | | _ < (_) | |_| | || __/
|_| |_|\___/ \__,_|\___|_| |_| \_\___/ \__,_|\__\___|
"""""
@application.route('/model/', methods=['GET', 'POST'])
def model():
if os.path.isfile("static/data.csv"):
df = pd.read_csv(fp)
else:
return redirect(url_for('error_page'))
targets = list(df.columns.values)
accuracy=0
final=''
Keymax=''
if request.method == 'POST':
print(fp)
df=pd.read_csv(fp)
targets = list(df.columns.values)
print(targets)
tar=request.form['target']
type=request.form['type']
# Identifying the categotical columns and label encoding them
le = LabelEncoder()
for col in df:
if(df[col].dtype=='object'):
df[col]=le.fit_transform(df[col])
# Identifying the columns with null values and filling them with mean
for col in df:
if(df[col].isnull().sum()!=0):
df[col]=df[col].fillna(df[col].dropna().median())
#Train-Test Split
a=df.pop(tar)
df[tar]=a
x=df.iloc[:,:-1].values
y=df.iloc[:,-1].values
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=1)
#Scaling
sc=StandardScaler()
x_train[:,:]=sc.fit_transform(x_train[:,:])
x_test[:,:]=sc.fit_transform(x_test[:,:])
# /\ /\ |‾| |‾|
# / \ / \ | | | |
# / /\ \ / /\ \ /‾‾‾‾‾\ /‾‾‾‾‾ | /‾‾‾‾‾ \ | |
# / / \ \/ / \ \ | |‾‾‾| | | |‾‾‾| | | |‾‾‾| | | |
# / / \__/ \ \ | | | | | | | | | |‾‾‾ | |
# / / \ \ | |___| | | |___| | | |___|‾| | |
# / / \ \ \_____/ \_____/ \______/ |_|
req = """Flask==1.1.2\ngunicorn==19.9.0\nrequests==2.24.0\nnumpy\npandas\nscikit-learn"""
final=f"""import os
requirements = ['Flask==1.1.2','numpy','pandas','scikit-learn']
with open('requirements.txt', 'w') as f:
for line in requirements:
f.write(line + '\\n')
os.system('pip install -r requirements.txt')
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import joblib
filepath='{orig_name}'
tar= '{tar}'
df=pd.read_csv(filepath)
feature_df = df.drop(tar, axis=1, inplace=False)
for col in feature_df:
if(feature_df[col].dtype=='object'):
feature_df[col]=feature_df[col].str.strip()
data = feature_df.iloc[0].to_json(indent= 2)
with open('data.json', 'w') as f:
f.write('[')
f.write(data)
f.write(']')
# Identifying the categotical columns and label encoding them
le = LabelEncoder()
le1 = LabelEncoder()
number = 1
for col in df:
if(df[col].dtype=='object'):
df[col]=df[col].str.strip()
if(col==tar):
df[col] = le1.fit_transform(df[col])
joblib.dump(le1,"y_encoder.pkl")
else:
df[col]=le.fit_transform(df[col])
temp="x_encoder%s"%number
temp=temp+".pkl"
joblib.dump(le,temp)
number = number + 1
# Identifying the columns with null values and filling them with mean
for col in df:
if(df[col].isnull().sum()!=0):
df[col]=df[col].fillna(df[col].dropna().median())
#Train-Test Split
a=df.pop(tar)
df[tar]=a
x=df.iloc[:,:-1].values
y=df.iloc[:,-1].values
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=1)
#Scaling
sc=StandardScaler()
x_train[:,:]=sc.fit_transform(x_train[:,:])
x_test[:,:]=sc.fit_transform(x_test[:,:])
joblib.dump(sc,"scaler.pkl")
"""
if type=="regression":
#Training various models
score = {}
model1 = LinearRegression()
model1.fit(x_train,y_train)
y_pred1=model1.predict(x_test)
score['LinearRegression()']=(r2_score(y_test, y_pred1))
model2 = DecisionTreeRegressor()
model2.fit(x_train,y_train)
y_pred2=model2.predict(x_test)
score['DecisionTreeRegressor()']=(r2_score(y_test, y_pred2))
model3 = SVR()
model3.fit(x_train,y_train)
y_pred3=model3.predict(x_test)
score['SVR()']=r2_score(y_test, y_pred3)
#Finding the best model
Keymax = max(score, key=score.get)
accuracy=score[Keymax]
if Keymax=="LinearRegression()":
modelstr="""#Training the model
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_train,y_train)
from sklearn.metrics import r2_score
y_pred=regressor.predict(x_test)
accuracy=r2_score(y_test, y_pred)
print("Accuracy:",accuracy*100,"%")
joblib.dump(regressor, 'model.pkl')"""
final+=modelstr
elif Keymax=='DecisionTreeRegressor()':
modelstr="""#Training the model
from sklearn.tree import DecisionTreeRegressor
regressor = DecisionTreeRegressor()
regressor.fit(x_train,y_train)
from sklearn.metrics import r2_score
y_pred=regressor.predict(x_test)
accuracy=r2_score(y_test, y_pred)
print("Accuracy:",accuracy*100,"%")
joblib.dump(regressor, 'model.pkl')"""
final+=modelstr
elif Keymax=="SVR()":
modelstr="""#Training the model
from sklearn.svm import SVR
regressor = SVR()
regressor.fit(x_train,y_train)
from sklearn.metrics import r2_score
y_pred=regressor.predict(x_test)
accuracy=r2_score(y_test, y_pred)
print("Accuracy:",accuracy*100,"%")
joblib.dump(regressor, 'model.pkl')"""
final+=modelstr
elif type=='classification':
#Training various models
score={}
model1 = LogisticRegression()
model1.fit(x_train, y_train)
y_pred1 = model1.predict(x_test)
score['LogisticRegression()']=(accuracy_score(y_test, y_pred1))
model2 = DecisionTreeClassifier(criterion = 'entropy', random_state =0 )
model2.fit(x_train,y_train)
y_pred2 = model2.predict(x_test)
score['DecisionTreeClassifier()']=(accuracy_score(y_test, y_pred2))
model3=RandomForestClassifier()
model3.fit(x_train,y_train)
y_pred3 = model3.predict(x_test)
score['RandomForestClassifier()']=(accuracy_score(y_test, y_pred3))
#Finding the best model
Keymax = max(score, key=score.get)
accuracy=score[Keymax]
if Keymax=="LogisticRegression()":
modelstr="""#Training the model
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(x_train,y_train)
from sklearn.metrics import accuracy_score
y_pred=classifier.predict(x_test)
accuracy=accuracy_score(y_test, y_pred)
print("Accuracy:",accuracy*100,"%")
joblib.dump(classifier, 'model.pkl')"""
final+=modelstr
elif Keymax=='DecisionTreeClassifier()':
modelstr="""#Training the model
from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion = 'entropy', random_state =0 )
classifier.fit(x_train,y_train)
from sklearn.metrics import accuracy_score
y_pred=classifier.predict(x_test)
accuracy=accuracy_score(y_test, y_pred)
print("Accuracy:",accuracy*100,"%")
joblib.dump(classifier, 'model.pkl')"""
final+=modelstr
elif Keymax=="RandomForestClassifier()":
modelstr="""#Training the model
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier()
classifier.fit(x_train,y_train)
from sklearn.metrics import accuracy_score
y_pred=classifier.predict(x_test)
accuracy=accuracy_score(y_test, y_pred)
print("Accuracy:",accuracy*100,"%")
joblib.dump(classifier, 'model.pkl')"""
final+=modelstr
code=open("static/output.py","w")
code.write(final)
os.remove(os.path.join("static","data.csv"))
accuracy = round(accuracy*100, 2)
return render_template('model.html', prediction_text='Trained {} model with {}% accuracy'.format(Keymax, accuracy), targets=targets)
@application.route('/error/')
def error_page():
return render_template("error.html")
@application.route('/return-code/')
def return_code():
try:
return send_file('static/output.py', as_attachment=True, attachment_filename='output.py')
except Exception as e:
return str(e)
@application.route('/return-api/')
def return_api():
try:
return send_file('static/api.py', as_attachment=True, attachment_filename='api.py')
except Exception as e:
return str(e)
@application.route('/return-csv/')
def return_csv_json():
try:
return send_file('static/sample/data.csv', as_attachment=True, attachment_filename='data.csv')
except Exception as e:
return str(e)
@application.route('/documentation/')
def documentation():
return render_template('documentation.html')
if __name__ == '__main__':
application.run(debug=True)