Create dataset_loader.py
Browse files- dataset_loader.py +81 -0
dataset_loader.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import datasets
|
4 |
+
|
5 |
+
# Metadata and descriptions for the dataset
|
6 |
+
_CITATION = """\
|
7 |
+
@InProceedings{huggingface:dataset,
|
8 |
+
title = {Test Repo Dataset},
|
9 |
+
author={huggingface, Inc.},
|
10 |
+
year={2020}
|
11 |
+
}
|
12 |
+
"""
|
13 |
+
|
14 |
+
_DESCRIPTION = """\
|
15 |
+
The Test Repo dataset includes multiple choice questions tailored for NLP research and testing.
|
16 |
+
"""
|
17 |
+
|
18 |
+
_HOMEPAGE = "https://huggingface.co/datasets/test_repo"
|
19 |
+
|
20 |
+
_LICENSE = "Apache License 2.0"
|
21 |
+
|
22 |
+
# Define URLs for different parts of the dataset if applicable
|
23 |
+
_URLS = {
|
24 |
+
"mcq_domain": "https://huggingface.co/datasets/anand-s/test_repo/train_mcq/",
|
25 |
+
}
|
26 |
+
|
27 |
+
class TestRepo(datasets.GeneratorBasedBuilder):
|
28 |
+
"""Dataset for multiple choice questions from Test Repo."""
|
29 |
+
|
30 |
+
VERSION = datasets.Version("1.0.0")
|
31 |
+
|
32 |
+
BUILDER_CONFIGS = [
|
33 |
+
datasets.BuilderConfig(name="mcq_domain", version=VERSION, description="This configuration covers multiple choice questions."),
|
34 |
+
]
|
35 |
+
|
36 |
+
DEFAULT_CONFIG_NAME = "mcq_domain"
|
37 |
+
|
38 |
+
def _info(self):
|
39 |
+
return datasets.DatasetInfo(
|
40 |
+
description=_DESCRIPTION,
|
41 |
+
features=datasets.Features({
|
42 |
+
"prompt": datasets.Value("string"),
|
43 |
+
"question": datasets.Value("string"),
|
44 |
+
"options": datasets.Sequence(datasets.Value("string")),
|
45 |
+
"answer": datasets.Value("string"),
|
46 |
+
"context": datasets.Value("string"), # Assuming all data includes context
|
47 |
+
"num_options": datasets.Value("string"),
|
48 |
+
"question_type": datasets.Value("string"),
|
49 |
+
}),
|
50 |
+
homepage=_HOMEPAGE,
|
51 |
+
license=_LICENSE,
|
52 |
+
citation=_CITATION,
|
53 |
+
)
|
54 |
+
|
55 |
+
def _split_generators(self, dl_manager):
|
56 |
+
# Download and extract all the files in the directory
|
57 |
+
data_dir = dl_manager.download_and_extract(_URLS[self.config.name])
|
58 |
+
return [
|
59 |
+
datasets.SplitGenerator(
|
60 |
+
name=datasets.Split.TRAIN,
|
61 |
+
gen_kwargs={"directory": data_dir, "split": "train"},
|
62 |
+
),
|
63 |
+
]
|
64 |
+
|
65 |
+
def _generate_examples(self, directory, split):
|
66 |
+
# Iterate over each file in the directory
|
67 |
+
for filename in os.listdir(directory):
|
68 |
+
filepath = os.path.join(directory, filename)
|
69 |
+
if filepath.endswith(".jsonl"):
|
70 |
+
with open(filepath, encoding="utf-8") as f:
|
71 |
+
for key, row in enumerate(f):
|
72 |
+
data = json.loads(row)
|
73 |
+
yield key, {
|
74 |
+
"prompt": data.get("prompt", ""),
|
75 |
+
"question": data["question"],
|
76 |
+
"options": data["options"],
|
77 |
+
"answer": data.get("answer", ""),
|
78 |
+
"context": data.get("context", ""),
|
79 |
+
"num_options": data.get("num_options", ""),
|
80 |
+
"question_type": data.get("question_type", ""),
|
81 |
+
}
|