v2ray commited on
Commit
17e2bcf
·
1 Parent(s): 5c28faf

Adapted make model tags script with the new JSON format.

Browse files
Files changed (3) hide show
  1. make_model_tags.py +23 -32
  2. scrape_gel.py +1 -1
  3. utils/utils.py +9 -4
make_model_tags.py CHANGED
@@ -5,31 +5,12 @@ import argparse
5
  from constants import *
6
  from collections import defaultdict
7
 
8
- def is_tag_unneeded(tag):
9
- if tag.endswith("_request"):
10
- return True
11
- if tag.startswith("bad_") and tag.endswith("id"):
12
- return True
13
- if tag.endswith("_mismatch"):
14
- return True
15
- match tag:
16
- case "tagme":
17
- return True
18
- case "bad_link":
19
- return True
20
- case "protected_link":
21
- return True
22
- case "inactive_account":
23
- return True
24
- case "bad_source":
25
- return True
26
- case "resized":
27
- return True
28
- return False
29
-
30
  def parse_args():
31
  parser = argparse.ArgumentParser(description="Create model tags based on tag frequency.")
32
- parser.add_argument("-i", "--min-images", type=int, default=0, help="Filter out tags with less than the specified amount of images, default to 0")
 
 
 
33
  args = parser.parse_args()
34
  if args.min_images < 0:
35
  print("Minimum images must be greater than or equal to 0!")
@@ -39,18 +20,28 @@ def parse_args():
39
  def main():
40
  args = parse_args()
41
  print("Starting...\nGetting paths...")
42
- image_id_image_tags_path_tuple_dict = utils.get_image_id_image_tags_path_tuple_dict(IMAGE_DIR)
43
- print("Got", len(image_id_image_tags_path_tuple_dict), "images.\nMaking buckets...")
44
  buckets = defaultdict(int)
45
- for _, tags_path in tqdm.tqdm(image_id_image_tags_path_tuple_dict.values(), desc="Making buckets"):
46
- for tag in utils.get_tags_set(tags_path):
47
  buckets[tag] += 1
48
- print("Filtering out tags with less than", args.min_images, "images and sorting...")
49
- buckets = sorted(bucket for bucket in buckets.items() if not is_tag_unneeded(bucket[0]) and bucket[1] >= args.min_images)
50
- print("The new model tags list contains", len(buckets), "tags.\nSaving the result...")
 
 
 
 
 
 
 
 
 
 
51
  with open(MODEL_TAGS_PATH, "w", encoding="utf8") as file:
52
- for i, bucket in enumerate(buckets):
53
- file.write(f"{i} {bucket[0]}\n")
54
  print("Finished.")
55
 
56
  if __name__ == "__main__":
 
5
  from constants import *
6
  from collections import defaultdict
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def parse_args():
9
  parser = argparse.ArgumentParser(description="Create model tags based on tag frequency.")
10
+ parser.add_argument("-m", "--min-images", type=int, default=0, help="Filter out tags with less than the specified amount of images, default to 0")
11
+ mutex = parser.add_mutually_exclusive_group()
12
+ mutex.add_argument("-e", "--exclude", nargs="+", help="Exclude tag groups with the specified group names, you can only set either exclude or include, but not both")
13
+ mutex.add_argument("-i", "--include", nargs="+", help="Include tag groups with the specified group names, you can only set either include or exclude, but not both")
14
  args = parser.parse_args()
15
  if args.min_images < 0:
16
  print("Minimum images must be greater than or equal to 0!")
 
20
  def main():
21
  args = parse_args()
22
  print("Starting...\nGetting paths...")
23
+ image_id_image_metadata_path_tuple_dict = utils.get_image_id_image_metadata_path_tuple_dict(IMAGE_DIR)
24
+ print("Got", len(image_id_image_metadata_path_tuple_dict), "images.\nMaking buckets...")
25
  buckets = defaultdict(int)
26
+ for _, metadata_path in tqdm.tqdm(image_id_image_metadata_path_tuple_dict.values(), desc="Making buckets"):
27
+ for tag in utils.get_tags(metadata_path, args.exclude, args.include):
28
  buckets[tag] += 1
29
+ ratings = []
30
+ for bucket in list(buckets.items()):
31
+ tag = bucket[0]
32
+ if not tag.startswith("rating:"):
33
+ continue
34
+ ratings.append(bucket)
35
+ buckets.pop(tag)
36
+ print("Sorting the tags based on alphabetical order...")
37
+ buckets = sorted(buckets.items())
38
+ print("Filtering out tags with less than", args.min_images, "images...")
39
+ buckets += ratings
40
+ tags = [bucket[0] for bucket in buckets if bucket[1] >= args.min_images]
41
+ print("The new model tags list contains", len(tags), "tags.\nSaving the result...")
42
  with open(MODEL_TAGS_PATH, "w", encoding="utf8") as file:
43
+ for i, tag in enumerate(tags):
44
+ file.write(f"{i} {tag}\n")
45
  print("Finished.")
46
 
47
  if __name__ == "__main__":
scrape_gel.py CHANGED
@@ -27,7 +27,7 @@ def get_type_tags_dict(soup):
27
  class_name = class_name[0]
28
  if not class_name.startswith("tag-type-"):
29
  continue
30
- tag = element.find("a", recursive=False).contents[0].replace(" ", "_").strip(",_")
31
  if tag in tags_in_dict:
32
  continue
33
  tag_type = class_name[9:]
 
27
  class_name = class_name[0]
28
  if not class_name.startswith("tag-type-"):
29
  continue
30
+ tag = element.find("a", recursive=False).contents[0].replace(",", "").replace(" ", "_").strip("_")
31
  if tag in tags_in_dict:
32
  continue
33
  tag_type = class_name[9:]
utils/utils.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import asyncio
3
  import aiohttp
4
  from PIL import Image
@@ -98,17 +99,21 @@ def get_tags(metadata_path, exclude=None, include=None):
98
  if exclude is not None and include is not None:
99
  raise ValueError("You can't set both exclude and include, please only set one.")
100
  metadata = get_metadata(metadata_path)
101
- tags = ["rating:" + metadata["rating"]]
102
- type_tags_dict = metadata.get("tags")
103
- if not type_tags_dict:
104
- return tags
105
  if exclude is not None:
106
  for e in exclude:
107
  type_tags_dict.pop(e, None)
 
108
  elif include is not None:
109
  for k in list(type_tags_dict):
110
  if k not in include:
111
  type_tags_dict.pop(k)
 
 
 
 
112
  for l in type_tags_dict.values():
113
  tags += l
 
 
114
  return tags
 
1
  import os
2
+ import json
3
  import asyncio
4
  import aiohttp
5
  from PIL import Image
 
99
  if exclude is not None and include is not None:
100
  raise ValueError("You can't set both exclude and include, please only set one.")
101
  metadata = get_metadata(metadata_path)
102
+ type_tags_dict = metadata.get("tags", {})
 
 
 
103
  if exclude is not None:
104
  for e in exclude:
105
  type_tags_dict.pop(e, None)
106
+ include_rating = "rating" not in exclude
107
  elif include is not None:
108
  for k in list(type_tags_dict):
109
  if k not in include:
110
  type_tags_dict.pop(k)
111
+ include_rating = "rating" in include
112
+ else:
113
+ include_rating = True
114
+ tags = []
115
  for l in type_tags_dict.values():
116
  tags += l
117
+ if include_rating:
118
+ tags.append("rating:" + metadata["rating"])
119
  return tags