File size: 1,201 Bytes
0a50834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
from pathlib import Path

from huggingface_hub import HfApi, hf_hub_download

from .config import REPO


def fetch(directory, revision, vectors):
    root = Path(directory)
    root.mkdir(parents=True, exist_ok=True)
    commit = HfApi().dataset_info(REPO, revision=revision).sha
    sums = Path(hf_hub_download(REPO, 'SHA256SUMS', repo_type='dataset', revision=commit, local_dir=root))
    hashes = dict(line.split('  ', 1)[::-1] for line in sums.read_text().splitlines())
    selected = [name for name in hashes if name in {'manifest.json', 'dumps/neo4j.dump'} or (vectors and name.startswith('parquet/'))]
    for name in selected:
        relative = Path(name)
        if relative.is_absolute() or '..' in relative.parts:
            raise ValueError('Unsafe checksum path.')
        path = Path(hf_hub_download(REPO, name, repo_type='dataset', revision=commit, local_dir=root))
        with path.open('rb') as handle:
            actual = hashlib.file_digest(handle, 'sha256').hexdigest()
        if actual != hashes[name]:
            raise ValueError('Checksum mismatch: ' + name)
        print('Verified', name, flush=True)
    print('Downloaded revision:', commit, flush=True)