Transformers.js documentation

utils/hub

You are viewing main version, which requires installation from source. If you'd like regular npm install, checkout the latest stable version (v3.0.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

utils/hub

Utility functions to interact with the 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]) β‡’ <code> Promise. < 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.<Uint8Array> - A Promise that resolves with the file content as a buffer.
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.


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 | URL

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>

Reads the contents of the file specified by the filePath property and returns a Promise that resolves with an ArrayBuffer containing the file’s contents.

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>

Reads the contents of the file specified by the filePath property and returns a Promise that resolves with a Blob containing the file’s contents.

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>

Reads the contents of the file specified by the filePath property and returns a Promise that resolves with a string containing the file’s contents.

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>

Reads the contents of the file specified by the filePath property and returns a Promise that resolves with a parsed JavaScript object containing the file’s contents.

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>

Checks whether the given request is in the cache.

Kind: instance method of FileCache

ParamType
requeststring

fileCache.put(request, response) β‡’ <code> Promise. < void > </code>

Adds the given response to the cache.

Kind: instance method of FileCache

ParamType
requeststring
responseResponse | FileResponse

utils/hub~CONTENT_TYPE_MAP

Mapping from file extensions to MIME types.

Kind: inner constant of utils/hub


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

Determines whether the given string is a valid URL.

Kind: inner method of utils/hub
Returns: boolean - True if the string is a valid URL, false otherwise.

ParamTypeDefaultDescription
stringstring | URL

The string to test for validity as an URL.

[protocols]Array.<string>

A list of valid protocols. If specified, the protocol must be in this list.

[validHosts]Array.<string>

A list of valid hostnames. If specified, the URL's hostname must be in this list.


utils/hub~handleError(status, remoteURL, fatal) β‡’ <code> null </code>

Helper method to handle fatal errors that occur while trying to load a file from the Hugging Face Hub.

Kind: inner method of utils/hub
Returns: null - Returns null if fatal = true.
Throws:

  • Error If `fatal = false`.
ParamTypeDescription
statusnumber

The HTTP status code of the error.

remoteURLstring

The URL of the file that could not be loaded.

fatalboolean

Whether to raise an error if the file could not be loaded.


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

Kind: inner method of utils/hub
Returns: Promise.<(FileResponse|Response|undefined)> - The item from the cache, or undefined if not found.

ParamTypeDescription
cacheFileCache | Cache

The cache to search

...namesArray.<string>

The names of the item to search for


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

Read and track progress when reading a Response object

Kind: inner method of utils/hub
Returns: Promise.<Uint8Array> - A Promise that resolves with the Uint8Array buffer

ParamTypeDescription
responseany

The Response object to read

progress_callbackfunction

The function to call with progress updates


utils/hub~pathJoin(...parts) β‡’ <code> string </code>

Joins multiple parts of a path into a single path, while handling leading and trailing slashes.

Kind: inner method of utils/hub
Returns: string - A string representing the joined path.

ParamTypeDescription
...partsstring

Multiple parts of a path.


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

Options for loading a pretrained model.

Kind: inner typedef of utils/hub
Properties

NameTypeDefaultDescription
[progress_callback]function

If specified, this function will be called during model construction, to provide the user with progress updates.

[config]*

Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when:

  • The model is a model provided by the library (loaded with the model id string of a pretrained model).
  • The model is loaded by supplying a local directory as pretrained_model_name_or_path and a configuration JSON file named config.json is found in the directory.
[cache_dir]stringnull

Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.

[local_files_only]booleanfalse

Whether or not to only look at local files (e.g., not try downloading the model).

[revision]string"'main'"

The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a git-based system for storing models and other artifacts on huggingface.co, so revision can be any identifier allowed by git. NOTE: This setting is ignored for local requests.


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

Options for loading a pretrained model.

Kind: inner typedef of utils/hub
Properties

NameTypeDefaultDescription
[subfolder]string"'onnx'"

In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can specify the folder name here.

[model_file_name]stringnull

If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models.

[device]*

The device to run the model on. If not specified, the device will be chosen from the environment settings.

[dtype]*

The data type to use for the model. If not specified, the data type will be chosen from the environment settings.

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

Whether to load the model using the external data format (used for models >= 2GB in size).

[session_options]*

(Optional) User-specified session options passed to the runtime. If not provided, suitable defaults will be chosen.


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

Options for loading a pretrained model.

Kind: inner typedef of utils/hub


< > Update on GitHub