Model Deployment using Streamlit | Deploy your first Machine Learning model with Streamlit

Hello and welcome.
Hope you are fine and your Machine learning journey is going very smoothly and I know there are a few ups and downs in understanding some of the concepts. Struggling a little bit in learning any technology and if we are starting from the very first it is important to take an original taste of particular technology. And Machine learning comes in as one of the top trending technology.

So, if you want to deploy your Machine learning model that can be used by end-user but do not have knowledge of frontend and how to design it then Streamlit is for you guys and if you want to build a web application for your project under 30 minutes then also streamlit is for you.

In this particular tutorial, we will see how to build a web app for any machine learning use case using streamlit. we will be developing and deploying a simple use case of predicting Employee salary based on years of experience and you can use it's a method to build a web app for your project.

In our previous article, we have studied the basics of streamlit and how to create a simple form and take user input in different forms. If you are first to streamlit then please check this article once.

let's get started.

Implementing Streamlit Web App

We will make 3 sections in the navigation bar which will be as follows
  1. Displaying Data and Plots
  2. Making Predictions
  3. Contribution
So, let's start coding for each section step by step and before that, we will set out a file with particular imports and make sure you download the data.
Create a file name '' you can name as you want and start.

Step-1) Import all the necessary libraries

we will need pandas for data manipulation, matplotlib for displaying non-interactive plots, and plotly for interactive plots. And streamlit for designing and creating an application

Step-2) First Create a model

we will be using a simple linear regression algorithm to predict a salary. so the first load a data and train a model which will be used in predict bar to predict the salary of the employee that we will be getting. 

Step-3) Create a Navigation Bar

let's create a navbar with three functionalities as we talked about.

Step-4) Code for displaying Plots

we will take a user's input as to whether he/she wants to see an interactive plot or a non-interactive plot. and according to user input, we will use matplotlib and plotly for creating and displaying plots. Before plotting we will also add an option to view data in the form of a table.

Step-5) Code for Making a Prediction

Now we will be coding for the second navigation that we added a prediction. here we will be taking a year of experience as input from a user and display the predicted salary based on input. For making the prediction first convert the year of experience into a 2-d array because the model takes input in 2-d format to predict the output.

Step-6) Code for taking a user Contribution

We have arrived at implementing the final functionality as taking a user contribution that if a user wants to add some data in our dataset on which model is been trained so he/she can add that. we will take years of experience and salary and create a dataframe of input that we got and add that to our dataset. 
This method will help our model to learn different hidden patterns and avoid making the same mistake again. 

That sit guys. The coding of the application is complete and you can see hardly it took 20 to 25 min to code a simple web application and it only takes 80 to 85 lines and no HTML or CSS is required. 


I hope you have enjoyed working with streamlit and its functionalities which provide an easy implementation for any Machine learning web app in very little time without creating any extra files. You can simply deploy this web application to the cloud for example Heroku by following the same steps that we have seen in Model Deployment with Flask article.
Be safe, Keep Learning, Happy Learning
Thank You!..

Post a Comment

If you have any doubt or suggestions then, please let me know.

Previous Post Next Post