Transformers.js documentation
Use custom models
Use custom models
By default, Transformers.js uses hosted pretrained models and precompiled WASM binaries, which should work out-of-the-box. You can customize this as follows:
Settings
import { env } from '@huggingface/transformers';
// Specify a custom location for models (defaults to '/models/').
env.localModelPath = '/path/to/models/';
// Disable the loading of remote models from the Hugging Face Hub:
env.allowRemoteModels = false;
// Set location of .wasm files. Defaults to use a CDN.
env.backends.onnx.wasm.wasmPaths = '/path/to/files/';For a full list of available settings, check out the API Reference.
Convert your models to ONNX
We recommend using Optimum to convert your PyTorch models to ONNX in a single command. For the full list of supported architectures, check out the Optimum documentation.
Resource-loading precedence
Global env values are defaults. A loader’s options.env object can override only the session-scopable resource-loading fields: allowRemoteModels, remoteHost, remotePathTemplate, allowLocalModels, localModelPath, fetch, hfToken, and cacheDir. Cache backend selection, filesystem capability, logging, and backend settings remain global.
import { pipeline } from "@huggingface/transformers";
// Two pipelines in the same process, each loading from its own host and cache directory.
const publicPipe = await pipeline(
"feature-extraction",
"Xenova/all-MiniLM-L6-v2",
);
const privatePipe = await pipeline(
"feature-extraction",
"acme/internal-embeddings",
{
env: {
remoteHost: "https://hub.internal.example/",
hfToken: process.env.ACME_HUB_TOKEN,
cacheDir: "/var/cache/acme-models/",
},
},
);Fields that a loader’s options.env does not override are read from the global env when they are needed, so later changes to the global env still apply. Pipelines also use their options.env when they download image or audio inputs given as URLs.
In Node.js, env.remoteHost defaults to HF_ENDPOINT when set and not empty, otherwise https://huggingface.co/. env.hfToken defaults to HF_TOKEN, falling back to HF_ACCESS_TOKEN. Both variables are read when they are needed, not when the library is imported, so they can be loaded afterwards (for example, with dotenv). Explicit global assignments replace those defaults, and an options.env value takes precedence for that loader. Tokens from HF_TOKEN, HF_ACCESS_TOKEN or env.hfToken are only sent to https://huggingface.co and https://hf.co. To authenticate against another host, pass hfToken and remoteHost together in options.env. Browser environments do not send token authorization headers.
The cache_dir and local_files_only options keep working as before, and are applied after environment resolution:
local_files_only: trueprevents remote requests even ifallowRemoteModelsistrue.cache_diroverrides bothoptions.env.cacheDirand globalenv.cacheDirfor that call.
Filesystem cache namespaces
Session-scoped hosts and tokens do not partition the filesystem cache. Persisted files keep the existing model ID/revision/file layout, without automatic host or token namespaces. If two hosts serve different content under the same model ID and revision, use separate options.env.cacheDir values (or explicit cache_dir values):
import { AutoTokenizer } from "@huggingface/transformers";
const modelId = "acme/shared-model";
const tokenizerA = await AutoTokenizer.from_pretrained(modelId, {
revision: "main",
env: {
remoteHost: "https://hub-a.example/",
cacheDir: "/var/cache/models/host-a/",
},
});
const tokenizerB = await AutoTokenizer.from_pretrained(modelId, {
revision: "main",
env: {
remoteHost: "https://hub-b.example/",
cacheDir: "/var/cache/models/host-b/",
},
});Cache directories separate content namespaces, not credential authorization boundaries: loading a cached file does not reauthorize it with the current token. Rotating a token within the same content namespace should keep using the same directory. An explicit cache_dir still takes precedence over session and global cacheDir settings.
The filesystem cache stages each download in a temporary file and publishes it with an atomic rename, so readers never observe a partially written file. Processes sharing a cache directory are not otherwise coordinated: if one process deletes an entry while another replaces it, the replacement can be removed, and it is downloaded again on the next load.
Update on GitHub