@@ -2,14 +2,16 @@ use std::{
22 collections:: { BTreeMap , HashSet } ,
33 fs,
44 path:: { Path , PathBuf } ,
5+ sync:: Arc ,
56} ;
67
78use clap:: Args ;
89use eyre:: { bail, Context , Result } ;
910use huggingface_hub:: {
10- AddSource , CommitOperation , CreateBranchParams , CreateCommitParams , CreateRepoParams ,
11- ListRepoFilesParams , ListRepoRefsParams , RepoType ,
11+ AddSource , CommitOperation , CommitProgressCallback , CreateBranchParams , CreateCommitParams ,
12+ CreateRepoParams , ListRepoFilesParams , ListRepoRefsParams , RepoType ,
1213} ;
14+ use indicatif:: { ProgressBar , ProgressStyle } ;
1315use walkdir:: WalkDir ;
1416
1517use crate :: {
@@ -60,6 +62,10 @@ pub struct UploadArgs {
6062 /// Repository type on Hugging Face Hub (`model` or `kernel`).
6163 #[ arg( long, value_enum, default_value_t = RepoTypeArg :: Model ) ]
6264 pub repo_type : RepoTypeArg ,
65+
66+ /// Suppress progress output.
67+ #[ arg( long, short) ]
68+ pub quiet : bool ,
6369}
6470
6571pub fn run_upload ( args : UploadArgs ) -> Result < ( ) > {
@@ -185,20 +191,30 @@ pub fn run_upload(args: UploadArgs) -> Result<()> {
185191 continue ;
186192 }
187193
188- eprintln ! (
189- "Uploading {} operations to branch `{}`..." ,
190- operations. len( ) ,
191- branch
192- ) ;
193-
194194 let batch_count = operations. len ( ) . div_ceil ( BUILD_COMMIT_BATCH_SIZE ) ;
195- if batch_count > 1 {
196- eprintln ! (
197- "Uploading in {} commits ({} operations)." ,
198- batch_count,
199- operations. len( )
195+ let progress = if args. quiet {
196+ ProgressBar :: hidden ( )
197+ } else {
198+ let pb = ProgressBar :: new ( operations. len ( ) as u64 ) ;
199+ pb. set_style (
200+ ProgressStyle :: with_template (
201+ "Uploading to `{msg}` [{bar:40.cyan/blue}] {pos}/{len} files" ,
202+ )
203+ . unwrap ( )
204+ . progress_chars ( "=> " ) ,
200205 ) ;
201- }
206+ pb. set_message ( branch. clone ( ) ) ;
207+ pb
208+ } ;
209+
210+ let progress_callback: Option < CommitProgressCallback > = if args. quiet {
211+ None
212+ } else {
213+ let pb = progress. clone ( ) ;
214+ Some ( Arc :: new ( move |_path : & str | {
215+ pb. inc ( 1 ) ;
216+ } ) )
217+ } ;
202218
203219 for ( batch_index, chunk) in operations. chunks ( BUILD_COMMIT_BATCH_SIZE ) . enumerate ( ) {
204220 let commit_message = if batch_count > 1 {
@@ -219,14 +235,13 @@ pub fn run_upload(args: UploadArgs) -> Result<()> {
219235 revision : Some ( branch. clone ( ) ) ,
220236 create_pr : None ,
221237 parent_commit : None ,
238+ progress_callback : progress_callback. clone ( ) ,
222239 } ;
223240 api. create_commit ( & params)
224241 . wrap_err_with ( || format ! ( "Cannot create commit on branch `{branch}`" ) ) ?;
225-
226- if batch_count > 1 {
227- eprintln ! ( " Uploaded batch {}/{batch_count}." , batch_index + 1 ) ;
228- }
229242 }
243+
244+ progress. finish_with_message ( format ! ( "Uploaded to `{branch}`" ) ) ;
230245 }
231246
232247 let total_ops: usize = operations_by_branch. values ( ) . map ( |v| v. len ( ) ) . sum ( ) ;
0 commit comments