muj1og / Integrating-a-Machine-Learning-Model-into-a-Web-app

Integrating a Machine Learning Model into a Web app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Integrating a Machine Learning Model into a Web app

In this post I will go over how to expose a machine learning model in a web application. Here is the technology stack in order to expose a machine learning model from Python into a web application.

alt text

Our first step is to encapsulate all the logic we want into a python class. In this example we already have a pre-trained model which was serialized into a pickle object. So in the class init method we load your persisted trained model. This object can be used for prediction from the web application.

alt text

There are two methods defined "classify" and "train" which we will expose as rest apis in the next section.

The second step is to expose rest apis that our web application would like to call. For this we will your Flask. The two apis we need from the last section are "classify_review" and "train_review". Both these are mapped to the invoke the MovieClassifier class and the appropriate methods within it. The return object from both these are json object of the results from the machine learning model.

alt text

Once we have the rest api's defined in Flask, we can run the flask app and we should see something like this:

alt text

The rest apis are available as the following end points: http://localhost:5010/classify_review?_id=5a24705fe3e52a74e2e02741 http://localhost:5010/train_review?_id=5a24705fe3e52a74e2e02741 where _id is the id of the content in our database.

These rest apis we just created can be wired into our AngularJS web application as shown below:

alt text

We now can consume these methods in our web application. Our application has two functions, text summarization and Sentiment Analysis.

alt text

Let us look at the sentiment analysis. Every time a new review is entered we store this in our backend database. We also classify the text using our pre-trained model for the sentiment. The following screen show all the reviews we have already tested.

alt text

We shall enter a new review "The movie was awesome" and submit.

alt text

The following results will show up. Notice that the sentiment of "positive" and probability % was computed from the Flask rest api and retrieved to the user to display in the front end page.

alt text

If we go to view all the sentiments we entered, we can see the new one we just entered. All is this is stored in the database.

alt text

About

Integrating a Machine Learning Model into a Web app


Languages

Language:JavaScript 92.7%Language:CSS 6.3%Language:HTML 0.7%Language:Python 0.3%