Deploying a machine learning model on the cloud

Original article was published on Artificial Intelligence on Medium

IDE: Download anaconda which consists of Spyder and Jupyter Notebook. Anyone IDE can be used for writing codes.

Pickle: This library is used for serialization and de-serialization. Meaning the trained machine using the model can be stored and can be used at some other points of time without the need of training the model again. Simply we can use predict function to calculate the output. For installing pickle following command can be used.

pip install pickle

ML model: Download the house price dataset from Kaggle or use this link. Apply suitable feature engineering. Deal with any missing values. Use an algorithm to predict the house price. In my repository, I have used LinearRegression.

Note: This article aims to teach how to deploy the model on the cloud. The efficiency of the model can be changed by data preprocessing and choosing better algorithms.

Save the build model using the pickle library. The following command can be used.

pickle.dump(regressor, open(‘model_house.pkl’,’wb’))

This will create a .pkl file which acts as the endpoints for accessing the model.

Flask: Flask is an API for developing web applications. Create an app.py file which will have the flask code to interact with the website. Load the created .pkl model in app.py using pickle library.

pickle.load(open('model_house.pkl', 'rb'))

The post method in app.py is used to read values from the website. The use the predict command to predict the output from the pre-build model.

model.predict(final_features)

Returns the output to the website on the index.html page.

HTML page: Create a index.html page. Which will be used to take input from the user. A button that will send the values to the app.py file and output will be displayed on the same index.html page in the box provided.

Procfile: It contains the command for Heroku to be executed on startup. Don’t worry too much about it.

Requirements.txt: Freeze the requirements using the below command. It freezes all the libraries installed in your virtual environment.

pip freeze > requiremeents.txt

GitHub account: Deploy the following files on the GitHub account through upload or commit.

dataset.csv

model.py

house_mode.pkl

app.py

index.html

Procfile

requirements.txt

Heroku (free): It is a free platform unto certain limits. The repository on GitHub can be directly connected to Heroku. Heroku will provide a domain that can be used for accessing the website.