|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build a VLMEvalKit TSV for LongDocURL from the public Hugging Face JSONL.""" |
| 3 | + |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +import os |
| 7 | +import os.path as osp |
| 8 | + |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | + |
| 12 | +REPO_ID = 'dengchao/LongDocURL' |
| 13 | +DATA_FILE = 'LongDocURL_public_with_subtask_category.jsonl' |
| 14 | + |
| 15 | + |
| 16 | +def hf_download(filename, download_dir, token=None): |
| 17 | + try: |
| 18 | + from huggingface_hub import hf_hub_download |
| 19 | + except ImportError as err: |
| 20 | + raise ImportError( |
| 21 | + 'huggingface_hub is required to download LongDocURL. ' |
| 22 | + 'Install it with `pip install huggingface_hub`, or pass --jsonl.' |
| 23 | + ) from err |
| 24 | + |
| 25 | + return hf_hub_download( |
| 26 | + repo_id=REPO_ID, |
| 27 | + filename=filename, |
| 28 | + repo_type='dataset', |
| 29 | + local_dir=download_dir, |
| 30 | + token=token, |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def json_dumps(value): |
| 35 | + return json.dumps(value, ensure_ascii=False) |
| 36 | + |
| 37 | + |
| 38 | +def relative_image_path(path): |
| 39 | + path = str(path) |
| 40 | + marker = '/pdf_pngs/' |
| 41 | + if marker in path: |
| 42 | + return path.split(marker, 1)[1] |
| 43 | + return path.lstrip('/') |
| 44 | + |
| 45 | + |
| 46 | +def row_from_sample(sample, idx): |
| 47 | + images = [relative_image_path(p) for p in sample.get('images', [])] |
| 48 | + answer = sample.get('answer', '') |
| 49 | + return { |
| 50 | + 'index': idx, |
| 51 | + 'question_id': sample.get('question_id', ''), |
| 52 | + 'question': sample.get('question', ''), |
| 53 | + 'answer': json_dumps(answer) if isinstance(answer, list) else answer, |
| 54 | + 'image_path': json_dumps(images), |
| 55 | + 'doc_no': sample.get('doc_no', ''), |
| 56 | + 'total_pages': sample.get('total_pages', ''), |
| 57 | + 'start_end_idx': json_dumps(sample.get('start_end_idx', [])), |
| 58 | + 'question_type': sample.get('question_type', ''), |
| 59 | + 'answer_format': sample.get('answer_format', ''), |
| 60 | + 'task_tag': sample.get('task_tag', ''), |
| 61 | + 'evidence_pages': json_dumps(sample.get('evidence_pages', [])), |
| 62 | + 'evidence_sources': json_dumps(sample.get('evidence_sources', [])), |
| 63 | + 'subTask': json_dumps(sample.get('subTask', [])), |
| 64 | + 'detailed_evidences': sample.get('detailed_evidences', ''), |
| 65 | + 'pdf_path': sample.get('pdf_path', ''), |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +def load_jsonl(path): |
| 70 | + rows = [] |
| 71 | + with open(path, 'r', encoding='utf-8') as f: |
| 72 | + for line in f: |
| 73 | + if line.strip(): |
| 74 | + rows.append(json.loads(line)) |
| 75 | + return rows |
| 76 | + |
| 77 | + |
| 78 | +def main(): |
| 79 | + parser = argparse.ArgumentParser(description='Build VLMEvalKit TSV for LongDocURL.') |
| 80 | + parser.add_argument('--jsonl', default=None, help='Optional local LongDocURL JSONL path.') |
| 81 | + parser.add_argument('--download-dir', default=None, help='Directory for downloaded JSONL.') |
| 82 | + parser.add_argument('--output', required=True, help='Output TSV path, e.g. LongDocURL.tsv.') |
| 83 | + parser.add_argument('--token', default=os.environ.get('HF_TOKEN'), help='Optional Hugging Face token.') |
| 84 | + parser.add_argument('--limit', type=int, default=None, help='Optional limit for debugging.') |
| 85 | + args = parser.parse_args() |
| 86 | + |
| 87 | + jsonl_path = args.jsonl |
| 88 | + if jsonl_path is None: |
| 89 | + download_dir = args.download_dir or osp.join(osp.dirname(osp.abspath(args.output)), 'downloads') |
| 90 | + os.makedirs(download_dir, exist_ok=True) |
| 91 | + print(f'downloading {DATA_FILE} from {REPO_ID} ...') |
| 92 | + jsonl_path = hf_download(DATA_FILE, download_dir, token=args.token) |
| 93 | + |
| 94 | + samples = load_jsonl(jsonl_path) |
| 95 | + if args.limit is not None: |
| 96 | + samples = samples[:args.limit] |
| 97 | + data = pd.DataFrame([row_from_sample(sample, i) for i, sample in enumerate(samples)]) |
| 98 | + os.makedirs(osp.dirname(osp.abspath(args.output)), exist_ok=True) |
| 99 | + data.to_csv(args.output, sep='\t', index=False) |
| 100 | + print(f'wrote {len(data)} rows -> {args.output}') |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + main() |
0 commit comments