Guide to deploy Streamlit ML Web Apps on AWS EC2

Sayantanee Sen
Analytics Vidhya
Published in
6 min readJun 11, 2022
Photo by Nathan Dumlao on Unsplash

By the end of this article you should be able to deploy and showcase your ML models on a web server.

For starters, I would definitely recommend that you should go through the Amazon AWS EC2 user documentation(link below), however here are few quick definitions that will help you better understand the steps in this article. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html

Amazon EC2 is one of the most popular AWS services nowadays. EC2 stands for Elastic Compute Cloud which allows you to rent virtual machines and run your computer applications on them. So you can enjoy the magnificent and immensely powerful configurations of these servers and pay based on the time of usage.

Amazon Machine Image or AMI can be considered as a template that consists of various configuration details like the operating system (Linux, Unix or Windows), storage or the system architecture (32-bit or 64-bit). You must select a AMI template when you launch an instance (Wondering what's that ? keep reading)

Instances are created when you launch a virtual server. At the point of launch, it copies the configuration details of the selected AMI and launches single or multiple instances based on your request and need. This is how a running instance looks like :

You can stop the instance if you plan to start it later. In this case your Amazon EBS will remain attached to the instance (Terminology alert : Amazon Elastic Block Store or EBS is the block level storage that is associated with the instance)

You can also terminate an instance, in that case you can’t restart the same at later point.

Instance types specify what hardware configuration you are signing up for. This can be storage capacity, memory management, computational capabilities etc. We will use t2.micro as this provides a decent option for the Free Tier users.

Lets set up a new AWS EC2 Instance

  1. The first thing you need to do is sign up at https://us-west-2.console.aws.amazon.com/console

2. Once you are in, as soon as you click on Launch an Instance button at top right corner, the below page will open. The first step is to select an AMI. I will select Ubuntu. Next from the drop down you can choose your preferrable configuration, but make sure you check the availability of that configuration if you are using Free Tier services.

3. Now select the Instance type, we will go ahead with t2.micro.

4. The next step is to generate a key pair to be able to login to your instance with a secured connection. A key pair consists of a public key at AWS end and a private key at your end. You can directly select the .ppk key if you will be connecting to the EC2 instance through putty.

5. Next under network settings, select “Anywhere” only if you do not want any restriction on the IPs who can access your instance and click on “Launch Instance”.

Woohoo! our instance is up and running! The next step is to connect to that instance so that you can transfer all the required scripts and files to the EC2 Server and deploy your ML app.

Connect to the AWS EC2 instance & run your app using Streamlit

  1. As mentioned above, you can either create a .ppk key pair directly or in my case I will convert the .pem key to a .ppk using Putty Generator (optional step if you already have the .ppk key).

2. Copy the IPv4 Address from the running instance and follow these steps to connect from putty.

Paste the IPv4 address in the “Host Name” text box.

Under Connection -> Data -> Login , mention the username as “ubuntu”.

Finally, choose the .ppk key under SSH -> Auth -> Browse -> Select the key.

3. Next we will have to transfer the files from our local machine to the EC2 Instance server. This can be done using various file transfer methods. I am using WinSCP. The host name will be the same IPv4 address and the Key will be the original .pem key file.

4. Once you drag and drop all the required files from your local to the EC2 server, go back to the terminal and run the following commands.

> sudo apt-get update && sudo apt-get install python3 pip

The sudo apt-get command is used to download package information from all configured sources.

5. Now install all the required libraries one by one or if you have multiple library dependencies you can also use requirement.txt file to pip install all at once.

· pip install streamlit

· pip install plotly_express

· pip install opencv-python

5. Run the Python Script with the “streamlit” command. Here is the code for my app.py. I have used my trained weights from YOLOV3 model to detect safety hats in still images from a construction site. You can deploy any other ML app in the same way.

> streamlit run app.py

# Importing required libraries
import streamlit as st
import cv2
from PIL import Image
import numpy as np
import os


def detect_objects(image):
confidenceThreshold = 0.2
NMSThreshold = 0.3

modelConfiguration = 'yolov3_testing.cfg'
modelWeights = 'yolov3_training_final.weights'

labelsPath = 'classes.txt'
labels = open(labelsPath).read().strip().split('\n')

np.random.seed(10)
COLORS = np.random.randint(0, 255, size=(len(labels), 3), dtype="uint8")

net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
image = np.array(image.convert('RGB'))
(H, W) = image.shape[:2]

# Determine output layer names
layerName = net.getLayerNames()
layerName = [layerName[i[0] - 1] for i in net.getUnconnectedOutLayers()]

blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
layersOutputs = net.forward(layerName)

boxes = []
confidences = []
classIDs = []
for output in layersOutputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > confidenceThreshold:
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype('int')
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))

boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)

outputs = {}
# Apply Non Maxima Suppression
detectionNMS = cv2.dnn.NMSBoxes(boxes, confidences, confidenceThreshold, NMSThreshold)
if len(detectionNMS) > 0:
for i in detectionNMS.flatten():
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])

color = [int(c) for c in COLORS[classIDs[i]]]
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
text = '{}: {:.4f}'.format(labels[classIDs[i]], confidences[i])
cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

return image


def about():
st.write('''Helmet Detection App using Streamlit''')


def main():
st.title("My ML App")

activities = ["Home", "About"]
choice = st.sidebar.selectbox("Select the Option", activities)

if choice == "Home":
st.write("Go to the About section from the sidebar to learn more about it.")

# You can specify more file types below if you want
image_file = st.file_uploader("Upload image", type=['jpeg', 'png', 'jpg'])

if image_file is not None:
image = Image.open(image_file)
if st.button("Process"):
result_img = detect_objects(image)
st.image(result_img, use_column_width=True)
# st.success("Found {} faces\n".format(len(result_faces)))

elif choice == "About":
about()


if __name__ == "__main__":
main()

6. Once you run the app.py, you will get similar URLs as below. Copy the External URL and paste it in your browser and voila!!

Your web app is ready to use.

I hope this article was able to help you. Always remember to practice and explore more.

Happy Learning!!

--

--