Machine Learning Model Deployment as a Web App using Streamlit

Charu Makhijani
5 min readNov 7, 2023

--

Complete Guide to deploy ML model using Streamlit

Photo by Alex Chumak on Unsplash

Once you create an ML model, the next step is to deploy and share it to use further. If your want ML model to do real time predictions and work as a web-app, then you have couple of options to deploy your model on the web, ranging from full stack frameworks like Pyramid and Django to lightweight services like Flask and FastAPI to specialized web app framework for data science applications like Streamlit.

If you want to learn how to deploy your ML model as a service using Flask and FastAPI, you can check the below articles. In this post, we will see how to deploy your ML model on the web using Streamlit.

What is Streamlit?

Streamlit is an open-source framework to easily build and share your web app. Using Streamlit you can create and deploy your ML model as a python service without any prior knowledge of UI tools.

Streamlit is a specialized framework for ML model deployment on the web, as

  • It provides a straightforward and fast way of hosting an ML model as a service.
  • You don’t need any prior knowledge of HTML, CSS, JavaScript, or handle any HTTP request.
  • You can do hot-deployment of your app, which means your changes will reflect instantly and there is no need to bring your web app down.

Model Deployment using Streamlit

Streamlit Setup

The setup for Streamlit is the same as any other Python module. Open the terminal and install Streamlit using the below command.

Image by Author

Creating web-app

To create a basic FastAPI, we’ll follow these steps-

  1. Import Streamlit and FastAPI
  2. Create a web page with all the columns of the ML model
  3. Create a predict button and call predict from its click action
  4. And finally, return the prediction with a text message

Test web app

To test Streamlit API, run the command below on the terminal-

Image by Author
Image by Author

It will open a webpage on your local port 8501.

Image by Author

Deploy ML Model

Let’s take an example to understand the ML model deployment with Streamlit. Here we’ll create a machine learning model for Income Prediction, then build an API & deploy it using Streamlit Server.

Create ML Model

As this post is about the deployment only, I won’t cover the model creation part here but you can get the complete code and dataset for creating the Income Prediction ML model, from this git repo. Once you create the ML model, save it. Also save any encodings you are doing while modeling creation, as we have to do the same encodings for the API request.

Deploy on Streamlit Server

After creating a ML model, you can deploy the model either on the cloud, like AWS, Google Cloud, or Azure. Or you can also deploy the web-app on Heroku.

I have written a separate post on deploying the ML model on Heroku, that you can refer to.

Streamlit also comes with a Streamlit Server to deploy a web app. To deploy on Streamlit, create a GitHub repo with all the files for the ML app. Now go to the Streamlit website and connect it with your GitHub repo. Click on the New App button.

Image by Author

It will open a page like below. Fill in all the fields and connect your streamlit repo, same as shown in the image below.

Image by Author

Now click on the Deploy button and deploy the web app on the Streamlit server. If there are any errors, you can see them in the logs. To see the logs, click on “Manage App” at the bottom-right corner of the deployed web app. It will be shown in the image below.

Image by Author
Image by Author

Test Web App

Once the web app is deployed on the Streamlit server, a web page will open in the browser as shown below-

https://app-ml-deployment-dhz5s2lrj7v8gdictzovvq.streamlit.app/

Fill in the fields and click on the Predict button. It will show the income prediction at the bottom of the page.

Image by Author

Conclusion

Streamlit is an impressive lightweight framework for web deployment. It is one of the best-suited technology for data science apps. Streamlit provides data scientists and analysts with a framework to create interactive, web-based applications. Using Streamlit, data scientists can focus on ML development leaving the hard part of making it available on the internet.

To access the complete source code to deploy the ML model as a web app using Streamlight, please refer GitHub link.

If you want to learn more about model deployment strategies, please refer-

Thank you for reading until the end. Before you go:

Please like, share, follow & subscribe for more such content. As always, please reach out for any questions/comments/feedback.

Github: https://github.com/charumakhijani
LinkedIn:
https://www.linkedin.com/in/charumakhijani/

--

--

Charu Makhijani

ML Engineering Leader | Writing about Data Science, Machine Learning, Product Engineering & Leadership | https://github.com/charumakhijani