File size: 1,848 Bytes
ee594f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
download_from_hf.py

Run this on your new (i9/3080ti) laptop to pull down the trimmed UCSF-PDGM
dataset you uploaded to HuggingFace (unknownBrain/ucsf).

Install first:
    pip install -U huggingface_hub

If the dataset repo is private, also run:
    huggingface-cli login

Run in two steps:
    python download_from_hf.py list      -> just prints file list/structure
    python download_from_hf.py download  -> actually downloads everything
"""

import sys
from huggingface_hub import HfApi, snapshot_download

HF_REPO_ID = "unknownBrain/ucsf"
LOCAL_DIR = r"D:\jainam\UCSF-PDGM-v5"   # where you want it locally

api = HfApi()


def list_files():
    files = api.list_repo_files(repo_id=HF_REPO_ID, repo_type="dataset")
    print(f"total files: {len(files)}")
    for f in files[:30]:
        print(" ", f)
    if len(files) > 30:
        print(f"  ... and {len(files) - 30} more")
    # This tells you whether folder structure survived the upload
    # (e.g. 'UCSF-PDGM-004/UCSF-PDGM-004_T1c.nii.gz') or got flattened
    # (e.g. 'UCSF-PDGM-004_T1c.nii.gz' with no subfolder) -- update
    # dataset.py's build_records() path template to match whichever it is.


def download():
    path = snapshot_download(
        repo_id=HF_REPO_ID,
        repo_type="dataset",
        local_dir=LOCAL_DIR,
        local_dir_use_symlinks=False,   # actual files, not symlinks -- safer on Windows
        # allow_patterns=["*_T1c.nii.gz", "*_T2.nii.gz", "*_tumor_segmentation.nii.gz"],
        # ^ uncomment + edit if you only want specific modalities right now
    )
    print("Downloaded to:", path)


if __name__ == "__main__":
    mode = sys.argv[1] if len(sys.argv) > 1 else "list"
    if mode == "list":
        list_files()
    elif mode == "download":
        download()
    else:
        print("usage: python download_from_hf.py [list|download]")