As a machine learning enthusiast and backend developer, I have no difficulties gathering data, organizing it, using it to train the machine learning model, and then making it public to the world as a Restful API.
For me, that is the easy part.
But the problem with having your machine learning model as an application program interface (API) is that it is hard to demonstrate, and let’s be fair, not all of our friends know what an API actually is and how to use it.
So, let’s start there.
An API is a software intermediary that allows two applications to talk to each other.
Let’s say you want to check the weather, sure you could look out the window, but this is the 21st century, so realistically you’re going to check your phone and open the weather app.
The app connects to a server via the internet, retrieves the data, and presents it back to you. Now, you know what you knew all along: it’s raining, but this is essentially what an API does.
Now, let’s think about this in terms of websites.
When it comes to frontend development – I struggle a lot. For me, it is hard to come up with a simple website that looks nice. I do not like coding websites from scratch, and deploying front-end applications is just not for me.
So, I decided to try to find out if website builders might help when creating graphical user interfaces for machine learning models. And for that reason, exposing my model to a beautiful Zyro website made perfect sense.
As an example for this tutorial, I will create a sentiment classification model, deploy it as a Restful API on Google Cloud Platform (CGP) as a Cloud Function, and then use the Zyro website builder to make my model accessible to the whole world.
In this tutorial, I am using labeled sentiment data from Kaggle datasets. Two labeled datasets are provided: Tweets from Twitter and comments from Reddit. Each tweet and comment is marked as Neutral, Positive, and Negative.
In order to prepare a model for the sentiment classification task, I am using sklearn library. In this tutorial, I will not be focusing on model training. Still, instead, I will just use the quickest way, to obtain model skipping train/test splitting, model evaluation, and all other standard steps.
If you want to know more, you can find the code used to train the model in this GitHub repository. I am using the TF-IDF text features and MultinomialNB classifier for sentiment classification problems.
The next step is to deploy the model as a Restful API service. For that reason, I am creating a Cloud Function in GCP. I will go through the steps quickly.
Let’s create a sentiment_prediction function.
The function is triggered using an HTTP request and has no authentication.
Now, set the runtime parameters.
With that done, it’s time to move on to function code. You can find the whole function code for this in GitHub.
So, this is what the Cloud Function doing:
- Loading the model
- Parsing request data
- Performing sentiment prediction
- Returning that prediction
Now, before we actually execute the cloud function, the trained sentiment classification model needs to be uploaded to Cloud Storage. I am putting it in my_testing_tmp_files bucket.
So with the model in place and the Cloud Function deployed, we can finally test if our API is working properly, and to do that I’m using a testing tool provided by GCP.
Sweet success, everything is working as expected.
Now comes the most challenging part for me: the front-end.
To create a website I’ll need to create an account on Zyro and look for a template that is right for me.
For the purposes of this tutorial, however, I will start with a blank template and apply the color palettes suggested automatically by the Zyro website builder.
Ok, so let’s add a section to the website and start building things up. 👍
Now that’s done, I will change the title and update fonts, and colors of the page.
And next, I am going to add images related to sentiment analysis to make the website look better, as well as a new About me page and an Embed code component where I will write the all-important code to execute my API.
As always, the code that I will be using in the Embed code component can be found in GitHub.
So, the big question: what does this code actually do?
Well, as you can see, the code calls my Cloud Function with user input, waits for a response, and displays the response for the user.
That’s not all, though. We also need to talk about the code that is required to call the Restful API from the website. To do that, I am executing an HTTP POST request to my public API and storing the result in an HTML element with the ID DataGoesHere.
In the HTML code part, I am defining how the form will look. I am using a single text input block, button, and H2 element to display the results.
And finally, the CSS that defines the way my form will look on screen.
With all that done, my sentiment prediction service is ready, looks great, and is easy to use.
Now, if you know your way around a little bit of code, then no surprises what comes next. But for those of you who don’t, let’s have a look at our work in action.
And here it is, a sentiment prediction service in action. 👇
Sure, it might look like a small thing, but it’s just one example of everything that goes into even the smallest of tasks.
AI and machine learning are constantly evolving, and at Zyro, we never stop experimenting, learning, and thinking of new and innovative ways to improve our platform.