Einstellung commited on
Commit
ab7320b
·
1 Parent(s): b0bc6f7

Upload wiki_art.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. wiki_art.py +111 -0
wiki_art.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+
4
+ import datasets
5
+
6
+
7
+ _CITATION = """\
8
+ @InProceedings{huggingface:dataset,
9
+ title = {WikiArt},
10
+ author={Medellín AI.
11
+ },
12
+ year={2023}
13
+ }
14
+ """
15
+
16
+ _DESCRIPTION = """\
17
+ Este dataset fue creado para el workshop de Medellin AI y Bancolombia con fines educativos.
18
+ """
19
+
20
+ _HOMEPAGE = "https://www.meetup.com/medellin-ai/"
21
+
22
+ _LICENSE = "mit"
23
+
24
+ _URLS = {
25
+ "train": "https://workshophuggingface.blob.core.windows.net/wikiart/train.zip",
26
+ "test": "https://workshophuggingface.blob.core.windows.net/wikiart/test.zip"
27
+ }
28
+
29
+ _NAMES = ["Baroque", "Realism"]
30
+
31
+
32
+
33
+
34
+ class WikiArt(datasets.GeneratorBasedBuilder):
35
+
36
+ VERSION = datasets.Version("1.0.0")
37
+ DEFAULT_WRITER_BATCH_SIZE = 200
38
+
39
+ BUILDER_CONFIGS = [
40
+ datasets.BuilderConfig(name="All", version=VERSION, description="This contains the whole dataset"),
41
+ datasets.BuilderConfig(name="Baroque", version=VERSION, description="This part of the dataset contains only Baroque style"),
42
+ datasets.BuilderConfig(name="Realism", version=VERSION, description="This part of the dataset contains only Realism style"),
43
+ ]
44
+
45
+ def _info(self):
46
+ features = datasets.Features(
47
+ {
48
+ "style": datasets.features.ClassLabel(names=_NAMES),
49
+ "artwork": datasets.Value("string"),
50
+ "image": datasets.Image(decode=True)
51
+ }
52
+ )
53
+
54
+ return datasets.DatasetInfo(
55
+ description=_DESCRIPTION,
56
+ features=features,
57
+ supervised_keys=("image", "style"),
58
+ homepage=_HOMEPAGE,
59
+ license=_LICENSE,
60
+ citation=_CITATION,
61
+ )
62
+
63
+
64
+ def _split_generators(self, dl_manager):
65
+
66
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
67
+
68
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
69
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
70
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
71
+
72
+ data_dir = dl_manager.download_and_extract(_URLS)
73
+
74
+ return [
75
+ datasets.SplitGenerator(
76
+ name=datasets.Split.TRAIN,
77
+ gen_kwargs={
78
+ "folderpath" : data_dir['train'],
79
+ "csv_file": 'wikiart_scraped_train.csv',
80
+ "split": "train",
81
+ },
82
+ ),
83
+ datasets.SplitGenerator(
84
+ name=datasets.Split.TEST,
85
+ gen_kwargs={
86
+ "folderpath" : data_dir['test'],
87
+ "csv_file": 'wikiart_scraped_test.csv',
88
+ "split": "test"
89
+ },
90
+ )
91
+ ]
92
+
93
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
94
+ def _generate_examples(self, folderpath, csv_file, split):
95
+
96
+
97
+ df_wiki_art = pd.read_csv(os.path.join(folderpath,split,csv_file), header=0)
98
+
99
+ if self.config.name != 'All':
100
+ df_wiki_art.query(f"Style == '{self.config.name}'", inplace=True)
101
+
102
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
103
+ for index, row in df_wiki_art.iterrows():
104
+
105
+ image_path = os.path.join(folderpath,split,row['Link'].split('/')[-1])
106
+ # Yields examples as (key, example) tuples
107
+ yield index, {
108
+ "style": row["Style"],
109
+ "artwork": row["Artwork"],
110
+ "image": image_path
111
+ }