Creating an ML Web App with Streamlit and Deploying it to AWS

Machine learning powers many applications we use everyday, from recommendation engines to chatbots to fraud detection systems. As a data scientist or ML engineer, it‘s exciting to develop new machine learning models that can drive innovation and solve real-world problems. However, a model isn‘t very useful on its own – it needs to be deployed to a production environment as a web app or service in order to provide value to end-users.

In this guide, we‘ll walk through the process of creating a machine learning web app using the popular Streamlit library, and show you how to deploy it to Amazon Web Services (AWS) to make it accessible to the world. By the end, you‘ll have the knowledge and skills to build and deploy your own ML web apps on AWS.

What is AWS?

Before we dive into building our app, let‘s discuss what AWS is and why it‘s a great choice for deploying machine learning web apps. AWS (Amazon Web Services) is a cloud platform that provides on-demand computing resources and services like servers, storage, databases, networking, and more. Instead of purchasing and maintaining your own physical servers and infrastructure, you can provision virtual servers and resources on AWS, and only pay for what you use.

There are many benefits to deploying apps on AWS:

  • Scalability – AWS makes it easy to scale your app up or down based on traffic and demand
  • Flexibility – Choose from a wide variety of instance types, operating systems, databases, and more to fit your app‘s needs
  • Reliability – AWS is known for its reliability, with multi-region infrastructure and 99.99% uptime SLA
  • Security – Robust security features to protect your app and data, and ensure compliance
  • Cost-effective – Pay only for the resources you consume, with low-cost options available

In this tutorial, we‘ll use Amazon EC2 (Elastic Compute Cloud) to deploy our Streamlit app. EC2 provides resizable virtual servers in the cloud, allowing you to scale your deployment based on requirements.

What is Streamlit?

Streamlit is an open-source Python library that allows you to quickly and easily build interactive web apps for machine learning and data science. It provides a simple way to create web UIs with Python scripts, without needing to know web development frameworks like HTML, CSS, or JavaScript.

Streamlit is designed specifically with data scientists in mind. Some key features and benefits include:

  • Write in Python – Build apps directly in Python without needing to learn other web technologies
  • Simple API – With just a few lines of code, you can create interactive widgets, plots, tables, and more
  • Fast development – Go from idea to deployed app in a matter of minutes
  • Easy sharing – Share your app publicly by deploying it to a web server
  • Customizable – Modify the look and feel of your app with themes, and add state-of-the-art visualizations

Streamlit supports many popular data science and machine learning libraries like Pandas, Matplotlib, Scikit-learn, Keras, PyTorch, and more. It‘s a great choice for data scientists looking to productionize ML models as interactive web apps.

Creating a Streamlit App

Now let‘s walk through the steps to create a machine learning web app with Streamlit. We‘ll build an app that predicts the sentiment (positive or negative) of movie reviews. Here‘s an overview of the process:

  1. Train and save a sentiment analysis model
  2. Create a new Python script for the Streamlit app
  3. Add widgets to accept user input
  4. Load the trained model and make predictions on user input
  5. Display the model predictions
  6. Test the app locally

Step 1: Train and Save Model

First, we‘ll train a sentiment analysis model on the IMDb movie review dataset. We can use the NLTK library to download the dataset:

import nltk
nltk.download(‘movie_reviews‘)

Then, we‘ll extract the reviews and labels, and preprocess the text data by converting to lowercase and removing stopwords and punctuation:

import string
from nltk.corpus import stopwords

def preprocess(text):
  text = text.lower()
  text = "".join([char for char in text if char not in string.punctuation])
  words = text.split()
  words = [w for w in words if w not in stopwords.words("english")]
  return " ".join(words)

reviews = []
labels = []
for fileid in movie_reviews.fileids():
  review = movie_reviews.raw(fileid)
  review = preprocess(review)
  label = "pos" if fileid.startswith("pos") else "neg"
  reviews.append(review)
  labels.append(label)

Next, we‘ll vectorize the reviews using TF-IDF, and train a Naive Bayes model:

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score

X_train, X_test, y_train, y_test = train_test_split(reviews, labels)
vectorizer = TfidfVectorizer()
X_train = vectorizer.fit_transform(X_train)
X_test = vectorizer.transform(X_test)

model = MultinomialNB()
model.fit(X_train, y_train)
preds = model.predict(X_test)

print(f"Accuracy: {accuracy_score(y_test, preds):.2f}")

Finally, we‘ll save the trained model and vectorizer using joblib:

import joblib
joblib.dump(model, "sentiment_model.pkl")
joblib.dump(vectorizer, "vectorizer.pkl") 

Step 2: Create Streamlit App

Now that we have a trained model, let‘s create our Streamlit app to serve predictions. Create a new file called app.py with the following code:

import streamlit as st
import joblib

vectorizer = joblib.load("vectorizer.pkl")
model = joblib.load("sentiment_model.pkl")

st.title("Movie Review Sentiment Predictor")
st.subheader("Enter review text to predict sentiment")

review_text = st.text_input("Review Text", "This movie was great!")
if st.button("Predict"):
  review_vector = vectorizer.transform([review_text])
  prediction = model.predict(review_vector)[0]
  proba = model.predict_proba(review_vector)[0]
  st.subheader(f"Prediction: {prediction}")
  st.write(f"Probability: {proba.max():.2f}")

Let‘s break this down:

  • We import the streamlit and joblib libraries
  • Load the saved vectorizer and model files
  • Add a title and subheader to the app
  • Create a text input widget to accept the review text
  • When the user clicks the "Predict" button:
    — Transform the input text into a vector using the loaded vectorizer
    — Make a prediction on the vector using the loaded model
    — Display the predicted sentiment and probability

Step 3: Test App Locally

To test the app, run it locally with the command:

streamlit run app.py

This will open the app in a new browser tab. Try entering different review texts and see the predicted sentiments.

Deploying to AWS

Once you‘re happy with your Streamlit app, you can deploy it to AWS EC2 to make it available on the web. Let‘s walk through the steps.

Step 1: Set Up AWS Account

First, you‘ll need to create an AWS account if you don‘t already have one. Go to https://aws.amazon.com and click "Create an AWS Account". You‘ll need to provide some information and a credit card. There is a free tier that provides limited usage of many AWS services.

Step 2: Launch EC2 Instance

Once your account is set up, sign in to the AWS Management Console. Go to the EC2 dashboard and click "Launch Instance".

Configure the following settings for your instance:

  • Choose an Amazon Machine Image (AMI): Select "Amazon Linux 2"
  • Choose an instance type: Select "t2.micro" (free tier eligible)
  • Configure instance: Leave as defaults
  • Add storage: Leave as default (8 GB is included in free tier)
  • Add tags: Optionally add tags to identify your instance
  • Configure security group: Create a new security group and add rule to allow HTTP traffic on port 80
  • Review and launch: Review your settings and click "Launch"

You‘ll be prompted to create a new key pair or use an existing one. This is necessary to SSH into your instance. Create a new key pair, download it, and store it in a secure location.

Step 3: Connect to Instance

Once your instance is launched, you can connect to it using SSH. In the EC2 dashboard, select your instance and click "Connect". Follow the instructions to SSH into the instance using the key pair you created.

Step 4: Set Up Environment

Before deploying the app, we need to set up the required environment and dependencies on the EC2 instance:

Update packages and install Git:

sudo yum update -y
sudo yum install git -y

Clone your app code from GitHub, or copy the files to the instance using scp.

Install tmux to allow the app to run in a separate session:

sudo yum install tmux

Install Miniconda:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

Create a new conda environment and install required packages:

conda create -n myenv python=3.8
conda activate myenv
pip install streamlit joblib scikit-learn nltk

Step 5: Deploy App

Now we‘re ready to deploy the app:

Navigate to the app directory:

cd /path/to/app

Use tmux to create a new session to run the app:

tmux new -s streamlit 

In the tmux session, start the Streamlit app:

streamlit run app.py --server.port 80

The app is now running on port 80 of the EC2 instance. To access it, open a web browser and navigate to http://<public-ip>, replacing <public-ip> with the public IP address of your EC2 instance.

You should see your Streamlit app running successfully in the browser! The app will continue running in the background in the tmux session. To stop the app, press Ctrl-C in the tmux session. To detach from the session, press Ctrl-B then D.

Advanced Topics and Best Practices

Congratulations on deploying your Streamlit app to AWS EC2! There are some additional topics and best practices to consider for a production-grade deployment:

  • HTTPS – Configure SSL/TLS certificates to enable HTTPS for secure communication
  • Authentication – Add authentication to your app to control access and protect sensitive info
  • Scaling – Use EC2 Auto Scaling to automatically adjust the number of instances based on load
  • Monitoring – Set up monitoring and alerts to track app performance and errors (e.g. CloudWatch)
  • CI/CD – Automate the deployment process with a CI/CD pipeline to enable easy updates
  • Cost optimization – Analyze costs and utilize discounts like Reserved Instances and Spot Instances

Conclusion

In this guide, we demonstrated how to create an interactive machine learning web app using Streamlit, and deploy it to AWS EC2. Streamlit makes it easy for data scientists to build web apps, while AWS provides a reliable and scalable platform for deployment.

By following the steps outlined here, you can deploy your own Streamlit apps to share your machine learning projects with the world. You can customize the app layout, add more complex ML models, and integrate with other data sources and APIs to build sophisticated applications.

The skills you learned will enable you to bridge the gap between developing ML models and deploying them to production, and deliver more value to your end-users. Try deploying your own Streamlit app to AWS, and share your experiences and creations!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts