fal.toolkit.utils package

Submodules

fal.toolkit.utils.download_utils module

exception fal.toolkit.utils.download_utils.DownloadError

Bases: Exception

fal.toolkit.utils.download_utils.clone_repository(https_url, *, commit_hash=None, target_dir=None, repo_name=None, force=False, include_to_path=False)

Clones a Git repository from the specified HTTPS URL into a local directory.

This function clones a Git repository from the specified HTTPS URL into a local directory. It can also checkout a specific commit if the commit_hash is provided.

If a custom target_dir or repo_name is not specified, a predefined directory is used for the target directory, and the repository name is determined from the URL.

Parameters:
  • https_url (str) – The HTTPS URL of the Git repository to be cloned.

  • commit_hash (str | None) – The commit hash to checkout after cloning.

  • target_dir (str | Path | None) – The directory where the repository will be saved. If not provided, a predefined directory is used.

  • repo_name (str | None) – The name to be used for the cloned repository directory. If not provided, the repository’s name from the URL is used.

  • force (bool) – If True, the repository is cloned even if it already exists locally and its commit hash matches the provided commit hash. Defaults to False.

  • include_to_path (bool) – If True, the cloned repository is added to the sys.path. Defaults to False.

Return type:

Path

Returns:

A Path object representing the full path to the cloned Git repository.

fal.toolkit.utils.download_utils.download_file(url, target_dir, *, force=False, request_headers=None, filesize_limit=None)

Downloads a file from the specified URL to the target directory.

The function downloads the file from the given URL and saves it in the specified target directory, provided it is below the given filesize limit.

It also checks whether the local file already exists and whether its content length matches the expected content length from the remote file. If the local file already exists and its content length matches the expected content length from the remote file, the existing file is returned without re-downloading it.

If the file needs to be downloaded or if an existing file’s content length does not match the expected length, the function proceeds to download and save the file. It ensures that the target directory exists and handles any errors that may occur during the download process, raising a DownloadError if necessary.

Parameters:
  • url (str) – The URL of the file to be downloaded.

  • target_dir (str | Path) – The directory where the downloaded file will be saved. If it’s not an absolute path, it’s treated as a relative directory to “/data”.

  • force (bool) – If True, the file is downloaded even if it already exists locally and its content length matches the expected content length from the remote file. Defaults to False.

  • request_headers (dict[str, str] | None) – A dictionary containing additional headers to be included in the HTTP request. Defaults to None.

  • filesize_limit (int | None) – An integer specifying the maximum downloadable size, in megabytes. Defaults to None.

Return type:

Path

Returns:

A Path object representing the full path to the downloaded file.

Raises:
  • ValueError – If the provided file_name contains a forward slash (‘/’).

  • DownloadError – If an error occurs during the download process.

fal.toolkit.utils.download_utils.download_model_weights(url, force=False, request_headers=None)

Downloads model weights from the specified URL and saves them to a predefined directory.

This function is specifically designed for downloading model weights and stores them in a predefined directory.

It calls the download_file function with the provided URL and the target directory set to a pre-defined location for model weights. The downloaded model weights are saved in this directory, and the function returns the full path to the downloaded weights file.

Parameters:
  • url (str) – The URL from which the model weights will be downloaded.

  • force (bool) – If True, the model weights are downloaded even if they already exist locally and their content length matches the expected content length from the remote file. Defaults to False.

  • request_headers (dict[str, str] | None) – A dictionary containing additional headers to be included in the HTTP request. Defaults to None.

Return type:

Path

Returns:

A Path object representing the full path to the downloaded model weights.

fal.toolkit.utils.endpoint module

fal.toolkit.utils.endpoint.cancel_on_disconnect(request)

Async context manager for async code that needs to be cancelled if client disconnects prematurely. The client disconnect is monitored through the Request object.

fal.toolkit.utils.retry module

fal.toolkit.utils.retry.retry(max_retries=3, base_delay=1.0, max_delay=60.0, backoff_type='exponential', jitter=False, should_retry=None)
Return type:

Callable

fal.toolkit.utils.setup_utils module

fal.toolkit.utils.setup_utils.patch_onnx_runtime(inter_op_num_threads=16, intra_op_num_threads=16, omp_num_threads=16)

Patch ONNX Runtime’s defaults to set the number of threads for inter-op, intra-op, and OpenMP.Trying to use an ONNX Runtime session within a fal app without explicitly setting these parameters can lead to issues, for example, it can cause several logs related to these parameters to be printed. Please run this function before importing any ONNX Runtime modules in your application.

Parameters:
  • inter_op_num_threads (int) – Number of threads for inter-op parallelism.

  • intra_op_num_threads (int) – Number of threads for intra-op parallelism.

  • omp_num_threads (int) – Number of threads for OpenMP parallelism.

Module contents