Datasets:
Upload laion2b_multi_korean_subset_with_image.py
Browse files
laion2b_multi_korean_subset_with_image.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from collections import defaultdict
|
3 |
+
|
4 |
+
from datasets import (
|
5 |
+
DatasetInfo,
|
6 |
+
GeneratorBasedBuilder,
|
7 |
+
Features,
|
8 |
+
SplitGenerator,
|
9 |
+
Value,
|
10 |
+
Image,
|
11 |
+
Split,
|
12 |
+
DownloadManager,
|
13 |
+
)
|
14 |
+
|
15 |
+
_URLS = [f"data/{i:0>5}.tar" for i in range(2123)]
|
16 |
+
|
17 |
+
|
18 |
+
class Laion2bMultiKoreanSubsetWithImage(GeneratorBasedBuilder):
|
19 |
+
|
20 |
+
VERSION = "0.1.0"
|
21 |
+
|
22 |
+
def _info(self):
|
23 |
+
return DatasetInfo(
|
24 |
+
description="laion2b_multi_korean_subset data with images downloaded successfully",
|
25 |
+
features=Features(
|
26 |
+
{
|
27 |
+
"image": Image(),
|
28 |
+
"text": Value("string"),
|
29 |
+
"width": Value("int32"),
|
30 |
+
"height": Value("int32"),
|
31 |
+
}
|
32 |
+
),
|
33 |
+
supervised_keys=None,
|
34 |
+
license="cc-by-4.0",
|
35 |
+
)
|
36 |
+
|
37 |
+
def _split_generators(self, dl_manager: DownloadManager):
|
38 |
+
downloaded = dl_manager.download(_URLS)
|
39 |
+
return [
|
40 |
+
SplitGenerator(
|
41 |
+
name=Split.TRAIN,
|
42 |
+
gen_kwargs={
|
43 |
+
"archives": [
|
44 |
+
dl_manager.iter_archive(archive) for archive in downloaded
|
45 |
+
]
|
46 |
+
},
|
47 |
+
)
|
48 |
+
]
|
49 |
+
|
50 |
+
def _generate_examples(self, archives):
|
51 |
+
idx = 0
|
52 |
+
temp = defaultdict(dict)
|
53 |
+
|
54 |
+
for archive in archives:
|
55 |
+
for path, file in archive:
|
56 |
+
if path.endswith(".txt"):
|
57 |
+
continue
|
58 |
+
|
59 |
+
file_id = path.split(".")[0]
|
60 |
+
|
61 |
+
if path.endswith(".json"):
|
62 |
+
meta = json.loads(file.read())
|
63 |
+
|
64 |
+
temp[file_id]["text"] = meta["caption"]
|
65 |
+
temp[file_id]["width"] = meta["width"]
|
66 |
+
temp[file_id]["height"] = meta["height"]
|
67 |
+
|
68 |
+
if "image" in temp[file_id]:
|
69 |
+
yield idx, temp[file_id]
|
70 |
+
idx += 1
|
71 |
+
del temp[file_id]
|
72 |
+
|
73 |
+
if path.endswith(".webp"):
|
74 |
+
temp[file_id]["image"] = {"path": path, "bytes": file.read()}
|
75 |
+
|
76 |
+
if "text" in temp[file_id]:
|
77 |
+
yield idx, temp[file_id]
|
78 |
+
idx += 1
|
79 |
+
del temp[file_id]
|
80 |
+
|
81 |
+
temp.clear()
|