Profile
Back to NewsBack
GitHub Trending 6 min
Reader Mode
huggingface/huggingface.js: Use Hugging Face with JavaScript

huggingface/huggingface.js: Use Hugging Face with JavaScript


huggingface javascript library logo

// Programmatically interact with the Hub

await createRepo({ repo: { type: "model", name: "my-user/nlp-model" }, accessToken: HF_TOKEN });

await uploadFile({ repo: "my-user/nlp-model", accessToken: HF_TOKEN, // Can work with native File in browsers file: { path: "pytorch_model.bin", content: new Blob(...) } });

// Use all supported Inference Providers!

await inference.chatCompletion({ model: "meta-llama/Llama-3.1-8B-Instruct", provider: "sambanova", // or together, fal-ai, replicate, cohere … messages: [ { role: "user", content: "Hello, nice to meet you!", }, ], max_tokens: 512, temperature: 0.5, });

await inference.textToImage({ model: "black-forest-labs/FLUX.1-dev", provider: "replicate", inputs: "a picture of a green bird", });

// and much more…

Hugging Face JS libraries

This is a collection of JS libraries to interact with the Hugging Face API, with TS types included.

  • @huggingface/hub: Interact with huggingface.co to create or delete repos and commit / download files
  • @huggingface/inference: Use all supported (serverless) Inference Providers or switch to Inference Endpoints (dedicated) to make calls to 100,000+ Machine Learning models
  • @huggingface/mcp-client: A Model Context Protocol (MCP) client, and a tiny Agent library, built on top of InferenceClient.
  • @huggingface/gguf: A GGUF parser that works on remotely hosted files.
  • @huggingface/dduf: Similar package for DDUF (DDUF Diffusers Unified Format)
  • @huggingface/lerobot: A parser for LeRobot robotics datasets, reading v2.x and v3.0 layouts over ranged HTTP requests.
  • @huggingface/usd-web-viewer: An OpenUSD viewer for the web: composes .usd, .usda, .usdc and .usdz files in WebAssembly and renders them with three.js.
  • @huggingface/tasks: The definition files and source-of-truth for the Hub's main primitives like pipeline tasks, model libraries, etc.
  • @huggingface/jinja: A minimalistic JS implementation of the Jinja templating engine, to be used for ML chat templates.
  • @huggingface/space-header: Use the Space mini_header outside Hugging Face
  • @huggingface/ollama-utils: Various utilities for maintaining Ollama compatibility with models on the Hugging Face Hub.
  • @huggingface/tiny-agents: A tiny, model-agnostic library for building AI agents that can use tools.

We use modern features to avoid polyfills and dependencies, so the libraries will only work on modern browsers / Node.js >= 18 / Bun / Deno.

The libraries are still very young, please help us by opening issues!

Installation

From NPM

To install via NPM, you can download the libraries as needed:

npm install @huggingface/hub
npm install @huggingface/inference
npm install @huggingface/mcp-client

Then import the libraries in your code:

import { createRepo, commit, deleteRepo, listFiles } from "@huggingface/hub";
import { InferenceClient } from "@huggingface/inference";
import { McpClient } from "@huggingface/mcp-client";
import type { RepoId } from "@huggingface/hub";

From CDN or Static hosting

You can run our packages with vanilla JS, without any bundler, by using a CDN or static hosting. Using ES modules, i.e. Chat with me