animelover commited on
Commit
147c0d5
·
1 Parent(s): 8482239

Update danbooru2022.py

Browse files
Files changed (1) hide show
  1. danbooru2022.py +22 -5
danbooru2022.py CHANGED
@@ -5,20 +5,30 @@ from datasets import DownloadManager, DatasetInfo
5
  from datasets.data_files import DataFilesDict
6
 
7
  _EXTENSION = [".png", ".jpg", ".jpeg"]
8
- _DESCRIPTION = ""
9
  _NAME = "animelover/danbooru2022"
10
  _REVISION = "main"
11
 
12
 
13
  class DanbooruDataset(datasets.GeneratorBasedBuilder):
 
 
 
 
 
 
 
 
 
 
14
 
15
  def _info(self) -> DatasetInfo:
16
  return datasets.DatasetInfo(
17
- description=_DESCRIPTION,
18
  features=datasets.Features(
19
  {
20
  "image": datasets.Image(),
21
- "tags": datasets.Value("string")
 
22
  }
23
  ),
24
  supervised_keys=None,
@@ -42,10 +52,17 @@ class DanbooruDataset(datasets.GeneratorBasedBuilder):
42
  for path in filepath:
43
  all_fnames = {os.path.relpath(os.path.join(root, fname), start=path)
44
  for root, _dirs, files in os.walk(path) for fname in files}
45
- image_fnames = sorted([fname for fname in all_fnames if os.path.splitext(fname)[1].lower() in _EXTENSION], reverse=True)
 
46
  for image_fname in image_fnames:
47
  image_path = os.path.join(path, image_fname)
48
  tags_path = os.path.join(path, os.path.splitext(image_fname)[0] + ".txt")
49
  with open(tags_path, "r", encoding="utf-8") as f:
50
  tags = f.read()
51
- yield image_fname, {"image": image_path, "tags": tags}
 
 
 
 
 
 
 
5
  from datasets.data_files import DataFilesDict
6
 
7
  _EXTENSION = [".png", ".jpg", ".jpeg"]
 
8
  _NAME = "animelover/danbooru2022"
9
  _REVISION = "main"
10
 
11
 
12
  class DanbooruDataset(datasets.GeneratorBasedBuilder):
13
+ BUILDER_CONFIGS = [
14
+ datasets.BuilderConfig(
15
+ name="sfw",
16
+ description="sfw subset",
17
+ ),
18
+ datasets.BuilderConfig(
19
+ name="full",
20
+ description="full dataset",
21
+ ),
22
+ ]
23
 
24
  def _info(self) -> DatasetInfo:
25
  return datasets.DatasetInfo(
26
+ description=self.config.description,
27
  features=datasets.Features(
28
  {
29
  "image": datasets.Image(),
30
+ "tags": datasets.Value("string"),
31
+ "post_id": datasets.Value("int64"),
32
  }
33
  ),
34
  supervised_keys=None,
 
52
  for path in filepath:
53
  all_fnames = {os.path.relpath(os.path.join(root, fname), start=path)
54
  for root, _dirs, files in os.walk(path) for fname in files}
55
+ image_fnames = sorted([fname for fname in all_fnames if os.path.splitext(fname)[1].lower() in _EXTENSION],
56
+ reverse=True)
57
  for image_fname in image_fnames:
58
  image_path = os.path.join(path, image_fname)
59
  tags_path = os.path.join(path, os.path.splitext(image_fname)[0] + ".txt")
60
  with open(tags_path, "r", encoding="utf-8") as f:
61
  tags = f.read()
62
+ if self.config.name == "sfw" and any(tag.strip() in nsfw_tags for tag in tags.split(",")):
63
+ continue
64
+ post_id = int(os.path.splitext(os.path.basename(image_fname))[0])
65
+ yield image_fname, {"image": image_path, "tags": tags, "post_id": post_id}
66
+
67
+
68
+ nsfw_tags = ["nude", "sex", "nipples", "pussy", "vaginal", "pubic_hair", "anus", "penis", "cum", "condom", "sex toy"]