matiasqr commited on
Commit
4860298
1 Parent(s): 7148711

folder to download only one folder of the dataset repository

Browse files
Files changed (1) hide show
  1. download_folder.py +41 -0
download_folder.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import HfApi, hf_hub_download
2
+
3
+
4
+ def download_folder(repo_id, repo_type, folder_path, local_dir):
5
+ """
6
+ Download an entire folder from a huggingface dataset repository.
7
+
8
+ repo_id : string
9
+ The ID of the repository (e.g., 'username/repo_name').
10
+ repo_type : string
11
+ Type of the repo, dataset or model.
12
+ folder_path : string
13
+ The path to the folder within the repository.
14
+ local_dir : string
15
+ Local folder to download the data. This mimics git behaviour
16
+ """
17
+ api = HfApi()
18
+ # list all files in the repo, keep the ones within folder_path
19
+ all_files = api.list_repo_files(repo_id, repo_type=repo_type)
20
+ files_list = [f for f in all_files if f.startswith(folder_path)]
21
+
22
+ # download each of those files
23
+ for file_path in files_list:
24
+ hf_hub_download(repo_id=repo_id, repo_type=repo_type,
25
+ filename=file_path, local_dir=local_dir)
26
+
27
+
28
+ # Download entire data/ folder
29
+ repo_id = "NUS-UAL/global-streetscapes"
30
+ repo_type = "dataset"
31
+ folder_path = "data/" # replace the folder you want
32
+ local_dir = "global-streetscapes/"
33
+
34
+ # By degfault, huggingface download them to the .cache/huggingface folder
35
+ download_folder(repo_id, repo_type, folder_path, local_dir)
36
+
37
+ # Download 2 additional files
38
+ hf_hub_download(repo_id=repo_id, repo_type=repo_type,
39
+ filename="cities688.csv", local_dir=local_dir)
40
+ hf_hub_download(repo_id=repo_id, repo_type=repo_type,
41
+ filename="info.csv", local_dir=local_dir)