
Security News
NIST Under Federal Audit for NVD Processing Backlog and Delays
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
@huggingface/hub
Advanced tools
Official utilities to use the Hugging Face Hub API.
pnpm add @huggingface/hub
npm add @huggingface/hub
yarn add @huggingface/hub
// esm.sh
import { uploadFiles, listModels } from "https://esm.sh/@huggingface/hub"
// or npm:
import { uploadFiles, listModels } from "npm:@huggingface/hub"
Check out the full documentation.
For some of the calls, you need to create an account and generate an access token.
Learn how to find free models using the hub package in this interactive tutorial.
import * as hub from "@huggingface/hub";
import type { RepoDesignation } from "@huggingface/hub";
const repo: RepoDesignation = { type: "model", name: "myname/some-model" };
const {name: username} = await hub.whoAmI({accessToken: "hf_..."});
for await (const model of hub.listModels({search: {owner: username}, accessToken: "hf_..."})) {
console.log("My model:", model);
}
const specificModel = await hub.modelInfo({name: "openai-community/gpt2"});
await hub.checkRepoAccess({repo, accessToken: "hf_..."});
await hub.createRepo({ repo, accessToken: "hf_...", license: "mit" });
await hub.uploadFiles({
repo,
accessToken: "hf_...",
files: [
// path + blob content
{
path: "file.txt",
content: new Blob(["Hello World"]),
},
// Local file URL
pathToFileURL("./pytorch-model.bin"),
// Local folder URL
pathToFileURL("./models"),
// Web URL
new URL("https://huggingface.co/xlm-roberta-base/resolve/main/tokenizer.json"),
// Path + Web URL
{
path: "myfile.bin",
content: new URL("https://huggingface.co/bert-base-uncased/resolve/main/pytorch_model.bin")
}
// Can also work with native File in browsers
],
});
// or
for await (const progressEvent of await hub.uploadFilesWithProgress({
repo,
accessToken: "hf_...",
files: [
...
],
})) {
console.log(progressEvent);
}
await hub.deleteFile({repo, accessToken: "hf_...", path: "myfile.bin"});
await (await hub.downloadFile({ repo, path: "README.md" })).text();
for await (const fileInfo of hub.listFiles({repo})) {
console.log(fileInfo);
}
await hub.deleteRepo({ repo, accessToken: "hf_..." });
You can use @huggingface/hub
in CLI mode to upload files and folders to your repo.
npx @huggingface/hub upload coyotte508/test-model .
npx @huggingface/hub upload datasets/coyotte508/test-dataset .
# Same thing
npx @huggingface/hub upload --repo-type dataset coyotte508/test-dataset .
# Upload new data with 0 history in a separate branch
npx @huggingface/hub create-branch coyotte508/test-model release --empty
npx @huggingface/hub upload coyotte508/test-model . --revision release
npx @huggingface/hub --help
npx @huggingface/hub upload --help
You can also instal globally with npm install -g @huggingface/hub
. Then you can do:
hfjs upload coyotte508/test-model .
hfjs create-branch --repo-type dataset coyotte508/test-dataset release --empty
hfjs upload --repo-type dataset coyotte508/test-dataset . --revision release
hfjs --help
hfjs upload --help
It's possible to login using OAuth ("Sign in with HF").
This will allow you get an access token to use some of the API, depending on the scopes set inside the Space or the OAuth App.
import { oauthLoginUrl, oauthHandleRedirectIfPresent } from "@huggingface/hub";
const oauthResult = await oauthHandleRedirectIfPresent();
if (!oauthResult) {
// If the user is not logged in, redirect to the login page
window.location.href = await oauthLoginUrl();
}
// You can use oauthResult.accessToken, oauthResult.accessTokenExpiresAt and oauthResult.userInfo
console.log(oauthResult);
Checkout the demo: https://huggingface.co/spaces/huggingfacejs/client-side-oauth
The @huggingface/hub
package provide basic capabilities to scan the cache directory. Learn more about Manage huggingface_hub cache-system.
scanCacheDir
You can get the list of cached repositories using the scanCacheDir
function.
import { scanCacheDir } from "@huggingface/hub";
const result = await scanCacheDir();
console.log(result);
Note: this does not work in the browser
downloadFileToCacheDir
You can cache a file of a repository using the downloadFileToCacheDir
function.
import { downloadFileToCacheDir } from "@huggingface/hub";
const file = await downloadFileToCacheDir({
repo: 'foo/bar',
path: 'README.md'
});
console.log(file);
Note: this does not work in the browser
snapshotDownload
You can download an entire repository at a given revision in the cache directory using the snapshotDownload
function.
import { snapshotDownload } from "@huggingface/hub";
const directory = await snapshotDownload({
repo: 'foo/bar',
});
console.log(directory);
The code use internally the downloadFileToCacheDir
function.
Note: this does not work in the browser
When uploading large files, you may want to run the commit
calls inside a worker, to offload the sha256 computations.
Remote resources and local files should be passed as URL
whenever it's possible so they can be lazy loaded in chunks to reduce RAM usage. Passing a File
inside the browser's context is fine, because it natively behaves as a Blob
.
Under the hood, @huggingface/hub
uses a lazy blob implementation to load the file.
@huggingface/tasks
: Typings only@huggingface/lz4
: URL join utilityFAQs
Utilities to interact with the Hugging Face hub
The npm package @huggingface/hub receives a total of 11,701 weekly downloads. As such, @huggingface/hub popularity was classified as popular.
We found that @huggingface/hub demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.
Security News
TypeScript Native Previews offers a 10x faster Go-based compiler, now available on npm for public testing with early editor and language support.