import datasets from tqdm import tqdm import os import json import multiprocessing as mp from functools import partial def process_item(idx_data, dataset_name): idx, d = idx_data conversations = d['conversations'] image = d['image'] image_path = f"{dataset_name}/{idx}.png" os.makedirs(os.path.dirname(f"data/{image_path}"), exist_ok=True) image.save(f"data/{image_path}") for i, c in enumerate(conversations): conversations[i]['content'] = c['content'].replace("", "") if len(conversations) > 1: conversations[1]['content'] = "" + conversations[1]['content'] return { "images": [image_path], "system": SYSTEM_PROMPT, "conversations": conversations } ds_id = "ahmedheakl/r1_90k_instruct" dataset_name = "r1_onevision_90k" out_root = "data" SYSTEM_PROMPT = ( "A conversation between User and Assistant. The user asks a question, and the Assistant solves it. The assistant " "first thinks about the reasoning process in the mind and then provides the user with the answer. The reasoning " "process and answer are enclosed within and tags, respectively, i.e., " " reasoning process here answer here " ) os.makedirs(f"{out_root}/{dataset_name}", exist_ok=True) print("Loading dataset...") ds = datasets.load_dataset(ds_id, split="train", trust_remote_code=True) num_processes = mp.cpu_count() - 6 print(f"Using {num_processes} processes") process_func = partial(process_item, dataset_name=dataset_name) with mp.Pool(processes=num_processes) as pool: data = list(tqdm(pool.imap(process_func, enumerate(ds)), total=len(ds), desc="Processing items")) output_path = f"{out_root}/{dataset_name}.json" print(f"Saving dataset to {output_path}") with open(output_path, "w") as f: json.dump(data, f, indent=4, ensure_ascii=False) with open(f"{out_root}/dataset_info.json", "r") as f: dataset_info = json.load(f) dataset_info[dataset_name] = { "file_name": f"{dataset_name}.json", "formatting": "sharegpt", "columns": { "messages": "conversations", "images": "images", "system": "system" }, "tags": { "role_tag": "role", "content_tag": "content", "user_tag": "user", "assistant_tag": "assistant" } } with open(f"{out_root}/dataset_info.json", "w") as f: json.dump(dataset_info, f, indent=4, ensure_ascii=False)