-
Notifications
You must be signed in to change notification settings - Fork 613
/
Copy pathtest_file_download.py
1355 lines (1138 loc) · 56.6 KB
/
test_file_download.py
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
# Copyright 2020 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import io
import os
import re
import shutil
import stat
import unittest
import warnings
from contextlib import contextmanager
from pathlib import Path
from typing import Iterable
from unittest.mock import Mock, patch
import pytest
import requests
from requests import Response
import huggingface_hub.file_download
from huggingface_hub import HfApi, RepoUrl, constants
from huggingface_hub._local_folder import write_download_metadata
from huggingface_hub.errors import (
EntryNotFoundError,
GatedRepoError,
LocalEntryNotFoundError,
RepositoryNotFoundError,
RevisionNotFoundError,
)
from huggingface_hub.file_download import (
_CACHED_NO_EXIST,
HfFileMetadata,
_check_disk_space,
_create_symlink,
_get_pointer_path,
_normalize_etag,
_request_wrapper,
cached_download,
filename_to_url,
get_hf_file_metadata,
hf_hub_download,
hf_hub_url,
http_get,
try_to_load_from_cache,
)
from huggingface_hub.utils import (
SoftTemporaryDirectory,
get_session,
hf_raise_for_status,
)
from .testing_constants import ENDPOINT_STAGING, OTHER_TOKEN, TOKEN
from .testing_utils import (
DUMMY_MODEL_ID,
DUMMY_MODEL_ID_PINNED_SHA1,
DUMMY_MODEL_ID_PINNED_SHA256,
DUMMY_MODEL_ID_REVISION_INVALID,
DUMMY_MODEL_ID_REVISION_ONE_SPECIFIC_COMMIT,
DUMMY_RENAMED_NEW_MODEL_ID,
DUMMY_RENAMED_OLD_MODEL_ID,
SAMPLE_DATASET_IDENTIFIER,
OfflineSimulationMode,
expect_deprecation,
offline,
repo_name,
use_tmp_repo,
with_production_testing,
xfail_on_windows,
)
REVISION_ID_DEFAULT = "main"
# Default branch name
DATASET_ID = SAMPLE_DATASET_IDENTIFIER
# An actual dataset hosted on huggingface.co
DATASET_REVISION_ID_ONE_SPECIFIC_COMMIT = "e25d55a1c4933f987c46cc75d8ffadd67f257c61"
# One particular commit for DATASET_ID
DATASET_SAMPLE_PY_FILE = "custom_squad.py"
class TestDiskUsageWarning(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Test with 100MB expected file size
cls.expected_size = 100 * 1024 * 1024
@patch("huggingface_hub.file_download.shutil.disk_usage")
def test_disk_usage_warning(self, disk_usage_mock: Mock) -> None:
# Test with only 1MB free disk space / not enough disk space, with UserWarning expected
disk_usage_mock.return_value.free = 1024 * 1024
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
_check_disk_space(expected_size=self.expected_size, target_dir=disk_usage_mock)
assert len(w) == 1
assert issubclass(w[-1].category, UserWarning)
# Test with 200MB free disk space / enough disk space, with no warning expected
disk_usage_mock.return_value.free = 200 * 1024 * 1024
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
_check_disk_space(expected_size=self.expected_size, target_dir=disk_usage_mock)
assert len(w) == 0
def test_disk_usage_warning_with_non_existent_path(self) -> None:
# Test for not existent (absolute) path
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
_check_disk_space(expected_size=self.expected_size, target_dir="path/to/not_existent_path")
assert len(w) == 0
# Test for not existent (relative) path
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
_check_disk_space(expected_size=self.expected_size, target_dir="/path/to/not_existent_path")
assert len(w) == 0
class StagingDownloadTests(unittest.TestCase):
_api = HfApi(endpoint=ENDPOINT_STAGING, token=TOKEN)
@use_tmp_repo()
def test_download_from_a_gated_repo_with_hf_hub_download(self, repo_url: RepoUrl) -> None:
"""Checks `hf_hub_download` outputs error on gated repo.
Regression test for #1121.
https://github.com/huggingface/huggingface_hub/pull/1121
Cannot test on staging as dynamically setting a gated repo doesn't work there.
"""
# Set repo as gated
response = get_session().put(
f"{self._api.endpoint}/api/models/{repo_url.repo_id}/settings",
json={"gated": "auto"},
headers=self._api._build_hf_headers(),
)
hf_raise_for_status(response)
# Cannot download file as repo is gated
with SoftTemporaryDirectory() as tmpdir:
with self.assertRaisesRegex(
GatedRepoError, "Access to model .* is restricted and you are not in the authorized list"
):
hf_hub_download(
repo_id=repo_url.repo_id, filename=".gitattributes", token=OTHER_TOKEN, cache_dir=tmpdir
)
@use_tmp_repo()
def test_download_regular_file_from_private_renamed_repo(self, repo_url: RepoUrl) -> None:
"""Regression test for #1999.
See https://github.com/huggingface/huggingface_hub/pull/1999.
"""
repo_id_before = repo_url.repo_id
repo_id_after = repo_url.repo_id + "_renamed"
# Make private + rename + upload regular file
self._api.update_repo_visibility(repo_id_before, private=True)
self._api.upload_file(repo_id=repo_id_before, path_in_repo="file.txt", path_or_fileobj=b"content")
self._api.move_repo(repo_id_before, repo_id_after)
# Download from private renamed repo
path = self._api.hf_hub_download(repo_id_before, filename="file.txt")
with open(path) as f:
self.assertEqual(f.read(), "content")
# Move back (so that auto-cleanup works)
self._api.move_repo(repo_id_after, repo_id_before)
@with_production_testing
class CachedDownloadTests(unittest.TestCase):
@expect_deprecation("cached_download")
@expect_deprecation("url_to_filename")
def test_bogus_url(self):
url = "https://bogus"
with self.assertRaisesRegex(ValueError, "Connection error"):
_ = cached_download(url, legacy_cache_layout=True)
@expect_deprecation("cached_download")
@expect_deprecation("url_to_filename")
def test_no_connection(self):
invalid_url = hf_hub_url(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
revision=DUMMY_MODEL_ID_REVISION_INVALID,
)
valid_url = hf_hub_url(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, revision=REVISION_ID_DEFAULT)
self.assertIsNotNone(cached_download(valid_url, force_download=True, legacy_cache_layout=True))
for offline_mode in OfflineSimulationMode:
with offline(mode=offline_mode):
with self.assertRaisesRegex(ValueError, "Connection error"):
_ = cached_download(invalid_url, legacy_cache_layout=True)
with self.assertRaisesRegex(ValueError, "Connection error"):
_ = cached_download(valid_url, force_download=True, legacy_cache_layout=True)
self.assertIsNotNone(cached_download(valid_url, legacy_cache_layout=True))
@expect_deprecation("cached_download")
def test_file_not_found_on_repo(self):
# Valid revision (None) but missing file on repo.
url = hf_hub_url(DUMMY_MODEL_ID, filename="missing.bin")
with self.assertRaisesRegex(
EntryNotFoundError,
re.compile("404 Client Error(.*)Entry Not Found", flags=re.DOTALL),
):
_ = cached_download(url, legacy_cache_layout=True)
def test_file_not_found_locally_and_network_disabled(self):
# Valid file but missing locally and network is disabled.
with SoftTemporaryDirectory() as tmpdir:
# Download a first time to get the refs ok
filepath = hf_hub_download(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
cache_dir=tmpdir,
local_files_only=False,
)
# Remove local file
os.remove(filepath)
# Get without network must fail
with pytest.raises(LocalEntryNotFoundError):
hf_hub_download(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
cache_dir=tmpdir,
local_files_only=True,
)
@expect_deprecation("cached_download")
@expect_deprecation("url_to_filename")
def test_file_not_found_locally_and_network_disabled_legacy(self):
# Valid file but missing locally and network is disabled.
url = hf_hub_url(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME)
with SoftTemporaryDirectory() as tmpdir:
# Get without network must fail
with pytest.raises(LocalEntryNotFoundError):
cached_download(
url,
legacy_cache_layout=True,
local_files_only=True,
cache_dir=tmpdir,
)
def test_private_repo_and_file_cached_locally(self):
api = HfApi(endpoint=ENDPOINT_STAGING, token=TOKEN)
repo_id = api.create_repo(repo_id=repo_name(), private=True).repo_id
api.upload_file(path_or_fileobj=b"content", path_in_repo=constants.CONFIG_NAME, repo_id=repo_id)
with SoftTemporaryDirectory() as tmpdir:
# Download a first time with token => file is cached
filepath_1 = hf_hub_download(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, cache_dir=tmpdir, token=TOKEN)
# Download without token => return cached file
filepath_2 = hf_hub_download(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, cache_dir=tmpdir)
self.assertEqual(filepath_1, filepath_2)
def test_file_cached_and_read_only_access(self):
"""Should works if file is already cached and user has read-only permission.
Regression test for https://github.com/huggingface/huggingface_hub/issues/1216.
"""
# Valid file but missing locally and network is disabled.
with SoftTemporaryDirectory() as tmpdir:
# Download a first time to get the refs ok
hf_hub_download(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, cache_dir=tmpdir)
# Set read-only permission recursively
_recursive_chmod(tmpdir, 0o555)
# Get without write-access must succeed
hf_hub_download(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, cache_dir=tmpdir)
# Set permission back for cleanup
_recursive_chmod(tmpdir, 0o777)
@expect_deprecation("cached_download")
def test_revision_not_found(self):
# Valid file but missing revision
url = hf_hub_url(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
revision=DUMMY_MODEL_ID_REVISION_INVALID,
)
with self.assertRaisesRegex(
RevisionNotFoundError,
re.compile("404 Client Error(.*)Revision Not Found", flags=re.DOTALL),
):
_ = cached_download(url, legacy_cache_layout=True)
@expect_deprecation("cached_download")
def test_repo_not_found(self):
# Invalid model file.
url = hf_hub_url("bert-base", filename="pytorch_model.bin")
with self.assertRaisesRegex(
RepositoryNotFoundError,
re.compile("401 Client Error(.*)Repository Not Found", flags=re.DOTALL),
):
_ = cached_download(url, legacy_cache_layout=True)
@expect_deprecation("cached_download")
@expect_deprecation("url_to_filename")
@expect_deprecation("filename_to_url")
def test_standard_object(self):
url = hf_hub_url(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, revision=REVISION_ID_DEFAULT)
filepath = cached_download(url, force_download=True, legacy_cache_layout=True)
metadata = filename_to_url(filepath, legacy_cache_layout=True)
self.assertEqual(metadata, (url, f'"{DUMMY_MODEL_ID_PINNED_SHA1}"'))
@expect_deprecation("cached_download")
@expect_deprecation("url_to_filename")
@expect_deprecation("filename_to_url")
def test_standard_object_rev(self):
# Same object, but different revision
url = hf_hub_url(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
revision=DUMMY_MODEL_ID_REVISION_ONE_SPECIFIC_COMMIT,
)
filepath = cached_download(url, force_download=True, legacy_cache_layout=True)
metadata = filename_to_url(filepath, legacy_cache_layout=True)
self.assertNotEqual(metadata[1], f'"{DUMMY_MODEL_ID_PINNED_SHA1}"')
# Caution: check that the etag is *not* equal to the one from `test_standard_object`
@expect_deprecation("cached_download")
@expect_deprecation("url_to_filename")
@expect_deprecation("filename_to_url")
def test_lfs_object(self):
url = hf_hub_url(DUMMY_MODEL_ID, filename=constants.PYTORCH_WEIGHTS_NAME, revision=REVISION_ID_DEFAULT)
filepath = cached_download(url, force_download=True, legacy_cache_layout=True)
metadata = filename_to_url(filepath, legacy_cache_layout=True)
self.assertEqual(metadata, (url, f'"{DUMMY_MODEL_ID_PINNED_SHA256}"'))
@expect_deprecation("cached_download")
@expect_deprecation("url_to_filename")
@expect_deprecation("filename_to_url")
def test_dataset_standard_object_rev(self):
url = hf_hub_url(
DATASET_ID,
filename=DATASET_SAMPLE_PY_FILE,
repo_type=constants.REPO_TYPE_DATASET,
revision=DATASET_REVISION_ID_ONE_SPECIFIC_COMMIT,
)
# now let's download
filepath = cached_download(url, force_download=True, legacy_cache_layout=True)
metadata = filename_to_url(filepath, legacy_cache_layout=True)
self.assertNotEqual(metadata[1], f'"{DUMMY_MODEL_ID_PINNED_SHA1}"')
@expect_deprecation("cached_download")
@expect_deprecation("url_to_filename")
@expect_deprecation("filename_to_url")
def test_dataset_lfs_object(self):
url = hf_hub_url(
DATASET_ID,
filename="dev-v1.1.json",
repo_type=constants.REPO_TYPE_DATASET,
revision=DATASET_REVISION_ID_ONE_SPECIFIC_COMMIT,
)
filepath = cached_download(url, force_download=True, legacy_cache_layout=True)
metadata = filename_to_url(filepath, legacy_cache_layout=True)
self.assertEqual(
metadata,
(url, '"95aa6a52d5d6a735563366753ca50492a658031da74f301ac5238b03966972c9"'),
)
@xfail_on_windows(reason="umask is UNIX-specific")
def test_hf_hub_download_custom_cache_permission(self):
"""Checks `hf_hub_download` respect the cache dir permission.
Regression test for #1141 #1215.
https://github.com/huggingface/huggingface_hub/issues/1141
https://github.com/huggingface/huggingface_hub/issues/1215
"""
with SoftTemporaryDirectory() as tmpdir:
# Equivalent to umask u=rwx,g=r,o=
previous_umask = os.umask(0o037)
try:
filepath = hf_hub_download(DUMMY_RENAMED_OLD_MODEL_ID, "config.json", cache_dir=tmpdir)
# Permissions are honored (640: u=rw,g=r,o=)
self.assertEqual(stat.S_IMODE(os.stat(filepath).st_mode), 0o640)
finally:
os.umask(previous_umask)
def test_download_from_a_renamed_repo_with_hf_hub_download(self):
"""Checks `hf_hub_download` works also on a renamed repo.
Regression test for #981.
https://github.com/huggingface/huggingface_hub/issues/981
"""
with SoftTemporaryDirectory() as tmpdir:
filepath = hf_hub_download(DUMMY_RENAMED_OLD_MODEL_ID, "config.json", cache_dir=tmpdir)
self.assertTrue(os.path.exists(filepath))
def test_download_from_a_renamed_repo_with_cached_download(self):
"""Checks `cached_download` works also on a renamed repo.
Regression test for #981.
https://github.com/huggingface/huggingface_hub/issues/981
"""
with pytest.warns(FutureWarning):
with SoftTemporaryDirectory() as tmpdir:
filepath = cached_download(
hf_hub_url(
DUMMY_RENAMED_OLD_MODEL_ID,
filename="config.json",
),
cache_dir=tmpdir,
)
self.assertTrue(os.path.exists(filepath))
def test_hf_hub_download_with_empty_subfolder(self):
"""
Check subfolder arg is processed correctly when empty string is passed to
`hf_hub_download`.
See https://github.com/huggingface/huggingface_hub/issues/1016.
"""
filepath = Path(
hf_hub_download(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
subfolder="", # Subfolder should be processed as `None`
)
)
# Check file exists and is not in a subfolder in cache
# e.g: "(...)/snapshots/<commit-id>/config.json"
self.assertTrue(filepath.is_file())
self.assertEqual(filepath.name, constants.CONFIG_NAME)
self.assertEqual(Path(filepath).parent.parent.name, "snapshots")
def test_hf_hub_download_offline_no_refs(self):
"""Regression test for #1305.
If "refs/" dir did not exists on "local_files_only" (or connection broken), a
non-explicit `FileNotFoundError` was raised (for the "/refs/revision" file) instead
of the documented `LocalEntryNotFoundError` (for the actual searched file).
See https://github.com/huggingface/huggingface_hub/issues/1305.
"""
with SoftTemporaryDirectory() as cache_dir:
with self.assertRaises(LocalEntryNotFoundError):
hf_hub_download(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
local_files_only=True,
cache_dir=cache_dir,
)
def test_hf_hub_download_with_user_agent(self):
"""
Check that user agent is correctly sent to the HEAD call when downloading a file.
Regression test for #1854.
See https://github.com/huggingface/huggingface_hub/pull/1854.
"""
def _check_user_agent(headers: dict):
assert "user-agent" in headers
assert "test/1.0.0" in headers["user-agent"]
assert "foo/bar" in headers["user-agent"]
with SoftTemporaryDirectory() as cache_dir:
with patch("huggingface_hub.file_download._request_wrapper", wraps=_request_wrapper) as mock_request:
# First download
hf_hub_download(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
cache_dir=cache_dir,
library_name="test",
library_version="1.0.0",
user_agent="foo/bar",
)
calls = mock_request.call_args_list
assert len(calls) == 3 # HEAD, HEAD, GET
for call in calls:
_check_user_agent(call.kwargs["headers"])
with patch("huggingface_hub.file_download._request_wrapper", wraps=_request_wrapper) as mock_request:
# Second download: no GET call
hf_hub_download(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
cache_dir=cache_dir,
library_name="test",
library_version="1.0.0",
user_agent="foo/bar",
)
calls = mock_request.call_args_list
assert len(calls) == 2 # HEAD, HEAD
for call in calls:
_check_user_agent(call.kwargs["headers"])
def test_hf_hub_url_with_empty_subfolder(self):
"""
Check subfolder arg is processed correctly when empty string is passed to
`hf_hub_url`.
See https://github.com/huggingface/huggingface_hub/issues/1016.
"""
url = hf_hub_url(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
subfolder="", # Subfolder should be processed as `None`
)
self.assertTrue(
url.endswith(
# "./resolve/main/config.json" and not "./resolve/main//config.json"
f"{DUMMY_MODEL_ID}/resolve/main/config.json",
)
)
@patch("huggingface_hub.file_download.constants.ENDPOINT", "https://huggingface.co")
@patch(
"huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE",
"https://huggingface.co/{repo_id}/resolve/{revision}/{filename}",
)
def test_hf_hub_url_with_endpoint(self):
self.assertEqual(
hf_hub_url(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
endpoint="https://hf-ci.co",
),
"https://hf-ci.co/julien-c/dummy-unknown/resolve/main/config.json",
)
@expect_deprecation("hf_hub_download")
@expect_deprecation("cached_download")
@expect_deprecation("filename_to_url")
@expect_deprecation("url_to_filename")
def test_hf_hub_download_legacy(self):
filepath = hf_hub_download(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
revision=REVISION_ID_DEFAULT,
force_download=True,
legacy_cache_layout=True,
)
metadata = filename_to_url(filepath, legacy_cache_layout=True)
self.assertEqual(metadata[1], f'"{DUMMY_MODEL_ID_PINNED_SHA1}"')
def test_try_to_load_from_cache_exist(self):
# Make sure the file is cached
filepath = hf_hub_download(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME)
new_file_path = try_to_load_from_cache(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME)
self.assertEqual(filepath, new_file_path)
new_file_path = try_to_load_from_cache(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, revision="main")
self.assertEqual(filepath, new_file_path)
# If file is not cached, returns None
self.assertIsNone(try_to_load_from_cache(DUMMY_MODEL_ID, filename="conf.json"))
# Same for uncached revisions
self.assertIsNone(
try_to_load_from_cache(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
revision="aaa",
)
)
# Same for uncached models
self.assertIsNone(try_to_load_from_cache("bert-base", filename=constants.CONFIG_NAME))
def test_try_to_load_from_cache_specific_pr_revision_exists(self):
# Make sure the file is cached
file_path = hf_hub_download(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, revision="refs/pr/1")
new_file_path = try_to_load_from_cache(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, revision="refs/pr/1")
self.assertEqual(file_path, new_file_path)
# If file is not cached, returns None
self.assertIsNone(try_to_load_from_cache(DUMMY_MODEL_ID, filename="conf.json", revision="refs/pr/1"))
# If revision does not exist, returns None
self.assertIsNone(
try_to_load_from_cache(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, revision="does-not-exist")
)
def test_try_to_load_from_cache_no_exist(self):
# Make sure the file is cached
with self.assertRaises(EntryNotFoundError):
_ = hf_hub_download(DUMMY_MODEL_ID, filename="dummy")
new_file_path = try_to_load_from_cache(DUMMY_MODEL_ID, filename="dummy")
self.assertEqual(new_file_path, _CACHED_NO_EXIST)
new_file_path = try_to_load_from_cache(DUMMY_MODEL_ID, filename="dummy", revision="main")
self.assertEqual(new_file_path, _CACHED_NO_EXIST)
# If file non-existence is not cached, returns None
self.assertIsNone(try_to_load_from_cache(DUMMY_MODEL_ID, filename="dummy2"))
def test_try_to_load_from_cache_specific_commit_id_exist(self):
"""Regression test for #1306.
See https://github.com/huggingface/huggingface_hub/pull/1306."""
with SoftTemporaryDirectory() as cache_dir:
# Cache file from specific commit id (no "refs/"" folder)
commit_id = HfApi().model_info(DUMMY_MODEL_ID).sha
filepath = hf_hub_download(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
revision=commit_id,
cache_dir=cache_dir,
)
# Must be able to retrieve it "offline"
attempt = try_to_load_from_cache(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
revision=commit_id,
cache_dir=cache_dir,
)
self.assertEqual(filepath, attempt)
def test_try_to_load_from_cache_specific_commit_id_no_exist(self):
"""Regression test for #1306.
See https://github.com/huggingface/huggingface_hub/pull/1306."""
with SoftTemporaryDirectory() as cache_dir:
# Cache file from specific commit id (no "refs/"" folder)
commit_id = HfApi().model_info(DUMMY_MODEL_ID).sha
with self.assertRaises(EntryNotFoundError):
hf_hub_download(
DUMMY_MODEL_ID,
filename="missing_file",
revision=commit_id,
cache_dir=cache_dir,
)
# Must be able to retrieve it "offline"
attempt = try_to_load_from_cache(
DUMMY_MODEL_ID,
filename="missing_file",
revision=commit_id,
cache_dir=cache_dir,
)
self.assertEqual(attempt, _CACHED_NO_EXIST)
def test_get_hf_file_metadata_basic(self) -> None:
"""Test getting metadata from a file on the Hub."""
url = hf_hub_url(
DUMMY_MODEL_ID,
filename=constants.CONFIG_NAME,
revision=DUMMY_MODEL_ID_REVISION_ONE_SPECIFIC_COMMIT,
)
metadata = get_hf_file_metadata(url)
# Metadata
self.assertEqual(metadata.commit_hash, DUMMY_MODEL_ID_REVISION_ONE_SPECIFIC_COMMIT)
self.assertIsNotNone(metadata.etag) # example: "85c2fc2dcdd86563aaa85ef4911..."
self.assertEqual(metadata.location, url) # no redirect
self.assertEqual(metadata.size, 851)
def test_get_hf_file_metadata_from_a_renamed_repo(self) -> None:
"""Test getting metadata from a file in a renamed repo on the Hub."""
url = hf_hub_url(
DUMMY_RENAMED_OLD_MODEL_ID,
filename=constants.CONFIG_NAME,
subfolder="", # Subfolder should be processed as `None`
)
metadata = get_hf_file_metadata(url)
# Got redirected to renamed repo
self.assertEqual(
metadata.location,
url.replace(DUMMY_RENAMED_OLD_MODEL_ID, DUMMY_RENAMED_NEW_MODEL_ID),
)
def test_get_hf_file_metadata_from_a_lfs_file(self) -> None:
"""Test getting metadata from an LFS file.
Must get size of the LFS file, not size of the pointer file
"""
url = hf_hub_url("gpt2", filename="tf_model.h5")
metadata = get_hf_file_metadata(url)
self.assertIn("cdn-lfs", metadata.location) # Redirection
self.assertEqual(metadata.size, 497933648) # Size of LFS file, not pointer
def test_file_consistency_check_fails_regular_file(self):
"""Regression test for #1396 (regular file).
Download fails if file size is different than the expected one (from headers metadata).
See https://github.com/huggingface/huggingface_hub/pull/1396."""
with SoftTemporaryDirectory() as cache_dir:
def _mocked_hf_file_metadata(*args, **kwargs):
metadata = get_hf_file_metadata(*args, **kwargs)
return HfFileMetadata(
commit_hash=metadata.commit_hash,
etag=metadata.etag,
location=metadata.location,
size=450, # will expect 450 bytes but will download 496 bytes
)
with patch("huggingface_hub.file_download.get_hf_file_metadata", _mocked_hf_file_metadata):
with self.assertRaises(EnvironmentError):
hf_hub_download(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, cache_dir=cache_dir)
def test_file_consistency_check_fails_LFS_file(self):
"""Regression test for #1396 (LFS file).
Download fails if file size is different than the expected one (from headers metadata).
See https://github.com/huggingface/huggingface_hub/pull/1396."""
with SoftTemporaryDirectory() as cache_dir:
def _mocked_hf_file_metadata(*args, **kwargs):
metadata = get_hf_file_metadata(*args, **kwargs)
return HfFileMetadata(
commit_hash=metadata.commit_hash,
etag=metadata.etag,
location=metadata.location,
size=65000, # will expect 65000 bytes but will download 65074 bytes
)
with patch("huggingface_hub.file_download.get_hf_file_metadata", _mocked_hf_file_metadata):
with self.assertRaises(EnvironmentError):
hf_hub_download(DUMMY_MODEL_ID, filename="pytorch_model.bin", cache_dir=cache_dir)
def test_hf_hub_download_when_tmp_file_is_complete(self):
"""Regression test for #2511.
See https://github.com/huggingface/huggingface_hub/issues/2511.
When downloading a file, we first download to a temporary file and then move it to the final location.
If the temporary file is already partially downloaded, we resume from where we left off.
However, if the temporary file is already fully downloaded, we should try to make a GET call with an empty range.
This was causing a "416 Range Not Satisfiable" error.
"""
with SoftTemporaryDirectory() as tmpdir:
# Download the file once
filepath = Path(hf_hub_download(DUMMY_MODEL_ID, filename="pytorch_model.bin", cache_dir=tmpdir))
# Fake tmp file
incomplete_filepath = Path(str(filepath.resolve()) + ".incomplete")
incomplete_filepath.write_bytes(filepath.read_bytes()) # fake a partial download
filepath.resolve().unlink()
# delete snapshot folder to re-trigger a download
shutil.rmtree(filepath.parents[2] / "snapshots")
# Download must not fail
hf_hub_download(DUMMY_MODEL_ID, filename="pytorch_model.bin", cache_dir=tmpdir)
@expect_deprecation("cached_download")
@expect_deprecation("url_to_filename")
def test_cached_download_from_github(self):
"""Regression test for #1449.
File consistency check was failing due to compression in HTTP request which made the expected size smaller than
the actual one. `cached_download` is deprecated but still heavily used so we need to make sure it works.
See:
- https://github.com/huggingface/huggingface_hub/issues/1449.
- https://github.com/huggingface/diffusers/issues/3213.
"""
with SoftTemporaryDirectory() as cache_dir:
cached_download(
url="https://raw.githubusercontent.com/huggingface/diffusers/v0.15.1/examples/community/lpw_stable_diffusion.py",
token=None,
cache_dir=cache_dir,
)
@unittest.skipIf(os.name == "nt", "Lock files are always deleted on Windows.")
def test_keep_lock_file(self):
"""Lock files should not be deleted on Linux."""
with SoftTemporaryDirectory() as tmpdir:
hf_hub_download(DUMMY_MODEL_ID, filename=constants.CONFIG_NAME, cache_dir=tmpdir)
lock_file_exist = False
locks_dir = os.path.join(tmpdir, ".locks")
for subdir, dirs, files in os.walk(locks_dir):
for file in files:
if file.endswith(".lock"):
lock_file_exist = True
break
self.assertTrue(lock_file_exist, "no lock file can be found")
@pytest.mark.usefixtures("fx_cache_dir")
class HfHubDownloadToLocalDir(unittest.TestCase):
# `cache_dir` is a temporary directory
# `local_dir` is a subdirectory in which files will be downloaded
# `hub_cache_dir` is a subdirectory in which files will be cached ("HF cache")
cache_dir: Path
file_name: str = "file.txt"
lfs_name: str = "lfs.bin"
@property
def local_dir(self) -> Path:
path = Path(self.cache_dir) / "local"
path.mkdir(exist_ok=True, parents=True)
return path
@property
def hub_cache_dir(self) -> Path:
path = Path(self.cache_dir) / "cache"
path.mkdir(exist_ok=True, parents=True)
return path
@property
def file_path(self) -> Path:
return self.local_dir / self.file_name
@property
def lfs_path(self) -> Path:
return self.local_dir / self.lfs_name
@classmethod
def setUpClass(cls):
cls.api = HfApi(endpoint=ENDPOINT_STAGING, token=TOKEN)
cls.repo_id = cls.api.create_repo(repo_id=repo_name()).repo_id
commit_1 = cls.api.upload_file(path_or_fileobj=b"content", path_in_repo=cls.file_name, repo_id=cls.repo_id)
commit_2 = cls.api.upload_file(path_or_fileobj=b"content", path_in_repo=cls.lfs_name, repo_id=cls.repo_id)
info = cls.api.get_paths_info(repo_id=cls.repo_id, paths=[cls.file_name, cls.lfs_name])
info = {item.path: item for item in info}
cls.commit_hash_1 = commit_1.oid
cls.commit_hash_2 = commit_2.oid
cls.file_etag = info[cls.file_name].blob_id
cls.lfs_etag = info[cls.lfs_name].lfs.sha256
@classmethod
def tearDownClass(cls) -> None:
cls.api.delete_repo(repo_id=cls.repo_id)
@contextmanager
def with_patch_head(self):
with patch("huggingface_hub.file_download._get_metadata_or_catch_error") as mock:
yield mock
@contextmanager
def with_patch_download(self):
with patch("huggingface_hub.file_download._download_to_tmp_and_move") as mock:
yield mock
def test_empty_local_dir(self):
# Download to local dir
returned_path = self.api.hf_hub_download(
self.repo_id, filename=self.file_name, cache_dir=self.hub_cache_dir, local_dir=self.local_dir
)
assert self.local_dir in Path(returned_path).parents
# Cache directory not used (no blobs, no symlinks in it)
for path in self.hub_cache_dir.glob("**/blobs/**"):
assert not path.is_file()
for path in self.hub_cache_dir.glob("**/snapshots/**"):
assert not path.is_file()
def test_metadata_ok_and_revision_is_a_commit_hash_and_match(self):
# File already exists + commit_hash matches (and etag not even required)
self.file_path.write_text("content")
write_download_metadata(self.local_dir, self.file_name, self.commit_hash_1, etag="...")
# Download to local dir => no HEAD call needed
with self.with_patch_head() as mock:
self.api.hf_hub_download(
self.repo_id, filename=self.file_name, revision=self.commit_hash_1, local_dir=self.local_dir
)
mock.assert_not_called()
def test_metadata_ok_and_revision_is_a_commit_hash_and_mismatch(self):
# 1 HEAD call + 1 download
# File already exists + commit_hash mismatch
self.file_path.write_text("content")
write_download_metadata(self.local_dir, self.file_name, self.commit_hash_1, etag="...")
# Mismatch => download
with self.with_patch_download() as mock:
self.api.hf_hub_download(
self.repo_id, filename=self.file_name, revision=self.commit_hash_2, local_dir=self.local_dir
)
mock.assert_called_once()
def test_metadata_not_ok_and_revision_is_a_commit_hash(self):
# 1 HEAD call + 1 download
# File already exists but no metadata
self.file_path.write_text("content")
# Mismatch => download
with self.with_patch_download() as mock:
self.api.hf_hub_download(
self.repo_id, filename=self.file_name, revision=self.commit_hash_1, local_dir=self.local_dir
)
mock.assert_called_once()
def test_local_files_only_and_file_exists(self):
# must return without error
self.file_path.write_text("content2")
path = self.api.hf_hub_download(
self.repo_id, filename=self.file_name, local_dir=self.local_dir, local_files_only=True
)
assert Path(path) == self.file_path
assert self.file_path.read_text() == "content2" # not overwritten even if wrong content
def test_local_files_only_and_file_missing(self):
# must raise
with self.assertRaises(LocalEntryNotFoundError):
self.api.hf_hub_download(
self.repo_id, filename=self.file_name, local_dir=self.local_dir, local_files_only=True
)
def test_metadata_ok_and_etag_match(self):
# 1 HEAD call + return early
self.file_path.write_text("something")
write_download_metadata(self.local_dir, self.file_name, self.commit_hash_1, etag=self.file_etag)
with self.with_patch_download() as mock:
# Download from main => commit_hash mismatch but etag match => return early
self.api.hf_hub_download(self.repo_id, filename=self.file_name, local_dir=self.local_dir)
mock.assert_not_called()
def test_metadata_ok_and_etag_mismatch(self):
# 1 HEAD call + 1 download
self.file_path.write_text("something")
write_download_metadata(self.local_dir, self.file_name, self.commit_hash_1, etag="some_other_etag")
with self.with_patch_download() as mock:
# Download from main => commit_hash mismatch but etag match => return early
self.api.hf_hub_download(self.repo_id, filename=self.file_name, local_dir=self.local_dir)
mock.assert_called_once()
def test_metadata_ok_and_etag_match_and_force_download(self):
# force_download=True takes precedence on any other rule
self.file_path.write_text("something")
write_download_metadata(self.local_dir, self.file_name, self.commit_hash_1, etag=self.file_etag)
with self.with_patch_download() as mock:
self.api.hf_hub_download(
self.repo_id, filename=self.file_name, local_dir=self.local_dir, force_download=True
)
mock.assert_called_once()
def test_metadata_not_ok_and_lfs_file_and_sha256_match(self):
# 1 HEAD call + 1 hash compute + return early
self.lfs_path.write_text("content")
with self.with_patch_download() as mock:
# Download from main
# => no metadata but it's an LFS file
# => compute local hash => matches => return early
self.api.hf_hub_download(self.repo_id, filename=self.lfs_name, local_dir=self.local_dir)
mock.assert_not_called()
def test_metadata_not_ok_and_lfs_file_and_sha256_mismatch(self):
# 1 HEAD call + 1 file hash + 1 download
self.lfs_path.write_text("wrong_content")
# Download from main
# => no metadata but it's an LFS file
# => compute local hash => mismatches => download
path = self.api.hf_hub_download(self.repo_id, filename=self.lfs_name, local_dir=self.local_dir)
# existing file overwritten
assert Path(path).read_text() == "content"
def test_file_exists_in_cache(self):
# 1 HEAD call + return early
self.api.hf_hub_download(self.repo_id, filename=self.file_name, cache_dir=self.hub_cache_dir)
with self.with_patch_download() as mock:
# Download to local dir
# => file is already in Hub cache
# => we assume it's faster to make a local copy rather than re-downloading
# => duplicate file locally
path = self.api.hf_hub_download(
self.repo_id, filename=self.file_name, cache_dir=self.hub_cache_dir, local_dir=self.local_dir
)
mock.assert_not_called()
assert Path(path) == self.file_path
def test_file_exists_and_overwrites(self):
# 1 HEAD call + 1 download
self.file_path.write_text("another content")
self.api.hf_hub_download(self.repo_id, filename=self.file_name, local_dir=self.local_dir)
assert self.file_path.read_text() == "content"