Deploying a basic Streamlit app. This article demonstrates the... | by KSV Muralidhar | Towards Data Science

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.
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.
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.
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.
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’.
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
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).The saved model (‘iris_model.pkl’).‘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.
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).
The saved model (‘iris_model.pkl’).
‘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.0numpy==1.19.2joblib==0.17.0scikit-learn==0.23.2
‘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.
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.
Below is the screenshot of the deployed app to Streamlit Sharing.
The test set used to evaluate the deployed model is shown below. The score on unseen data is found to be 1 (by chance).
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).