Deploying deep learning models for free😎

Sankalp ManeSankalp Mane
4 min read

While learning machine learning, the advice you get most is to create end-to-end projects or atleast deploy models for showcasing. Now as the model size is less for conventional ML models, it's easier to deploy them on platforms(e.g Heroku) with free subscriptions. In contrast, deep learning models are quite large and thus difficult to deploy.

The other problem we might face is developing a web app with a frontend(e.g HTML, CSS) and a web framework(e.g. Flask, FastAPI) to handle the routes. The suggestion that you get most often is to deploy the app using serverless services on different cloud platforms or create a VM for more customization. But after a certain limit, these services might incur charges which is not a viable option for small-scaled projects.

The solutions to these problems are:

Gradio

One of the best ways to share your machine learning model, API, or data science workflow with others is to create an interactive app that allows your users or colleagues to try out the demo in their browsers.

Spaces

Hugging Face Spaces offer a simple way to host ML demo apps directly on your profile. This allows you to create your ML portfolio, showcase your projects to stakeholders, and work collaboratively with other people in the ML ecosystem.

Till now you must be like bored So here it is!🚀

Creating a gradio app

1) Installing Gradio in the python environment

pip install gradio

2) Creating the interactive app

In this case, I wanted a simple heading and an interface where I can upload an image and showcase the output. For this, I used blocks object that lets you combine multiple components.

  • Markdown - used here to add heading to the page
  • Interface - Gradio class which has predefined structure which might be helpful for your ML needs
  • predict_image - function which takes the input image and runs object detection code over it

3) Run the script

In the project folder containing app.py file run the following command in the terminal:

python app.py

Here's the complete code for the project.

Deploying on Spaces

When you are deploying a gradio app on spaces the file name should be app.py and there should be a requirements.txt file to install the required libraries.

Now, at first, you need to push all your project files and model weights to Hugging Face Hub. The tools that we'll be using are:

After activating your python environment execute the below commands:

$ python -m pip install huggingface_hub

$ huggingface-cli login

The login command will ask you to enter a token for validation. Login to your huggingface account and go to https://huggingface.co/settings/tokens Access Tokens -> New Token -> role-write -> Generate

Now enter this token value for validation in your login command.

Here comes the tricky part

The git-lts needs to be setup already to track your large files when they are added to your local repo. If not, then they will be tracked using git and you won't be able to push your files to the remote repo(Github, huggingface Hub).

Workaround🛠

1) create a new space (you need to be logged in)

spaces_warehouse.png

This new created repo will contain 2 files:

  • .gitattributes - will help git-lts to track large files
  • README

2) Bring the repo to local computer

git clone https://huggingface.co/spaces/<your-username>/<your-space-name>

3) Before adding your project files to this cloned repo, we need to initialize git-lts to track large files that we will add

# terminal location should be inside the cloned repo
git lfs install

4) Now we can copy-paste all the files in this folder and then commit those changes

$ git add .
$ git commit -m "First model version"  # You can choose any descriptive message
$ git push

After this, the code will be pushed to the remote repo where requirements will get installed and then the app.py script will get executed. The final output will be reflected in the App section of your space.

Here's the link to the space that I created; Do check it out!

Thanks for exploring🤗​

8
Subscribe to my newsletter

Read articles from Sankalp Mane directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sankalp Mane
Sankalp Mane

I am a Data Science Enthusiast inquisitive about the different innovations happening in the field of AI. Fond of modular programming in python. Sharing the knowledge that I gather in this journey of continuous learning.