def start_train(project_id): if not current_user.is_authenticated: print('not logged in') return redirect(url_for('login')) content = None data = None data = Project.from_user(current_user.user_id) project_specific_data = [] html = None titles = None if Project.check_auth(current_user.user_id, int(project_id)): project_specific_data = Project.get_one(current_user.user_id, int(project_id)) if project_specific_data[0]['model_available']: return jsonify(result='trained') q.enqueue(DLModel.train_model, project_specific_data[0]['dataset'][0]['name'], int(project_id), app.config['UPLOAD_FOLDER']) Database.update_one(collection='projects', query=[{ 'project_id': int(project_id) }, { "$set": { "in_training": True } }]) return jsonify(result='done') else: return jsonify(result='error')
def edit_profile(): if 'user' in session: form = updateProfileForm() if form.validate_on_submit(): Database.update_one(collection="users", query=[{'user_id': session['user'].uid}, {"$set": {"uname": form.username.data, "fname": form.fname.data, "lname": form.lname.data, "bio": form.bio.data, "phone": form.phone.data, "address": form.address.data, "city": form.city.data, "zipcode": form.zipcode.data, "state": form.state.data}}]) flash('Your profile has been updated.') # change session username to newone once update the database session['user'].name = form.username.data # print(session['user'].name) # return to myprofile page userProfile = User.get_by_id(session['user'].uid) return render_template('/myprofile.html', user=userProfile) elif request.method == 'GET': userProfile = User.get_by_id(session['user'].uid) form.username.data = userProfile.uname form.fname.data = userProfile.fname form.lname.data = userProfile.lname form.bio.data = userProfile.bio form.phone.data = userProfile.phone form.address.data = userProfile.address form.city.data = userProfile.city form.zipcode.data = userProfile.zip form.state.data = userProfile.state return render_template('/updateProfile.html', title='Update', form=form) else: return redirect(url_for('web.index'))
def start_train(project_id): """ STRICT API To allow ADMIN user to force sart training for the Deep Leanring Model Allows an ADMIN user to start traning the user Deep LEarning model customer's activities. \n\n API/URL must be accessed with GET request and supply project_id the URL\n method: GET\n Args: project_id (str): ID of the poject/Customer need to be sent in url. It is made to do so via Front end href Returns: response: JSON object On Success \n response = { 'result': 'done' } \n On Fail:\n response = { 'result': 'error' } \n """ if not current_user.is_authenticated: print('not logged in') return redirect(url_for('login')) content = None data = None data = Project.from_user(current_user.user_id) project_specific_data = [] html = None titles = None if Project.check_auth(current_user.user_id, int(project_id)): project_specific_data = Project.get_one(current_user.user_id, int(project_id)) if project_specific_data[0]['model_available']: return jsonify(result='trained') q.enqueue(DLModel.train_model, project_specific_data[0]['dataset'][0]['name'], int(project_id), app.config['UPLOAD_FOLDER']) Database.update_one(collection='projects', query=[{ 'project_id': int(project_id) }, { "$set": { "in_training": True } }]) return jsonify(result='done') else: return jsonify(result='error')
def updatefname(uname, fname): Database.update_one(collection="users", query=[{ 'uname': uname }, { "$set": { "fname": fname } }])
def change_password(): if 'user' in session: form = changePasswordForm() if form.validate_on_submit(): #update the password Database.update_one(collection="users", query=[{'uname':session['user'].name},{"$set":{"password":form.password.data}}]) flash('Your password has been changed.') #back to profile page userProfile = User.get_by_username(session['user'].name) return render_template('/myprofile.html', user=userProfile) return render_template('/changePassword.html', title='Change Password', form=form) else: return redirect(url_for('web.index'))
def auth_verify_email(user_id, email_token): """ This is used to verfiy user via the link the receive on their email. \n\n API/URL must be accessed with GET request and supply user_id and email_token in the URL\n method: GET\n Args: user_id (str): ID of the poject/Customer need to be sent in url. It is made to do so via email template email_token (str): UUID generated email token need to be sent in url. It is made to do so via email template Returns: response: JSON object On Success \n response = { 'status': 'success', 'message': 'Email verified' } \n On Fail:\n response = { 'status': 'fail', 'message': 'Email already verified' } \n """ user = User.get_by_id(int(user_id)) if user.is_email_verified: responseObject = { 'status': 'fail', 'message': 'Email already verified' } return make_response(jsonify(responseObject)), 202 email_auth_data = Database.find_one(collection='email_token', query={'user_id': int(user_id)}) if email_auth_data['email_token'] == email_token: Database.update_one(collection="users", query=[{ 'user_id': int(user_id) }, { "$set": { "is_email_verified": True } }]) responseObject = {'status': 'success', 'message': 'Email verified'} return make_response(jsonify(responseObject)), 201
def auth_verify_email(user_id, email_token): user = User.get_by_id(int(user_id)) if user.is_email_verified: responseObject = { 'status': 'fail', 'message': 'Email already verified' } return make_response(jsonify(responseObject)), 202 email_auth_data = Database.find_one(collection='email_token', query={'user_id': int(user_id)}) if email_auth_data['email_token'] == email_token: Database.update_one(collection="users", query=[{'user_id': int(user_id)}, {"$set": { "is_email_verified": True }} ]) responseObject = { 'status': 'success', 'message': 'Email verified' } return make_response(jsonify(responseObject)), 201
def reset_token(token): """ This is used to reset user/admin password after they click rest link method: POST, GET\n GET: will render the web page Args: token (token): UUID generated token Returns: redirect: for login """ user = User.verify_reset_token(token) if user is None: flash('An invalid token', 'warning') return redirect(url_for('web.reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): pw_hash = form.password.data Database.update_one(collection="users", query=[{ 'user_id': user.user_id }, { "$set": { "password": pw_hash } }]) flash('Your password has been updated! you are now able to login') return redirect(url_for('web.login')) return render_template('pages/reset_token.html', title='Reset password', form=form)
def train_model(cls, csv_path, project_id, upload_dir): model_id = int(str(uuid.uuid4().int)[:6]) df = pd.read_csv(upload_dir + "dataset/" + csv_path, parse_dates=['timestamp'], index_col="timestamp") f_columns = DLModel.get_numerical(df) print(f_columns) df['hour'] = df.index.hour df['day_of_month'] = df.index.day df['day_of_week'] = df.index.dayofweek df['month'] = df.index.month train_size = int(len(df) * 0.9) test_size = len(df) - train_size train, test = df.iloc[0:train_size], df.iloc[train_size:len(df)] print(len(train), len(test)) f_transformer = RobustScaler() cnt_transformer = RobustScaler() f_transformer = f_transformer.fit(train[f_columns].to_numpy()) cnt_transformer = cnt_transformer.fit(train[['cnt']]) train.loc[:, f_columns] = f_transformer.transform( train[f_columns].to_numpy()) train['cnt'] = cnt_transformer.transform(train[['cnt']]) test.loc[:, f_columns] = f_transformer.transform( test[f_columns].to_numpy()) test['cnt'] = cnt_transformer.transform(test[['cnt']]) time_steps = 10 print("Data Preprocessing Done") # reshape to [samples, time_steps, n_features] X_train, y_train = cls.create_dataset(train, train.cnt, time_steps) X_test, y_test = cls.create_dataset(test, test.cnt, time_steps) print(X_train.shape, y_train.shape) print("Data spliting done") model = keras.Sequential() model.add( keras.layers.Bidirectional( keras.layers.LSTM(units=128, input_shape=(X_train.shape[1], X_train.shape[2])))) model.add(keras.layers.Dropout(rate=0.2)) model.add(keras.layers.Dense(units=1)) model.compile(loss='mean_squared_error', optimizer='adam') print("Traning") history = model.fit(X_train, y_train, epochs=30, batch_size=32, validation_split=0.1, shuffle=False) print("Predicting") y_pred = model.predict(X_test) y_train_inv = cnt_transformer.inverse_transform(y_train.reshape(1, -1)) y_test_inv = cnt_transformer.inverse_transform(y_test.reshape(1, -1)) y_pred_inv = cnt_transformer.inverse_transform(y_pred) y_train_inv = cnt_transformer.inverse_transform(y_train.reshape(1, -1)) y_test_inv = cnt_transformer.inverse_transform(y_test.reshape(1, -1)) y_pred_inv = cnt_transformer.inverse_transform(y_pred) plt.plot(np.arange(0, len(y_train)), y_train_inv.flatten(), 'g', label="history") plt.plot(np.arange(len(y_train), len(y_train) + len(y_test)), y_test_inv.flatten(), marker='.', label="true") plt.plot(np.arange(len(y_train), len(y_train) + len(y_test)), y_pred_inv.flatten(), 'r', label="prediction") plt.ylabel('Bike Count') plt.xlabel('Time Step') plt.legend() model_pred_with_train = str(project_id) + "_" + str( model_id) + "_with_train.png" plt.savefig(upload_dir + "images/" + model_pred_with_train) plt.clf() plt.close() plt.plot(y_test_inv.flatten(), marker='.', label="true") plt.plot(y_pred_inv.flatten(), 'r', label="prediction") plt.ylabel('Bike Count') plt.xlabel('Time Step') plt.legend() model_pred_test = str(project_id) + "_" + str(model_id) + "_test.png" plt.savefig(upload_dir + "images/" + model_pred_test) plt.clf() plt.close() rmse = mean_squared_error( np.reshape(y_test_inv, (max(y_test_inv.shape[0], y_test_inv.shape[1]))), np.reshape(y_pred_inv, (max(y_pred_inv.shape[0], y_pred_inv.shape[1]))), squared=False) smape = DLModel.smape( np.reshape(y_test_inv, (max(y_test_inv.shape[0], y_test_inv.shape[1]))), np.reshape(y_pred_inv, (max(y_pred_inv.shape[0], y_pred_inv.shape[1])))) nrmse = rmse / (max( np.reshape(y_test_inv, (max(y_test_inv.shape[0], y_test_inv.shape[1])))) - min( np.reshape(y_test_inv, (max(y_test_inv.shape[0], y_test_inv.shape[1]))))) # Fetch the Keras session and save the model # The signature definition is defined by the input and output tensors, # and stored with the default serving key print("Saving") export_path = os.path.join(upload_dir + "models/", str(model_id)) print('export_path = {}\n'.format(export_path)) tf.keras.models.save_model(model, export_path, overwrite=True, include_optimizer=True, save_format=None, signatures=None, options=None) print('\nSaved model') DLModel.initialize_database() new_model = cls(project_id, csv_path, model_id, metric={ "rmse": rmse, "smape": smape, "nrmse": nrmse }, prediction_img={ "model_pred_test": model_pred_test, "model_pred_with_train": model_pred_with_train }, model_path=export_path) new_model.save_to_mongo() Database.update_one(collection='projects', query=[{ 'project_id': int(project_id) }, { "$set": { "model_available": True } }])