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 上更新