avatarZoumana Keita

Summary

This article provides a comprehensive guide to deploying a sentiment analysis application using Streamlit and Docker, including training a machine learning model, structuring a Streamlit project, and containerizing the application for deployment.

Abstract

The article offers a step-by-step tutorial for Machine Learning and Software Engineers to deploy a Streamlit application within a Docker container. It begins with an introduction to the benefits of using Docker for replicating development environments and ensuring consistent application performance across different systems. The tutorial covers the training of a simple sentiment classifier, the creation of a well-structured Streamlit application, and the process of Dockerizing the application for deployment. The author emphasizes the ease of collaboration, code maintainability, and streamlined deployment as key advantages of following the outlined project structuring. The article also touches on the use of Streamlit for rapid data visualization and model deployment, contrasting it with more time-consuming alternatives like Flask, Django, or FastAPI. Finally, the article concludes with instructions on building and running the Dockerized Streamlit application, celebrating the successful deployment and suggesting potential next steps involving cloud platforms like Azure, AWS, or GCP.

Opinions

  • The author believes that Docker containers are a superior method for sharing applications and their dependencies with others, avoiding the pitfalls of inconsistent environments.
  • Streamlit is presented as a valuable tool for data science and machine learning teams, enabling quick data visualization and model deployment compared to other web frameworks.
  • Proper project structuring is highly recommended for easier collaboration, cleaner code, and simpler deployment processes.
  • The author encourages the use of cloud provider platforms to further deploy the Docker container, indicating a preference for scalable and managed cloud solutions.
  • The article suggests that following the outlined steps will save time and resources, implicitly endorsing the Streamlit and Docker combination for efficient application deployment.

Create An Awesome Streamlit App & Deploy it With Docker

This article is a comprehensive overview of a step-by-step guide to deploy a simple sentiment analysis application using streamlit and Docker

Photo by Timelab Pro on Unsplash

Introduction

As Machine Learning or Software Engineer, we would like to share our applications/models and their dependencies with others. This process, if not done properly can cost us not only time but also money. Nowadays, one of the most preferred ways to collaborate with other developers is by using source code management systems (e.g. Git). This might not be enough, because the awesome model that you developed on your computer might not work on someone else’s computer for different reasons. So a better way to tackle this issue might be to use Docker containers, in order to be able to “replicate” your computer and make it accessible to others. In this article, we will learn this concept in the following order:

  • Train a simple sentiment classifier
  • Create the corresponding Streamlit application using good software project structuring
  • Create the Docker image for streamlit application
  • Run the image in a container

Machine Learning Model

Since the main goal is to walk you through the steps to deploy your model using Streamlit and Docker, we will not spend too much time on model training.

Model training in Notebook

Useful Libraries Here are the useful libraries for the

(Image by Author)

Read Spam data Read the dataset and show 3 random samples

(Image by Author)

spam_data.v1.unique() ==> array([‘ham’, ‘spam’], dtype=object)

Observation: we have two labels, which corresponds to a 2-class classification problem. Then, we need to encode those two labels into a numerical format for model training.

(Image by Author)

Feature and Target

(Image by Author)

Feature Extraction & Model training We use the Naive Bayes Classifier

(Image by Author)

What Streamlit is and how it can be useful

Streamlit is an open-source framework that can be used by Data Science and Machine Learning teams to perform better data visualization for Business and also to deploy models into production environments (instead of using Flask, Django, FastAPI which might take a considerable amount of time for model deployment) as shown on the Data Science Life Cycle below.

Image by author customized from Analytics Vidhya

Create Our Streamlit App

Prerequisites

  1. Create and activate a virtual environment
(Image by Author)

2. Install project dependencies

(Image by Author)

3. Structure the Project

Structuring the project this way as shown below has the advantage of :

  • making collaboration easier
  • maintaining a clean a robust code
  • making the deployment easier
  • etc.
(Image by Author)

Implementation Project packages The __init__.py is an empty file. And below are the contents of the remaining two files (data_processor.py and model_trainer.py)

Content of data_processor.py
Content of model_trainer.py
Content of train_model.py
Content of app.py

Run the app There are two main steps to run the model. They have to be done from the root of the project:

(Image by Author)

We can finally interact with our application after running the second command. Below are two scenarios:

  1. A case where the provided message is classified as spam
(Image by Author)

2. A case where the provided message is classifier as ham (not spam)

(Image by Author)

Time to Dockerize the application

Create docker layers To be able to dockerize the application, we need to create a Docker file within the docker folder. This file contains seven main instructions as shown below.

  1. (FROM): pulls an official python image from docker hub, then our files will be built from that image.
  2. (WORKDIR): create /app as the working directory for the application
  3. (COPY source destination): copy file(s) from the source folder to the destination folder.
  4. (RUN): runs the requirements.txt file in order to install the project dependencies.
  5. (EXPOSE): used in order to expose the port to be used by the model.
  6. (ENTRYPOINT) & (CMD): create an entry point in order to finally make the image executable.
(Image by Author)

Build the Docker Image The following command builds the image called streamlitspamapp tagged with the latest version. This image is built from the previous docker file.

(Image by Author)
(Image by Author)

From the previous result of the docker build command, we can observe that our image has been created as previously explained.

Run the container The image is built, then we need to run it into a container with the following command.

🚫 Make sure to stop the previous streamlit app before running this command, otherwise, you might face a pretty message error telling that the port is already being used

(Image by Author)

Two URLs are automatically generated from the previous command. Yours might be different from mine.

(Image by Author)

You deployed your streamlit application using docker 🎉🎉🎉 !

End of article

In this article, you have learned how to build a simple machine learning model, implement the corresponding streamlit app, and finally deploy it using Docker. You can now use some cloud provider platforms such as Azure, AWS, GCP to deploy the container in a few minutes! For further readings do not hesitate to follow me on YouTube, and consult the following links:

Additional Ressources

Github code Streamlit documentation Docker Layers Explained from DZone Create and share beautiful images of your source code

Bye for now 🏃🏾

Streamlit
Data Science
Docker
Containers
Sentiment Analysis
Recommended from ReadMedium
avatarMahesh
Streamlit

Hello,

13 min read