File size: 2,824 Bytes
94033a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
"""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))