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)>
.getModelText(modelPath, fileName, [fatal], [options])
⇒Promise.<(string|null)>
.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>
使用 Fetch API 或 FileSystem API 获取文件的辅助函数。
类型: utils/hub
的静态方法
返回: Promise.<(FileResponse|Response)>
- 解析为 FileResponse 对象(如果文件使用 FileSystem API 获取)或 Response 对象(如果文件使用 Fetch API 获取)的 Promise。
参数量 | 类型 | 描述 |
---|---|---|
urlOrPath | URL | string | 要获取的文件的 URL/路径。 |
utils/hub.getModelFile(path_or_repo_id, filename, [fatal], [options], [return_path]) ⇒ <code> Promise. < (string|Uint8Array) > </code>
从远程 URL 使用 Fetch API 或从本地文件系统使用 FileSystem API 检索文件。如果文件系统可用且 env.useCache = true
,则文件将被下载并缓存。
类型: utils/hub
的静态方法
返回: Promise.<(string|Uint8Array)>
- 如果 return_path
为 false,则解析为 Uint8Array 格式的文件内容的 Promise;如果 return_path
为 true,则解析为字符串格式的文件路径的 Promise。
抛出:
- 如果未找到文件且
fatal
为 true,则会抛出错误。
参数量 | 类型 | 默认 | 描述 |
---|---|---|---|
path_or_repo_id | 字符串 | 这可以是
| |
filename | 字符串 | 要在 | |
[fatal] | boolean | true | 如果未找到文件,是否抛出错误。 |
[options] | PretrainedOptions | 包含可选参数的对象。 | |
[return_path] | boolean | false | 是否返回文件路径而不是文件内容。 |
.getModelFile(path_or_repo_id, filename, [fatal], [options], [return_path])
⇒Promise.<(string|Uint8Array)>
getModelFile~cacheKey : <code> string </code>
类型: getModelFile
的内部属性
getModelFile~response : <code> Response </code> | <code> FileResponse </code> | <code> undefined </code>
类型: getModelFile
的内部属性
getModelFile~buffer : <code> Uint8Array </code>
类型: getModelFile
的内部属性
utils/hub.getModelText(modelPath, fileName, [fatal], [options]) ⇒ <code> Promise. < (string|null) > </code>
从给定路径和文件名获取文本文件。
类型: utils/hub
的静态方法
返回: Promise.<(string|null)>
- 文件的文本内容。
抛出:
- 如果未找到文件且
fatal
为 true,则会抛出错误。
参数量 | 类型 | 默认 | 描述 |
---|---|---|---|
modelPath | 字符串 | 包含文件的目录路径。 | |
fileName | 字符串 | 要获取的文件名。 | |
[fatal] | boolean | true | 如果未找到文件,是否抛出错误。 |
[options] | PretrainedOptions | 包含可选参数的对象。 |
utils/hub.getModelJSON(modelPath, fileName, [fatal], [options]) ⇒ <code> Promise. < Object > </code>
从给定路径和文件名获取 JSON 文件。
类型: utils/hub
的静态方法
返回: Promise.<Object>
- 解析为 JavaScript 对象的 JSON 数据。
抛出:
- 如果未找到文件且
fatal
为 true,则会抛出错误。
参数量 | 类型 | 默认 | 描述 |
---|---|---|---|
modelPath | 字符串 | 包含文件的目录路径。 | |
fileName | 字符串 | 要获取的文件名。 | |
[fatal] | boolean | true | 如果未找到文件,是否抛出错误。 |
[options] | PretrainedOptions | 包含可选参数的对象。 |
utils/hub~FileResponse
类型: 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)
创建一个新的 FileResponse
对象。
参数量 | 类型 |
---|---|
filePath | 字符串 |
fileResponse.updateContentType() ⇒ <code> void </code>
根据当前对象的 filePath 属性指定的文件扩展名,更新响应的“content-type”头部属性。
类型: FileResponse
的实例方法
fileResponse.clone() ⇒ <code> FileResponse </code>
克隆当前的 FileResponse 对象。
类型: FileResponse
的实例方法
返回: FileResponse
- 一个新的 FileResponse 对象,具有与当前对象相同的属性。
fileResponse.arrayBuffer() ⇒ <code> Promise. < ArrayBuffer > </code>
读取 filePath 属性指定的文件内容,并返回一个解析为包含文件内容的 ArrayBuffer 的 Promise。
类型: FileResponse
的实例方法
返回: Promise.<ArrayBuffer>
- 解析为包含文件内容的 ArrayBuffer 的 Promise。
抛出:
Error
如果文件无法读取。
fileResponse.blob() ⇒ <code> Promise. < Blob > </code>
读取 filePath 属性指定的文件内容,并返回一个解析为包含文件内容的 Blob 的 Promise。
类型: FileResponse
的实例方法
返回: Promise.<Blob>
- 解析为包含文件内容的 Blob 的 Promise。
抛出:
Error
如果文件无法读取。
fileResponse.text() ⇒ <code> Promise. < string > </code>
读取 filePath 属性指定的文件内容,并返回一个解析为包含文件内容的字符串的 Promise。
类型: FileResponse
的实例方法
返回: Promise.<string>
- 解析为包含文件内容的字符串的 Promise。
抛出:
Error
如果文件无法读取。
fileResponse.json() ⇒ <code> Promise. < Object > </code>
读取 filePath 属性指定的文件内容,并返回一个解析为包含文件内容的已解析 JavaScript 对象的 Promise。
类型: FileResponse
的实例方法
返回: Promise.<Object>
- 解析为包含文件内容的已解析 JavaScript 对象的 Promise。
抛出:
Error
如果文件无法读取。
utils/hub~FileCache
类型: utils/hub
的内部类
- ~FileCache
new FileCache(path)
.match(request)
⇒Promise.<(FileResponse|undefined)>
.put(request, response, [progress_callback])
⇒Promise.<void>
new FileCache(path)
实例化一个 FileCache
对象。
参数量 | 类型 |
---|---|
path | 字符串 |
fileCache.match(request) ⇒ <code> Promise. < (FileResponse|undefined) > </code>
检查给定请求是否在缓存中。
类型: FileCache
的实例方法
参数量 | 类型 |
---|---|
request | 字符串 |
fileCache.put(request, response, [progress_callback]) ⇒ <code> Promise. < void > </code>
将给定响应添加到缓存中。
类型: FileCache
的实例方法
参数量 | 类型 | 描述 |
---|---|---|
request | 字符串 | |
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。
参数量 | 类型 | 默认 | 描述 |
---|---|---|---|
字符串 | 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。
参数量 | 类型 | 描述 |
---|---|---|
字符串 | 字符串 | 要测试的字符串 |
utils/hub~handleError(status, remoteURL, fatal) ⇒ <code> null </code>
处理从 Hugging Face Hub 加载文件时发生的致命错误的辅助方法。
类型: utils/hub
的内部方法
返回: null
- 如果 fatal = true
,则返回 null
。
抛出:
Error
如果 `fatal = false`。
参数量 | 类型 | 描述 |
---|---|---|
status | 数字 | 错误的 HTTP 状态码。 |
remoteURL | 字符串 | 无法加载的文件的 URL。 |
fatal | boolean | 如果文件无法加载,是否抛出错误。 |
utils/hub~tryCache(cache, ...names) ⇒ <code> Promise. < (FileResponse|Response|undefined) > </code>
类型: utils/hub
的内部方法
返回: Promise.<(FileResponse|Response|undefined)>
- 缓存中的项,如果未找到则为 undefined。
参数量 | 类型 | 描述 |
---|---|---|
cache | FileCache | Cache | 要搜索的缓存 |
...names | Array.<string> | 要搜索的项的名称 |
utils/hub~readResponse(response, progress_callback) ⇒ <code> Promise. < Uint8Array > </code>
读取和跟踪 Response 对象读取时的进度
类型: utils/hub
的内部方法
返回: Promise.<Uint8Array>
- 解析为 Uint8Array 缓冲区的 Promise
参数量 | 类型 | 描述 |
---|---|---|
response | Response | FileResponse | 要读取的 Response 对象 |
progress_callback | * | 用于进度更新的回调函数 |
utils/hub~pathJoin(...parts) ⇒ <code> string </code>
将路径的多个部分连接成一个路径,同时处理前导和尾随斜杠。
类型: utils/hub
的内部方法
返回: string
- 表示连接路径的字符串。
参数量 | 类型 | 描述 |
---|---|---|
...parts | 字符串 | 路径的多个部分。 |
utils/hub~ExternalData : <code> boolean </code> | <code> number </code>
是否使用外部数据格式加载模型(用于大小大于等于 2GB 的模型)。如果为 true
,则模型将使用外部数据格式加载。如果为数字,则将使用外部数据格式加载这么多块(格式为:“model.onnxdata[{chunk_number}]”)。
类型: utils/hub
的内部类型定义
utils/hub~PretrainedOptions : <code> Object </code>
加载预训练模型的选项。
类型: utils/hub
的内部类型定义
属性
名称 | 类型 | 默认 | 描述 |
---|---|---|---|
[progress_callback] | * |
| 如果指定,此函数将在模型构建期间调用,以便向用户提供进度更新。 |
[config] | * |
| 要使用的模型配置,而不是自动加载的配置。当满足以下条件时,可以自动加载配置:
|
[cache_dir] | 字符串 | null | 如果不需要使用标准缓存,则下载的预训练模型配置的缓存目录路径。 |
[local_files_only] | boolean | false | 是否只查看本地文件(例如,不尝试下载模型)。 |
[revision] | 字符串 | "'main'" | 要使用的特定模型版本。它可以是分支名称、标签名称或提交 ID,因为我们在 huggingface.co 上使用基于 git 的系统存储模型和其他工件,因此 |
utils/hub~ModelSpecificPretrainedOptions : <code> Object </code>
加载预训练模型的选项。
类型: utils/hub
的内部类型定义
属性
名称 | 类型 | 默认 | 描述 |
---|---|---|---|
[subfolder] | 字符串 | "'onnx'" | 如果相关文件位于 huggingface.co 上模型仓库的子文件夹中,可以在此处指定文件夹名称。 |
[model_file_name] | 字符串 | null | 如果指定,则使用此名称(不包括 .onnx 后缀)加载模型。目前仅适用于编码器或解码器模型。 |
[device] | * |
| 运行模型的设备。如果未指定,则将从环境设置中选择设备。 |
[dtype] | * |
| 模型使用的数据类型。如果未指定,则将从环境设置中选择数据类型。 |
[use_external_data_format] | ExternalData | Record<string, ExternalData> | false | 是否使用外部数据格式加载模型(用于大小大于等于 2GB 的模型)。 |
[session_options] | * | (可选)用户指定的会话选项,传递给运行时。如果未提供,将选择合适的默认值。 |
utils/hub~PretrainedModelOptions : <code> * </code>
加载预训练模型的选项。
类型: utils/hub
的内部类型定义
< > 在 GitHub 上更新