Nasty.Statistics.Neural.Transformers.CacheManager (Nasty v0.3.0)

View Source

Manages caching of downloaded transformer models.

Handles model versioning, disk space management, and cache lookup to avoid re-downloading large models from HuggingFace Hub.

Summary

Functions

Calculates total cache size in bytes.

Clears cached models.

Gets the cached model path if it exists.

Lists all cached models with their metadata.

Records a model in the cache registry.

Types

cache_entry()

@type cache_entry() :: %{
  model_name: atom(),
  path: String.t(),
  size_bytes: integer(),
  downloaded_at: DateTime.t(),
  version: String.t()
}

Functions

cache_size(cache_dir)

@spec cache_size(String.t()) :: {:ok, integer()} | {:error, term()}

Calculates total cache size in bytes.

Examples

{:ok, size} = CacheManager.cache_size(cache_dir)
# => {:ok, 1_234_567_890}

clear_cache(model_name, cache_dir)

@spec clear_cache(atom() | :all, String.t()) :: :ok | {:error, term()}

Clears cached models.

Examples

# Clear specific model
CacheManager.clear_cache(:roberta_base, cache_dir)

# Clear all models
CacheManager.clear_cache(:all, cache_dir)

get_cached_model(model_name, cache_dir)

@spec get_cached_model(atom(), String.t()) :: {:ok, String.t()} | :not_found

Gets the cached model path if it exists.

Examples

CacheManager.get_cached_model(:roberta_base, "/path/to/cache")
# => {:ok, "/path/to/cache/roberta-base"}
# or :not_found

list_cached_models(cache_dir)

@spec list_cached_models(String.t()) :: [cache_entry()]

Lists all cached models with their metadata.

Examples

CacheManager.list_cached_models(cache_dir)
# => [%{model_name: :roberta_base, size_bytes: 500_000_000, ...}, ...]

register_cached_model(model_name, cache_dir, opts \\ [])

@spec register_cached_model(atom(), String.t(), keyword()) :: :ok

Records a model in the cache registry.

Examples

CacheManager.register_cached_model(:roberta_base, "/path/to/cache")