File size: 1,753 Bytes
157df53
5425c8b
aba1895
bacfe1f
aba1895
 
 
 
 
 
5425c8b
aba1895
5425c8b
aba1895
 
 
 
a57b850
 
7217586
5425c8b
aba1895
 
 
a57b850
aba1895
 
 
5425c8b
83443a0
aba1895
5425c8b
aba1895
5425c8b
 
 
 
 
 
aba1895
5425c8b
 
 
 
 
 
 
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
import os
import datasets

class Demo(datasets.GeneratorBasedBuilder):
    def _info(self):
        return datasets.DatasetInfo(
            features=datasets.Features({
                "id": datasets.Value("string"),
                "problem": datasets.Value("string"),
                "solution": datasets.Value("string"),
                "image": datasets.Image(),  # Enables image previews
                "img_height": datasets.Value("int32"),
                "img_width": datasets.Value("int32"),
            }),
        )

    def _split_generators(self, dl_manager):
        # Get the data files from the config
        data_files = dl_manager.download_and_extract(self.config.data_files)
        base_path = dl_manager.download_and_extract(".")
        
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": data_files["train"], "base_path": base_path}
            ),
        ]

    def _generate_examples(self, filepath, base_path):
        import pandas as pd
        df = pd.read_parquet(filepath)
        
        for idx, row in df.iterrows():
            # Handle image path correctly
            if isinstance(row["image"], dict) and "path" in row["image"]:
                image_path = os.path.join(base_path, row["image"]["path"])
            else:
                image_path = str(row["image"])
            
            yield idx, {
                "id": str(row["id"]),
                "problem": str(row["problem"]),
                "solution": str(row["solution"]),
                "image": image_path,  # Full path to the image
                "img_height": int(row["img_height"]),
                "img_width": int(row["img_width"]),
            }