Download load_tasks.py from Alibaba-VELLDEPTH/MMA-SafetyBench: direct link, hf CLI and curl.
- Browser
- Download file 2.82 kB
-
https://huggingface.co/datasets/Alibaba-VELLDEPTH/MMA-SafetyBench/resolve/main/load_tasks.py
- Command line
-
hf download hf://datasets/Alibaba-VELLDEPTH/MMA-SafetyBench/load_tasks.py
-
curl -L -o load_tasks.py https://huggingface.co/datasets/Alibaba-VELLDEPTH/MMA-SafetyBench/resolve/main/load_tasks.py
2.82 kB
| """Offline task index reader. No model calls and no execution of image instructions.""" | |
| import csv | |
| import json | |
| from collections import Counter | |
| from pathlib import Path | |
| from urllib.parse import urlsplit | |
| EXPECTED = {'GUI': 209, 'Document': 200, 'Web': 200, 'Mobile': 283, 'Video': 191} | |
| def local_file(root, value): | |
| root = Path(root).resolve() | |
| if not value or urlsplit(value).scheme or Path(value).is_absolute(): | |
| raise ValueError('Expected a root-relative local file') | |
| p = (root / value).resolve() | |
| if not p.is_relative_to(root) or not p.is_file(): | |
| raise ValueError('Missing or out-of-root file') | |
| return p | |
| def load_tasks(root=None, domain=None): | |
| """Yield manifest dictionaries with parsed history lists, preserving root-relative paths.""" | |
| root = Path(root or Path(__file__).parent).resolve() | |
| with (root / 'task_manifest.csv').open(newline='', encoding='utf-8') as f: | |
| rows = list(csv.DictReader(f)) | |
| if Counter(r['domain'] for r in rows) != Counter(EXPECTED): | |
| raise ValueError('Unexpected benchmark membership') | |
| if len({r['record_id'] for r in rows}) != len(rows): | |
| raise ValueError('Duplicate record IDs') | |
| if domain is not None and domain not in EXPECTED: | |
| raise ValueError('Unknown domain') | |
| for row in rows: | |
| if domain is not None and row['domain'] != domain: | |
| continue | |
| row['history_media'] = json.loads(row.pop('history_media_json')) | |
| for key in ('annotation_file', 'input_media', 'clean_media'): | |
| if row[key]: | |
| local_file(root, row[key]) | |
| for h in row['history_media']: | |
| local_file(root, h) | |
| yield row | |
| def load_annotation(root, row): | |
| """Return the original task record; Video returns the video ID, QA and video info.""" | |
| p = local_file(root, row['annotation_file']) | |
| if row['annotation_line'] != '': | |
| j = json.loads(p.read_text().splitlines()[int(row['annotation_line']) - 1]) | |
| q = j['qa'][int(row['qa_index'])] | |
| if str(j['key']) + ':' + str(q['uid']) != row['task_id']: | |
| raise ValueError('Video locator no longer matches task') | |
| return {'video_id': j['key'], 'qa': q, 'video_info': j['video_info']} | |
| j = json.loads(p.read_text()) | |
| if row['annotation_index'] != '': | |
| j = j[int(row['annotation_index'])] | |
| if str(j['task_id']) != row['task_id']: | |
| raise ValueError('Annotation locator no longer matches task') | |
| return j | |
| if __name__ == '__main__': | |
| root = Path(__file__).parent | |
| rows = list(load_tasks(root)) | |
| for row in rows: | |
| load_annotation(root, row) | |
| print(json.dumps({'tasks': len(rows), 'domains': dict(Counter(r['domain'] for r in rows)), | |
| 'all_indexed_files_exist': True, 'all_annotation_locators_match': True}, indent=2)) | |