DEV Community

Cover image for Huggingface.js: Quick- Guide to Getting Started
Olu
Olu

Posted on

Huggingface.js: Quick- Guide to Getting Started

Have you ever wondered about the friendly emoji-like logo associated with HuggingFace? No, its not just another cute emoji; instead, it is the heart and symbol of a thriving community who's dedicated to making machine learning and artificial intelligence more approachable for enthusiasts, developers and researcher's alike. Lets dive deeper into understanding HuggingFace and how anyone use their awesome features.

What exactly is HuggingFace ?

It's a vibrant community and versatile platform centered around ML/AL models. The platform allows users to create, fine-tune, and deploy cutting-edge models effortlessly. The community fosters collaboration among professionals and hobbyists eager to advance ML research and applications.

Why join the HuggingFace Community ?

Aside from being part of an interesting ,and passionate group committed to pushing ML boundaries , joining the HuggingFace community offers several benefits:

  • Share and discover thousands of ready-to-use models, datasets, and demos tailored to various ML tasks.

  • Connect with fellow ML engineers, DS, researchers and beginners to exchange ideas , seek guidance and collaborate on exciting projects.

  • Contribute to open-source initiatives and improve existing models or datasets based on use-cases.

Exploring The Hugging Face Model Hub

Think of the Hugging Face Model Hub as an extensive online library filled with valuable resources for programmers. Here is what you can do:

  • Models: These recipes teach computers to process and analyze text, images, audio and other types of input.With access to over 350K pre-trained models, anyone can quickly get started on any ML project! (No need to reinvent the wheel)

  • Datasets: These are like stacks of books waiting to be read , datasets provide essential raw material for trading and refining ML models. The Hub hosts more than 75K diverse datasets , enabling you to experiment with various scenarios and enhance your models performance.

  • Spaces: These are awesome! Spaces are environments ( dedicated place to host your model / Use Models in your Web Browser) to showcase live demos of ML models right inside your web browser. By offering 150K+ such demos , Hugging Face makes it super easy to explore and tinker with state-of-the -art techniques without setting up complex infrastructure.

In addition, the Hugging Face Hub keeps track of every change made to these assets, similar to your browsers history option.

Now that we've explored the basics let's discuss using Hugging Face effectively.

Getting Started with Hugging Face

To begin leveraging Hugging Face, follow these simple steps:

Step 1. Visit the official website

Step 2. Obtain a user access token (API key) – Note: To utilize the Inference API on your private models, providing an API token is mandatory.Once set up, here are some cool features available to you:

  • Choose from 100+ high-quality pre-built models

  • Easily upload, manage, and host your custom models securely

  • Perform various ML tasks, including classification, named entity recognition, conversational agents, summarization, translation, question answering, and embedding extraction

  • Deploy large models seamlessly, even if they pose challenges during production deployment

  • Develop your business solutions on top of the trusted open-source ML framework

Lets Code - Use any IDE you want ( VSCODE works , but sublime, replit or GitHub codespaces are fine!)

  1. Open a new / empty directory!

  2. Import required libraries & Run 'npm init -y' for Node SDK (In your new package.json file - add the following after name, version, main.

 "main": "huggingface.js",
  "type": "module", 
Enter fullscreen mode Exit fullscreen mode
  1. Specify your Hugging Face access token

  2. Initialize the Hugging Face Inference class

  3. Define the model and the image you want to caption

  4. Fetch the image as blob

  5. Use the ImageToText Function to get the image caption

  6. Log the results!

Note: To install the package and dependencies use:

Models from hugging face

Node SDK - Run 'npm init -y' to setup your Node.Js environment and create the package.json file

# Step 1 - Import required libraries 
import { HfInference } from "@huggingface/inference" 
import dotenv from "dotenv"
dotenv.config 
# Step 2 -  Specify your Hugging Face access token
const HF_ACCESS_TOKEN = process.env.HF_ACCESS_TOKEN 
# Step 3 -  Initialize the Hugging Face Inference Class 
const inference = new HfInference(HF_ACCESS_TOKEN);
#Step 4 - Define the model (see hugging face docs) and the image you want to caption 
const model = ""
const imageUrl = "URL_OF_ANY_IMAGE_YOU_WANT"
# IMPORTANT NOTE - You need to play different imgUrls to get the right format! Feel free to check the repo or huggingface space!

Example of imageUrl = "https://ankur3107.github.io/assets/images/image-captioning-example.png"

# Step 5 - Fetch the image as a blog 
const response = await fetch(imageUrl);
const imageBlob = await response.blob();
# Step 6 - Use the ImageToText function to get the Image Caption 
const results = await inference.imageToText({
   data: imageBlob,
   model: model,
});
# Log in Console!
console.log(results); 

Enter fullscreen mode Exit fullscreen mode

Notes:

  • Active API / Access Token

  • From project directory run 'node huggingface.js'

  • You a pass the imageUrl of any image you want/ find online

  • Review the Hugginface JS Inference for how to use and interact with the library.

With Hugging Face, discovering the potential of AI becomes enjoyable and achievable for individuals and organizations alike. So why wait? Join the Hugging Face community today and start exploring!

For further assistance, check out these helpful resources:

Hugging Face Inference API Documentation

**

A one year obsession can change your life
**

My big plan for 2024 is to really focus on learning about smart computer programs, also known as machine learning. I'll be exploring things like Hugging Face, TensorFlow, and PyTorch, which are tools to help make these smart programs. Also, I'll dive into the basics of how these programs work and how we can use them in everyday life.

I believe that spending one whole year learning about this stuff can really make a big difference. So, LFG!!!!

Top comments (0)