Coders Packet

Making a web application and deploying Machine Learning models using Flask


In this project, I trained a machine learning model for Diabetes prediction and then used the trained model to make a web application using the Flask Python framework.

Flask is a framework that is used for creating web frameworks using Python. It provides all the necessary tools and is also lightweight, making it quick to set up and deploy.

In this project, I first created a Machine Learning model for Diabetes classification and then 'pickled' the model using Pickle. Pickle is a Python module used for flattening the model’s structure so that it can be used on the web app. After that, I built a web app to deploy the trained model using Flask. Below is the step by step guide for the whole implementation:-


1) First create a machine learning model using a certain ML algorithm.

2) After the model has been created pickle your model using the code given below

import pickle

file = 'diabetes-model.pkl' # Naming the file pickle.dump(model, open(file,'wb')) # Saving the pickled file model = pickle.load(open('diabetes-model.pkl','rb')) # Loading the pickled file

3) Now, create a root folder in which you will store all the required files. Make sure you place the pickled file i.e. with the .pkl extension in that folder.

4) In the root folder create three folders: static, with a folder css inside it, and templates.

NOTE: I would strongly recommend installing 'Anaconda' before doing this. Anaconda is a package with all the machine learning and other python libraries already installed and is a brilliant and versatile platform. If you happen to have Anaconda already installed on your system then all the required python libraries are already installed in it.

If you are doing it on Windows first u have to install python 3 to the command prompt and then go ahead with the upcoming steps

5) Now we create three more files :

i) in the root for storing the Python code needed for the web page to function.
ii) index.html in templates directory for the HTML code to be displayed on the web page.
iii) styles.css in static/css directory for the CSS code to add some aesthetics to the app.

6) Finally run the command python

Now your web server will be hosted at the given address. Just copy and paste it into your browser and then you will be able to fill the form at this address.

My finalized application looks like this:-

This is how you can make your own web app for your own ML model and deploy it on the web using Flask.

Download Complete Code


No comments yet