|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
import pandas as pd |
|
import datasets |
|
from huggingface_hub import hf_hub_url |
|
|
|
_INPUT_CSV = "docci.csv" |
|
_INPUT_IMAGES = "docci_images" |
|
_REPO_ID = "yonatanbitton/docci" |
|
_SUFFIX = 'jpg' |
|
|
|
class Dataset(datasets.GeneratorBasedBuilder): |
|
VERSION = datasets.Version("1.1.0") |
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig(name="docci", version=VERSION, description="Docci dataset"), |
|
] |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
features=datasets.Features({ |
|
"image_key": datasets.Value("string"), |
|
"description": datasets.Value('string'), |
|
"image": datasets.Image(), |
|
}), |
|
supervised_keys=None, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
data_dir = dl_manager.download_and_extract({ |
|
"examples_csv": hf_hub_url(repo_id=_REPO_ID, repo_type='dataset', filename=_INPUT_CSV), |
|
"images_dir": hf_hub_url(repo_id=_REPO_ID, repo_type='dataset', filename=f"{_INPUT_IMAGES}.zip") |
|
}) |
|
|
|
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs=data_dir)] |
|
|
|
def _generate_examples(self, examples_csv, images_dir): |
|
"""Yields examples.""" |
|
df = pd.read_csv(examples_csv) |
|
|
|
for r_idx, r in df.iterrows(): |
|
image_path = os.path.join(images_dir, _INPUT_IMAGES, f"{r['image_key']}.{_SUFFIX}") |
|
yield r_idx, { |
|
"image_key": r['image_key'], |
|
"description": r['description'], |
|
"image": image_path |
|
} |
|
|