ML model deployment with Flask on localhost - PART I

Hrisav Bhowmick
Analytics Vidhya
Published in
4 min readMay 25, 2021

In this 2-article series, we would like to discuss how to deploy a machine learning model with Flask on localhost (using Python localhost Server) and on webhost (using Amazon-EC2). In this Part I, we will show how to build the model, design the HTML/CSS page and finally run it on localhost.

1. Build the Machine Learning model

The objective of the problem is to identify the customers who are eligible for a home loan based on the input that they have provided like Gender, Marital Status, Education, Number of Dependents, Income, Loan Amount, Credit History and others.

The dataset is ‘loan_acceptance_data.csv’. We will use PyCharm IDE to do the entire project. First, create a virtual environment and give a project name. In our case, it is ‘loan-approval-new’. Now, make a python file where the model building part will be done.

Save the file as ‘loan.py’.

The steps we are doing here are:

  • Imputing missing values
  • Feature Engineering
  • Labeling categorical columns
  • Selecting required features
  • Train test split
  • Model building using Logistic Regression
  • Creating the pickle file
file = open('loan_approval.pkl', 'wb')
pickle.dump(model, file)

The above two lines of code will create a pickle file by the name ‘loan_approval.pkl’.

2. Make the HTML/CSS frontend file

Design the HTML file which will have the content which will be shown on the website. CSS part can also be added to the same file, to provide some design to the webpage.

Here we are creating a form where the user will provide his input details.

Save the file as ‘index.html’.

3. Make the Python/Flask backend file

Here we will create a Python file that will get the values from HTML file and on the basis of the inputs, it will perform the operations and finally predict the outcome. We need to have Flask installed in the system to run this.

Here’s the code snippet to initialize the Flask app.

from flask import Flask, render_template, request
import pickle
import numpy as np
model = pickle.load(open('loan_approval.pkl', 'rb')) # read modeapp = Flask(__name__) # initializing Flask app

Get the details of index.html through the below code snippet.

@app.route("/",methods=['GET'])
def hello():
return render_template('index.html')

Now the final part where we are doing the prediction.

Note the last line. To run on a local system following code should be enabled.

app.run(debug=True)

Save the file as ‘app.py’.

4. Installing dependencies

In Pycharm terminal, install pip. Force reinstall if already installed.

> python -m pip install -U --force-reinstall pip

Install dependencies or libraries required to run the project.

> python -m pip install Flask

Like this do for all the libraries like sklearn, pandas, numpy, etc.

Once all the libraries are installed make sure to generate a ‘requirements.txt’ file which will have name and version of all the libraries required to run the project.

> pip freeze > requirements.txt

4. Running on localhost

Make sure now the local PyCharm directory looks like this.

‘Loan_Acceptance.ipynb’ can be ignored. That is basically the notebook file where EDA and model comparison part was done. The ‘index.html’ file is stored inside the templates folder. Ignore the rest of the folders as those are automatically generated when you make the virtual environment in PyCharm.

Finally in Pycharm terminal, run the app.

> python app.py

Output screen will show like:

Click on the blue link to run the web app on browser. Or copy-paste the link and run it on a browser:

http://127.0.0.1:5000/

If there are no errors done during the entire process, the output screen on the browser will show the webpage.

Conclusion

So finally we have successfully run the app on the Chrome browser. Once we input the values and click on Submit button, the screen will show the output if a loan will be sanctioned for the user or not.

In Part II, we will run our model on webhost using Amazon EC2.

Thanks for reading my article! If you like my article do 👏.

For the full code of this project, check the Github link.

Connect with me on Linked-in for more updates or if you are stuck anywhere.

--

--

Hrisav Bhowmick
Analytics Vidhya

A machine learning enthusiast, eager to solve real world problems.