ColabCode: Deploying Machine Learning Models From Google Colab
New to ColabCode? Learn how to use it to start a VS Code Server, Jupyter Lab, or FastAPI.
By Kaustubh Gupta, Python Developer
Photo by Niclas Illg on Unsplash
Google colab is the handiest online IDE for Python and Data Science enthusiasts. Released in 2017 for the public, it was initially an internal project used by the Google research team to collaborate on different AI projects. Since then, it has gained a lot of popularity due to its easy to use interface, Jupyter notebooks similarity, and GPU support gave it a boost.
Most of the popular machine learning libraries such as numpy, pandas, seaborn, matplotlib, sklearn, TensorFlow come pre-installed in this cloud environment so you don’t require any explicit prerequisite. You can also install any python package of your choice to run your code. In this article, I will explain to you a simple way to deploy your machine learning model as an API using FastAPI and ngrok.
What is FastAPI?
It is a high-performance web framework to build APIs in Python. Traditionally, most of the developers used flask as the first option to build the API but due to some of the limitations but not limited to data validation, authentication, async, and many more, FastAPI gained a lot of popularity. FastAPI offers automatic docs generation functionality, authentication, data validation via pydantic models. To know more about the significant differences between the two frameworks in detail, you can check-out my article on Analytics Vidya here:
FastAPI: The Right Replacement For Flask?
This article was published as a part of the Data Science Blogathon. Introduction After you are done with model building…
FastAPI helps in setting up the production-ready server but what if you want to share this with your team before deploying it in an actual cloud platform such as Heroku. The ngrok comes to the rescue by tunneling your localhost and exposing it to the internet. Now, anyone can access your API endpoint via the link provided but what if, all of this could be done on the Google Colab only? The solution to this is ColabCode!
What is ColabCode?
It is a Python package that allows you to start a code server right from your Colab notebooks without setting up anything locally on your system. It can be used to start a VS Code environment, Jupyter Lab server, or tunnel the FastAPI server to the web, all in the colab notebook. This can be a great plus point for the enthusiast coders who train their models on the cloud and now want to share their findings with the world in the form of APIs. We will discuss all the functions of ColabCode as we go till the end of this article.
Preparing the FastAPI Code for ColabCode
The problem statement I would be using for this article is a decision tree classifier to classify between two music genres: Hip-Hop or Rock. I have already done the cleaning of the dataset and it is available on my GitHub repository. Now that I have the dataset, I have simply imported it into the notebook and trained a decision tree model without any preprocessing or EDA stuff as this article is more about deployment. Therefore, this model may return some wrong results!
Code for model creation
1. Now, if you are aware of the FastAPI architecture, we will require a data class for the inputs. This data class allows FastAPI to validate the inputs to be sent to the model and if any wrong input is given, it simply raises the error without giving it to the model.
2. Creating a data class is pretty straightforward and requires only the parameters to be accepted along with the data type. To further customize, you can also add a custom example to quickly test out the results. The code for this class is:
Data Class Code
3. Now, we need to create an endpoint where all the requests will be served. The endpoint creation in FastAPI is very similar to the flask and only requires the endpoint function to take in the data class for validation.
If you wish to explore some cool projects then check out my GitHub profile:
kaustubhgupta - Overview
🔭 I'm currently working on Exploring Python and all its functionalities. 🌱 I'm currently learning how to work…
Our FastAPI is ready and now the only thing needed is to run this via the colab environment. Firstly, import this package and initialize the server:
from colabcode import ColabCodeserver = ColabCode(port=10000, code=False)
The port number can be of your choice and the code parameter should be false. Now, the server is ready to receive the FastAPI object and with the help of ngrok, the local host is tunneled and exposed to the public via a unique URL.
And that’ it! You will get a ngrok URL which you can share with your community, team, or whichever person. The link will expire as soon as you terminate the cell process and if you don’t, Google Colab terminates it after some time. An example GIF of this process is shown below:
GIF by Author
Here, after clicking on the link, I navigated to the endpoint /docs that the FastAPI generates automatically, and here, we can test out the results. The whole notebook code is available here if you want to run and try this thing. Just open this notebook and run all the cells.
Code by Author
ColabCode is not limited to running FastAPI codes on Colab but it can also provide VS code server and Jupyter Lab server! It can help users to have a familiar environment on the cloud-based services. A user with very limited resources can also benefit from these services. It becomes very similar to the newly introduced GitHub Codespaces that provides VS Code environment on the web. To run VS Code server via ColabCodes, do the following:
from colabcode import ColabCode ColabCode()
You will be prompted with a ngrok URL and after the loading is completed, you will have the VS Code running on your web browser like this:
image by Author
Similarly, if you want to open a Jupyter lab server, pass the lab parameter as true and code false.
from colabcode import ColabCode ColabCode(code=False, lab=True)
You will get a URL and a token for authentication to the server. Enter the token and hit login to get a screen similar to this:
Image by Author
In this article, I explained what the ColabCode offers with a little introduction to FastAPI. Currently, v0.2.0 is released by Abhishek Thakur, the creator of this package. Check out the GitHub repository of this package if you wish to contribute.
With this said, it’s the end of this article, I hope you got something new to ponder about and I will pop in your feeds via some other articles!
My Other Popular Articles:
Build Dashboards in Less Than 10 Lines of Code!
Exploring ExplainerDashBoard, the easiest way to Develop Interactive DashBoards
Rebuilding My 7 Python Projects
This is how I rebuilt My Python Projects: Data Science, Web Development & Android Apps
Run Python Code on Websites: Exploring Brython
GitHub Action That Automates Portfolio Generation
Dockerized GitHub Action using Python and Basic Front-end.
Bio: Kaustubh Gupta is a Python Developer interested in Data Science and Machine Learning, having worked on various data related projects, with an interest in real-world applications of Machine Learning.
Original. Reposted with permission.
- How to Create and Deploy a Simple Sentiment Analysis App via API
- Multilingual CLIP with Huggingface + PyTorch Lightning
- When to Retrain an Machine Learning Model? Run these 5 checks to decide on the schedule