File size: 2,452 Bytes
d9534e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a4fa79
 
 
 
 
 
 
 
 
d9534e5
 
 
 
 
 
 
 
 
 
 
 
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
"""Download one inventory task with pinned source selection and archive verification."""
import argparse
import hashlib
import json
from pathlib import Path
import tarfile
from huggingface_hub import HfApi, hf_hub_download

def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('task_id')
    parser.add_argument('--output', type=Path, required=True)
    parser.add_argument('--revision', default='main')
    args = parser.parse_args()
    api = HfApi()
    repo = 'harness-hub/agent-tasks'
    revision = api.dataset_info(repo, revision=args.revision).sha
    manifest = hf_hub_download(repo, 'data/tasks.jsonl', repo_type='dataset', revision=revision)
    rows = [json.loads(line) for line in Path(manifest).read_text().splitlines()]
    row = next((r for r in rows if r['task_id'] == args.task_id), None)
    if row is None:
        parser.error('Task ID is absent from this inventory')
    args.output.mkdir(parents=True, exist_ok=True)
    if row['bundle_archive']:
        archive = Path(hf_hub_download(repo, row['bundle_archive'], repo_type='dataset', revision=revision))
        if hashlib.sha256(archive.read_bytes()).hexdigest() != row['bundle_sha256']:
            raise ValueError('Archive checksum mismatch')
        with tarfile.open(archive) as tf:
            if row['pool'] == 'cyber_authorization':
                prefix = row['task_path'] + '/'
                members = [m for m in tf.getmembers()
                           if m.name.startswith((prefix, 'licenses/'))]
                if not any(m.name == prefix + 'task.toml' for m in members):
                    raise ValueError('Task directory is missing from the archive')
                tf.extractall(args.output, members=members, filter='data')
            else:
                tf.extractall(args.output, filter='data')
        print(args.output / row['task_path'])
    else:
        files = list(api.list_repo_tree(row['task_repo'], path_in_repo=row['task_path'], recursive=True, repo_type='dataset', revision=row['task_revision']))
        names = [f.path for f in files if hasattr(f, 'size')]
        if not names:
            raise ValueError('Pinned task directory contains no files')
        for name in names:
            hf_hub_download(row['task_repo'], name, repo_type='dataset', revision=row['task_revision'], local_dir=args.output)
        print(args.output / row['task_path'])

if __name__ == '__main__':
    main()