Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: download progress callback #70

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

newfla
Copy link

@newfla newfla commented Sep 24, 2024

Motivation

Hf-hub is used by ML application/framework as entry point for model resources. When integrating hf-hub in libraries or desktop apps, it can be useful to show information about the on-going download process in different ways than a cli progress-bar (i.e. events forwarded to the tauri js side).

Techinical details

The PR adds the method ApiBuilder::with_progress_builder (both sync and tokio modules) that allows to specify a callback everytime the download progress.

The callback are so specified:

  • sync: pub fn with_progress_callback<F>(mut self, callback: F) -> Self where F: FnMut(ProgressEvent) + 'static
  • tokio: pub fn with_progress_callback<F, Fut>(mut self, callback: F) -> Self where F: 'static + Send + Sync + Fn(ProgressEvent) -> Fut, Fut: Future<Output = ()> + Send + 'static,

The event is defined as follow:

/// The download progress event
#[derive(Debug, Clone, Serialize)]
pub struct ProgressEvent {
    /// The resource to download
    pub url: String,

    /// The progress expressed as a value between 0 and 1
    pub percentage: f32,

    /// Time elapsed since the download as being started
    pub elapsed_time: Duration,

    /// Estimated time to complete the download
    pub remaining_time: Duration,
}

Serialize has been derived to simplify the usage in tauri applications.

Alternatives

  • Define ApiRepo:download(&self, filename: &str, callback: C):
    For symmetry with ApiBuilder::with_progress I've prefered to implement the method on the ApiBuilder itself.

  • Async callback implementation is far away from being ergonomics: a novel approch is under development #![feature(async_closure)] but I was not sure to bring it into this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant