-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathsync.rs
More file actions
1362 lines (1230 loc) · 43.5 KB
/
sync.rs
File metadata and controls
1362 lines (1230 loc) · 43.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use super::{RepoInfo, HF_ENDPOINT};
use crate::api::sync::ApiError::InvalidHeader;
use crate::api::Progress;
use crate::{Cache, Repo, RepoType};
use http::{StatusCode, Uri};
use indicatif::ProgressBar;
use rand::Rng;
use std::collections::HashMap;
use std::io::Read;
use std::io::Seek;
use std::num::ParseIntError;
use std::path::{Component, Path, PathBuf};
use std::str::FromStr;
use thiserror::Error;
use ureq::config::ConfigBuilder;
use ureq::config::RedirectAuthHeaders;
use ureq::tls::{TlsConfig, TlsProvider};
use ureq::typestate::{AgentScope, WithoutBody};
use ureq::{Agent, RequestBuilder};
/// Current version (used in user-agent)
const VERSION: &str = env!("CARGO_PKG_VERSION");
/// Current name (used in user-agent)
const NAME: &str = env!("CARGO_PKG_NAME");
const RANGE: &str = "Range";
const CONTENT_RANGE: &str = "Content-Range";
const LOCATION: &str = "Location";
const USER_AGENT: &str = "User-Agent";
const AUTHORIZATION: &str = "Authorization";
type HeaderMap = HashMap<&'static str, String>;
type HeaderName = &'static str;
/// Specific name for the sync part of the resumable file
const EXTENSION: &str = "part";
struct Wrapper<'a, P: Progress, R: Read> {
progress: &'a mut P,
inner: R,
}
fn wrap_read<P: Progress, R: Read>(inner: R, progress: &mut P) -> Wrapper<'_, P, R> {
Wrapper { inner, progress }
}
impl<P: Progress, R: Read> Read for Wrapper<'_, P, R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let read = self.inner.read(buf)?;
self.progress.update(read);
Ok(read)
}
}
/// Simple wrapper over [`ureq::Agent`] to include default headers
#[derive(Clone, Debug)]
pub struct HeaderAgent {
agent: Agent,
headers: HeaderMap,
}
impl HeaderAgent {
fn new(agent: Agent, headers: HeaderMap) -> Self {
Self { agent, headers }
}
fn get(&self, url: &str) -> RequestBuilder<WithoutBody> {
let mut request = self.agent.get(url);
for (header, value) in &self.headers {
request = request.header(*header, value);
}
request
}
}
struct Handle {
file: std::fs::File,
}
impl Drop for Handle {
fn drop(&mut self) {
unlock(&self.file);
}
}
fn lock_file(mut path: PathBuf) -> Result<Handle, ApiError> {
path.set_extension("lock");
let file = std::fs::File::create(path.clone())?;
let mut res = lock(&file);
for _ in 0..5 {
if res == 0 {
break;
}
std::thread::sleep(std::time::Duration::from_secs(1));
res = lock(&file);
}
if res != 0 {
Err(ApiError::LockAcquisition(path))
} else {
Ok(Handle { file })
}
}
#[cfg(target_family = "unix")]
mod unix {
use std::os::fd::AsRawFd;
pub(crate) fn lock(file: &std::fs::File) -> i32 {
unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }
}
pub(crate) fn unlock(file: &std::fs::File) -> i32 {
unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_UN) }
}
}
#[cfg(target_family = "unix")]
use unix::{lock, unlock};
#[cfg(target_family = "windows")]
mod windows {
use std::os::windows::io::AsRawHandle;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::Storage::FileSystem::{
LockFileEx, UnlockFile, LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY,
};
pub(crate) fn lock(file: &std::fs::File) -> i32 {
unsafe {
let mut overlapped = std::mem::zeroed();
let flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
let res = LockFileEx(
file.as_raw_handle() as HANDLE,
flags,
0,
!0,
!0,
&mut overlapped,
);
1 - res
}
}
pub(crate) fn unlock(file: &std::fs::File) -> i32 {
unsafe { UnlockFile(file.as_raw_handle() as HANDLE, 0, 0, !0, !0) }
}
}
#[cfg(target_family = "windows")]
use windows::{lock, unlock};
#[cfg(not(any(target_family = "unix", target_family = "windows")))]
mod other {
pub(crate) fn lock(file: &std::fs::File) -> i32 {
0
}
pub(crate) fn unlock(file: &std::fs::File) -> i32 {
0
}
}
#[cfg(not(any(target_family = "unix", target_family = "windows")))]
use other::{lock, unlock};
#[derive(Debug, Error)]
/// All errors the API can throw
pub enum ApiError {
/// Api expects certain header to be present in the results to derive some information
#[error("Header {0} is missing")]
MissingHeader(HeaderName),
/// The header exists, but the value is not conform to what the Api expects.
#[error("Header {0} is invalid")]
InvalidHeader(HeaderName),
// /// The value cannot be used as a header during request header construction
// #[error("Invalid header value {0}")]
// InvalidHeaderValue(#[from] InvalidHeaderValue),
// /// The header value is not valid utf-8
// #[error("header value is not a string")]
// ToStr(#[from] ToStrError),
/// Error in the request
#[error("request error: {0}")]
RequestError(#[from] Box<ureq::Error>),
/// Error parsing some range value
#[error("Cannot parse int")]
ParseIntError(#[from] ParseIntError),
/// I/O Error
#[error("I/O error {0}")]
IoError(#[from] std::io::Error),
/// We tried to download chunk too many times
#[error("Too many retries: {0}")]
TooManyRetries(Box<ApiError>),
/// Native tls error
#[error("Native tls: {0}")]
#[cfg(feature = "native-tls")]
Native(#[from] native_tls::Error),
/// The part file is corrupted
#[error("Invalid part file - corrupted file")]
InvalidResume,
/// We failed to acquire lock for file `f`. Meaning
/// Someone else is writing/downloading said file
#[error("Lock acquisition failed: {0}")]
LockAcquisition(PathBuf),
}
/// Helper to create [`Api`] with all the options.
#[derive(Debug)]
pub struct ApiBuilder {
endpoint: String,
cache: Cache,
token: Option<String>,
max_retries: usize,
progress: bool,
user_agent: Vec<(String, String)>,
}
impl Default for ApiBuilder {
fn default() -> Self {
Self::new()
}
}
impl ApiBuilder {
/// Default api builder
/// ```
/// use hf_hub::api::sync::ApiBuilder;
/// let api = ApiBuilder::new().build().unwrap();
/// ```
pub fn new() -> Self {
let cache = Cache::default();
Self::from_cache(cache)
}
/// Creates API with values potentially from environment variables.
/// HF_HOME decides the location of the cache folder
/// HF_ENDPOINT modifies the URL for the huggingface location
/// to download files from.
/// ```
/// use hf_hub::api::sync::ApiBuilder;
/// let api = ApiBuilder::from_env().build().unwrap();
/// ```
pub fn from_env() -> Self {
let cache = Cache::from_env();
let mut builder = Self::from_cache(cache);
if let Ok(endpoint) = std::env::var(HF_ENDPOINT) {
builder = builder.with_endpoint(endpoint);
}
builder
}
/// From a given cache
/// ```
/// use hf_hub::{api::sync::ApiBuilder, Cache};
/// let path = std::path::PathBuf::from("/tmp");
/// let cache = Cache::new(path);
/// let api = ApiBuilder::from_cache(cache).build().unwrap();
/// ```
pub fn from_cache(cache: Cache) -> Self {
let token = cache.token();
let max_retries = 0;
let progress = true;
let endpoint = "https://huggingface.co".to_string();
let user_agent = vec![
("unknown".to_string(), "None".to_string()),
(NAME.to_string(), VERSION.to_string()),
("rust".to_string(), "unknown".to_string()),
];
Self {
endpoint,
cache,
token,
max_retries,
progress,
user_agent,
}
}
/// Whether to show a progressbar
pub fn with_progress(mut self, progress: bool) -> Self {
self.progress = progress;
self
}
/// Changes the endpoint of the API. Default is `https://huggingface.co`.
pub fn with_endpoint(mut self, endpoint: String) -> Self {
self.endpoint = endpoint;
self
}
/// Changes the location of the cache directory. Defaults is `~/.cache/huggingface/`.
pub fn with_cache_dir(mut self, cache_dir: PathBuf) -> Self {
self.cache = Cache::new(cache_dir);
self
}
/// Sets the token to be used in the API
pub fn with_token(mut self, token: Option<String>) -> Self {
self.token = token;
self
}
/// Sets the number of times the API will retry to download a file
pub fn with_retries(mut self, max_retries: usize) -> Self {
self.max_retries = max_retries;
self
}
/// Adds custom fields to headers user-agent
pub fn with_user_agent(mut self, key: &str, value: &str) -> Self {
self.user_agent.push((key.to_string(), value.to_string()));
self
}
fn build_headers(&self) -> HeaderMap {
let mut headers = HeaderMap::new();
let user_agent = self
.user_agent
.iter()
.map(|(key, value)| format!("{key}/{value}"))
.collect::<Vec<_>>()
.join("; ");
headers.insert(USER_AGENT, user_agent.to_string());
if let Some(token) = &self.token {
headers.insert(AUTHORIZATION, format!("Bearer {token}"));
}
headers
}
/// Consumes the builder and builds the final [`Api`]
pub fn build(self) -> Result<Api, ApiError> {
let headers = self.build_headers();
let bldr = builder()?.redirect_auth_headers(RedirectAuthHeaders::SameHost);
let agent: Agent = bldr.build().into();
let client = HeaderAgent::new(agent, headers.clone());
let no_redirect_agent: Agent = builder()?
.max_redirects(0)
.build()
.into();
let no_redirect_client = HeaderAgent::new(no_redirect_agent, headers);
Ok(Api {
endpoint: self.endpoint,
cache: self.cache,
client,
no_redirect_client,
max_retries: self.max_retries,
progress: self.progress,
})
}
}
/// File metadata.
#[derive(Debug)]
pub struct Metadata {
commit_hash: String,
etag: String,
size: usize,
}
impl Metadata {
/// Get the commit hash of the file.
pub fn commit_hash(&self) -> &str {
&self.commit_hash
}
/// Get the etag of the file.
pub fn etag(&self) -> &str {
&self.etag
}
/// Get the file size.
pub fn size(&self) -> usize {
self.size
}
}
/// The actual Api used to interact with the hub.
/// Use any repo with [`Api::repo`]
#[derive(Clone, Debug)]
pub struct Api {
endpoint: String,
cache: Cache,
client: HeaderAgent,
no_redirect_client: HeaderAgent,
max_retries: usize,
progress: bool,
}
fn make_relative(src: &Path, dst: &Path) -> PathBuf {
let path = src;
let base = dst;
assert_eq!(
path.is_absolute(),
base.is_absolute(),
"This function is made to look at absolute paths only"
);
let mut ita = path.components();
let mut itb = base.components();
loop {
match (ita.next(), itb.next()) {
(Some(a), Some(b)) if a == b => (),
(some_a, _) => {
// Ignoring b, because 1 component is the filename
// for which we don't need to go back up for relative
// filename to work.
let mut new_path = PathBuf::new();
for _ in itb {
new_path.push(Component::ParentDir);
}
if let Some(a) = some_a {
new_path.push(a);
for comp in ita {
new_path.push(comp);
}
}
return new_path;
}
}
}
}
fn symlink_or_rename(src: &Path, dst: &Path) -> Result<(), std::io::Error> {
if dst.exists() {
return Ok(());
}
let rel_src = make_relative(src, dst);
#[cfg(target_os = "windows")]
{
if std::os::windows::fs::symlink_file(rel_src, dst).is_err() {
std::fs::rename(src, dst)?;
}
}
#[cfg(target_family = "unix")]
std::os::unix::fs::symlink(rel_src, dst)?;
Ok(())
}
fn jitter() -> usize {
rand::rng().random_range(0..=500)
}
fn exponential_backoff(base_wait_time: usize, n: usize, max: usize) -> usize {
(base_wait_time + n.pow(2) + jitter()).min(max)
}
impl Api {
/// Creates a default Api, for Api options See [`ApiBuilder`]
pub fn new() -> Result<Self, ApiError> {
ApiBuilder::new().build()
}
/// Get the underlying api client
/// Allows for lower level access
pub fn client(&self) -> &HeaderAgent {
&self.client
}
/// Get metadata for the file at the given URL.
pub fn metadata(&self, url: &str) -> Result<Metadata, ApiError> {
let mut response = self
.no_redirect_client
.get(url)
.header(RANGE, "bytes=0-0")
.call()
.map_err(Box::new)?;
// Closure to check if status code is a redirection
let should_redirect = |status_code: StatusCode| {
matches!(
status_code,
StatusCode::MOVED_PERMANENTLY
| StatusCode::FOUND
| StatusCode::SEE_OTHER
| StatusCode::TEMPORARY_REDIRECT
| StatusCode::PERMANENT_REDIRECT
)
};
// Follow redirects until `host.is_some()` i.e. only follow relative redirects
// See: https://github.com/huggingface/huggingface_hub/blob/9c6af39cdce45b570f0b7f8fad2b311c96019804/src/huggingface_hub/file_download.py#L411
let response = loop {
// Check if redirect
if should_redirect(response.status()) {
// Get redirect location
if let Some(location) = response.headers().get("Location") {
// Parse location
let uri = Uri::from_str(
std::str::from_utf8(location.as_bytes())
.map_err(|_| InvalidHeader("location"))?,
)
.map_err(|_| InvalidHeader("location"))?;
// Check if relative i.e. host is none
if uri.host().is_none() {
// Merge relative path with url
let mut parts = Uri::from_str(url).unwrap().into_parts();
parts.path_and_query = uri.into_parts().path_and_query;
// Final uri
let redirect_uri = Uri::from_parts(parts).unwrap();
// Follow redirect
response = self
.no_redirect_client
.get(&redirect_uri.to_string())
.header(RANGE, "bytes=0-0")
.call()
.map_err(Box::new)?;
continue;
}
};
}
break response;
};
// let headers = response.headers();
let header_commit = "x-repo-commit";
let header_linked_etag = "x-linked-etag";
let header_etag = "etag";
let etag = match response.headers().get(header_linked_etag) {
Some(etag) => etag,
None => response
.headers()
.get(header_etag)
.ok_or(ApiError::MissingHeader(header_etag))?,
};
// Cleaning extra quotes
let etag = std::str::from_utf8(etag.as_bytes())
.map_err(|_| ApiError::InvalidHeader("etag"))?
.replace('"', "");
let commit_hash = std::str::from_utf8(
response
.headers()
.get(header_commit)
.ok_or(ApiError::MissingHeader(header_commit))?
.as_bytes(),
)
.map_err(|_| ApiError::InvalidHeader("commit_hash"))?
.to_string();
// The response was redirected to S3 most likely which will
// know about the size of the file
let status = response.status();
let is_redirection = status.is_redirection();
let response = if is_redirection {
let location = response
.headers()
.get(LOCATION)
.expect("location header in redirect");
let location = std::str::from_utf8(location.as_bytes())
.map_err(|_| ApiError::InvalidHeader("etag"))?;
self.client
.get(location)
.header(RANGE, "bytes=0-0")
.call()
.map_err(Box::new)?
} else {
response
};
let content_range = response
.headers()
.get(CONTENT_RANGE)
.ok_or(ApiError::MissingHeader(CONTENT_RANGE))?;
let content_range = std::str::from_utf8(content_range.as_bytes())
.map_err(|_| ApiError::InvalidHeader(CONTENT_RANGE))?;
let size = content_range
.split('/')
.next_back()
.ok_or(ApiError::InvalidHeader(CONTENT_RANGE))?
.parse()?;
Ok(Metadata {
commit_hash,
etag,
size,
})
}
fn download_tempfile<P: Progress>(
&self,
url: &str,
size: usize,
mut progress: P,
tmp_path: PathBuf,
filename: &str,
) -> Result<PathBuf, ApiError> {
progress.init(size, filename);
let filepath = tmp_path;
// Create the file and set everything properly
let mut file = match std::fs::OpenOptions::new().append(true).open(&filepath) {
Ok(f) => f,
Err(_) => std::fs::File::create(&filepath)?,
};
// In case of resume.
let start = file.metadata()?.len();
if start > size as u64 {
return Err(ApiError::InvalidResume);
}
let mut res = self.download_from(url, start, size, &mut file, filename, &mut progress);
if self.max_retries > 0 {
let mut i = 0;
while let Err(dlerr) = res {
let wait_time = exponential_backoff(300, i, 10_000);
std::thread::sleep(std::time::Duration::from_millis(wait_time as u64));
let current = file.stream_position()?;
res = self.download_from(url, current, size, &mut file, filename, &mut progress);
i += 1;
if i > self.max_retries {
return Err(ApiError::TooManyRetries(dlerr.into()));
}
}
}
res?;
Ok(filepath)
}
fn download_from<P>(
&self,
url: &str,
current: u64,
size: usize,
file: &mut std::fs::File,
filename: &str,
progress: &mut P,
) -> Result<(), ApiError>
where
P: Progress,
{
let range = format!("bytes={current}-");
let response = self
.client
.get(url)
.header(RANGE, &range)
.call()
.map_err(Box::new)?;
let (_, body) = response.into_parts();
let reader = body.into_reader();
progress.init(size, filename);
progress.update(current as usize);
let mut reader = Box::new(wrap_read(reader, progress));
std::io::copy(&mut reader, file)?;
progress.finish();
Ok(())
}
/// Creates a new handle [`ApiRepo`] which contains operations
/// on a particular [`Repo`]
pub fn repo(&self, repo: Repo) -> ApiRepo {
ApiRepo::new(self.clone(), repo)
}
/// Simple wrapper over
/// ```
/// # use hf_hub::{api::sync::Api, Repo, RepoType};
/// # let model_id = "gpt2".to_string();
/// let api = Api::new().unwrap();
/// let api = api.repo(Repo::new(model_id, RepoType::Model));
/// ```
pub fn model(&self, model_id: String) -> ApiRepo {
self.repo(Repo::new(model_id, RepoType::Model))
}
/// Simple wrapper over
/// ```
/// # use hf_hub::{api::sync::Api, Repo, RepoType};
/// # let model_id = "gpt2".to_string();
/// let api = Api::new().unwrap();
/// let api = api.repo(Repo::new(model_id, RepoType::Dataset));
/// ```
pub fn dataset(&self, model_id: String) -> ApiRepo {
self.repo(Repo::new(model_id, RepoType::Dataset))
}
/// Simple wrapper over
/// ```
/// # use hf_hub::{api::sync::Api, Repo, RepoType};
/// # let model_id = "gpt2".to_string();
/// let api = Api::new().unwrap();
/// let api = api.repo(Repo::new(model_id, RepoType::Space));
/// ```
pub fn space(&self, model_id: String) -> ApiRepo {
self.repo(Repo::new(model_id, RepoType::Space))
}
}
/// Shorthand for accessing things within a particular repo
/// You can inspect repos with [`ApiRepo::info`]
/// or download files with [`ApiRepo::download`]
#[derive(Debug)]
pub struct ApiRepo {
api: Api,
repo: Repo,
}
impl ApiRepo {
fn new(api: Api, repo: Repo) -> Self {
Self { api, repo }
}
}
#[cfg(feature = "native-tls")]
fn builder() -> Result<ConfigBuilder<AgentScope>, ApiError> {
Ok(Agent::config_builder().tls_config(
TlsConfig::builder()
.provider(TlsProvider::NativeTls)
.root_certs(ureq::tls::RootCerts::PlatformVerifier)
.build(),
))
}
#[cfg(not(feature = "native-tls"))]
fn builder() -> Result<ConfigBuilder<AgentScope>, ApiError> {
Ok(Agent::config_builder()
.tls_config(TlsConfig::builder().provider(TlsProvider::Rustls).build()))
}
impl ApiRepo {
/// Get the fully qualified URL of the remote filename
/// ```
/// # use hf_hub::api::sync::Api;
/// let api = Api::new().unwrap();
/// let url = api.model("gpt2".to_string()).url("model.safetensors");
/// assert_eq!(url, "https://huggingface.co/gpt2/resolve/main/model.safetensors");
/// ```
pub fn url(&self, filename: &str) -> String {
let endpoint = &self.api.endpoint;
let revision = &self.repo.url_revision();
let repo_id = self.repo.url();
format!("{endpoint}/{repo_id}/resolve/{revision}/{filename}")
}
/// This will attempt the fetch the file locally first, then [`Api.download`]
/// if the file is not present.
/// ```no_run
/// use hf_hub::{api::sync::Api};
/// let api = Api::new().unwrap();
/// let local_filename = api.model("gpt2".to_string()).get("model.safetensors").unwrap();
pub fn get(&self, filename: &str) -> Result<PathBuf, ApiError> {
if let Some(path) = self.api.cache.repo(self.repo.clone()).get(filename) {
Ok(path)
} else {
self.download(filename)
}
}
/// This function is used to download a file with a custom progress function.
/// It uses the [`Progress`] trait and can be used in more complex use
/// cases like downloading a showing progress in a UI.
/// ```no_run
/// # use hf_hub::api::{sync::Api, Progress};
/// struct MyProgress{
/// current: usize,
/// total: usize
/// }
///
/// impl Progress for MyProgress{
/// fn init(&mut self, size: usize, _filename: &str){
/// self.total = size;
/// self.current = 0;
/// }
///
/// fn update(&mut self, size: usize){
/// self.current += size;
/// println!("{}/{}", self.current, self.total)
/// }
///
/// fn finish(&mut self){
/// println!("Done !");
/// }
/// }
/// let api = Api::new().unwrap();
/// let progress = MyProgress{current: 0, total: 0};
/// let local_filename = api.model("gpt2".to_string()).download_with_progress("model.safetensors", progress).unwrap();
/// ```
pub fn download_with_progress<P: Progress>(
&self,
filename: &str,
progress: P,
) -> Result<PathBuf, ApiError> {
let url = self.url(filename);
let metadata = self.api.metadata(&url)?;
let blob_path = self
.api
.cache
.repo(self.repo.clone())
.blob_path(&metadata.etag);
std::fs::create_dir_all(blob_path.parent().unwrap())?;
let lock = lock_file(blob_path.clone())?;
let mut tmp_path = blob_path.clone();
tmp_path.set_extension(EXTENSION);
let tmp_filename =
self.api
.download_tempfile(&url, metadata.size, progress, tmp_path, filename)?;
std::fs::rename(tmp_filename, &blob_path)?;
drop(lock);
let mut pointer_path = self
.api
.cache
.repo(self.repo.clone())
.pointer_path(&metadata.commit_hash);
pointer_path.push(filename);
std::fs::create_dir_all(pointer_path.parent().unwrap()).ok();
symlink_or_rename(&blob_path, &pointer_path)?;
self.api
.cache
.repo(self.repo.clone())
.create_ref(&metadata.commit_hash)?;
assert!(pointer_path.exists());
Ok(pointer_path)
}
/// Downloads a remote file into the cache directory
/// to be used locally.
/// ```no_run
/// # use hf_hub::api::sync::Api;
/// let api = Api::new().unwrap();
/// let local_filename = api.model("gpt2".to_string()).download("model.safetensors").unwrap();
/// ```
pub fn download(&self, filename: &str) -> Result<PathBuf, ApiError> {
if self.api.progress {
self.download_with_progress(filename, ProgressBar::new(0))
} else {
self.download_with_progress(filename, ())
}
}
/// Get information about the Repo
/// ```
/// use hf_hub::{api::sync::Api};
/// let api = Api::new().unwrap();
/// api.model("gpt2".to_string()).info();
/// ```
pub fn info(&self) -> Result<RepoInfo, ApiError> {
let mut response = self.info_request().call().map_err(Box::new)?;
Ok(response.body_mut().read_json().map_err(Box::new)?)
}
/// Get the raw [`Request`] with the url and method already set
/// ```
/// # use hf_hub::api::sync::Api;
/// let api = Api::new().unwrap();
/// api.model("gpt2".to_owned())
/// .info_request()
/// .query("blobs", "true")
/// .call();
/// ```
pub fn info_request(&self) -> RequestBuilder<WithoutBody> {
let url = format!("{}/api/{}", self.api.endpoint, self.repo.api_url());
self.api.client.get(&url)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::api::Siblings;
use crate::assert_no_diff;
use hex_literal::hex;
use rand::{distr::Alphanumeric, Rng};
use serde_json::{json, Value};
use sha2::{Digest, Sha256};
use std::io::{Seek, SeekFrom, Write};
use std::time::Duration;
struct TempDir {
path: PathBuf,
}
impl TempDir {
pub fn new() -> Self {
let s: String = rand::rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect();
let mut path = std::env::temp_dir();
path.push(s);
std::fs::create_dir(&path).unwrap();
Self { path }
}
}
impl Drop for TempDir {
fn drop(&mut self) {
std::fs::remove_dir_all(&self.path).unwrap()
}
}
#[test]
fn simple() {
let tmp = TempDir::new();
let api = ApiBuilder::new()
.with_progress(false)
.with_cache_dir(tmp.path.clone())
.build()
.unwrap();
let model_id = "julien-c/dummy-unknown".to_string();
let downloaded_path = api.model(model_id.clone()).download("config.json").unwrap();
assert!(downloaded_path.exists());
let val = Sha256::digest(std::fs::read(&*downloaded_path).unwrap());
assert_eq!(
val[..],
hex!("b908f2b7227d4d31a2105dfa31095e28d304f9bc938bfaaa57ee2cacf1f62d32")
);
// Make sure the file is now seeable without connection
let cache_path = api
.cache
.repo(Repo::new(model_id, RepoType::Model))
.get("config.json")
.unwrap();
assert_eq!(cache_path, downloaded_path);
}
#[test]
fn resume() {
let tmp = TempDir::new();
let api = ApiBuilder::new()
.with_progress(false)
.with_cache_dir(tmp.path.clone())
.build()
.unwrap();
let model_id = "julien-c/dummy-unknown".to_string();
let downloaded_path = api.model(model_id.clone()).download("config.json").unwrap();
assert!(downloaded_path.exists());
let val = Sha256::digest(std::fs::read(&*downloaded_path).unwrap());
assert_eq!(
val[..],
hex!("b908f2b7227d4d31a2105dfa31095e28d304f9bc938bfaaa57ee2cacf1f62d32")
);
let blob = std::fs::canonicalize(&downloaded_path).unwrap();
let file = std::fs::OpenOptions::new().write(true).open(&blob).unwrap();
let size = file.metadata().unwrap().len();
let truncate: f32 = rand::random();
let new_size = (size as f32 * truncate) as u64;
file.set_len(new_size).unwrap();
let mut blob_part = blob.clone();
blob_part.set_extension("part");
std::fs::rename(blob, &blob_part).unwrap();
std::fs::remove_file(&downloaded_path).unwrap();
let content = std::fs::read(&*blob_part).unwrap();
assert_eq!(content.len() as u64, new_size);
let val = Sha256::digest(content);
// We modified the sha.
assert!(
val[..] != hex!("b908f2b7227d4d31a2105dfa31095e28d304f9bc938bfaaa57ee2cacf1f62d32")
);
let new_downloaded_path = api.model(model_id.clone()).download("config.json").unwrap();
let val = Sha256::digest(std::fs::read(&*new_downloaded_path).unwrap());
assert_eq!(downloaded_path, new_downloaded_path);
assert_eq!(
val[..],
hex!("b908f2b7227d4d31a2105dfa31095e28d304f9bc938bfaaa57ee2cacf1f62d32")
);
// Here we prove the previous part was correctly resuming by purposefully corrupting the
// file.
let blob = std::fs::canonicalize(&downloaded_path).unwrap();
let mut file = std::fs::OpenOptions::new().write(true).open(&blob).unwrap();
let size = file.metadata().unwrap().len();
// Not random for consistent sha corruption
let truncate: f32 = 0.5;
let new_size = (size as f32 * truncate) as u64;
// Truncating
file.set_len(new_size).unwrap();
// Corrupting by changing a single byte.
file.seek(SeekFrom::Start(new_size - 1)).unwrap();
file.write_all(&[0]).unwrap();