File size: 4,815 Bytes
c905b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9717fb
 
 
 
 
 
 
c905b66
 
 
c9717fb
 
 
 
 
 
 
c905b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9717fb
 
 
c905b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright 2020 The HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""ScienceQA loading script."""


import json
from pathlib import Path
import os

import datasets


_CITATION = """\
@inproceedings{lu2022learn,
    title={Learn to Explain: Multimodal Reasoning via Thought Chains for Science Question Answering},
    author={Lu, Pan and Mishra, Swaroop and Xia, Tony and Qiu, Liang and Chang, Kai-Wei and Zhu, Song-Chun and Tafjord, Oyvind and Clark, Peter and Ashwin Kalyan},
    booktitle={The 36th Conference on Neural Information Processing Systems (NeurIPS)},
    year={2022}
}
"""

_DESCRIPTION = """\
This is the ScienceQA dataset.
"""

_HOMEPAGE = "https://scienceqa.github.io/"

_LICENSE = "CC BY-NC-SA (Attribution-NonCommercial-ShareAlike)"

_URLS = {
    "all": {
        "pid_splits": "https://drive.google.com/uc?id=1OXlNBuW74dsrwYZIpQMshFqxkjcMPPgV&export=download",
        "problems": "https://drive.google.com/uc?id=1nJ86OLnF2C6eDoi5UOAdTAS5Duc0wuTl&export=download",
        "train": "https://drive.google.com/uc?id=1swX4Eei1ZqrXRvM-JAZxN6QVwcBLPHV8&export=download",
        "val": "https://drive.google.com/uc?id=1ijThWZc1tsoqGrOCWhYYj1HUJ48Hl8Zz&export=download",
        "test": "https://drive.google.com/uc?id=1eyjFaHxbvEJZzdZILn3vnTihBNDmKcIj&export=download",
    }
}

_SUB_FOLDER_OR_FILE_NAME = {
    "all": {
        "pid_splits": "pid_splits.json",
        "problems": "problems.json",
        "train": "train",
        "val": "val",
        "test": "test",
    }
}


class ScienceQADataset(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        features = datasets.Features(
            {
                "question": datasets.Value("string"),
                "choices": datasets.Sequence(datasets.Value("string")),
                "answer": datasets.Value("int32"),
                "hint": datasets.Value("string"),
                "image": datasets.Image(),
                "task": datasets.Value("string"),
                "grade": datasets.Value("string"),
                "subject": datasets.Value("string"),
                "topic": datasets.Value("string"),
                "category": datasets.Value("string"),
                "skill": datasets.Value("string"),
                "lecture": datasets.Value("string"),
                "solution": datasets.Value("string"),
                "split": datasets.Value("string"),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data_dir = dl_manager.download_and_extract(_URLS)
        gen_kwargs = {}
        for split_name in ["train", "val", "test"]:
            gen_kwargs_per_split = {}
            gen_kwargs_per_split["pid_splits_path"] = Path(data_dir["all"]["pid_splits"]) / _SUB_FOLDER_OR_FILE_NAME["all"]["pid_splits"]
            gen_kwargs_per_split["problems_path"] = Path(data_dir["all"]["problems"]) / _SUB_FOLDER_OR_FILE_NAME["all"]["problems"]
            gen_kwargs_per_split["images_path"] = Path(data_dir["all"][split_name]) / _SUB_FOLDER_OR_FILE_NAME["all"][split_name]
            gen_kwargs_per_split["split_name"] = split_name
            gen_kwargs[split_name] = gen_kwargs_per_split
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs=gen_kwargs["train"],
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs=gen_kwargs["val"],
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs=gen_kwargs["test"],
            ),
        ]

    def _generate_examples(self, pid_splits_path, problems_path, images_path, split_name):
        pid_splits = json.load(open(pid_splits_path, "r"))
        problems = json.load(open(problems_path, "r"))

        for idx, key in enumerate(pid_splits[split_name]):
            example = problems[key]
            if example["image"]:
                example["image"] = os.path.join(images_path, key, example["image"])
            yield idx, example