from datasets import load_dataset from torchvision.transforms import InterpolationMode from torchvision.transforms import functional as F def art2prompt(example): image = example["image"] image = F.resize(image, 512, InterpolationMode.LANCZOS) artist = example["artist_str"].replace("-", " ").title() if example["genre_str"] == "Unknown Genre": genre = "painting" else: genre = example["genre_str"].replace("_", " ") style = example["style_str"].replace("_", " ").lower() captions = [ # a landscape in the style of Vincent Van Gogh f"a {genre} in the style of {artist}", # a landscape in the style of realism f"a {genre} in the style of {style}", # a realism painting by Vincent Van Gogh f"a {style} painting by {artist}", # a landscape by Vincent Van Gogh f"a {genre} by {artist}", ] return {"text": captions, "image": image} dataset = load_dataset("huggan/wikiart", split="train") # map the integer labels to their strings dataset = dataset.map( lambda ex: { "artist_str": dataset.features["artist"].int2str(ex["artist"]), "genre_str": dataset.features["genre"].int2str(ex["genre"]), "style_str": dataset.features["style"].int2str(ex["style"]), }, remove_columns=["artist", "genre", "style"], ) # generate prompts from attributes dataset = dataset.map( art2prompt, remove_columns=["artist_str", "genre_str", "style_str"], num_proc=8, writer_batch_size=100 ) dataset.push_to_hub("fusing/wikiart_captions", split="train")