Machine Learning

Developing a Web Application for a Machine Learning Model

This post describes developing a web application for a machine learning model and deploying it so that it can be accessed by anyone. The web application is available at:

https://arrear-model.herokuapp.com/

The process of deployment consists of transferring all flask application files from a local computer to the web server. Once completed the web application can be visited by anyone through a public URL. The cloud platform used is Heroku because it supports Python web applications built with various programming languages including applications built with Python flask. Heroku makes things easier by handling administrative tasks by itself so one can focus on the programming part. Another good thing is that web applications is hosted for free.  As the traffic increases, one may want to sign up for one of the better plans so that the web application performs well in high traffic.

The steps involved are as follows:

Creating a simple model that can be deployed to the web, where users can input variables to get predictions

For this post, the model is a simple linear regression model based on the paper “Assessment of Irish Mortgage Arrears at County Level using Machine Learning Techniques and Open Data”. The code and the paper is available for download here. The model is a simple linear regression and predicts the mortgage arrears for the year for county of Ireland.

Building the components needed by Flask microframework to create a web app

Flask is a framework which when loaded in Python automatically executes the routine code and one focus on the specific parts of the web application. A detailed tutorial is available here.

Deploying the final web app using the Heroku platform.

  1. Using the git tool to send local files to the online webserver or cloud. So, the first thing to do is to download git from https://git-scm.com/downloads and install it.
  2. Sign up for a free account on Heroku: com.
  3. Download and install Heroku Toolbelt from https://toolbelt.heroku.com/.
    Heroku Toolbelt is a package that allows you to interact with the Heroku cloud through computer command line and it needs git to do that. You already installed git in step 1.
  4. Make a git repository for the app using git init (use Git CMD for windows)
  5. Start interacting with Heroku account by typing : heroku login
    Enter heroku account credentials when asked about them.
  6. Create a custom Heroku app: heroku create arrear-model
  7. Create the required Heroku files :
  • Create an empty file named Procfile in current folder. Then enter this line inside the empty file: web: gunicorn app:app . The file shouldn’t have any extension, so make sure the file name is not getting a .txt extension.
  • Create a requirements.txt file to tell Heroku which packages to install for web app by typing:
    • Flask
    • Pandas
    • gunicorn
    • sklearn
  1. Add all local files to the online repository by typing:
    git add .
    Make sure to include the dot after add. The dot means adding the entire directory to the repository.
  2. Commit your files with:
    git commit –m “First commit”
  3. Set the remote destination for pushing from git to Heroku

heroku git:remote -a arrear-model

Makes it easier to push local web app to Heroku, using git.

  1. Push application to Heroku:
    git push heroku master
  2. Make sure at least one instance of the app is running with:
    heroku ps:scale web=1

If everything goes as expected, the app is now live. To check out the deployed app either use heroku open command or just go to

https://arrear-model.herokuapp.com/

Tagged ,

Leave a Reply

Your email address will not be published. Required fields are marked *