Diabetes Prediction Analysis in Python and it’s implementation in Flask-Part 2

Devleena Banerjee
3 min readJul 1, 2020

--

Photo by Sharon McCutcheon on Unsplash

This is the second part of the Diabetes Prediction Model and it’s implementation in Flask. I hope you have already gone through Part-1.

Once the Model is created, we need to convert the model to a format that allows sharing or storage in the disk in a form that allows to read it in its original format. In order to do that, we use Pickle module. Install the Pickle module in Python using the following command:

pip install pickle-mixin
Saving the model in ‘model.pkl’ file

Here, we used the model “modelRFnew” which is the new Random Forest model we created; model.pkl is the file saved on the disk and ‘wb’ stands for opening for Writing in Binary mode.

This file will be saved in the same folder where you have saved your notebook.

This model needs to be shared with users or clients, we need to create an interface or tool where in user can input the values to get the Predictions. That is where we can use Flask. Flask is a micro web framework written in Python. It allows you to send data, and receive the prediction as a response.

Now install Flask in Python with the following command:

pip install Flask

I have worked on Spyder to create app.py

import numpy as np
from flask import Flask, request, jsonify, render_template
import pickle

app = Flask(__name__)
model = pickle.load(open(‘model.pkl’, ‘rb’))

@app.route(‘/’)
def home():
return render_template(‘index.html’)

app = Flask(__name__): We are creating an instance of the Flask class and calling it app.

model = pickle.load(open(‘model.pkl’, ‘rb’)) : Loading the pickle file in Read in Binary mode.

render_template(‘index.html’) : This will load index.html as homepage from the Template Folder

The Flask Framework looks for HTML files in a folder called templates. You need to create a templates folder and “index.html’ file there.

In order to add CSS to the HTML, we will create a folder called static.Create another folder called css inside static.

The Prediction API starts from

@app.route(‘/predict’,methods=[‘POST’])
def predict():

float_features = [float(x) for x in request.form.values()]
final_features = [np.array(float_features)]
prediction = model.predict(final_features)

output = round(prediction[0], 2)
if (output ==1):
text= “You have Diabetes”
else:
text= “Hurray!! No Diabetes”
return render_template(‘’index.html’, prediction_text=’Diabetes Prediction : {}’.format(text))

In our method definition we the request method to be POST and so we can access its data. We parse the variable to float.

We then call the predict_api(), in which the jsonify method of Flask sends the response data as JSON.

@app.route(‘/predict_api’,methods=[‘POST’])
def predict_api():

data = request.get_json(force=True)
prediction = model.predict([np.array(list(data.values()))])

output = prediction[0]
return jsonify(output)

if __name__ == “__main__”:
app.run(debug=True)

Once this is done, I opened Anaconda prompt, navigated to the folder where my app.py is placed. Then executed the command : app.py

(base) E:\Machine Learning\Kaggle\Diabetes Prediction model>app.py
* Serving Flask app “app” (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with windowsapi reloader
* Debugger is active!
* Debugger PIN: 309–793–396
* Running on
http://127.0.0.1:5000/ (Press CTRL+C to quit)

Open browser( I checked on Chrome and Firefox), and navigate to the URL

http://127.0.0.1:5000/ . Finally I got my interface which looks like this :

Final Prediction on Browser using Flask, HTML

You can visit my GitHub Profile for the complete code.

I hope you liked this article. Please give a clap, share and leave a comment.

In case you have any suggestions, please do share.

References

--

--

No responses yet