Deploy a Machine Learning Model using Streamlit Library
Machine learning is the ability of a computer to learn from its experience, even if it has not been programmed. Machine Learning is a hot field right now, and many top companies around the globe are using it to improve their products and services. A Machine Learning model that is not trained in our Jupyter Notebook is useless. We need to make these models available for everyone so they can be used.
In this tutorial we will train an Iris Species classification classifier and then deploy the model with Streamlit, an open-source app framework that allows us to deploy ML models easily.
Streamlit allows us to create apps for our machine-learning project with simple Python scripts. Hot reloading is also supported, so our app can be updated live while we edit and save our file. Streamlit API allows us to create an app in a few lines of code (as we'll see below). Declaring a variable is the same thing as adding a widget. We don't need to create a backend, handle HTTP requests or define different routes. It's easy to set up and maintain.
First, we will train the model. As the primary purpose of this tutorial, we will not be doing much pre-processing.
Required Modules and Libraries:
First, we must install the following:
We will now drop the Id column as it is not necessary for Iris species classification. Next, we will divide the data into a training and testing set and use a Random Forest Classifier. Any other classifier can also be used, such as logistic regression or support vector machine.
Our Prediction Accuracy is: 0.9777777777777777
As, we got the accuracy of 97.77%, which is quite good.
To use this model for predicting unknown data, we must save it. A pickle is a tool that serializes and deserializes a Python object structure.
A new file called "classifier1.pkl", will be created in the same directory. We can now use Streamlit to deploy our model -
Copy the code below into another Python file.
This command can be executed by entering the following command into the terminal:
app1.py is where the Streamlit code was written.
After the website opens in our browser, we can then test it. We can also use this method to deploy deep learning and machine-learning models.