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

chore: ApiBuilder should support Default without a token #60

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/api/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,21 @@ pub enum ApiError {
#[derive(Debug)]
pub struct ApiBuilder {
endpoint: String,
cache: Cache,
url_template: String,
token: Option<String>,
cache: Cache,
progress: bool,
token: Option<String>,
}

impl Default for ApiBuilder {
fn default() -> Self {
Self::new()
Self {
endpoint: "https://huggingface.co".to_string(),
url_template: "{endpoint}/{repo_id}/resolve/{revision}/{filename}".to_string(),
cache: Cache::default(),
progress: true,
token: None,
}
}
}

Expand All @@ -117,14 +123,10 @@ impl ApiBuilder {
pub fn from_cache(cache: Cache) -> Self {
let token = cache.token();

let progress = true;

Self {
endpoint: "https://huggingface.co".to_string(),
url_template: "{endpoint}/{repo_id}/resolve/{revision}/{filename}".to_string(),
cache,
token,
progress,
..Self::default()
}
}

Expand Down Expand Up @@ -170,12 +172,11 @@ impl ApiBuilder {
let no_redirect_client = HeaderAgent::new(no_redirect_agent, headers);

Ok(Api {
client,
no_redirect_client,
endpoint: self.endpoint,
url_template: self.url_template,
cache: self.cache,
client,

no_redirect_client,
progress: self.progress,
})
}
Expand All @@ -193,11 +194,11 @@ struct Metadata {
/// or download files with [`Api::download`]
#[derive(Clone, Debug)]
pub struct Api {
client: HeaderAgent,
no_redirect_client: HeaderAgent,
endpoint: String,
url_template: String,
cache: Cache,
client: HeaderAgent,
no_redirect_client: HeaderAgent,
progress: bool,
}

Expand Down
41 changes: 23 additions & 18 deletions src/api/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,31 @@ pub enum ApiError {
#[derive(Debug)]
pub struct ApiBuilder {
endpoint: String,
cache: Cache,
url_template: String,
cache: Cache,
progress: bool,
token: Option<String>,
// Async specific:
max_files: usize,
chunk_size: usize,
parallel_failures: usize,
max_retries: usize,
progress: bool,
}

impl Default for ApiBuilder {
fn default() -> Self {
Self::new()
ApiBuilder {
endpoint: "https://huggingface.co".to_string(),
url_template: "{endpoint}/{repo_id}/resolve/{revision}/{filename}".to_string(),
cache: Cache::default(),
progress: true,
token: None,
// Async specific:
max_files: num_cpus::get(),
chunk_size: 10_000_000,
parallel_failures: 0,
max_retries: 0,
}
}
}

Expand All @@ -110,18 +122,10 @@ impl ApiBuilder {
pub fn from_cache(cache: Cache) -> Self {
let token = cache.token();

let progress = true;

Self {
endpoint: "https://huggingface.co".to_string(),
url_template: "{endpoint}/{repo_id}/resolve/{revision}/{filename}".to_string(),
cache,
token,
max_files: num_cpus::get(),
chunk_size: 10_000_000,
parallel_failures: 0,
max_retries: 0,
progress,
..Self::default()
}
}

Expand Down Expand Up @@ -185,16 +189,16 @@ impl ApiBuilder {
.default_headers(headers)
.build()?;
Ok(Api {
client,
relative_redirect_client,
endpoint: self.endpoint,
url_template: self.url_template,
cache: self.cache,
client,
relative_redirect_client,
progress: self.progress,
max_files: self.max_files,
chunk_size: self.chunk_size,
parallel_failures: self.parallel_failures,
max_retries: self.max_retries,
progress: self.progress,
})
}
}
Expand All @@ -211,16 +215,17 @@ struct Metadata {
/// or download files with [`Api::download`]
#[derive(Clone, Debug)]
pub struct Api {
client: Client,
relative_redirect_client: Client,
endpoint: String,
url_template: String,
cache: Cache,
client: Client,
relative_redirect_client: Client,
progress: bool,
// Async specific:
max_files: usize,
chunk_size: usize,
parallel_failures: usize,
max_retries: usize,
progress: bool,
}

fn make_relative(src: &Path, dst: &Path) -> PathBuf {
Expand Down