Model Deployment using Streamlit (A Practical Approach)
Table of Contents
- Overview of Model Deployment and its importance
- Introduction to Streamlit and its features
- Case study; School grading machine model
- Model Deployment of the School Grading model using Streamlit
What is Model Deployment?
The model deployment in data science refers to making the predicted model become an application using new data.
Building a model is generally not the end of the project. If the model is to increase knowledge of the data, the knowledge gained will need to be organized, presented such that the customer/ client can use it or access it in a simple form such as Website Application, Andriod Application, or Desktop Application.
In many cases, it will be the customer, not the data analyst, who will carry out the deployment steps.
For example, a company needs to understand which of their staff should be promoted or not, the deployment model needs to be deployed in such a way that the Human Relation officer who is a user and needs to process the information can access it in its easy form.
Why model deployment?
A machine learning model can only begin to add value to an organization when that model’s insights routinely become available to the users for which it was built. The process of taking a trained ML model and making its predictions available to users or other systems is known as deployment.
In order to start using a model for practical decision-making, it needs to be effectively deployed into production. If you cannot reliably get practical insights from your model, then the impact of the model is severely limited.
What should you know about Streamlit
Streamlit is a popular open-source framework used for model deployment by machine learning and data science teams. And the best part is it’s free of cost and purely in python.
It is easy to visualize, mutate, and share data. The API reference is organized by activity type, like displaying data or optimizing performance
Note: To understand the model deployment, you should be a data scientist, who has been building models. the case study will focus on the deployment of the model using Streamlit.
Features of Streamlit
Here are some of the key features of Streamlit which I found really interesting and useful:
- It quickly turns data scripts into shareable web applications. You just have to pass a running script to the tool and it can convert that to a web app.
- Everything in Python. The best thing about Streamlit is that everything we do is in Python. Starting from loading the model to creating the frontend, all can be done using Python.
- All for free. It is open source and hence no cost is involved. You can deploy your apps without paying for them.
- No front-end experience is required. Model deployment generally contains two parts, frontend, and backend. The backend is generally a working model, a machine learning model in our case, which is built-in python. And the front end part, which generally requires some knowledge of other languages like java scripts, etc. Using Streamlit, we can create this front end in Python itself. So, we need not learn any other programming languages or web development techniques. Understanding Python is enough.
Case study: School Grading Machine Model deployed using STREAMLIT.
We will be working through the deployment of the School Grading Machine Model deployed using STREAMLIT.
The school grading machine model is to understand the probability for a candidate to get admission to college based on factors such as CGPA, GRE Score, TOEFL Score, Research Experience, Rating of the school you are trying to get into.
Model built?
The model was made using a dataset from Kaggle along with using Kaggle notebooks to train the model. We made use of Sci-Kit learn in order to make our Linear Regression Model.
you can check out a likewise notebook on Kaggle here
Features
Some of the features for the built and deploy model are
- CGPA
- GRE Score
- TOEFL Score
- Research Experience
- Rating of the school you are trying to get into.
Saved model
After you have successfully built your model, as a data scientist the next thing to do is to save your model on your working using a python library called PICKLE.
Model Deployment of the School Grading model using Streamlit
We will start with the basic installations:
We have installed 2 libraries here. This will help us to host our web app. Streamlit will be used to make our web app.
Next, we will have to create a separate session in Streamlit for our app.
This will help you to create a session for your app. Finally, we have to create the python script for our app.
Let me show the code first and then I will explain it to you in detail:
This is the entire python script that will create the app for us. Let me break it down and explain in detail:
In this part with #libraries, we are loading the required libraries which are pickle to load the trained model and streamlit to build the app. Then we are loading the trained model and saving it in a variable named model.
in the part with #definning the function of the model, here is the main app. First of all, we created markdown text showing the title and description of the project, we are defining the header of the app. It will display “What is this Project about”. To do that, we are using the markdown function from streamlit.
Next, we are creating five boxes in the app to take input from the users. These 5 boxes will represent the five features on which our model is trained.
The first box is for the CGPA of the user. The user will have a range of 0.0 to 10 sliders, and they will have to pick one from them. Also, the GRE Score within the range of 0 to 340, we have the research to be slider between Yes=1, No = 0, Next, we are defining the slider for the University rating between the scale of 1 to 5.
At the end of the app, there will be a predict button and after filling in the details, users have to click that button. Once that button is clicked, the main function will be called and the result of the school grading will be displayed in the app.
This completes the web app creating part. And you must have noticed that everything we did is in python. This is amazing, it is easier to relate with from your model-building journey.
Now, let's run the web application
This will generate a result that like this
The web application looks like this
You can see, we first have the name displayed at the top. Then we have 5 different boxes that will take input from the user and finally, we have the predict button. Once the user fills in the details and clicks on the Predict button, they will get the probability score of the student generating admission.
We have successfully deployed your machine learning models using Streamlit.
For Practice,
To run the same code, check the code out at my Github link here
After downloading it, load the folder in a VS code editor.
Then run the code on the terminal
The numbers of input in your model must be the same when trying to deploy the model.
About the author
Odeajo Israel is a Google TensorFlow Certified professional with four years of expertise in the analysis sector. He helps organizations make data-driven decisions and design thresholds and metrics that are specific to their organization. Israel is also the Zindi ambassador for Nigeria.
He is quite enthusiastic about topics such as deep learning, machine learning, big data, and artificial intelligence. In Nigeria, he is one of the co-organizers of the AI movement and acts as a facilitator for it. He leads AI meetups, workshops, and events with the goal of constructing a community of data scientists who can tackle local problems.