File size: 4,023 Bytes
9f356c7
d583a77
 
 
 
b81749a
 
d583a77
 
 
 
 
 
 
8d72b47
d583a77
 
 
8d72b47
d583a77
8d72b47
 
d583a77
 
 
 
 
 
0b8ac77
 
 
 
 
 
 
 
 
 
 
 
 
 
e846eac
f62a17c
 
0b8ac77
f62a17c
e846eac
 
0b8ac77
 
 
 
 
 
e846eac
 
 
7ae852e
d583a77
 
 
784f684
d583a77
 
 
 
 
9f356c7
 
 
 
 
d583a77
 
 
4027e25
d583a77
 
 
 
 
 
 
 
 
7ae852e
d583a77
 
7ae852e
395e0e5
 
c62b2d7
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import datasets

_CITATION = """\
@InProceedings{huggingface:dataset,
title = {Unsplash Lite Dataset 1.2.0 Photos},
author={Unsplash},
year={2022}
}
"""

_DESCRIPTION = """\
This is a dataset that streams photos data from the Unsplash 25K servers.
"""
_HOMEPAGE = "https://github.com/unsplash/datasets/"

_LICENSE = ""

_URL = "https://unsplash.com/data/lite/latest"

class Unsplash(datasets.GeneratorBasedBuilder):
    """The Unsplash 25K dataset for photos"""

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    'photo_id': datasets.Value("string"),
                    'photo_url': datasets.Value("string"),
                    'photo_image_url': datasets.Value("string"),
                    'photo_submitted_at': datasets.Value("string"),
                    'photo_featured': datasets.Value("string"),
                    'photo_width': datasets.Value("int32"),
                    'photo_height': datasets.Value("int32"),
                    'photo_aspect_ratio': datasets.Value("float32"),
                    'photo_description': datasets.Value("string"),
                    'photographer_username': datasets.Value("string"),
                    'photographer_first_name': datasets.Value("string"),
                    'photographer_last_name': datasets.Value("string"),
                    'exif_camera_make': datasets.Value("string"),
                    'exif_camera_model': datasets.Value("string"),
                    'exif_iso': datasets.Value("string"),
                    'exif_aperture_value': datasets.Value("string"),
                    'exif_focal_length': datasets.Value("string"),
                    'exif_exposure_time': datasets.Value("string"),
                    'photo_location_name': datasets.Value("string"),
                    'photo_location_latitude': datasets.Value("string"),
                    'photo_location_longitude': datasets.Value("string"),
                    'photo_location_country': datasets.Value("string"),
                    'photo_location_city': datasets.Value("string"),
                    'stats_views': datasets.Value("int32"),
                    'stats_downloads': datasets.Value("int32"),
                    'ai_description': datasets.Value("string"),
                    'ai_primary_landmark_name': datasets.Value("string"),
                    'ai_primary_landmark_latitude': datasets.Value("string"),
                    'ai_primary_landmark_longitude': datasets.Value("string"),
                    'ai_primary_landmark_confidence': datasets.Value("string"),
                    'blur_hash': datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage="https://github.com/unsplash/datasets/",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        new_url = dl_manager.download_and_extract(_URL)
        # remove extra files
        for file in os.listdir(new_url):
            if os.path.isfile(new_url+"/"+file):
                if file != 'photos.tsv000':
                    os.remove(new_url+'/'+file)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": os.path.join(new_url, "photos.tsv000")}
            ),
        ]

    def _generate_examples(self, filepath):
        """This function returns the examples in the raw (text) form."""
        with open(filepath, "r") as f:
            id_ = 0
            for line in f:
                if id_ == 0:
                    cols = line.strip().split("\t")
                    id_ += 1
                else:
                    values = line.strip().split("\t")
                    if len(values) != len(cols):
                        values.extend(['']*(len(cols)-len(values)))
                    yield id_, {cols[i]: values[i] for i in range(len(cols))}
                    id_ += 1