yonatanbitton commited on
Commit
78c1ad2
1 Parent(s): 61e73c7

Create dollar_street.py

Browse files
Files changed (1) hide show
  1. dollar_street.py +74 -0
dollar_street.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 the HuggingFace Datasets Authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import os
17
+ import pandas as pd
18
+ import datasets
19
+ import json
20
+ from huggingface_hub import hf_hub_url
21
+
22
+ _INPUT_CSV = "dollar_street_test.csv"
23
+ _INPUT_IMAGES = "dollarstreet_test_images"
24
+ _REPO_ID = "nlphuji/dollar_street_test"
25
+
26
+ class Dataset(datasets.GeneratorBasedBuilder):
27
+ VERSION = datasets.Version("1.1.0")
28
+ BUILDER_CONFIGS = [
29
+ datasets.BuilderConfig(name="TEST", version=VERSION, description="test"),
30
+ ]
31
+
32
+ def _info(self):
33
+ return datasets.DatasetInfo(
34
+ features=datasets.Features(
35
+ {
36
+ "image": datasets.Image(),
37
+ "id": datasets.Value('string'),
38
+ "country": datasets.Value('string'),
39
+ "country.id": datasets.Value('string'),
40
+ "region.id": datasets.Value('string'),
41
+ "type": datasets.Value('string'),
42
+ "imageRelPath": datasets.Value('string'),
43
+ "topics": [datasets.Value('string')],
44
+ "place": datasets.Value('string'),
45
+ "income": datasets.Value('string'),
46
+ "imagenet_synonyms": [datasets.Value('string')],
47
+ "imagenet_sysnet_id": [datasets.Value('string')],
48
+ "image_name": datasets.Value('string')
49
+ }
50
+ ),
51
+ task_templates=[],
52
+ )
53
+
54
+ def _split_generators(self, dl_manager):
55
+ """Returns SplitGenerators."""
56
+
57
+ repo_id = _REPO_ID
58
+ data_dir = dl_manager.download_and_extract({
59
+ "examples_csv": hf_hub_url(repo_id=repo_id, repo_type='dataset', filename=_INPUT_CSV),
60
+ "images_dir": hf_hub_url(repo_id=repo_id, repo_type='dataset', filename=f"{_INPUT_IMAGES}.zip")
61
+ })
62
+
63
+ return [datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs=data_dir)]
64
+
65
+
66
+ def _generate_examples(self, examples_csv, images_dir):
67
+ """Yields examples."""
68
+ df = pd.read_csv(examples_csv)
69
+
70
+ for r_idx, r in df.iterrows():
71
+ r_dict = r.to_dict()
72
+ image_path = os.path.join(images_dir, _INPUT_IMAGES, r_dict['image_name'])
73
+ r_dict['image'] = image_path
74
+ yield r_idx, r_dict