Skip to content

Commit ac5d89d

Browse files
committed
feat: upload progress bar
1 parent 34d72fd commit ac5d89d

4 files changed

Lines changed: 94 additions & 18 deletions

File tree

kernel-builder/Cargo.lock

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kernel-builder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ clap_complete = "4"
1616
eyre = "0.6.12"
1717
git2 = "0.20"
1818
huggingface-hub = { git = "https://github.com/huggingface/huggingface_hub_rust.git", rev = "8cbc662035e04d4be8e829316272893e980f5926", package = "huggingface-hub", features = ["blocking", "xet"] }
19+
indicatif = "0.17"
1920
itertools = "0.13"
2021
minijinja = "2.5"
2122
minijinja-embed = "2.5"

kernel-builder/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ enum Commands {
107107
/// Repository type on Hugging Face Hub (`model` or `kernel`).
108108
#[arg(long, value_enum, default_value_t = RepoTypeArg::Model)]
109109
repo_type: RepoTypeArg,
110+
111+
/// Suppress progress output.
112+
#[arg(long, short)]
113+
quiet: bool,
110114
},
111115

112116
/// Upload kernel build artifacts to the Hugging Face Hub.
@@ -222,6 +226,7 @@ fn main() -> Result<()> {
222226
branch,
223227
private,
224228
repo_type,
229+
quiet,
225230
} => {
226231
run_build(
227232
kernel_dir.clone(),
@@ -236,6 +241,7 @@ fn main() -> Result<()> {
236241
branch,
237242
private,
238243
repo_type,
244+
quiet,
239245
})
240246
}
241247
Commands::CreatePyproject {

kernel-builder/src/upload.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ use std::{
22
collections::{BTreeMap, HashSet},
33
fs,
44
path::{Path, PathBuf},
5+
sync::Arc,
56
};
67

78
use clap::Args;
89
use eyre::{bail, Context, Result};
910
use 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};
1315
use walkdir::WalkDir;
1416

1517
use 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

6571
pub 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

Comments
 (0)