Model Deployment with Streamlit on AWS EC2

Hasan Ersan YAĞCI
9 min readFeb 12, 2021
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;

sudo apt install python3-venv

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

python3 -m venv web_app_env

And activate it;

source web_app_env/bin/activate

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.)

pip install streamlit

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.

streamlit run my_first_app.py

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.

import streamlit as st
import pickle
import pandas as pd
html_temp = """
<div style="background-color:tomato;padding:10px">
<h1 style="color:white;text-align:center;">Car Price Prediction </h1>
</div>"""
st.markdown(html_temp,unsafe_allow_html=True)
model = pickle.load(open("my_model", "rb"))

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.

hp=st.slider("What is the hp of your car?", 60, 200, step=5)
km=st.slider("What is the km of your car", 0,100000, step=500)
car_model=st.selectbox("Select model of your car", ('Audi A1', 'Audi A2', 'Audi A3'))
body_type=st.selectbox("Select body type of your car", ('Convertible', 'Coupe', 'Off-Road', 'Other', 'Sedans', 'Station wagon', 'Compact'))
color=st.selectbox("What is the color of your car", ('Black', 'Red', 'Brown','White','Grey','Blue','Silver','Beige','Violet','Yellow','Green','Bronze','Orange'))
gear=st.selectbox("What is the gearing type of your car", ('Automatic', 'Manual', 'Semi-automatic'))
extras=st.multiselect("Select extras for your car", ('Alloy wheels', 'Cab or rented Car', 'Catalytic Converter','Handicapped enabled','Right hand drive','Roof rack','Shift paddles','Ski bag','Sport package','Sport seats','Sport suspension','Touch screen','Trailer hitch','Tuned car','Voice Control','Winter tyres'))

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.

my_dict = {
"hp": hp,
"km": km,
"make_model": car_model,
"body_type": body_type,
"Body Color": color,
"Gearing Type": gear,
"Extras":extras
}
df = pd.DataFrame.from_dict([my_dict])columns=['hp',
'km',
'make_model_A1', 'make_model_A2', 'make_model_A3',
'body_type_Convertible', 'body_type_Coupe', 'body_type_Off-Road', 'body_type_Other', 'body_type_Coupe', 'body_type_Sedans',
'body_type_Station wagon', 'body_type_Compact'
'Body Color_Black', 'Body Color_Blue', 'Body Color_Bronze', 'Body Color_Brown', 'Body Color_Green', 'Body Color_Grey', 'Body Color_Orange', 'Body Color_Red', 'Body Color_Silver', 'Body Color_Violet', 'Body Color_White', 'Body Color_Yellow', 'Body Color_Beige',
'Gearing Type_Manual', 'Gearing Type_Semi-automatic', 'Gearing Type_Automatic']
df1 = df.drop(['Extras'], axis = 1)df1 = pd.get_dummies(df1).reindex(columns=columns, fill_value=0)df_extra = df['Extras'].str.get_dummies(',')df = df1.join(df_extra)

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

prediction = model.predict(df)st.success("The estimated price of your car is €{}. ".format(int(prediction[0])))

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.

pip freeze > requirements.txt

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.

sudo apt-get update -y
sudo apt-get install python3-pip -y
sudo apt-get install git -y

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

git clone https://github.com/NavarraB/Streamlit.git

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.

sudo pip3 install -r requirements.txt

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

sudo pip3 freeze

Finally, we can run the web app;

streamlit run my_first_app.py &

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.

--

--