Tired of making SK-Learn models on Kaggle? Models to Beautiful Web Apps in few minutes for FREE!
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.
- Exporting the Model
- Creating the Web Application
- Hosting the Web Application
Exporting the Model
There are many libraries/approaches that can help us in this
- Pickle Approach
- Joblib Approach
- 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.
Then, import pickleshare library and dump 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.
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.
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.
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
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
- A ProcFile
- requirements.txt(case sensitive)
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.
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!
Hopefully now all the things should work. If not, try heroku restart dyno or check logs for some hint. If things workout then Cheers!
Some of the best places to learn streamlit (1) (2) (3)
About the project and link to the Repo (Web) / (Mobile)
My kaggle notebook for model building