How to build your machine learning application
By: Frank Xu
Tags: Machine Learning, Application Development, Google Cloud Platform, Data Science, Towards Data Science

How to build your machine learning application

Building and deploying a Dash app through Google App Engine

It’s easier than building a horse

Machine Learning today is not a fancy or magical thing anymore. In fact, all the online tutorials have made it as feasible as it ever was. Just grabbing chunks of code from the Internet and debug a little, and your model is good to go. However, there are hardly resources online telling you how to present your machine learning progress to others. No matter it is for a product prototype, your portfolio, or explaining what your model does to the non-technical crowd, building an application for your machine learning project could always be a nice finishing touch.

What are machine learning applications? Let me give you some examples:

Virtual Personal Assistants , such as Siri and Alexa. Your voice was used as an input, they extract your words and respond with corresponding commands, through their models trained with gigantic datasets.

, such as Siri and Alexa. Your voice was used as an input, they extract your words and respond with corresponding commands, through their models trained with gigantic datasets. Email Spam filtering . Basic information about an incoming email was used to decide if an email could be spam.

. Basic information about an incoming email was used to decide if an email could be spam. Product recommendations. You shopped some sneakers online days ago and all of a sudden, you and your roommate’s social media started receiving sports shoe advertisements. Your browsing history some personal information was used to recommend advertisements just for you.

If you think about it, machine learning applications are all around you. I have to admit that a lot of time and energy would be needed to make an application industrialized, but we can build our own simple machine learning application on a cloud base, without considering all the cornering cases and load testing. It can be served as a perfect personal portfolio, or for development testing. With the help of Google Cloud Service, let’s build an application!

Part 1. Build the model

We will first be building our model for this app. Let’s just use the Iris dataset. We load the Iris dataset from Scikit-learn, and build a simple logistic regression on it:

Part 2. Build the app locally

Then we will build the app locally with Dash. Our purpose will be building a web interface, where you can put in different values you measured for this Iris flower you encountered, and it will return the most likely species predicted based on the input.

Workflow of our app

This is a long chunk of code, I know, but please bear with me. First, we initialize the app. We have to introduce flask here to help with deploying the app with Google App Engine.

The second part here is setting up the app layout. The code is commented in different parts and what they are for. If you don’t have any front-end experience, it would be a little hard to read, but do trust the learning process. We are not making any calculations here, just setting up parts and their corresponding IDs.

The third part here is a callback function, which is what makes the app “interactive”. The output and inputs are stated before the actual function: We have one output, corresponding to the output area from the layout section. We have five inputs, the GO button, and the 4 numeric inputs of flower measurements. The use of State here is to make sure the app getting the values from the user only after the whole form is finished, you can learn more about it here. The Button here decides only to start working after you press the button.

Now if you put all those codes in a notebook or python file and run it locally, you will get such information as output.

Output from the code

Simply click on the URL or paste it into your browser to visit the app you built. Do notice that this is localhost, which means other people could not visit it through this link unless you set up a special firewall rule.

If you put in 1s as value and click GO button…

Part 3. Deploy the app

We now have the app up and running locally, but if you are going to share it with somebody, you will need to share all your code and data with the person and have him running it. Just the difference in the environment is hard enough to debug, not to mention if you are showing it to your boss who can’t code at all. This is why we need to deploy it using a cloud base platform: You only need to configure it once and share it with a simple URL, like this: https://frankxu.uc.r.appspot.com (You can actually click in it and visit this App). We prefer the Google Cloud platform, thanks to the free $300/1yr credit for every user.

Before anything, since we would be needing Google Cloud Engine, let’s make sure everyone’s on the same page. You would need an active Google Cloud project set up, and select a Project with billing enabled. Then, install and set up the Google Cloud SDK tool on your laptop, so that you can easily deploy your app through a few lines of code.

The first thing to do is to create a new folder, in this case, let’s call it Dash. Under Dash we want to have three files: main.py containing all your source code, app.yaml containing the cloud deployment settings, and requirements.txt containing all required packages.

Code examples of creating the files we need

In the main.py, simply paste all the code above into it in the same order, or you can get access to it here. In app.yaml, you will put in such two lines, which specify the environment and the name of the server you are running.

Content of app.yaml

In requirements.txt, simply put in a list of python packages you used.

Content of requirements.txt

You can download all three files here. Now we got everything prepared for the deploy.

Open up your terminal and go to the Dash directory. Depending on whether you have created an App for this project or not, you might need to create an app first. Select the location you desired for the app, then put in Y to continue when prompted, and wait for a few minutes for it to deploy.

After deploy, you can either enter gcloud app browse or paste the URL into your browser to browse the app. It will take a few seconds to load, but you should be seeing the exact app from cloud service. The app will be up and running until you disable it (go to the setting under the App Engine), and the link is shareable, which means you can just send it to anyone to use it as long as they have internet. It would also be nice to add the app as a part of your portfolio, this is definitely more impressive than a plain resume or a presentation.

What are the next steps? Hopefully, after this tutorial, you will be able to set up your own machine learning application. Just play with the main.py, don’t change the initializing and finalizing part of it, learn more from the technical communities when you have questions. The whole project and the related files can be found at my Github.