Transformers.js 文档

utils/hub

您正在查看 main 版本,该版本需要从源代码安装。如果您想要常规 npm 安装,请查看最新的稳定版本 (v3.0.0)。
Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

utils/hub

用于与 Hugging Face Hub 交互的实用工具函数 (https://huggingface.co/models)


utils/hub.getFile(urlOrPath) ⇒ <code> Promise. < (FileResponse|Response) > </code>

Helper function to get a file, using either the Fetch API or FileSystem API.

Kind: static method of utils/hub
Returns: Promise.<(FileResponse|Response)> - A promise that resolves to a FileResponse object (if the file is retrieved using the FileSystem API), or a Response object (if the file is retrieved using the Fetch API).

ParamTypeDescription
urlOrPathURL | string

The URL/path of the file to get.


utils/hub.getModelFile(path_or_repo_id, filename, [fatal], [options], [return_path]) ⇒ <code> Promise. < (string|Uint8Array) > </code>

Retrieves a file from either a remote URL using the Fetch API or from the local file system using the FileSystem API. If the filesystem is available and env.useCache = true, the file will be downloaded and cached.

Kind: static method of utils/hub
Returns: Promise.<(string|Uint8Array)> - A Promise that resolves with the file content as a Uint8Array if return_path is false, or the file path as a string if return_path is true.
Throws:

  • Will throw an error if the file is not found and fatal is true.
ParamTypeDefaultDescription
path_or_repo_idstring

This can be either

  • a string, the model id of a model repo on huggingface.co.
  • a path to a directory potentially containing the file.
filenamestring

The name of the file to locate in path_or_repo.

[fatal]booleantrue

Whether to throw an error if the file is not found.

[options]PretrainedOptions

An object containing optional parameters.

[return_path]booleanfalse

Whether to return the path of the file instead of the file content.


getModelFile~cacheKey : <code> string </code>

Kind: inner property of getModelFile


getModelFile~response : <code> Response </code> | <code> FileResponse </code> | <code> undefined </code>

Kind: inner property of getModelFile


getModelFile~buffer : <code> Uint8Array </code>

Kind: inner property of getModelFile


utils/hub.getModelJSON(modelPath, fileName, [fatal], [options]) ⇒ <code> Promise. < Object > </code>

Fetches a JSON file from a given path and file name.

Kind: static method of utils/hub
Returns: Promise.<Object> - The JSON data parsed into a JavaScript object.
Throws:

  • Will throw an error if the file is not found and fatal is true.
ParamTypeDefaultDescription
modelPathstring

The path to the directory containing the file.

fileNamestring

The name of the file to fetch.

[fatal]booleantrue

Whether to throw an error if the file is not found.

[options]PretrainedOptions

An object containing optional parameters.


utils/hub~FileResponse

Kind: inner class of utils/hub


new FileResponse(filePath)

Creates a new FileResponse object.

ParamType
filePathstring

fileResponse.updateContentType() ⇒ <code> void </code>

Updates the ‘content-type’ header property of the response based on the extension of the file specified by the filePath property of the current object.

Kind: instance method of FileResponse


fileResponse.clone() ⇒ <code> FileResponse </code>

Clone the current FileResponse object.

Kind: instance method of FileResponse
Returns: FileResponse - A new FileResponse object with the same properties as the current object.


fileResponse.arrayBuffer() ⇒ <code> Promise. < ArrayBuffer > </code>

读取由 filePath 属性指定的文件内容,并返回一个 Promise,该 Promise 解析为一个包含文件内容的 ArrayBuffer。

Kind: instance method of FileResponse
Returns: Promise.<ArrayBuffer> - A Promise that resolves with an ArrayBuffer containing the file’s contents.
Throws:

  • Error If the file cannot be read.

fileResponse.blob() ⇒ <code> Promise. < Blob > </code>

读取由 filePath 属性指定的文件内容,并返回一个 Promise,该 Promise 解析为一个包含文件内容的 Blob。

Kind: instance method of FileResponse
Returns: Promise.<Blob> - A Promise that resolves with a Blob containing the file’s contents.
Throws:

  • Error If the file cannot be read.

fileResponse.text() ⇒ <code> Promise. < string > </code>

读取由 filePath 属性指定的文件内容,并返回一个 Promise,该 Promise 解析为一个包含文件内容的字符串。

Kind: instance method of FileResponse
Returns: Promise.<string> - A Promise that resolves with a string containing the file’s contents.
Throws:

  • Error If the file cannot be read.

fileResponse.json() ⇒ <code> Promise. < Object > </code>

读取由 filePath 属性指定的文件内容,并返回一个 Promise,该 Promise 解析为一个包含文件内容的 JavaScript 对象。

Kind: instance method of FileResponse
Returns: Promise.<Object> - A Promise that resolves with a parsed JavaScript object containing the file’s contents.
Throws:

  • Error If the file cannot be read.

utils/hub~FileCache

Kind: inner class of utils/hub


new FileCache(path)

Instantiate a FileCache object.

ParamType
pathstring

fileCache.match(request) ⇒ <code> Promise. < (FileResponse|undefined) > </code>

检查给定的请求是否在缓存中。

类型FileCache 的实例方法

ParamType
requeststring

fileCache.put(request, response, [progress_callback]) ⇒ <code> Promise. < void > </code>

将给定的响应添加到缓存中。

类型FileCache 的实例方法

ParamTypeDescription
requeststring
responseResponse
[progress_callback]*

可选。用于接收进度更新的回调函数


utils/hub~CONTENT_TYPE_MAP

文件扩展名到 MIME 类型的映射。

类型utils/hub 的内部常量


utils/hub~isValidUrl(string, [protocols], [validHosts]) ⇒ <code> boolean </code>

确定给定的字符串是否为有效的 URL。

类型utils/hub 的内部方法
返回boolean - 如果字符串是有效的 URL,则为 True,否则为 False。

ParamTypeDefaultDescription
stringstring | URL

要测试其作为 URL 有效性的字符串。

[protocols]Array.<string>

有效协议的列表。如果指定,协议必须在此列表中。

[validHosts]Array.<string>

有效主机名的列表。如果指定,URL 的主机名必须在此列表中。


utils/hub~isValidHfModelId(string) ⇒ <code> boolean </code>

测试字符串是否为有效的 Hugging Face 模型 ID。改编自 https://github.com/huggingface/huggingface_hub/blob/6378820ebb03f071988a96c7f3268f5bdf8f9449/src/huggingface_hub/utils/_validators.py#L119-L170

类型utils/hub 的内部方法
返回boolean - 如果字符串是有效的模型 ID,则为 True,否则为 False。

ParamTypeDescription
stringstring

要测试的字符串


utils/hub~handleError(status, remoteURL, fatal) ⇒ <code> null </code>

用于处理在尝试从 Hugging Face Hub 加载文件时发生的致命错误的辅助方法。

类型utils/hub 的内部方法
返回null - 如果 fatal = true,则返回 null
Throws:

  • Error 如果 fatal = false
ParamTypeDescription
statusnumber

错误的 HTTP 状态代码。

remoteURLstring

无法加载的文件的 URL。

fatalboolean

如果文件无法加载,是否引发错误。


utils/hub~tryCache(cache, ...names) ⇒ <code> Promise. < (FileResponse|Response|undefined) > </code>

类型utils/hub 的内部方法
返回Promise.<(FileResponse|Response|undefined)> - 来自缓存的项目,如果未找到则为 undefined。

ParamTypeDescription
cacheFileCache | Cache

要搜索的缓存

...namesArray.<string>

要搜索的项目的名称


utils/hub~readResponse(response, progress_callback) ⇒ <code> Promise. < Uint8Array > </code>

读取和跟踪读取 Response 对象时的进度

类型utils/hub 的内部方法
返回Promise.<Uint8Array> - 一个 Promise,它解析为 Uint8Array 缓冲区

ParamTypeDescription
responseResponse | FileResponse

要读取的 Response 对象

progress_callback*

用于接收进度更新的回调函数


utils/hub~pathJoin(...parts) ⇒ <code> string </code>

将路径的多个部分连接成单个路径,同时处理前导和尾随斜杠。

类型utils/hub 的内部方法
返回string - 表示连接路径的字符串。

ParamTypeDescription
...partsstring

路径的多个部分。


utils/hub~ExternalData : <code> boolean </code> | <code> number </code>

是否使用外部数据格式加载模型(用于大小 >= 2GB 的模型)。如果为 true,将使用外部数据格式加载模型。如果为数字,则将使用外部数据格式加载这么多块(形式为:“model.onnx_data[{chunk_number}]”)。

类型utils/hub 的内部类型定义


utils/hub~PretrainedOptions : <code> Object </code>

用于加载预训练模型的选项。

类型utils/hub 的内部类型定义
属性

名称TypeDefaultDescription
[progress_callback]*

如果指定,此函数将在模型构建期间被调用,以向用户提供进度更新。

[config]*

用于模型的配置,以代替自动加载的配置。当以下情况时,可以自动加载配置

  • 该模型是由库提供的模型(使用预训练模型的*模型 ID* 字符串加载)。
  • 通过提供本地目录作为 pretrained_model_name_or_path 加载模型,并且在目录中找到名为 *config.json* 的配置 JSON 文件。
[cache_dir]stringnull

下载的预训练模型配置应该缓存到的目录路径,如果不想使用标准缓存。

[local_files_only]booleanfalse

是否仅查看本地文件(例如,不尝试下载模型)。

[revision]string"'main'"

要使用的特定模型版本。它可以是分支名称、标签名称或提交 ID,因为我们使用基于 git 的系统来存储 huggingface.co 上的模型和其他工件,因此 revision 可以是 git 允许的任何标识符。注意:此设置对于本地请求将被忽略。


utils/hub~ModelSpecificPretrainedOptions : <code> Object </code>

用于加载预训练模型的选项。

类型utils/hub 的内部类型定义
属性

名称TypeDefaultDescription
[subfolder]string"'onnx'"

如果相关文件位于 huggingface.co 上模型仓库的子文件夹中,您可以在此处指定文件夹名称。

[model_file_name]stringnull

如果指定,则加载具有此名称的模型(不包括 .onnx 后缀)。目前仅对仅编码器或仅解码器模型有效。

[device]*

运行模型的设备。如果未指定,将从环境设置中选择设备。

[dtype]*

用于模型的数据类型。如果未指定,将从环境设置中选择数据类型。

[use_external_data_format]ExternalData | Record<string, ExternalData>false

是否使用外部数据格式加载模型(用于大小 >= 2GB 的模型)。

[session_options]*

(可选)传递给运行时的用户指定的会话选项。如果未提供,将选择合适的默认值。


utils/hub~PretrainedModelOptions : <code> * </code>

用于加载预训练模型的选项。

类型utils/hub 的内部类型定义


< > 在 GitHub 上更新