Setting Up HuggingFace Spaces with Gradio
Gradio provides an easy way to explore and test your ML model´s performance. Here´s how (a review of FastAI´s deployment lesson).
After training a ML model, the next step is to show its capabilities to others. Now, production and the development of a complete full-stack application to showcase your models can turn into a new project by itself, requiring a different set of abilities to those for machine learning (CSS horror starts here).
Gradio is a great alternative to this. It provides an easy way to explore and test your model´s performance. Here´s how (a review of FastAI´s deployment section):
Something that allowed me to move faster throughout the process of utilizing a pre-trained model, testing it, and deploying it is realizing the different instances in which you would use online services like Kaggle notebooks or Google Colab and your local setup.
Downloading your Trained Model
The first obvious step is to get your model into your laptop. We´ll assume the app you want to deploy is based on your local machine through any virtual environment config. Ever heard of GPUs? These can be used to train and download your model from resources like the ones mentioned above (Kaggle or Colab) to download the model you want into a folder on your machine. This is the first instance in which I would utilize an online resource over a local setup. Kaggle allows an easy way to download files that your notebook delivers in its output section.
A great way to do so is with FastAI´s libraries, you can check a summary of this process in this Kaggle notebook.
learn.export('model.pkl')
This is what you would expect from the first part of the process: a trained model, download it, and set it locally.
HuggingFace Setup
If you haven´t already, set up a HuggingFace account to showcase your projects, models, and datasets. We will be creating a space to host our machine learning model on the web for everybody to try it. The process for this is very simple:
Create a HuggingFace account.
Click on 'new' and select 'space'.
Add a name, and a license (Apache works fine), and remember to select Gradio as the tool to create the space.
This will create a git project which you will clone into your laptop.
If you are unfamiliar with Git, watch some tutorials on YouTube (completely worth it) so you know the basics of the steps you will perform now.
Now after creating your space, HF will guide you through the setup on your computer.
What you are doing is setting up a Python application to a Gradio interface on the web. This is, instead of an 'input' field as with regular Python, Gradio will now assign a text input field to place your text, a function to run every time you click a button, and the output your function returns.
You should now have a local Python application that takes a name and returns a "Hello" + your name
next to it. Believe it or not, this is a great stepping stone to construct the base for our whole application.
import gradio as gr
def greet(name, intensity):
return "Hello, " + name + "!" * int(intensity)
demo = gr.Interface(
fn=greet,
inputs=["text", "slider"],
outputs=["text"],
)
demo.launch()
Testing our Model
To test our model before deploying it to Gradio, we can utilize Google Colab to easily play with the model we want to try (trained in Kaggle) and some files to test the model with. This is just to see if our app will work whenever we deploy it to Gradio (change the function above) and to know what code should be placed inside our app.py
file, created in the last step. If you followed the notebook mentioned above, refer here to see how you can easily test and expand its capabilities. The only thing you need is to upload to your local session the model you just downloaded.
Now, why so many setups for a single model? My personal preference is to pip install
as few things as I can do to my local setup. Both Kaggle and Colab are useful to avoid the infinite setup loop that may take you hours to run just before you can import some essential libraries (Tensorflow or FastAI for instance) and make them work locally. Sure, you can do the complete online setup only on Colab, and that´s what I would recommend to be honest (Gradio interface works better on Colab), but I like Kaggle´s 'output' section and it was easier for me to download my model that way for this project.
Remember that, in the end, we just want to verify what is the code we want to place inside our app.py
file.
Deployment
Now once you checked the Colab resource and made sure your model works as intended, you can just add the needed lines of code to your local app.py
file created by Gradio! My finished product deployed in HF looks like this internally and you can get an idea of how the notebook and the Python script share the same components.
Now the model we trained classifies both the breeds of dogs and cats, but you may get a sense of how you only need a dataset and know its labeling function to build anything you want. The gradio interface accelerates the addition of inputs, outputs, headings, descriptions, and other 'front-end' terms we don´t need to focus on for a quick setup.
Extra Bits
You can easily try for this project many of the models labeled in the Colab notebook provided above. A great notebook explaining this is Jeremy Howard´s. Now, you will see an image below with all the different models from the list in a graph. You may think the right approach to build a great ML setup would be to read it right-to-left since these are the most 'advanced' models. Jeremy mentions this is the mistake of many beginners: trying to get everything perfect the first time.
Instead, aim to have results as quickly as possible and iterate from there. Try the models from left to right as you may not even need to improve your accuracy by 0.001% in most cases.