Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Utilities to use the Hugging Face Hub API

License

NotificationsYou must be signed in to change notification settings

omahs/huggingface.js

 
 

Repository files navigation


huggingface javascript library logo

// Programatically interact with the HubawaitcreateRepo({repo:{type:"model",name:"my-user/nlp-model"},accessToken:HF_TOKEN});awaituploadFile({repo:"my-user/nlp-model",accessToken:HF_TOKEN,// Can work with native File in browsersfile:{path:"pytorch_model.bin",content:newBlob(...)}});// Use HF Inference API, or external Inference Providers!awaitinference.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,});awaitinference.textToImage({model:"black-forest-labs/FLUX.1-dev",provider:"replicate",inputs:"a picture of a green bird",});// and much more…

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

  • @huggingface/inference: Use HF Inference API (serverless), Inference Endpoints (dedicated) and all supported Inference Providers to make calls to 100,000+ Machine Learning models
  • @huggingface/hub: Interact with huggingface.co to create or delete repos and commit / download files
  • @huggingface/agents: Interact with HF models through a natural language interface
  • @huggingface/gguf: A GGUF parser that works on remotely hosted files.
  • @huggingface/dduf: Similar package for DDUF (DDUF Diffusers Unified Format)
  • @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 Spacemini_header outside Hugging Face
  • @huggingface/ollama-utils: Various utilities for maintaining Ollama compatibility with models on the Hugging Face Hub.

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/inferencenpm install @huggingface/hubnpm install @huggingface/agents

Then import the libraries in your code:

import{InferenceClient}from"@huggingface/inference";import{HfAgent}from"@huggingface/agents";import{createRepo,commit,deleteRepo,listFiles}from"@huggingface/hub";importtype{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. UsingES modules, i.e.<script type="module">, you can import the libraries in your code:

<scripttype="module">import{InferenceClient}from'https://cdn.jsdelivr.net/npm/@huggingface/inference@3.6.1/+esm';import{createRepo,commit,deleteRepo,listFiles}from"https://cdn.jsdelivr.net/npm/@huggingface/hub@1.1.2/+esm";</script>

Deno

// esm.shimport{InferenceClient}from"https://esm.sh/@huggingface/inference"import{HfAgent}from"https://esm.sh/@huggingface/agents";import{createRepo,commit,deleteRepo,listFiles}from"https://esm.sh/@huggingface/hub"// or npm:import{InferenceClient}from"npm:@huggingface/inference"import{HfAgent}from"npm:@huggingface/agents";import{createRepo,commit,deleteRepo,listFiles}from"npm:@huggingface/hub"

Usage examples

Get your HF access token in youraccount settings.

@huggingface/inference examples

import{InferenceClient}from"@huggingface/inference";constHF_TOKEN="hf_...";constinference=newInferenceClient(HF_TOKEN);// Chat completion APIconstout=awaitinference.chatCompletion({model:"meta-llama/Llama-3.1-8B-Instruct",messages:[{role:"user",content:"Hello, nice to meet you!"}],max_tokens:512});console.log(out.choices[0].message);// Streaming chat completion APIforawait(constchunkofinference.chatCompletionStream({model:"meta-llama/Llama-3.1-8B-Instruct",messages:[{role:"user",content:"Hello, nice to meet you!"}],max_tokens:512})){console.log(chunk.choices[0].delta.content);}/// Using a third-party provider:awaitinference.chatCompletion({model:"meta-llama/Llama-3.1-8B-Instruct",messages:[{role:"user",content:"Hello, nice to meet you!"}],max_tokens:512,provider:"sambanova",// or together, fal-ai, replicate, cohere …})awaitinference.textToImage({model:"black-forest-labs/FLUX.1-dev",inputs:"a picture of a green bird",provider:"fal-ai",})// You can also omit "model" to use the recommended model for the taskawaitinference.translation({inputs:"My name is Wolfgang and I live in Amsterdam",parameters:{src_lang:"en",tgt_lang:"fr",},});// pass multimodal files or URLs as inputsawaitinference.imageToText({model:'nlpconnect/vit-gpt2-image-captioning',data:await(awaitfetch('https://picsum.photos/300/300')).blob(),})// Using your own dedicated inference endpoint: https://hf.co/docs/inference-endpoints/constgpt2=inference.endpoint('https://xyz.eu-west-1.aws.endpoints.huggingface.cloud/gpt2');const{ generated_text}=awaitgpt2.textGeneration({inputs:'The answer to the universe is'});// Chat CompletionconstllamaEndpoint=inference.endpoint("https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.1-8B-Instruct");constout=awaitllamaEndpoint.chatCompletion({model:"meta-llama/Llama-3.1-8B-Instruct",messages:[{role:"user",content:"Hello, nice to meet you!"}],max_tokens:512,});console.log(out.choices[0].message);

@huggingface/hub examples

import{createRepo,uploadFile,deleteFiles}from"@huggingface/hub";constHF_TOKEN="hf_...";awaitcreateRepo({repo:"my-user/nlp-model",// or { type: "model", name: "my-user/nlp-test" },accessToken:HF_TOKEN});awaituploadFile({repo:"my-user/nlp-model",accessToken:HF_TOKEN,// Can work with native File in browsersfile:{path:"pytorch_model.bin",content:newBlob(...)}});awaitdeleteFiles({repo:{type:"space",name:"my-user/my-space"},// or "spaces/my-user/my-space"accessToken:HF_TOKEN,paths:["README.md",".gitattributes"]});

@huggingface/agents example

import{HfAgent,LLMFromHub,defaultTools}from'@huggingface/agents';constHF_TOKEN="hf_...";constagent=newHfAgent(HF_TOKEN,LLMFromHub(HF_TOKEN),[...defaultTools]);// you can generate the code, inspect it and then run itconstcode=awaitagent.generateCode("Draw a picture of a cat wearing a top hat. Then caption the picture and read it out loud.");console.log(code);constmessages=awaitagent.evaluateCode(code)console.log(messages);// contains the data// or you can run the code directly, however you can't check that the code is safe to execute this way, use at your own risk.constmessages=awaitagent.run("Draw a picture of a cat wearing a top hat. Then caption the picture and read it out loud.")console.log(messages);

There are more features of course, check each library's README!

Formatting & testing

sudo corepack enablepnpm installpnpm -r format:checkpnpm -r lint:checkpnpm -r test

Building

pnpm -r build

This will generate ESM and CJS javascript files inpackages/*/dist, egpackages/inference/dist/index.mjs.

About

Utilities to use the Hugging Face Hub API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript89.1%
  • JavaScript8.5%
  • Python1.3%
  • Jinja0.7%
  • Shell0.3%
  • Svelte0.1%

[8]ページ先頭

©2009-2025 Movatter.jp