UltraTerminals / download_task.py
kshitijthakkar's picture burtenshaw's picture
burtenshaw HF Staff
Duplicate from harness-hub/UltraTerminals
5719dc5
Raw History Blame Contribute Delete
2.45 kB
"""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()