Christy Jacob

Image classification with Hugging Face

Image classification is a key application of machine learning that involves categorizing images into predefined classes. Hugging Face is a leading AI company that offers an extensive library of pre-trained models for various natural language processing and computer vision tasks, including image classification. By integrating Hugging Face's powerful models with your applications, you can leverage state-of-the-art technology to automate and enhance image analysis.

How does the integration work?

You can utilize a pre-built Appwrite function template to add image classification with Hugging Face to your app. This will allow you to upload an image to an Appwrite storage bucket and store labels from the image in an Appwrite collection.

How to implement

To implement the Hugging Face image classification function, there are several steps you must complete:

Step 1: Sign up for Hugging Face

First, you must sign up for a Hugging Face account. Once your account is set up, visit your profile settings, head to the Access Tokens page, and create an access token with the Inference permissions. Save this token for further usage.

Create API token

Step 2: Create the Appwrite Function

For this step, you must create an account on Appwrite Cloud or self-host Appwrite if you haven’t already. If you decide to self-host Appwrite, there are additional setup steps to use Appwrite Function templates.

Head over to the Appwrite console, navigate to the Functions page, click on the Templates tab, and search for the Image Classification function template.

Function template

During the setup process, click on the checkbox to generate an Appwrite API key on completion and add the Hugging Face access token in the Variables step. If you are self-hosting Appwrite, then click on the optional variables dropdown and update the Appwrite endpoint to your instance’s publicly accessible endpoint.

Environment variables

Then, create a new repository with the default branch and root directory settings. You can edit this repository later to update the function logic.

Step 3: Test the function

Once all the steps are complete, it is time to test the function! Use the Appwrite console or one of Appwrite’s SDKs to upload an image to the image_classification storage bucket. If successful, you will find a response saved in the image_classification collection in the ai database in the following format:

imagelabels
66a13bf100318c752f1b[{"label":"llama","score":0.9664694666862488},{"label":"comic book","score":0.019199883565306664},{"label":"suit, suit of clothes","score":0.005637330934405327},{"label":"television, television system","score":0.00037627643905580044},{"label":"muzzle","score":0.00019786457414738834}]

The image attribute contains the ID of the image file uploaded to the image_classification storage bucket, and the labels attribute contains the response from Hugging Face, including all the labels and their confidence score.

Read more about Hugging Face and Appwrite Functions

If you would like to learn more about Hugging Face and Appwrite Functions, we have some resources that you should visit:

Become a Technology Partner

Join our Technology Partners program to integrate your solutions with Appwrite’s API, enhancing functionality and expanding your reach.

Get Started