The world’s leading publication for data science, AI, and ML professionals.

NLP MLops Project With DagsHub – Deploy Your Streamlit App On AWS EC2 Instance – Part 2

Make your Machine Learning App accessible to the world without the headache!

Photo by SpaceX on Unsplash
Photo by SpaceX on Unsplash

Introduction

In addition to developing Machine Learning models, being able to make them accessible to the outside world is a highly valued skill. Trying to help you acquire such a skill is the main goal of this article. It focuses on the use of DagsHub, DVC, and EC2instance for the smooth industrialization of your machine learning application.

First, you will be able to build your Streamlitapp, then understand the step-by-step approach to deploying it into production.

Machine Learning Task – Reminder

The application we will be deploying is derived from the Multi-language classification project. The goal was to predict the underlying polarities of a given text in one of the following 5 languages: English, French, German, Russian, and Spanish. Please refer to the link above for more details.

Streamlit App – building process

The goal of this section is to focus on building the two main features of the streamlit application and running it on our local machine.

Note: You can also find the full source code for the app at the end of the article.

First main features – First Part of the App

This first part is related to displaying the model’s global metrics (F1 score and Accuracy) for each of the 5 languages. But before that, we will also cover the implementation of the logo and description sections.

First Part of the App - Logo, Description, and Metrics sections (Image by Author)
First Part of the App – Logo, Description, and Metrics sections (Image by Author)
  • Logo, description, and metrics sections

Displaying an image is straightforward with the streamlit [st.image](https://docs.streamlit.io/library/api-reference/media/st.image). Also [st.markdown](https://docs.streamlit.io/library/api-reference/text/st.markdown) makes it possible to display texts formatted as Markdown.

By using [st.selectbox](https://docs.streamlit.io/library/api-reference/widgets/st.selectbox), we are able to choose between F1- score and Accuracy. The model’s F1-score is automatically displayed by default. But the user has the possibility to change it to accuracy.

User input and model predictions – Second part of the app

This section is more the Intelligent part of the app. It predicts the underlying sentiment polarities of a sentence provided by the user. To do that, two options are provided as shown below.

Second Part of the App - Description, Dropdown and Free text sections (Image by Author)
Second Part of the App – Description, Dropdown and Free text sections (Image by Author)
  • Description Section

This section performs two actions: show the 🚀 description and the interaction options (option 1 & 2) available to the user. Each action is performed with a simple [st.markdown](https://docs.streamlit.io/library/api-reference/text/st.markdown)

  • Dropdown and free text sections

The user has the choice of selecting one of the 5 sentences from the dropdown list, each one in a different language. On the other hand, he/she can provide his/her own text to predict in the free text area. In both cases, we use streamlit [st.form](https://docs.streamlit.io/library/api-reference/control-flow/st.form)function to get the users’ input, then finally load the model and run the predictions.

In the free text area, a first checking is performed to make sure the sentence provided by the user is one of the 5 languages considered by the model. If it is not the case a nice message is displayed informing the user.

Run your streamlit app

After building the app, you can run it with the streamlit runcommand.

streamlit run app.py

The previous command generates the following message to access the app from two URLs.

  • Local URL is my localhost, only I can access it on my machine.
  • Network URL is the URL anyone can within the same subnetwork as I can access. However, no one outside these two networks can have access to my app.
Streamlit app running Locally (Image by author)
Streamlit app running Locally (Image by author)

The goal is to make it accessible to anyone on this planet !🪐 which is what we will be doing in the next section.

App Deployment process on AWS

The more natural step is to deploy the app into production; which is done as follows: configure the EC2 instance, push the model and metrics data to Dagshub with DVC, and run the app on EC2.

Choose, Create and Configure the EC2 instance

EC2 stands for Elastic Compute Cloud. It’s a virtual server for running applications on the Amazon Web Service infrastructure. In this section, we will see how to create an instance and perform the configurations.

Choose EC2 Instance

After logging into the AWS management console, you can search for EC2 from the Services tab. which automatically displays a second page to select the actual EC2 service.

Choosing the EC2 from the Services tab (Image by Author)
Choosing the EC2 from the Services tab (Image by Author)

Next, choose the Instances right below the Instances choice. You can see that I have a t2.xlarge instance hosting my current streamlit app😉 . This page might be empty for you in case you are not running any instance yet.

Launching EC2 instance (Image by Author)
Launching EC2 instance (Image by Author)

Create EC2 Instance – 6 main steps

After selecting the Launch instances tab, you get the following page with all the steps, from choosing your Amazon Machine Image (AMI) to Configuring the Security Group.

6 main steps for creating an EC2 instance (Image by Author)
6 main steps for creating an EC2 instance (Image by Author)
  1. Choose AMI: it is a template containing a software configuration such as operating system, app server, and apps. It can be used to launch one or multiple instances in the cloud. Let’s choose the first instance by clicking the Select button.
Choosing the AMI instance: Amazon Linux 2 AMI - Kernel 5.10, SSD Volume type (Image by Author)
Choosing the AMI instance: Amazon Linux 2 AMI – Kernel 5.10, SSD Volume type (Image by Author)

2. Choose Instance Type: the instance type provides specific compute, memory, and storage capabilities. Based on my app requirement, using a large model with 2.5 Go, I decided to go for a t2.xlarge. If your app does not require enough storage, you can go for t2.micro because it’s free. After choosing, click Next to go to the 3rd step.

Choosing the t2.xlarge instance type - ECUs, 4vCPUs, 2.3 GHz, 16GiB Memory, EBS only (Image by Author)
Choosing the t2.xlarge instance type – ECUs, 4vCPUs, 2.3 GHz, 16GiB Memory, EBS only (Image by Author)

3. Configure Instance Details: from here, we can choose the number of instances required for our use case, the type of Network, etc. as we can see, the names are self-explanatory. I decided to leave the configuration in the default mode. Select Next to continue

Configuration of instance details - everything in default mode (Image by Author)
Configuration of instance details – everything in default mode (Image by Author)

4. Add Storage: the default storage value is 8GiB, but I decided to increase it to 16 because of my app’s requirement. Click Next to continue.

Adding 16 GiB Storage (Image by Author)
Adding 16 GiB Storage (Image by Author)

5. Add Tags: As per the description, a tag consists of a case-sensitive key-value pair. It’s possible to add up to 50 tags. I skipped this step for simplicity’s sake. Select Next to go to the 6th and final step.

Tags section - Skipped (Image by Author)
Tags section – Skipped (Image by Author)

6. Configure Security Group: this is a set of firewall rules aiming to control the incoming and outgoing information for your EC2 instance. Selecting Add twice creates two new rows in the table. Below are their configurations respectively for the first and the second rule.

  • Type: Custom TCP Port
  • Protocol: TCP
  • Port Range: 8501 corresponding to streamlit port.
  • Source: Anywhere, allowing the port to be connected anywhere. Changing this way automatically sets the address to 0.0.0.0, ::/0

for the second rule, we get a similar configuration, but you just need to change the Port Range to 8502 in order to take into consideration the second port of streamlit.

Security Group Configuration for streamlit ports - 8501 and 8502 (Image by Author)
Security Group Configuration for streamlit ports – 8501 and 8502 (Image by Author)

We can finally click Review and Launch to check the configurations. Then click Launch when you’re satisfied with everything. When doing so, you’ll be requested to create or provide a key pair which is going to be required when trying to SSH-connect to your EC2 instance.

  • From the first dropdown list, choose the option Create a new key pair.
  • I decided to Leave the key pair type to RSA.
  • Provide a meaningful name in the Key pair name section. It is dagsbub_streamlit_keypairin my case.

Click Download Key Pair to download the key pair. Only then you will be able to launch your instance by selecting Launch Instances.

Key pair configuration and Instance Launch (Image by Author)
Key pair configuration and Instance Launch (Image by Author)

You get the successful launch message "Your instances are now running". From that page, you can access your instances by clicking the View Instances button.

Successful Launch message (Image by Author)
Successful Launch message (Image by Author)

As you can see from the image below, I have the newly created instance, which is in a Running state.

My running EC2 instances (Image by Author)
My running EC2 instances (Image by Author)

Voilà, You can be proud of yourself for making it this far!!! Bravo👏🏼👏🏼👏🏼

What’s next???It’s true that we want to run the application on EC2, but we first have to be able to SSH connect to our EC2 instance.

Now on, I decided to continue with the first instance I created before, to avoid being overcharged. 😀

SSH Connection to the EC2 instance

Get connection variables

Connecting to our instance is done with the following information:

  • keypair file generated
  • The public IP address of the EC2 instance
  • Public IPv4 DNS

By performing steps 1 and 2, you can get the public address and the public DNS.

Steps to get the SSH connection information (Image by Author)
Steps to get the SSH connection information (Image by Author)

Configure ~/.ssh/config file

Add the following information to your config file. The goal of this file is to make it possible to connect to the EC2 instance with a single sshcommand.

Host streamlit-dagshub
     HostName 18.117.129.66
     User ec2-user
     IdentityFile <PATH TO YOUR KEY PAIR FILE>/<KEY PAIR.pem>
  • Hostcan be any name. In my case I chose streamlit-dagshub.
  • Hostname is the public IPv4 address.
  • Usercan not be modified, it remains ec2-user.
  • IdentityFile is the full path to your key pair .pem file you downloaded.

Next, grant the user read permission, and remove all other permission with the following command.

chmod 400 <PATH TO YOUR KEY PAIR FILE>/<KEY PAIR.pem>

After that you can connect to the EC2 instance as follow:

ssh streamlit-dagshub
SSH successful connection to my EC2 instance
SSH successful connection to my EC2 instance

Interactions between DagsHub, DVC, and EC2 instance

You can find below the general workflow from pushing the source code, model, and data to running the app in the EC2 instance.

General Industrialization Workflow - From local Machine to EC2 instance (Image by Author)
General Industrialization Workflow – From local Machine to EC2 instance (Image by Author)
  • Task 1: push the source code, the model, and data to DagsHub using DVC.
  • Task 2: clone the source code, pull the model, then the data, and run the application into EC2.

Task 1: Performed from your local project repository

To be able to perform this task you first need to configure your DVC remote, then push the model and the data to DagsHub storage.

Below is the format of the params.yaml file used in the previous script.

Task 2: Performed from your local EC2 instance

Before running the application, we need to get the application source code and dependencies, then pull the model and the data from DagsHub storage. From the EC2 instance:

  • git clone the source code
  • pull the mode and the data
  • install the dependencies
  • run the streamlit application

    Add the following lines to your params.yaml.

    We can finally run the prepare_instance script from the EC2 instance, followed by the streamlit runcommand to run the application.

# Run the script to prepare EC2 instance
python prepare_ec2.py
# Run the streamlit app
streamlit run app.py

At the end of the process, we get the following URLs information from the EC2 terminal. Then copy-paste the External URL to your favorite browser.

Streamlit Running from the EC2 instance
Streamlit Running from the EC2 instance

Great!!! 🎉🎉🎉 Our Application is running on the EC2 instance. But there is one issue though. When we quit the console, the application stops running. This problem can be solved by using tm[ux.](https://doc.ubuntu-fr.org/tmux)

Streamlit App – Make it run continuously

First, from your EC2 instance, install tmux library

sudo yum install tmux

Next, create a tmux session with the following command

tmux new -s MultiLanguageSentimentStreamlitAppInstance

You could replace MultiLanguageSentimentStreamlitAppInstance with whatever name you want, as long as it is meaningful. Then after running the command, I get the following result.

My streamlit application running under a tmux session (Image by Author)
My streamlit application running under a tmux session (Image by Author)

As opposed to the first time running the without tmux, this time, we have a green horizontal line at the bottom of the terminal, meaning that the tmux session is one. You can exit the terminal with Ctrl+C and the application will keep running, and you can send the link to someone living on a different continent, he or she will access the same application.

What is next??? Map the Public Adress to a Domain Name Server so that we can use for instance my_app.com instead of accessing from the Public IP address.

Conclusion

Congratulations! 🎉 🍾 You have just learned how to deploy your streamlit application to AWS EC2 instance using DagsHub. If you’re still hesitant about using DagsHub, now is the time for you and your teammates to leverage the power of DagsHub.

If you like reading my stories and wish to support my writing, consider becoming a Medium member. By doing so, I’ll receive a small commission.

Also, Feel free to add me on LinkedIn, and YouTube, or follow me on Twitter. It is always a pleasure to discuss AI, ML, Data Science, and NLP stuff!

Additional Ressources

AWS EC2 Documentation

DagsHub Website

Part One of the Article

Source Code On DagsHub

Link to the App on AWS EC2

Bye for now 🏃🏾 ‍♂️


Related Articles