Building and Hosting LLM Apps with Streamlit on AWS
Large Language Models (LLMs) have revolutionized various natural language processing and understanding applications. However, deploying these powerful models for public use can be a challenging task. Streamlit, an open-source Python library, simplifies the process of creating web apps for machine learning and data science projects. Coupled with the robust infrastructure provided by AWS EC2, deploying LLM applications becomes seamless and scalable. This blog post will guide you through building and hosting an LLM app using Streamlit on AWS.
What is Streamlit?
Streamlit is a powerful and user-friendly library that allows data scientists and machine learning engineers to convert their data scripts into interactive web applications. With Streamlit, you can create beautiful, performant apps with just a few lines of code, making it an ideal choice for showcasing your machine-learning models and data visualizations.
What is AWS EC2?
Amazon Web Services Elastic Compute Cloud (AWS EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. EC2 offers a wide range of instance types optimized to fit different use cases, including those requiring intensive computational resources, making it perfect for hosting LLM applications.
Prerequisites
Before we dive into the step-by-step guide, ensure you have the following prerequisites:
- AWS Account: You need an active AWS account to launch and manage EC2 instances.
- AWS CLI: Install and configure the AWS Command Line Interface (CLI) on your local machine.
- Python: Install Python (preferably version 3.9 or above).
- Streamlit: Install Streamlit using pip.
- LLM Model: Have a pre-trained LLM model ready for deployment (e.g., GPT-3, GPT-4, etc.).
- Basic knowledge of AWS and EC2: Familiarity with launching and managing EC2 instances.
Step-by-step Guide
Set Up Your AWS Environment
- Create an AWS Account: If you don’t have an AWS account, sign up at AWS Free Tier.
- Install AWS CLI: Download and install the AWS CLI from the AWS CLI User Guide.
- Configure AWS CLI: Run
aws configure
and enter your AWS access key, secret key, region, and output format.
Launch an EC2 Instance
- Navigate to EC2 Dashboard: Go to the AWS Management Console and open the EC2 Dashboard.
- Launch Instance: Click “Launch Instance” and configure the following:
- Choose AMI: Select an Amazon Machine Image (AMI). For this guide, use an Ubuntu AMI.
- Instance Type: Choose an instance type that suits your needs (e.g., t2.micro for free tier or a more powerful instance for better performance).
- Configure Instance: Configure instance details as needed.
- Add Storage: Allocate storage based on your requirements.
- Configure Security Group: Create a new security group or use an existing one. Ensure to add rules to allow inbound traffic on ports 22 (SSH) and 8501 (Streamlit default port).
- Review and Launch: Review your configurations and launch the instance. Don’t forget to download the key pair (.pem file) for SSH access.
Set Up Your EC2 Instance
Project Structure
Audio-To-Text/
├── src/
├── audio/
├── streamlit_app.py
├── requirements.txt
├── .env
├── .gitignore
└── README.md
Update the Package Lists
- These commands update the list of available packages and their versions but do not install or upgrade any packages.
sudo apt update && sudo apt-get update
Check Python Version
- These commands check the currently installed version of Python.
python3 -v python3 --version
Install Required Software
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.10
sudo apt install -y python3-pip
pip3 --version
Install Virtualenv
- This command installs
virtualenv
, a tool to create isolated Python environments.
pip3 install virtualenv
Switch to the Root User
- Switches to the root user for administrative tasks.
sudo -i
Set Up Directory for the Project
- Changes to the
/var/
directory, a standard location for web files. - Creates a directory named
www
for the web application. - Changes to the
www
directory.
cd /var/
mkdir www
cd www/
Clone the GitHub Repository
- Clones the specified GitHub repository into the current directory.
- Changes to the newly created
Audio-To-Text
directory.
git clone https://github.com/Bhavik-Jikadara/Audio-To-Text.git
cd Audio-To-Text/
Set Up the Virtual Environment
- Reinstalls
virtualenv
(if not already installed). - Creates a virtual environment named
venv
. - Activates the virtual environment.
pip3 install virtualenv
virtualenv venv
source venv/bin/activate
Set Environment Variables
- Opens the
.env
file using thevim
editor to set any necessary environment variables (e.g., API keys, configurations).
vim .env
# Add below line
ASSEMBLYAI_API_KEY="api_key"
Install Project Dependencies
- Installs all the Python dependencies listed in the
requirements.txt
file.
pip3 install -r requirements.txt
Run the Streamlit Application
- Starts the Streamlit application by running the specified Python file.
streamlit run streamlit_app.py
Conclusion
Deploying LLM applications using Streamlit on AWS EC2 provides a powerful and scalable solution for showcasing and utilizing large language models. By leveraging the user-friendly features of Streamlit and the robust infrastructure of AWS, you can build interactive web applications that effectively demonstrate the capabilities of your machine-learning models. This guide has walked you through the necessary steps, from setting up your AWS environment and launching an EC2 instance to configuring your project and running your Streamlit application. With these steps, you’re well on your way to making your LLM applications accessible and impactful.
Happy coding!