Adds loader
Browse files- explainxkcd.py +55 -0
explainxkcd.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import datasets
|
| 4 |
+
|
| 5 |
+
class ExplainXKCD(datasets.GeneratorBasedBuilder):
|
| 6 |
+
"""DatasetBuilder for Explain XKCD (JSONL + images)."""
|
| 7 |
+
VERSION = datasets.Version("1.0.0")
|
| 8 |
+
BUILDER_CONFIGS = [
|
| 9 |
+
datasets.BuilderConfig(
|
| 10 |
+
name="default",
|
| 11 |
+
version=VERSION,
|
| 12 |
+
description="Explain XKCD comics with image loading",
|
| 13 |
+
),
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
def _info(self):
|
| 17 |
+
return datasets.DatasetInfo(
|
| 18 |
+
description="xkcd comics imported from JSONL with local image files",
|
| 19 |
+
features=datasets.Features({
|
| 20 |
+
"number": datasets.Value("string"),
|
| 21 |
+
"date": datasets.Value("string"),
|
| 22 |
+
"title": datasets.Value("string"),
|
| 23 |
+
"before": datasets.Value("string"),
|
| 24 |
+
"image": datasets.Image(), # will resolve local file paths
|
| 25 |
+
"titletext": datasets.Value("string"),
|
| 26 |
+
"transcript": datasets.Value("string"),
|
| 27 |
+
"explanation": datasets.Value("string"),
|
| 28 |
+
}),
|
| 29 |
+
supervised_keys=None,
|
| 30 |
+
homepage="https://www.explainxkcd.com/wiki/index.php/Main_Page",
|
| 31 |
+
citation="See repository licensing and citation information.",
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
def _split_generators(self, dl_manager):
|
| 35 |
+
# No remote download: assume both JSONL and images/ live in repo root
|
| 36 |
+
return [
|
| 37 |
+
datasets.SplitGenerator(
|
| 38 |
+
name=datasets.Split.TRAIN,
|
| 39 |
+
gen_kwargs={
|
| 40 |
+
"jsonl_path": "explainxkcd_2795.json",
|
| 41 |
+
"images_dir": "images",
|
| 42 |
+
},
|
| 43 |
+
),
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
def _generate_examples(self, jsonl_path, images_dir):
|
| 47 |
+
"""Yields examples as (key, example_dict)."""
|
| 48 |
+
with open(jsonl_path, encoding="utf-8") as f:
|
| 49 |
+
for idx, line in enumerate(f):
|
| 50 |
+
record = json.loads(line)
|
| 51 |
+
# Attach local image file path, if present
|
| 52 |
+
img_file = record.get("image")
|
| 53 |
+
if img_file:
|
| 54 |
+
record["image"] = os.path.join(images_dir, img_file)
|
| 55 |
+
yield idx, record
|