Creating ML Apps the easy way using Streamlit

sanzgiri
3 min readFeb 29, 2020

--

Streamlit is a new Python library that makes it very easy to create beautiful ML apps. If you have struggled with doing this using Jupyter/IPython widgets, Flask, or Dash, look no further.

This intro blog post on Streamlit gives you a great overview of the library and its capabilities. The Streamlit docs site has tutorials and the github repo has links to demo projects that you can look at to get started. In this post, I will primarily show you how you can host your Streamlit app on AWS and share some sample apps I have created.

Create an EC2 instance

  1. Sign into the AWS console, select EC2 service, choose the AWS Region closest to your location and click on ‘Launch Instance’
  2. Choose an AMI. I recommend fastAI (ami-951609ec) from the community AMIs since it comes pre-installed with most of the packages/libraries you will need.
  3. Choose the t2.micro (free tier eligible) if you are just getting started.
  4. Create a new VPC, use 10.0.0.0/28 for the IPv4 CIDR block setting.
  5. Create a new subnet, connect it to the VPC above and use 10.0.0.0/28 as the IPv4 CIDR block.
  6. Create a route table and internet gateway, and attach them to the VPC.
  7. Edit the route table, and attach subnet association to the subnet you just created. Create a route ‘0.0.0.0/0’ with the target as the gateway you just created.
  8. Configure Security Group by adding an entry of type “Custom TCP”, port range “8501–8510” and source “Anywhere”. (Change ssh Source to “Anywhere”?)
  9. Create and download a new key pair (.pem file). You will use it to ssh to your instance. I recommend storing it in a directory ~/.aws/keys. Changed its permissions to 400.
  10. Launch instance. This will put you in the EC2 Running Instances dashboard. Assign a name to your instance. Once the Instance State turns green (“running”), you are ready to use it.
  11. Select the instance, choose Action: Networking -> Manage IP Addresses -> Allocate an Elastic IP.
  12. Test access by ssh-ing to your instance:
ssh -i <path-to-your-.pem-file> <instance-elastic-ip>

Connect AWS Cloud 9 IDE to your EC2 instance

  1. Search for “Cloud9” in AWS services, and from the start page, select “Create Environment”.
  2. Give it a name, select “Connect & run in remote server”. In the “SSH server connection” section, provide the following: User = ubuntu, Host is your elastic IP address above.
  3. Copy public SSH key to clipboard and append it to the end of .ssh/authorized_keys on your EC2 instance.
  4. Install nvm and node using the commands below
# install nvm version 0.33.0
sudo curl -o-https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
# install node
nvm install node
# get path to node
which node

5. Under “Advanced Settings: Node.js binary path”, provide the path to node from above e.g. “/home/ubuntu/.nvm/versions/node/v13.1.0/bin/node”

6. You will be prompted to install the Cloud 9 IDE and in a few minutes you will be this development environment.

Get a free domain name from freenom and point your IP to this domain using AWS Cloud 53

  1. Go to freenom.com and register for an account.
  2. Go to Services -> Register a new domain.
  3. Enter the desired domain name and check its availability. Currently, domains with the “.ml” extension are available for free up to 12 months.
  4. Add your domain(s) to your shopping cart and complete the order.
  5. From your AWS console, search for Route 53 and select Hosted zones. Click on Create Hosted Zone.
  6. Provide the domain name you obtained from freenom, select Type = Public Hosted Zone.
  7. You will now see four name servers (Record Set Type = NS) for your domain.
  8. Create two Record Sets of Type A — IPv4 address with TTL of 300 sec and value set to the elastic IP address for your EC2 instance. The first one should have just your domain name as the Name and the second one should have www.<your domain> as the Name.
  9. Go back to freenom.com: Services -> My Domains and click on “Manage Domain” for your domain.
  10. Go to Management Tools -> Nameservers, select custom nameservers and enter all the 4 nameservers and click the Change Nameservers button.
  11. From your Route 53 Hosted Zones screen, if you select “Test Record Set”, you should be able to get correct responses for Types A, NS and SOA.

Create a conda environment for your ml-apps on your EC2 instance.

Now that you have Cloud9 up and running, you can open up a bash terminal and run these commands from there.

conda create -n ml-apps python=3.6
source activate ml-apps
pip install --upgrade pip
pip install streamlit

In order to connect to your app, you will also need to create ~/.streamlit/config.toml with the following lines:

[server]
enableCORS=false

--

--