Deploying a basic Streamlit app

This article demonstrates the deployment of a basic Streamlit app (that predicts the Iris’ species) to Streamlit Sharing.

KSV Muralidhar
Towards Data Science

--

Photo by Joan Gamell on Unsplash

Streamlit is an app framework to deploy machine learning apps built using Python. It is an open-source framework which is similar to the Shiny package in R. This article assumes the reader to have basic working knowledge of Conda environment, Git and machine learning with Python.

Model Development

Model 1

We’ll fit a Logistic Regression model to the Iris dataset from the Scikit-Learn package. The code below splits the dataset into train and test sets, to evaluate the model after deployment on the test set. We’ll use the mutual information metric for feature selection using ‘SelectKBest’ method. Hyperparameter tuning of ‘SelectKBest’ and the regularization parameter of Logistic Regression is done using GridSearchCV with 3 folds.

Image by author

From the above ‘GridSearchCV’ result, only 3 input features were found to be important. We’ve refit the pipeline to the data using the best hyperparameters found from ‘GridSearchCV’ and found that ‘sepal width (cm)’ wasn’t important.

Model 2

We’ll build another model by eliminating the ‘feature_selection’ step from the pipeline. Also, the ‘sepal width (cm)’ column is removed from the training data.

Image by author

The above ‘GridSearchCV’ result returned the same cross-validation score for model 1 & 2. However, we’ll deploy ‘model 1' to keep things simple . The ‘model 1’ is saved using ‘joblib’ to a file named ‘iris_model.pkl’.

App deployment to Streamlit Sharing

Once the model is developed, evaluated and saved, we need to import the ‘streamlit’ package to develop our app. Using the (above) code we used for model development will not work. We need to create a new Python script (not a Jupyter notebook) for model deployment. We’ll create a deployment/project folder having

  1. The deployment Python script named ‘iris_streamlit_demo.py’ (by convention, the name of the file should be ‘streamlit_app.py’, but it is not compulsory).
  2. The saved model (‘iris_model.pkl’).
  3. ‘requirements.txt’ file which specifies the packages to be installed. The versions of the packages must be specified to avoid the ‘it works on my machine’ problem. The version of the packages must match with the ones in our Conda environment. The ‘requirements.txt’ file and the deployment/project folder structure we’ll be using are shown below.
streamlit==0.79.0
numpy==1.19.2
joblib==0.17.0
scikit-learn==0.23.2
Deployment folder structure (Image by author)

‘iris_streamlit_demo.py’ is shown below. We’ve added 4 numeric input text boxes using ‘numeric_input’ method of Streamlit. We’ve added a ‘Predict’ button which returns an error ‘Inputs must be greater than 0’ when any of the input is lesser than or equal to 0. If all the inputs are greater than 0, then the result would be a prediction.

We’ll test the app by running it from the terminal using the below command. The screenshot of the app running on the local machine is shown below.

Image by author
App running on the local machine (Image by author)

To deploy the app to Streamlit Sharing, we need to create an account with Streamlit. Once the account is created, we need to push the deployment folder (local repo) to ‘Github’ (remote repo) and Streamlit takes care of the rest. Below is the screenshot of the deployment page on streamlit.io.

https://share.streamlit.io/deploy

Below is the screenshot of the deployed app to Streamlit Sharing.

Screenshot of the app deployed to Streamlit Sharing (Image by author)

The test set used to evaluate the deployed model is shown below. The score on unseen data is found to be 1 (by chance).

Unseen data to evaluate the deployed model

This is a demonstration of deploying a basic app to Streamlit Sharing. An important point to note is that the apps deployed to Streamlit Sharing are not private. Streamlit has become an alternative to Flask for deploying machine learning apps. Apps built using Streamlit can also be deployed on popular cloud platforms (not only Streamlit Sharing).

Know more about my work at https://ksvmuralidhar.in/

--

--

Data Science | ML | DL | NLP | CV | Web scraping | Kaggler | Python | SQL | Excel VBA | Tableau | About Me: https://ksvmuralidhar.in/