As the projects about Machine Learning start to become widespread, there are more and more innovations about the practices related to how these projects are transferred to live environments. In this context, with this article, I will make an example of how to transfer a machine learning model to the living environment in the fastest and most effective way of using container infrastructures. I hope it will be a useful study in terms of awareness.
Before starting our example, I want to give some information about this infrastructure verbally.
When we examine modern software development architectures today, we see that new practices and frameworks are developing in every step of the software development life cycle. We see that different approaches have been tried in every step from the development of the application to the testing and commissioning of the application. While examining modern software development architectures, we see that one of the most basic components we encounter is undoubtedly container technologies. Briefly, to mention the advantages that container technologies offer us;
- It enables the applications we develop to be run very easily and quickly.
- By keeping all the additional library dependencies required by the application integrated with our application, it enables us to deploy and distribute our application in an easy, fast, and effective.
- It is very easy to manage and maintain the applications put into use as containers.
Container technologies are an issue that needs to be examined in depth. Therefore, I will not go into more detail on this subject in this article. For more detailed information, you can review Docker documentation and read hundreds of blog articles about this subject.
In a world where we talk about container technologies, Docker is undoubtedly the locomotive technology. Docker, which has become a defacto standard in the industry, is a tool used by almost everyone who develops container-based software. Today, I will perform this example using Docker.
To make this example, Docker must be installed in the environment in which we work. You can prepare Docker in your working environment by following the line below.
Our model that will be the subject of this article is the same model I used in my previous article, so it is the same as in the previous article to better understand the example in this article. In other words, I will work on the model I shared in the article “Simple Sentiment Analysis With NLP”, and I would like to emphasize again that the purpose of this article is not to make a state of the art sentiment analyzer.
First of all, let me talk a little bit about my script. I want to develop a sentiment analysis model. I will train this model with a data set collected from IMDB, yelp, and amazon and develop a simple sentiment prediction model. After finishing this model, I will design a web page and make the model available to end-users through this web page. Then, I will convert this application as a container image and run on Docker.
Model Training Part
Yes, we created our model. What we need next is to record the model we created and the vocabulary output of the model with the pickle. We will use these outputs on the web page we will develop later.
Our outputs related to our model were recorded on a disk. Now we can start the development of the web page that will find the sentiment of the text that will be sent as input to this model and return.
I will use python’s flask library because I will do the development with python. A library that allows you to develop web-based applications with Flask python. I will develop a web service using this library. First of all, let’s install the flask in our development environment.
Now that we have completed our Flask installation, we can now develop our website.
Before I start developing, I want to show the directory structure I have installed in my local. I think it would be useful for you to set up a similar directory structure for such a study. Since this will already be a web application, webform.py, which we write the main code, will look for the .html file that it will render while running, under the / templates directory. So after developing your HTML page, you will have to put it under the templates directory. Below you can see the directory structure that will work successfully.
As you can see in the directory production, I moved the model outputs that I developed and output to under the models folder. I created and put my webform.html file under the Templates directory. Likewise, I created and saved my code file named webform.py under the main directory.
Since this application will be delivered to the user via the web page, I design a simple HTML form where the user will enter text and see the result with a very simple design. This web page can be equipped with all the current infrastructures provided by web technologies, I just want to show how it is made with a simple design as much as possible.
Now let’s see what the form we created is like.
Yes, as we can see, our design is very simple. Let’s write your python code that will use this form and run our model, find the sentence of the text sent from the page above, and return the answer to the form.
There are four things to note here. The first one is how I called the HTML code I wrote. The second is to load our model outputs and make them ready for use.
The third one is to include the text that will come from outside as a parameter in a variable and the fourth one is returned to the web page.
Yes, we have completed the developments. Let’s run the webform.py code we wrote.
Yes, our service got up and the endpoint address was shown to us on the screen. Let’s test the service. For the test, it will be enough to write the endpoints on the screen and we will do the test with an input we want.
Now let’s write dockerfile to create my image that will turn this app into a Docker container. I will use the skeleton of the image I will create from the official python images in dockerhub.
Now, let’s run our code that will produce our image from Dockerfile we wrote on the terminal screen.
The image may take a long time to build. After the build process is finished, you can check whether the image comes to the Docker registry with the following command.
Yes, as we have seen, our docker image has been created with the tag name we have provided.
Let’s put this image in a container, run it, and test it.
Yes, the container stood up. Now let’s try to access our application running in the container in our main machine.
Yes, we were able to reach the application we run through the container from our browser and test it.