Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
pub fn with_progress_callback<F>(mut self, callback: F) -> Self where F: FnMut(ProgressEvent) + 'static
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:
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