|  | import json | 
					
						
						|  | from pathlib import Path | 
					
						
						|  |  | 
					
						
						|  | import datasets | 
					
						
						|  |  | 
					
						
						|  | _DESCRIPTION = """Science Question Answering (ScienceQA), a new benchmark that consists of 21,208 multimodal | 
					
						
						|  | multiple choice questions with a diverse set of science topics and annotations of their answers | 
					
						
						|  | with corresponding lectures and explanations. | 
					
						
						|  | The lecture and explanation provide general external knowledge and specific reasons, | 
					
						
						|  | respectively, for arriving at the correct answer.""" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | _HOMEPAGE = "https://scienceqa.github.io" | 
					
						
						|  |  | 
					
						
						|  | _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} | 
					
						
						|  | } | 
					
						
						|  | """ | 
					
						
						|  |  | 
					
						
						|  | _LICENSE = "Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | class ScienceQA(datasets.GeneratorBasedBuilder): | 
					
						
						|  | """Science Question Answering (ScienceQA), a new benchmark that consists of 21,208 multimodal | 
					
						
						|  | multiple choice questions with a diverse set of science topics and annotations of their answers | 
					
						
						|  | with corresponding lectures and explanations. | 
					
						
						|  | The lecture and explanation provide general external knowledge and specific reasons, | 
					
						
						|  | respectively, for arriving at the correct answer.""" | 
					
						
						|  |  | 
					
						
						|  | VERSION = datasets.Version("1.0.0") | 
					
						
						|  |  | 
					
						
						|  | def _info(self): | 
					
						
						|  | return datasets.DatasetInfo( | 
					
						
						|  | description=_DESCRIPTION, | 
					
						
						|  | features=datasets.Features( | 
					
						
						|  | { | 
					
						
						|  | "image": datasets.Image(), | 
					
						
						|  | "question": datasets.Value("string"), | 
					
						
						|  | "choices": datasets.features.Sequence(datasets.Value("string")), | 
					
						
						|  | "answer": datasets.Value("int8"), | 
					
						
						|  | "hint": datasets.Value("string"), | 
					
						
						|  | "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") | 
					
						
						|  | } | 
					
						
						|  | ), | 
					
						
						|  | homepage=_HOMEPAGE, | 
					
						
						|  | citation=_CITATION, | 
					
						
						|  | license=_LICENSE, | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  | def _split_generators(self, dl_manager): | 
					
						
						|  | text_path = Path.cwd() / 'text' / 'problems.json' | 
					
						
						|  | image_dir = Path.cwd() / 'images' | 
					
						
						|  | return [ | 
					
						
						|  | datasets.SplitGenerator( | 
					
						
						|  | name=datasets.Split.TRAIN, | 
					
						
						|  |  | 
					
						
						|  | gen_kwargs={ | 
					
						
						|  | "text_path": text_path, | 
					
						
						|  | "image_dir": image_dir, | 
					
						
						|  | "split": "train", | 
					
						
						|  | }, | 
					
						
						|  | ), | 
					
						
						|  | datasets.SplitGenerator( | 
					
						
						|  | name=datasets.Split.VALIDATION, | 
					
						
						|  |  | 
					
						
						|  | gen_kwargs={ | 
					
						
						|  | "text_path": text_path, | 
					
						
						|  | "image_dir": image_dir, | 
					
						
						|  | "split": "val", | 
					
						
						|  | }, | 
					
						
						|  | ), | 
					
						
						|  | datasets.SplitGenerator( | 
					
						
						|  | name=datasets.Split.TEST, | 
					
						
						|  |  | 
					
						
						|  | gen_kwargs={ | 
					
						
						|  | "text_path": text_path, | 
					
						
						|  | "image_dir": image_dir, | 
					
						
						|  | "split": "test" | 
					
						
						|  | }, | 
					
						
						|  | ), | 
					
						
						|  | ] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | def _generate_examples(self, text_path, image_dir, split): | 
					
						
						|  | with open(text_path, encoding="utf-8") as f: | 
					
						
						|  |  | 
					
						
						|  | data = json.load(f) | 
					
						
						|  | ignore_keys = ['image', 'split'] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | for image_id, row in data.items(): | 
					
						
						|  |  | 
					
						
						|  | if row['split'] == split: | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if row['image']: | 
					
						
						|  | image_path = image_dir / split / image_id / 'image.png' | 
					
						
						|  | image_bytes = image_path.read_bytes() | 
					
						
						|  | image_dict = {'path': str(image_path), 'bytes': image_bytes} | 
					
						
						|  | else: | 
					
						
						|  | image_dict = None | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | relevant_row = {k: v for k, v in row.items() if k not in ignore_keys} | 
					
						
						|  |  | 
					
						
						|  | return_dict = { | 
					
						
						|  | 'image': image_dict, | 
					
						
						|  | **relevant_row | 
					
						
						|  | } | 
					
						
						|  | yield image_id, return_dict | 
					
						
						|  |  |