Model Deployment with Streamlit on AWS EC2

Tutorial Series

We have already touched on the importance of model deployment and sharing this model with others. We need to share our model with stakeholders to collaborate or get feedback. Therefore, we need a web app with powerful and interactive content for our colleagues or clients for who we want to showcase our work. Streamlit is the most practical and fastest way to create a web app. You can browse the below to learn more about Streamlit and see simple model deployment examples.

In this tutorial, we will create a virtual environment. In this virtual environment, we will prepare a python file for the web app and a requirements file for the list of libraries. Then we will import the model (trained before in the same virtual environment), python and requirements files to Github. We will launch a virtual machine (EC2) on AWS and pull the files and model from Github. Then we will build our web app with these files on EC2. Our web app will run on AWS EC2 even though we shut down our computer.

We will use the car price prediction model for this tutorial. It is not real data so the prices can be different and some features may not affect the price as expect as normal. You can download the model from this link.

We will go step by step. At the end of the steps, we will reach the web app that estimates the car prices according to the user’s choices, and we will be able to cooperate and get feedback by sharing this link with others.

1 — Creating Virtual Environment

First of all, we need a virtual environment. We can think of the virtual environment as an isolated space. Libraries we use when training a model or creating a Streamlit python file are kept in this virtual environment with their current versions, and updates to the libraries will not affect our model or files when we run them later in this virtual environment. We can keep a list of libraries and if we want to run our works in other environments or platforms, we can install versions of the libraries on the list. When working on AWS EC2 we will need this list of libraries called the requirements.txt file.

Let’s create a virtual environment;

I am an ubuntu user, this process is similar to me for MacOs and Linux distributions. You can follow me or you can check this link for windows and other details.

First of all, we need the venv module on the computer;

After installing the venv module, we can easily create our environment. I will create an environment named web_app_env.

And activate it;

Our virtual environment is ready to use. We can understand that we are in a virtual environment from the brackets in front of our username.

2 — Python File

We need a python file (py extension) for an app. We will write the codes on this file for a web app. We will build our app on this file. It can be created and edited with a text editor but requires a python interpreter to run. py files are often used for programming web servers and other administrative computer systems.

First, we will create a python file, I will do this on the terminal. I go to the relevant directory where my model is located (we will use the car price prediction model trained before with random forest regression.) I create a py file with touch my_first_app.py.

If you don’t have Streamlit, you need to install Streamlit. You just need to type pip install streamlit into your terminal and it is ready to use. (If you don’t have pip, install pip first.)

Then type streamlit run my_first_app.py in the relevant directory in the terminal and run it. Python file works even if it is empty and gives you a URL Our Streamlit web app is ready, now we can fill this file.

Click on the URL (it doesn’t matter which one), this link will open in your browser.

When we write something to the Python file (my_first_app.py) and every time we save the file, the web app will change at the same time. You can save the file (ctrl + s or command + s) and the web app asks you “Stream file changed rerun or rerun always.”. Click the rerun always so it doesn’t warn us every time and also changes the view.

First, we will import the libraries and model and putting a nice title.

We will now add some interactivity to our web app. We will put slider, selection box and drop down menu for user choices. So user can select values and control the price based on their input.

Our original dataset is as follows. There are 7 columns outside of our target column (price). That’s why we will add 7 interactive boxes/sliders to our app.

We will put sliders for hp and km columns.

We will put in selection boxes for the make_model, body_type, Body Color, and Gearing Type columns. We will add all the unique values found in these columns as a selection in these selection boxes.

We will put a multi-selection box for the Extras column. Because there are a total of 16 unique features in the entire dataframe in the Extras column, and each car has a different number of features. Some cars can have one while some have several.

The user will select the desired features and the price will be shown.

Now we will convert our dataframe into model structure.

First, we will create a dataframe with no price column, then add all unique values for categorical columns as column names. We will implement one hot encoding to the dataframe. Because we will get categorical values from users and this python file will work as an interface between the model and the web app.

For one hot encoding, we will use the ‘Pandas get_dummies’ function. We will use the ‘Fill_value = 0’ parameter. That is, the selected value/column will be 1, the others will be 0. We use str.get_dummies for the Extras column because there are multiple selections. Check this article for details on get_dummies.

In the last part of the file, we will make prediction and give an output.

The latest version of the my_first_app.py file will be as follows.

Our web app and python file are ready.

3 — requirements.txt

We save the libraries we use when training the model and creating the Python file, along with their versions, in a requirements.txt file. If we want to run our works in another environment or platform, we can install versions of the libraries on the list. When working on AWS EC2, we will use the requirements.txt file.

It is very easy to create the requirements.txt file, we will write pip freeze> requirements.txt in the terminal.

We created this in the web_app_env virtual environment, so there are only a few libraries.

Finally, I have pushed my_model, my_first_app.py, and requirements.txt file to Github for later use on AWS EC2. We can now start our virtual machine on AWS.

4 — Run the EC2 on AWS

If you do not have an account on AWS, you can create an AWS free tier from this link.

If you have an account on AWS, follow it,

Open your AWS Management Console and select Launch a virtual machine;

Then choose Ubuntu Server 20.04 LTS from the AMI list for our virtual machine;

Go directly to step 6, leaving steps 2, 3, 4, and 5 at the default values.

In step 6, in the Configure Security Group section, we will add the port range for Streamlit. For TCP Protocols, we will add a port range of 8501, 8502 and Source will be Anywhere.

In step 7, create a key pair if you don’t have a key pair. Then select it and Launch Instances.

Our EC2 is ready, then go to the instances page and select the relevant instance. Then click the connect, it will give you code snippets to connect the virtual machine. Take these and paste them into your terminal to connect to EC2.

After connecting to the virtual machine, type the following commands to install the latest updates, python3, pip, and git.

Then we will clone the python file we prepared, the model, and the requirements.txt files we created.

The files we need are in the Car_Price directory in the Streamlit repository. So we will go to this directory and install the libraries with requirements.txt.

We installed the libraries for the web app. You can control what you have;

Finally, we can run the web app;

With ampersand (&), our web app will run in AWS EC2 even if we turn off our computer. You can exit AWS EC2 with ctrl + d or command + d and test whether your web app is working. Just let EC2 run.

Now that the app is deployed you can easily share it and collaborate on it.

You can build a machine learning web app for your model and show people how is it works, about its predictions, about its accuracy. You can share your link and get feedback and collaborate with others. Your web app link will remain active as long as you do not stop EC2.

Data Scientist | Machine Learning Proficiency

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store