Deploying Gradio on AWS: A Beginner’s Quick-Start Guide 🚀
4 min readOct 15, 2023
1. Introduction
- Gradio provides a rapid gateway to expose your newly developed machine learning model to the public, but performance is non-negotiable. Taking your Gradio application beyond the confines of your local notebook is crucial, especially when showcasing your model as a Proof of Concept (POC). For more extensive projects, Streamlit might be a more suitable choice.
2. Deployment with AWS
- In this guide, we will walk you through the process of deploying a basic Gradio application using Amazon Web Services (AWS). Rest assured, you don’t need prior experience with AWS; we will provide clear explanations every step of the way.
3. Sample Gradio App
- To illustrate the deployment steps, we’ll use a simple Gradio app as an example:
import gradio as gr
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()
4. Step 1: Converting to a Python Script
- Before using AWS, ensure your Gradio app is in a Python script. If it’s currently in a notebook file, convert it to a script using the following command:
$ jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb
- Additionally, set the parameter
server_name
to "0.0.0.0" in your script:
import gradio as gr
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch(server_name="0.0.0.0")
5. Step 2: Create a GitHub Repository
- Create a GitHub repository and upload your Gradio project there. Don’t forget to generate a “requirements.txt” file to facilitate the installation of all necessary libraries on the EC2 instance with a single command. You can create this file by running the following command within your Gradio project directory:
python -m pipreqs.pipreqs .
6. Step 3: Set Up Your EC2 Instance
- Begin by creating an AWS account and then proceed to create an EC2 instance. You can do this by searching for “ec2” and selecting the “launch instance” option.
- When configuring your instance, consider making it accessible from anywhere since this is intended as a quick deployment using the free tier.
- Before launching the instance, generate and download a key pair to access the instance from your local machine by clicking “Create new key pair.”
7. Step 4: Configure Security Inbound Rules
- After successfully launching the instance, navigate to the security tab and select the security group name. This action will open a window allowing you to modify the inbound rules.
- Click on the “Inbound rules” tab and then select “Edit inbound rules.” Add a new rule with the following specifications:
- Type: Custom TCP
- Port Range: 7860–7870
- Source: Anywhere (to enable access from all locations)
- Click “Save rules.”
8. Step 5: Connect to the AWS Instance via SSH
- To connect to the instance, you must complete two steps:
1- You must be the owner of the key-pair file. We will demonstrate how to achieve this on both Windows and Linux/macOS platforms.
- For Windows users:
1. Right-click the key-pair file
2. Go to properties
3. Navigate to the security tab
4. Click “Advanced”
5. Inspect permission entries. If your username is absent, click “change” (admin privileges required) to add your username, and subsequently, remove all other privileges from SYSTEM and Administrators. - For Linux/macOS users: Execute the following command:
chmod 400 path_to_your_keypair_file.pem
2- Execute the following command to establish a connection:
ssh -i path_to_your_keypair_file.pem ec2-user@Public-IPv4-address
- Note that the use of “ec2-user” before the “@” symbol may not always be applicable; it depends on your instance. To determine the correct username, click on the instance, navigate to “Connect,” and under the “EC2 instance connect” tab, you will find the required username. Moreover, you can locate your Public IPv4 address in your EC2 instance panel.
9. Step 6: Configuration on the EC2 Machine
- Once connected to your EC2 machine, perform the following tasks:
- Initiate a system update:
sudo apt-get update
- Install Git:
sudo apt-get install git
- Install Pip:
sudo apt-get install python3-pip
- Clone your GitHub repository:
git clone your_repository_url
- Run your Gradio app:
python script_name.py
After launching the app, you will be provided with a local URL. Of interest to us is the port number at the end, which is 7860. Combine this port with your previously used Public IPv4 address in the following format: “Public-IPv4-address:7860.” You now have a fully operational Gradio application accessible from anywhere with outstanding performance. credits to Abubakar Abid for his amazing post which inspired me to write mine 🙏🏼