bershka / huggingface-images.py
seansullivan's picture
Upload huggingface-images.py
edbad79
raw
history blame contribute delete
No virus
2.81 kB
import os
import datasets
# Constants for your dataset
_DESCRIPTION = """\
This dataset includes images with associated IDs, titles, and URLs. There are two types of images: 'Listing Image' and 'Search-image'.
"""
_LABEL_MAP = {
'Listing Image': 'listing_image',
'Search-image': 'search_image',
}
class MyDatasetConfig(datasets.BuilderConfig):
"""BuilderConfig for MyDataset."""
def __init__(self, **kwargs):
"""BuilderConfig for MyDataset.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(MyDatasetConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
class MyDataset(datasets.GeneratorBasedBuilder):
"""My custom dataset."""
BUILDER_CONFIGS = [
MyDatasetConfig(
name="default",
description="This version of the dataset contains two types of images with metadata.",
)
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"id": datasets.Value("string"),
"listing_title": datasets.Value("string"),
"url": datasets.Value("string"),
"listing_image": datasets.Image(),
"search_image": datasets.Image(),
}
),
supervised_keys=None,
homepage="Your dataset homepage here",
license="Your dataset's license here",
citation="Your dataset's citation here",
)
def _split_generators(self, dl_manager):
# You would have a way to access and download your data, for example, from a Google Cloud Storage Bucket
# For simplicity, we are assuming your data is already downloaded and accessible
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"datapath": "path_to_your_downloaded_data",
},
)
]
def _generate_examples(self, datapath):
# Here you will write the logic to read your dataset's contents
# For example, let's say you have a CSV file with all the metadata and the links to the images
# You would read the CSV file and for each row, yield the following:
with open(datapath, encoding="utf-8") as csv_file:
reader = csv.DictReader(csv_file)
for idx, row in enumerate(reader):
yield idx, {
"id": row['id'],
"listing_title": row['listing-title'],
"url": row['url'],
"listing_image": row['Listing Image'],
"search_image": row['Search-image'],
}