Most of the beginners start their Data Science journey from Jupyter notebook but they get stuck while representing their model results to non-technical people. We can’t show some fancy python code to a non-technical person. So for that we need something more graphical and user-friendly like a website. We can deploy our machine learning model to a web page where user can give input parameters and get the predicted value on the screen.

For this tutorial, we’ll be using Flask as a framework. We’ll take the data from the user input form on the screen and send it to the model and show the results on the user screen. The dataset which is used here is of Mental Health in Tech Survey. It’s available on Kaggle - Dataset Link

Let’s get started. First we need to build the model. You can use any classification model for this problem. I have made a notebook for this. In that I have some Data Analysis as well as modeling. You can check it out on this link.

After building the model we’ll dump it into a pickle file using the following code:

import pickle
pickle.dump(model, open('model.pkl', 'wb'))

Now we can access the model using this pickle file. After dumping the model we need to create a flask web app. For front-end, we’ll jinja2 templates. In the html page, we’ll create a form where we allow the user to give the features values for the model. After inserting the values, when user submits these value will go to the post method of flask backend. Following is the code:

def ValuePredictor(to_predict_list):
	to_predict = np.array(to_predict_list).reshape(1,24)
	print (to_predict)
	loaded_model = pickle.load(open("model.pkl","rb"))
	result = loaded_model.predict(to_predict)
	return result[0]

@app.route('/',methods = ['POST','GET'])
def result():
	if request.method == 'POST':
		to_predict_list = request.form.to_dict()
		to_predict_list=list(to_predict_list.values())
		to_predict_list = list(map(int, to_predict_list))
		print (to_predict_list)
		result = ValuePredictor(to_predict_list)
		if int(result)==1:
			prediction='Yes, Leave can be given'
		else:
			prediction='No, model do not suggest to give leave'
		return render_template("result.html",prediction=prediction)
	else:
		return flask.render_template('index.html')

Let’s understand this code in depth. We can get the input form values using the following lines of code:

to_predict_list = request.form.to_dict()
to_predict_list=list(to_predict_list.values())
to_predict_list = list(map(int, to_predict_list))

Then we pass this list of values to a function which returns the predicted output. Our ValuePredictor function loads the model pickle file and pass the input values to its predict function.

to_predict = np.array(to_predict_list).reshape(1,24)
loaded_model = pickle.load(open("model.pkl","rb"))
result = loaded_model.predict(to_predict)

After getting the result, we’ll render another jinja template and pass the output to it. It’ll show the output result to the end-user. This was a simple way to represent your model to everyone. Hope you guys liked it. :) Following is the link for complete code with model and flask app:

Source