Hands on machine learning deployment using flask in 5 min
By: Smriti Jain
Tags: Deployment, Flask Framework, Data Science, Web Development, Machine Learning

Machine learning is a part of computer science,which helps to find future outcomes it can be predict the results.It provide some insights on the basis of previous stats.

In this blog,i am going to show you hands on demonstration of deploy a machine learning model using the flask on Heroku platform.

For this hands on implementation i am using car price prediction ml model, in which feature engineering and feature selection techniques are applied.

Apply ExtraTreeRegressor model. Extra Trees is an ensemble machine learning algorithm that combines the predictions from many decision trees. Each tree is provided with a random sample of k features from the feature-set from which each decision tree must select the best feature to split the data based on some mathematical criteria. This random sample of features leads to the creation of multiple de-correlated decision trees. Extra tree provisioning features are Year,Volume,mileage,fuel type and transmission.

▶Let’s start the deployment of machine learning model:

⪢First dump your machine learning model into pickle file.For this you need to import a pickle library in your python notebook.Pickle library is used to dump the ml model,Pickling is a way to convert a python object into a character stream this contains all the information necessary to reconstruct the object in another python script.

import pickle

Here i dump my model into “model1.pkl” file.This pickle file contain all the information of ml model, it used in the prediction.

⪢After that create a folder on your local which contain your file,csv file and pickle file.

⪢Create a template folder which contain all html files.

⪢Add app.py file,it use to get data from user, this input helps to predict car value.

Now add this code to your “app.py” file.In this i import various libraries, these libraries produce a final document which displays in users browser.

In this, render_template ->A template is rendered with specific data to produce a final document. Flask uses the Jinja template library to render templates. request ->The requests module in Python allows you to exchange requests on the web.That mean you can input and get the output on web server using GET and POST method. url_for-> use to create a endpoint url.

Apply conditions and convert categorical data into numerical form, provide the route of web on the local host.

⪢Before that insert all html files in template folder and give the placeholder, for linking.For an example create an index.html page and put into the template folder.

In this html file, there are two placeholder first for predict and second is for the dashboard which is on tableau.

Run your app on visual studio and the output will look like this.In this output we can predict the car price.

Result will be redirect on next page using render template,for accessing the result.html file, you can go on my git repository.

Lets bind up all the steps:

1️⃣ Dump ml model into pickle file.

2️⃣ Save all files in the folder. Pickle file , flask file, python file , csv data and add one more template folder; in this contain all html files.

3️⃣ After add code into the python file.

4️⃣ run this python file into your local server and check it works properly or not.

After that add some required file which help to deploy a web on platform,here we deploy a web on Heroku server which is platform as a service.

5️⃣ Add procfile and requirement.txt file into the root directory of your folder.

🔰 Proc file is a text file which explicitly declare what command should be executed to start your app.

🔰The requirements.txt file lists the app dependencies together. When an app is deployed, Heroku reads this file and installs the python dependencies using the pip install -r.

Before moving on to the final step , we create a dashboard on tableau,dashboard is a visualization tool which gives us better understanding of data and help to analyse it.

For this must you have an account on tableau, tableau is give 14 days free trial,you can use it. Upload your data on tableau and create a sheets, after that add your sheets on dashboard. You can add many features as well.

After finish the work of dashboard.

6️⃣ Copy this embedded code. And paste into your html page.Share dashboard on the web.

You can to other settings to make it more visible for others.

7️⃣ After embedding the code into the html page, create github repository and clone into local. Add all files and push it .

8️⃣ Open Heroku login page and follow some steps to deploy your final web-app.

Go to create app▶give the unique name to your app▶connect to github▶find the repository which you create ▶enable automatic deploys (if you do any changes in your github it will do automatic changes)▶deploy branch.

Wait for a while it takes time, after some time you can see deployment is done and you can able to access your Heroku web-app.

💥💥💥Now you have a link to access your first ml base web-app.😍😍

And its done,in this web app you can predict the car value and as well if you have access then you can see the dashboard.

So what we did, in just 8 steps we use ml model,deploy it,create html files for template and make web interactive.Use flask frame work to build a web on local server.Use tableau to add extra feature provide visualization of data🤩 to authorized users,push all these files onto the github and connect github repo to Heroku and deploy model.🥳🥳

Follow me on my github.

Follow me on linkdin. #connect network #share knowledge #grow.

#machine learning #deployment #web technology #flask #html # python programming #data science