Transformers.js 文件

utils/hub

您正在檢視的是需要從原始碼安裝。如果想進行常規 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>

使用 Fetch API 或 FileSystem API 獲取檔案的輔助函式。

型別: utils/hub 的靜態方法
返回: Promise.<(FileResponse|Response)> - 解析為 FileResponse 物件(如果檔案使用 FileSystem API 獲取)或 Response 物件(如果檔案使用 Fetch API 獲取)的 Promise。

引數量型別描述
urlOrPathURL | 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字串

這可以是

  • 一個字串,huggingface.co 上模型倉庫的 模型 ID
  • 一個可能包含該檔案的 目錄 路徑。
filename字串

要在 path_or_repo 中定位的檔名。

[fatal]booleantrue

如果未找到檔案,是否丟擲錯誤。

[options]PretrainedOptions

包含可選引數的物件。

[return_path]booleanfalse

是否返回檔案路徑而不是檔案內容。


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]booleantrue

如果未找到檔案,是否丟擲錯誤。

[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]booleantrue

如果未找到檔案,是否丟擲錯誤。

[options]PretrainedOptions

包含可選引數的物件。


utils/hub~FileResponse

型別: utils/hub 的內部類


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 的內部類


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。

fatalboolean

如果檔案無法載入,是否丟擲錯誤。


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

型別: utils/hub 的內部方法
返回: Promise.<(FileResponse|Response|undefined)> - 快取中的項,如果未找到則為 undefined。

引數量型別描述
cacheFileCache | Cache

要搜尋的快取

...namesArray.<string>

要搜尋的項的名稱


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

讀取和跟蹤 Response 物件讀取時的進度

型別: utils/hub 的內部方法
返回: Promise.<Uint8Array> - 解析為 Uint8Array 緩衝區的 Promise

引數量型別描述
responseResponse | 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]*

要使用的模型配置,而不是自動載入的配置。當滿足以下條件時,可以自動載入配置:

  • 模型是庫提供的模型(透過預訓練模型的 模型 ID 字串載入)。
  • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到名為 config.json 的配置 JSON 檔案。
[cache_dir]字串null

如果不需要使用標準快取,則下載的預訓練模型配置的快取目錄路徑。

[local_files_only]booleanfalse

是否只檢視本地檔案(例如,不嘗試下載模型)。

[revision]字串"'main'"

要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統儲存模型和其他工件,因此 revision 可以是 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 上更新

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