Building ML web app using Streamlit and deploy to Heroku

Plaban Nayak
4 min readJul 1, 2020

Introduction

Streamlit is an open-source Python library that makes it easy to build beautiful custom web-apps for machine learning and data science.

Streamlit is an awesome new tool that allows engineers to quickly build highly interactive web applications around their data, machine learning models, and pretty much anything.

The best thing about Streamlit is it doesn’t require any knowledge of web development. If you know Python, you’re good to go!

Follow these steps and to get a sample app running in less than 5 minutes.

  1. You need to have Python 3.6 or greater installed
  2. Install streamlit

Getting Started — Building a Penguin Gender Prediction Web App

dataset source

Sample data

Add Text Data

st.title is suitable for the main title. We can use specific text functions to add content to your app, or you can use st.write() and add our own markdown.

st.write("""
# Penguin Gender Prediction App
This app predicts the **Palmer Penguin** sex!
Data obtained from the [palmerpenguins library](
https://github.com/allisonhorst/palmerpenguins) in R by Allison Horst.
""")

For section titles, use st.header or st.subheader.

st.sidebar.header('User Input Parameters')

Sample Code for the webapp:

import streamlit as st
import pandas as pd
import numpy as np
import pickle
from sklearn.ensemble import RandomForestClassifier
st.write("""
# Penguin Gender Prediction App
This app predicts the **Palmer Penguin** sex!
Data obtained from the [palmerpenguins library](https://github.com/allisonhorst/palmerpenguins) in R by Allison Horst.
""")
st.sidebar.header('User Input Features')# Encoding of ordinal features
encode = ['species','island']
# Collects user input features into dataframe
uploaded_file = st.sidebar.file_uploader("Upload your input CSV file", type=["csv"])
if uploaded_file is not None:
input_df = pd.read_csv(uploaded_file)
for col in encode:
dummy = pd.get_dummies(input_df[col])
input_df = pd.concat([input_df,dummy],axis=1)

else:
def user_input_features():
island = st.sidebar.selectbox('Island',('Biscoe','Dream','Torgersen'))
species = st.sidebar.selectbox('Species',('Adelie', 'Gentoo', 'Chinstrap'))
bill_length_mm = st.sidebar.slider('Bill length (mm)', 32.1,59.6,43.9)
bill_depth_mm = st.sidebar.slider('Bill depth (mm)', 13.1,21.5,17.2)
flipper_length_mm = st.sidebar.slider('Flipper length (mm)', 172.0,231.0,201.0)
body_mass_g = st.sidebar.slider('Body mass (g)', 2700.0,6300.0,4207.0)
data = {'island': island,
'bill_length_mm': bill_length_mm,
'bill_depth_mm': bill_depth_mm,
'flipper_length_mm': flipper_length_mm,
'body_mass_g': body_mass_g,
'species': species}
data['Biscoe'] =0
data['Dream'] = 0
data['Torgersen'] =0

#
data['Adelie'] =0
data['Gentoo'] = 0
data['Chinstrap'] =0
#
#
if data['species'] == 'Adelie':
data['Adelie'] =1
elif data['species'] == 'Gentoo':
data['Gentoo'] = 1
else:
data['Chinstrap'] =1
#
if data['island'] == 'Biscoe':
data['Biscoe'] = 1
elif data['island'] == 'Dream':
data['Dream'] == 1
else:
data['Torgersen'] =1

features = pd.DataFrame(data, index=[0])
return features
input_df = user_input_features()
# Combines user input features with entire penguins dataset
# This will be useful for the encoding phase
#penguins_raw = pd.read_csv(url)
#penguins = penguins_raw.drop(columns=['sex'])
#df = pd.concat([input_df,penguins],axis=0)
df = input_df.copy()
df = df.drop(encode,axis=1)df = df[:1] # Selects only the first row (the user input data)# Displays the user input features
st.subheader('User Input features')
if uploaded_file is not None:
st.write(df)
else:
st.write('Awaiting CSV file to be uploaded. Currently using example input parameters (shown below).')
st.write(df)
# Reads in saved classification model
load_clf = pickle.load(open('penguins_clf.pkl', 'rb'))
# Apply model to make predictions
prediction = load_clf.predict(df)

The app takes input via the sliders in the User Input Features tab or via uploading a csv file

  1. Upload .csv file
Upload CSV file for the data we need to make predictions

Output

Gender predicted by the Model

2. Adjust the sliders

Deploy the webapp using Heroku

Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.

Login to Heroku and create a new app “penguin-gender-classification”

Choose Deployment using Heroku GIT

Link for the app deployed :https://penguin-gender-prediction.herokuapp.com/

Note : This is an short attempt to build a simple web app using streamlit and deploy using Heroku app

Connect with me

--

--