# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor. # # 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 json import datasets _CITATION = """\ @InProceedings{huggingface:dataset, title = {Graffiti}, author={UR }, year={2023} } """ _DESCRIPTION = """\ Graffiti dataset taken from https://www.graffiti.org/ and https://www.graffiti-database.com/. """ _HOMEPAGE = "https://huggingface.co/datasets/artificialhoney/graffiti" _LICENSE = "Apache License 2.0" _VERSION = "0.1.0" _SOURCES = [ "graffiti.org", "graffiti-database.com" ] class GraffitiConfig(datasets.BuilderConfig): """BuilderConfig for Graffiti.""" def __init__(self, **kwargs): """BuilderConfig for Graffiti. Args: **kwargs: keyword arguments forwarded to super. """ super(GraffitiConfig, self).__init__(**kwargs) class Graffiti(datasets.GeneratorBasedBuilder): """Graffiti dataset taken from https://www.graffiti.org/ and https://www.graffiti-database.com/.""" BUILDER_CONFIG_CLASS = GraffitiConfig BUILDER_CONFIGS = [ GraffitiConfig( name="default", ), ] def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "image": datasets.Image(), "text": datasets.Value("string") } ), homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, version=_VERSION, task_templates=[], ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" images = [] metadata = [] for source in _SOURCES: images.append(dl_manager.iter_archive(dl_manager.download("./data/{0}/images.tar.gz".format(source)))) metadata.append(dl_manager.download("./data/{0}/metadata.jsonl".format(source))) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, # These kwargs will be passed to _generate_examples gen_kwargs={ "images": images, "metadata": metadata, }, ) ] def _generate_examples(self, metadata, images): idx = 0 for index, meta in enumerate(metadata): m = [] with open(meta, encoding="utf-8") as f: for row in f: m.append(json.loads(row)) for file_path, file_obj in images[index]: data = [x for x in m if file_path.endswith(x["file"])][0] text = data["caption"] if data["artist"] != None: text += ", with text \"" + data["artist"] + "\"" text += ", in the art of \"" + data["artist"] + "\"" if data["city"] != None: text += ", located in " + data["city"] yield idx, { "image": {"path": file_path, "bytes": file_obj.read()}, "text": text } idx+=1