Datasets:

Languages:
English
ArXiv:
License:
docci / docci.py
yasumasaonoe's picture
Upload 3 files
6931a7f
raw
history blame
2.63 kB
# coding=utf-8
# Copyright 2022 the HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import pandas as pd
import datasets
from huggingface_hub import hf_hub_url
_INPUT_CSV = "docci.csv" # Update the CSV file name
_INPUT_IMAGES = "docci_images" # Update the images directory name
_REPO_ID = "yonatanbitton/docci" # Update the repository ID
_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, # Update or remove if your dataset is supervised
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
# hf_auth_token = dl_manager.download_config.use_auth_token
# if hf_auth_token is None:
# raise ConnectionError(
# "Please set use_auth_token=True or use_auth_token='<TOKEN>' to download this dataset"
# )
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
}