Commit
·
c905b66
1
Parent(s):
bec04f2
ScienceQA
Browse files- ScienceQA.py +123 -0
ScienceQA.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 The HuggingFace Datasets Authors.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
"""ScienceQA loading script."""
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import json
|
| 19 |
+
from pathlib import Path
|
| 20 |
+
import os
|
| 21 |
+
|
| 22 |
+
import datasets
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
_CITATION = """\
|
| 26 |
+
@inproceedings{lu2022learn,
|
| 27 |
+
title={Learn to Explain: Multimodal Reasoning via Thought Chains for Science Question Answering},
|
| 28 |
+
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},
|
| 29 |
+
booktitle={The 36th Conference on Neural Information Processing Systems (NeurIPS)},
|
| 30 |
+
year={2022}
|
| 31 |
+
}
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
_DESCRIPTION = """\
|
| 35 |
+
This is the ScienceQA dataset.
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
_HOMEPAGE = "https://scienceqa.github.io/"
|
| 39 |
+
|
| 40 |
+
_LICENSE = "CC BY-NC-SA (Attribution-NonCommercial-ShareAlike)"
|
| 41 |
+
|
| 42 |
+
_URLS = {
|
| 43 |
+
"pid_splits": "https://drive.google.com/uc?id=1OXlNBuW74dsrwYZIpQMshFqxkjcMPPgV&export=download",
|
| 44 |
+
"problems": "https://drive.google.com/uc?id=1nJ86OLnF2C6eDoi5UOAdTAS5Duc0wuTl&export=download",
|
| 45 |
+
"train": "https://drive.google.com/uc?id=1swX4Eei1ZqrXRvM-JAZxN6QVwcBLPHV8&export=download",
|
| 46 |
+
"val": "https://drive.google.com/uc?id=1ijThWZc1tsoqGrOCWhYYj1HUJ48Hl8Zz&export=download",
|
| 47 |
+
"test": "https://drive.google.com/uc?id=1eyjFaHxbvEJZzdZILn3vnTihBNDmKcIj&export=download",
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
_SUB_FOLDER_OR_FILE_NAME = {
|
| 51 |
+
"pid_splits": "pid_splits.json",
|
| 52 |
+
"problems": "problems.json",
|
| 53 |
+
"train": "train",
|
| 54 |
+
"val": "val",
|
| 55 |
+
"test": "test",
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class ScienceQADataset(datasets.GeneratorBasedBuilder):
|
| 60 |
+
|
| 61 |
+
VERSION = datasets.Version("1.0.0")
|
| 62 |
+
|
| 63 |
+
def _info(self):
|
| 64 |
+
features = datasets.Features(
|
| 65 |
+
{
|
| 66 |
+
"question": datasets.Value("string"),
|
| 67 |
+
"choices": datasets.Sequence(datasets.Value("string")),
|
| 68 |
+
"answer": datasets.Value("int32"),
|
| 69 |
+
"hint": datasets.Value("string"),
|
| 70 |
+
"image": datasets.Image(),
|
| 71 |
+
"task": datasets.Value("string"),
|
| 72 |
+
"grade": datasets.Value("string"),
|
| 73 |
+
"subject": datasets.Value("string"),
|
| 74 |
+
"topic": datasets.Value("string"),
|
| 75 |
+
"category": datasets.Value("string"),
|
| 76 |
+
"skill": datasets.Value("string"),
|
| 77 |
+
"lecture": datasets.Value("string"),
|
| 78 |
+
"solution": datasets.Value("string"),
|
| 79 |
+
"split": datasets.Value("string"),
|
| 80 |
+
}
|
| 81 |
+
)
|
| 82 |
+
return datasets.DatasetInfo(
|
| 83 |
+
description=_DESCRIPTION,
|
| 84 |
+
features=features,
|
| 85 |
+
homepage=_HOMEPAGE,
|
| 86 |
+
license=_LICENSE,
|
| 87 |
+
citation=_CITATION,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
def _split_generators(self, dl_manager):
|
| 91 |
+
data_dir = dl_manager.download_and_extract(_URLS)
|
| 92 |
+
gen_kwargs = {}
|
| 93 |
+
for split_name in ["train", "val", "test"]:
|
| 94 |
+
gen_kwargs_per_split = {}
|
| 95 |
+
gen_kwargs_per_split["pid_splits_path"] = Path(data_dir["pid_splits"]) / _SUB_FOLDER_OR_FILE_NAME["pid_splits"]
|
| 96 |
+
gen_kwargs_per_split["problems_path"] = Path(data_dir["problems"]) / _SUB_FOLDER_OR_FILE_NAME["problems"]
|
| 97 |
+
gen_kwargs_per_split["images_path"] = Path(data_dir[split_name]) / _SUB_FOLDER_OR_FILE_NAME[split_name]
|
| 98 |
+
gen_kwargs_per_split["split_name"] = split_name
|
| 99 |
+
gen_kwargs[split_name] = gen_kwargs_per_split
|
| 100 |
+
return [
|
| 101 |
+
datasets.SplitGenerator(
|
| 102 |
+
name=datasets.Split.TRAIN,
|
| 103 |
+
gen_kwargs=gen_kwargs["train"],
|
| 104 |
+
),
|
| 105 |
+
datasets.SplitGenerator(
|
| 106 |
+
name=datasets.Split.VALIDATION,
|
| 107 |
+
gen_kwargs=gen_kwargs["val"],
|
| 108 |
+
),
|
| 109 |
+
datasets.SplitGenerator(
|
| 110 |
+
name=datasets.Split.TEST,
|
| 111 |
+
gen_kwargs=gen_kwargs["test"],
|
| 112 |
+
),
|
| 113 |
+
]
|
| 114 |
+
|
| 115 |
+
def _generate_examples(self, pid_splits_path, problems_path, images_path, split_name):
|
| 116 |
+
pid_splits = json.load(open(pid_splits_path, "r"))
|
| 117 |
+
problems = json.load(open(problems_path, "r"))
|
| 118 |
+
|
| 119 |
+
for idx, key in enumerate(pid_splits[split_name]):
|
| 120 |
+
example = problems[key]
|
| 121 |
+
if example["image"]:
|
| 122 |
+
example["image"] = os.path.join(images_path, key, example["image"])
|
| 123 |
+
yield idx, example
|