Huggingface.js 文件

🤗 Hugging Face Hub API

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

🤗 Hugging Face Hub API

用於使用 Hugging Face Hub API 的官方工具。

安裝

pnpm add @huggingface/hub

npm add @huggingface/hub

yarn add @huggingface/hub

Deno

// esm.sh
import { uploadFiles, listModels } from "https://esm.sh/@huggingface/hub"
// or npm:
import { uploadFiles, listModels } from "npm:@huggingface/hub"

用法

對於某些呼叫,您需要建立一個帳戶並生成訪問令牌

透過此互動教程瞭解如何使用 hub 包查詢免費模型。

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_..." });

CLI 用法

您可以在 CLI 模式下使用 `huggingface/hub` 將檔案和資料夾上傳到您的倉庫。

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 branch create coyotte508/test-model release --empty
npx @huggingface/hub upload coyotte508/test-model . --revision release

npx @huggingface/hub --help
npx @huggingface/hub upload --help

您也可以使用 `npm install -g @huggingface/hub` 全域性安裝。然後您可以執行:

hfjs upload coyotte508/test-model .

hfjs branch create --repo-type dataset coyotte508/test-dataset release --empty
hfjs upload --repo-type dataset coyotte508/test-dataset . --revision release

hfjs --help
hfjs  upload --help

OAuth 登入

可以使用 OAuth(“透過 HF 登入”)進行登入。

這將允許您獲取訪問令牌以使用部分 API,具體取決於 Space 或 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);

檢視演示:https://huggingface.co/spaces/huggingfacejs/client-side-oauth

Hugging Face 快取

`@huggingface/hub` 包提供了掃描快取目錄的基本功能。瞭解更多關於管理 huggingface_hub 快取系統

scanCacheDir

您可以使用 `scanCacheDir` 函式獲取快取的倉庫列表。

import { scanCacheDir } from "@huggingface/hub";

const result = await scanCacheDir();

console.log(result);

注意:這在瀏覽器中不起作用。

downloadFileToCacheDir

您可以使用 `downloadFileToCacheDir` 函式快取倉庫檔案。

import { downloadFileToCacheDir } from "@huggingface/hub";

const file = await downloadFileToCacheDir({
  repo: 'foo/bar',
  path: 'README.md'
});

console.log(file);

注意:這在瀏覽器中不起作用。

snapshotDownload

您可以使用 `snapshotDownload` 函式在快取目錄中下載給定修訂版的整個倉庫。

import { snapshotDownload } from "@huggingface/hub";

const directory = await snapshotDownload({
  repo: 'foo/bar',
});

console.log(directory);

該程式碼內部使用 `downloadFileToCacheDir` 函式。

注意:這在瀏覽器中不起作用。

效能考量

上傳大型檔案時,您可能希望在 worker 中執行 `commit` 呼叫,以解除安裝 sha256 計算。

遠端資源和本地檔案應儘可能以 `URL` 形式傳遞,以便它們可以分塊延遲載入以減少 RAM 使用。在瀏覽器上下文中傳遞 `File` 是可以的,因為它本身就表現為 `Blob`。

在底層,`@huggingface/hub` 使用惰性 Blob 實現來載入檔案。

依賴

  • @huggingface/tasks:僅型別定義
  • @huggingface/lz4:URL 連線工具
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.