Transformers.js 文档
utils/hub
并获得增强的文档体验
开始使用
utils/hub
用于与 Hugging Face Hub 交互的实用工具函数 (https://huggingface.co/models)
- utils/hub
- 静态方法
.getFile(urlOrPath)
⇒Promise.<(FileResponse|Response)>
.getModelFile(path_or_repo_id, filename, [fatal], [options], [return_path])
⇒Promise.<(string|Uint8Array)>
.getModelJSON(modelPath, fileName, [fatal], [options])
⇒Promise.<Object>
- 内部方法
- ~FileResponse
new FileResponse(filePath)
.updateContentType()
⇒void
.clone()
⇒FileResponse
.arrayBuffer()
⇒Promise.<ArrayBuffer>
.blob()
⇒Promise.<Blob>
.text()
⇒Promise.<string>
.json()
⇒Promise.<Object>
- ~FileCache
new FileCache(path)
.match(request)
⇒Promise.<(FileResponse|undefined)>
.put(request, response, [progress_callback])
⇒Promise.<void>
~CONTENT_TYPE_MAP
~isValidUrl(string, [protocols], [validHosts])
⇒boolean
~isValidHfModelId(string)
⇒boolean
~handleError(status, remoteURL, fatal)
⇒null
~tryCache(cache, ...names)
⇒Promise.<(FileResponse|Response|undefined)>
~readResponse(response, progress_callback)
⇒Promise.<Uint8Array>
~pathJoin(...parts)
⇒string
~ExternalData
:boolean
|number
~PretrainedOptions
:Object
~ModelSpecificPretrainedOptions
:Object
~PretrainedModelOptions
:*
- ~FileResponse
- 静态方法
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).
Param | Type | Description |
---|---|---|
urlOrPath | URL | 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.
Param | Type | Default | Description |
---|---|---|---|
path_or_repo_id | string | This can be either
| |
filename | string | The name of the file to locate in | |
[fatal] | boolean | true | Whether to throw an error if the file is not found. |
[options] | PretrainedOptions | An object containing optional parameters. | |
[return_path] | boolean | false | Whether to return the path of the file instead of the file content. |
.getModelFile(path_or_repo_id, filename, [fatal], [options], [return_path])
⇒Promise.<(string|Uint8Array)>
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.
Param | Type | Default | Description |
---|---|---|---|
modelPath | string | The path to the directory containing the file. | |
fileName | string | The name of the file to fetch. | |
[fatal] | boolean | true | 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
- ~FileResponse
new FileResponse(filePath)
.updateContentType()
⇒void
.clone()
⇒FileResponse
.arrayBuffer()
⇒Promise.<ArrayBuffer>
.blob()
⇒Promise.<Blob>
.text()
⇒Promise.<string>
.json()
⇒Promise.<Object>
new FileResponse(filePath)
Creates a new FileResponse
object.
Param | Type |
---|---|
filePath | string |
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
- ~FileCache
new FileCache(path)
.match(request)
⇒Promise.<(FileResponse|undefined)>
.put(request, response, [progress_callback])
⇒Promise.<void>
new FileCache(path)
Instantiate a FileCache
object.
Param | Type |
---|---|
path | string |
fileCache.match(request) ⇒ <code> Promise. < (FileResponse|undefined) > </code>
检查给定的请求是否在缓存中。
类型: FileCache
的实例方法
Param | Type |
---|---|
request | string |
fileCache.put(request, response, [progress_callback]) ⇒ <code> Promise. < void > </code>
将给定的响应添加到缓存中。
类型: FileCache
的实例方法
Param | Type | Description |
---|---|---|
request | string | |
response | Response | |
[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。
Param | Type | Default | Description |
---|---|---|---|
string | string | 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。
Param | Type | Description |
---|---|---|
string | string | 要测试的字符串 |
utils/hub~handleError(status, remoteURL, fatal) ⇒ <code> null </code>
用于处理在尝试从 Hugging Face Hub 加载文件时发生的致命错误的辅助方法。
类型: utils/hub
的内部方法
返回: null
- 如果 fatal = true
,则返回 null
。
Throws:
Error
如果fatal = false
。
Param | Type | Description |
---|---|---|
status | number | 错误的 HTTP 状态代码。 |
remoteURL | string | 无法加载的文件的 URL。 |
fatal | boolean | 如果文件无法加载,是否引发错误。 |
utils/hub~tryCache(cache, ...names) ⇒ <code> Promise. < (FileResponse|Response|undefined) > </code>
类型: utils/hub
的内部方法
返回: Promise.<(FileResponse|Response|undefined)>
- 来自缓存的项目,如果未找到则为 undefined。
Param | Type | Description |
---|---|---|
cache | FileCache | Cache | 要搜索的缓存 |
...names | Array.<string> | 要搜索的项目的名称 |
utils/hub~readResponse(response, progress_callback) ⇒ <code> Promise. < Uint8Array > </code>
读取和跟踪读取 Response 对象时的进度
类型: utils/hub
的内部方法
返回: Promise.<Uint8Array>
- 一个 Promise,它解析为 Uint8Array 缓冲区
Param | Type | Description |
---|---|---|
response | Response | FileResponse | 要读取的 Response 对象 |
progress_callback | * | 用于接收进度更新的回调函数 |
utils/hub~pathJoin(...parts) ⇒ <code> string </code>
将路径的多个部分连接成单个路径,同时处理前导和尾随斜杠。
类型: utils/hub
的内部方法
返回: string
- 表示连接路径的字符串。
Param | Type | Description |
---|---|---|
...parts | string | 路径的多个部分。 |
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
的内部类型定义
属性
名称 | Type | Default | Description |
---|---|---|---|
[progress_callback] | * |
| 如果指定,此函数将在模型构建期间被调用,以向用户提供进度更新。 |
[config] | * |
| 用于模型的配置,以代替自动加载的配置。当以下情况时,可以自动加载配置
|
[cache_dir] | string | null | 下载的预训练模型配置应该缓存到的目录路径,如果不想使用标准缓存。 |
[local_files_only] | boolean | false | 是否仅查看本地文件(例如,不尝试下载模型)。 |
[revision] | string | "'main'" | 要使用的特定模型版本。它可以是分支名称、标签名称或提交 ID,因为我们使用基于 git 的系统来存储 huggingface.co 上的模型和其他工件,因此 |
utils/hub~ModelSpecificPretrainedOptions : <code> Object </code>
用于加载预训练模型的选项。
类型: utils/hub
的内部类型定义
属性
名称 | Type | Default | Description |
---|---|---|---|
[subfolder] | string | "'onnx'" | 如果相关文件位于 huggingface.co 上模型仓库的子文件夹中,您可以在此处指定文件夹名称。 |
[model_file_name] | string | null | 如果指定,则加载具有此名称的模型(不包括 .onnx 后缀)。目前仅对仅编码器或仅解码器模型有效。 |
[device] | * |
| 运行模型的设备。如果未指定,将从环境设置中选择设备。 |
[dtype] | * |
| 用于模型的数据类型。如果未指定,将从环境设置中选择数据类型。 |
[use_external_data_format] | ExternalData | Record<string, ExternalData> | false | 是否使用外部数据格式加载模型(用于大小 >= 2GB 的模型)。 |
[session_options] | * | (可选)传递给运行时的用户指定的会话选项。如果未提供,将选择合适的默认值。 |
utils/hub~PretrainedModelOptions : <code> * </code>
用于加载预训练模型的选项。
类型: utils/hub
的内部类型定义
< > 在 GitHub 上更新