pub struct ModelCache { /* private fields */ }Expand description
Local cache for ML models downloaded from HuggingFace Hub.
Models are stored under {cache_dir}/{repo_id}/{filename}. Files are
downloaded only once; subsequent calls return the cached path immediately.
§Examples
use blazen_model_cache::ModelCache;
let cache = ModelCache::new()?;
let path = cache.download("bert-base-uncased", "config.json", None).await?;
println!("model config at: {}", path.display());Implementations§
Source§impl ModelCache
impl ModelCache
Sourcepub fn new() -> Result<Self, CacheError>
pub fn new() -> Result<Self, CacheError>
Create a cache in the default location.
Uses $BLAZEN_CACHE_DIR/models/ if the BLAZEN_CACHE_DIR environment
variable is set, otherwise falls back to ~/.cache/blazen/models/.
§Errors
Returns CacheError::CacheDir if the home directory cannot be
determined and BLAZEN_CACHE_DIR is not set.
Sourcepub fn with_dir(cache_dir: impl Into<PathBuf>) -> Self
pub fn with_dir(cache_dir: impl Into<PathBuf>) -> Self
Create a cache rooted at a specific directory.
The directory does not need to exist yet; it will be created on the first download.
Sourcepub fn is_cached(&self, repo_id: &str, filename: &str) -> bool
pub fn is_cached(&self, repo_id: &str, filename: &str) -> bool
Check if a file is already present in the cache (without downloading).
Sourcepub async fn download(
&self,
repo_id: &str,
filename: &str,
progress: Option<Arc<dyn ProgressCallback>>,
) -> Result<PathBuf, CacheError>
pub async fn download( &self, repo_id: &str, filename: &str, progress: Option<Arc<dyn ProgressCallback>>, ) -> Result<PathBuf, CacheError>
Download a file from HuggingFace Hub if it is not already cached.
Returns the local filesystem path to the cached file.
The file is first downloaded via hf-hub into its own managed cache,
then hard-linked (or copied as fallback) into our
{cache_dir}/{repo_id}/{filename} layout so that callers get a stable,
predictable path.
§Progress
Pass an Arc<dyn ProgressCallback> to receive byte-level progress
updates during the download. Pass None to download silently.
§Errors
Returns CacheError::Download if the HuggingFace API request fails,
or CacheError::Io if filesystem operations fail.