Skip to main content

ModelCache

Struct ModelCache 

Source
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

Source

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.

Source

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.

Source

pub fn cache_dir(&self) -> &Path

The root cache directory path.

Source

pub fn is_cached(&self, repo_id: &str, filename: &str) -> bool

Check if a file is already present in the cache (without downloading).

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more