File size: 2,750 Bytes
f0d6538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import base64
import io
import os
from typing import List

import pandas as pd
from datasets import load_dataset
from tqdm import tqdm


def write_items_to_disk(items: List[dict], output_dir: str, n_parts: int = 1):
    part_size = len(items) // n_parts
    remainder = len(items) % n_parts

    os.makedirs(output_dir, exist_ok=True)

    offset = 0
    for part_nb in range(n_parts):
        next_offset = offset + part_size
        if part_nb < remainder:
            next_offset += 1
        df = pd.DataFrame(items[offset:next_offset])
        df.to_json(
            os.path.join(output_dir, f"part-{part_nb:04}.jsonl"),
            orient="records",
            lines=True,
        )
        offset = next_offset


def download_smoltalk_10k(output_dir: str, n_parts: int = 1):
    print("Loading smoltalk_10k dataset")
    dataset = load_dataset("maharnab/smol-smoltalk-10k", split="train")

    # Convert into the required format
    items = []
    for i, row in enumerate(tqdm(dataset)):
        prompt = row["messages"]
        items.append({"prompt": prompt, "extra_info": {"item_id": i}})

    # Split to parts and write to disk
    print("Writing to disk")
    write_items_to_disk(
        items,
        output_dir=os.path.join(output_dir, "smoltalk_10k", "train"),
        n_parts=n_parts,
    )


def download_geo3k(output_dir: str, n_parts: int = 1):
    print("Loading geo3k dataset")
    dataset = load_dataset("hiyouga/geometry3k", split="train")

    # Convert into the required format
    print("Forming conversations")
    items = []
    for i, row in enumerate(tqdm(dataset)):
        images = row["images"]
        problem = row["problem"]
        answer = row["answer"]

        prompt = [
            {"role": "user", "content": problem},
            {"role": "assistant", "content": f"<think>Model is thinking</think><answer>{answer}</answer>"},
        ]

        image_elements = []
        for image in images:
            buf = io.BytesIO()
            image.save(buf, format="PNG")
            image_bytes = buf.getvalue()
            image_b64 = base64.b64encode(image_bytes).decode()

            image_elements.append({"type": "image", "image": f"data:image/png;base64,{image_b64}"})

        item = {"prompt": prompt, "images": image_elements, "extra_info": {"item_id": i}}
        items.append(item)

    # Split to parts and write to disk
    print("Writing to disk")
    write_items_to_disk(
        items,
        output_dir=os.path.join(output_dir, "geo3k", "train"),
        n_parts=n_parts,
    )


def main():
    output_dir = "./sample_data"
    n_parts = 128

    download_smoltalk_10k(output_dir, n_parts=n_parts)
    download_geo3k(output_dir, n_parts=n_parts)


if __name__ == "__main__":
    main()