Tired of making SK-Learn models on Kaggle? Models to Beautiful Web Apps in few minutes for FREE!

Aakash Gupta
4 min readOct 14, 2020

--

This tutorial is for the people who create ML models on Kaggle datasets and then wonder how to make a simple yet interactive web application or dashboard out of it.

Streamlit based Web App

Contents

  1. Exporting the Model
  2. Creating the Web Application
  3. Hosting the Web Application
  4. References

Exporting the Model

There are many libraries/approaches that can help us in this

  1. Pickle Approach
  2. Joblib Approach
  3. Manual Save and Restore to JSON approach

Each of the Approach has few advantages and few disadvantages. This tutorial will be showing the usage of Pickleshare. Pickleshare is the most commonly used libraries and works well most of the time. More details about each approach can be accessed using the link in the references section.

First of all, we need to train the model then test the model and calculate the metrics.

If satisfied with the results, Proceed!

Then, import pickleshare library and dump the model

Dumping the model

In kaggle, the output file is available to download in the output tab, simply right-click and download or after committing, scroll down to the bottom and download.

Downloading after Committing

This .pkl file contains our trained model that we will use in our web application.

PS: The data set is not needed anymore.

Creating the Web Application

There are many options to create a web application like Flask, Django etc but the latest and most apt for making a ML interface is Streamlit.

Streamlit has 13+ calls that are used to make some awesome dashboards and applications. Many tutorials can be found online, Links to few of the best tutorial according to my opinion are mentioned in the references section.

Firstly, Import the pickle file created in step 1 to the Streamlit (app_name).py file.

Importing the Model

PS : For detailed explanation and Repo, please visit this link(web)/mobile

Now, create input fields for all of the parameters required by the model.

Defining Input Fields.

The return value(user input) of the input fields are stored in the variables where they are defined.

For eg: ssc=st.number_input(“Text”,Range_min,Range_max) will store input in the ssc variable.

The input variables are then used to create a single row DataFrame to predict the output using Model.Predict() function of SK-learn

Inputs to Model

The Result/prediction can then be displayed as per personal choice.

Hosting the Web Application

Pythonanywhere, Zeit, Amazon Cloud Services etc are some services where we can host our web application. I personally like heroku for the same as it provides free hosting even with custom domain but on the flipside, it isn’t the fastest and initial setup is not the smoothest.

Apart from the Pickle File and Streamlit file we need to create

  1. A ProcFile
  2. requirements.txt(case sensitive)
  3. setup.sh

requirements.txt contains the libraries that we need Heroku to install for running the app, setup.sh contains server configuration code(just clone the file from the repo and do not change anything) and ProcFile gives the command to the server to run our streamlit files.

Just change the name of the app after cloning

I recommend everyone to clone a working Repo, replace the pickle and app file and use the cloned setup.sh,requirements.txt and ProcFile as it is tricky to get them work. Again, link to Repo as above and in references section.

After gathering all the files, login to github and create a Repository with all the necessary files and then head to heroku. Create a new application and then select Deploy section and connect the Github repository, select the branch and deploy!

Connecting with the Repo

Hopefully now all the things should work. If not, try heroku restart dyno or check logs for some hint. If things workout then Cheers!

References

Some of the best places to learn streamlit (1) (2) (3)

Streamlit official website

Pickleshare

My web app on heroku

About the project and link to the Repo (Web) / (Mobile)

My kaggle notebook for model building

About the other libraries/approaches

--

--