You have worked on a spectrum of Machine learning problems, worked on various data science courses, spent hours on data science projects, and also dealt with various datasets available. For any project you mention in your resume, it’s always a cherry on a cake to present your projects in a form of Web Application. This not only shows your Machine learning skills but also shows the recruiter that you can embed your ML projects as a web application. Afterall, A Machine Learning project is never really complete if we don’t have a good way to showcase it. By the end of this blog, you are going to get comfortable with using Streamlit to build a basic streamlit web-application which you can easily customize for any of your Machine learning Project.

So firstly, let’s understand, what is streamlit? Streamlit is an open-source app framework that is the easiest way for data scientists and machine learning engineers to create beautiful, performant apps in only a few hours! Because of the ease with which one can develop a data science web app, many developers use it in their daily workflow. Under the hood, it uses React as a frontend framework to render the data on the screen. So, React Developers can easily manipulate the UI with few changes in the code.

Create ml application using streamlit

Let me introduce you all, with the dataset, I would be dealing with, to demonstrate building our basic web-application on streamlit. Dataset considered, is the simplest Iris dataset, extracted from Kaggle, It includes three iris species with 50 samples each as well as some properties about each flower. One flower species is linearly separable from the other two, but the other two are not linearly separable from each other. The link for this dataset is given below.

Link: https://www.kaggle.com/arshid/iris-flower-dataset

In this web-application, we will enable users to select the dimensions of petal and sepal lengths of the iris flower and based on the dimensions selected by our users, our application will be predicting which iris species the dimensions belong to!

I will be training this dataset, using a simple logistic regression technique (not giving much emphasis on the training part), and saving the trained model as a Pickle file. Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk, in our case we will be saving our trained model in a pickle file and later will be using that model for predictions in the back-end of our application. Please refer to the GitHub link for directly downloading the pickle file for iris data.

GitHub Link for Pickle file:

https://github.com/GauravSahani1417/Kaggle-Datasets/blob/master/IRIS-model.pkl

Before starting off with building streamlit application, Please make sure, you have installed streamlit, if not, Please enter the following command in your respective terminal.

Now, Let’s get started! We’ll start calling the important libraries, which would be used to build your first streamlit application, as well as importing our Iris classification model, saved previously as (.pkl) file in a variable named “model”, using pickle.

Importing PIL Library is optional since it's used to import images in our web-application, but recommended to import so, just because it makes our application more presentable.

Now, we’ll give a header for our application, and import an Image, making our application look better:)

Well, coming to the important part, it’s time to take feature values as input from our Users, and based on those inputs, our Iris Classification model would predict the class of the Iris flower respectively, Streamlit provides us wonderful options to accept the inputs in form of text-boxes, as well as Slider options, here, we would go with Slider options, which makes easier for users to insert values.

We’ll accept each feature values such as Sepal length and width, and Petal length and width respectively, using slider option defining the range between the user can select a value, and store these values, in a variable named “data”, shown as,

The data, we collected, needs to be converted into a Data Frame, before using this for prediction, which can be done as follows,

Now, it's time to provide these inputs to our classifier model, which will finally predict the class of the Iris flower, as an Output!

Here, “predict_proba” command will basically give the prediction, in the form of probability percentages of each class predicted by our model, or you can just use “predict”, to simply predict the class.

We are done model predictions, except presenting the results to our Users, which can be done by,

Finally, we are done with coding your basic Streamlit Web-application.

Now perform the following steps, to open your Web-Application, which would render on your Local server,

  • Open your terminal, and Activate the virtual environment(if any).
  • Change the directory, in which you have saved the python file of your Web-application.
  • Execute the command, “streamlit run app.py” to render your application on your local server!

Congratulations! you have created your own Streamlit application!, and Probably your application would look like this,

Now, this application can be a baseline step for creating any of such applications of your choice! You can enhance the designing experience by exploring more amazing features offered by streamlit. Presenting your Machine learning projects in the form of web application not only lets you learn data science with an end-to-end approach, but also you get a glance of the data lifecycle which is followed in industries.

Additionally, would suggest reading the official documentation of streamlit, which will surely open a spectrum of features you can try with.

Github Link for the whole Streamlit code:

https://github.com/GauravSahani1417/Kaggle-Datasets/blob/master/app.py

Stay tuned with Board Infinity for more such amazing blogs and Thanks for reading.