Upload read_danbooru_parquet.py with huggingface_hub
Browse files- read_danbooru_parquet.py +183 -0
read_danbooru_parquet.py
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import time
|
4 |
+
import json
|
5 |
+
|
6 |
+
from tag_tool import year_tag, rating_tag, quality_tag, meta_tags_filter, extract_special_tags
|
7 |
+
|
8 |
+
import random
|
9 |
+
import dateutil.parser
|
10 |
+
|
11 |
+
|
12 |
+
# {'artist_tags': 'beudelb', 'caption_base': '1girl, projekt red (arknights), projekt red (light breeze) (arknights), arknights, beudelb, |||simple background, white background, solo, cropped torso, upper body, blush, hands in pockets, looking at viewer, wolf girl, animal ears, brown hair, hair between eyes, medium hair, tail, wolf ears, wolf tail, yellow eyes, official alternate costume, black one-piece swimsuit, jacket, one-piece swimsuit, open jacket, red jacket, swimsuit, zipper pull tab, open clothes, absurdres, highres, sensitive, newest, best quality',
|
13 |
+
# 'character_tags': 'projekt_red_(arknights), projekt_red_(light_breeze)_(arknights)', 'copyright_tags': 'arknights', 'danbooru_quality_tags': 'best quality',
|
14 |
+
# 'flo2_caption_ft_long': 'Anime girl is standing with her arms crossed. She has long brown hair with pointy ears. She is wearing a red jacket with a black shirt underneath. The shirt has a zipper on the front. The background is white.',
|
15 |
+
# 'gen_tags': 'simple_background, white_background, solo, cropped_torso, upper_body, blush, hands_in_pockets, looking_at_viewer, wolf_girl, animal_ears, brown_hair, hair_between_eyes, medium_hair, tail, wolf_ears, wolf_tail, yellow_eyes, official_alternate_costume, black_one-piece_swimsuit, jacket, one-piece_swimsuit, open_jacket, red_jacket, swimsuit, zipper_pull_tab, open_clothes',
|
16 |
+
# 'image_height': 3792, 'image_key': 'danbooru_4668577', 'image_width': 2777, 'meta_tags': 'absurdres, highres', 'rating': 's', 'rating_tags': 'sensitive',
|
17 |
+
# 'res_source': 10530384, 'score_as2': 0.8126193881034851, 'score_ws': 0.5031498491764068, 'special_tags': '1girl', 'year': 2021, 'year_tags': 'newest'}
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
def get_meta_data_by_id(df, id):
|
22 |
+
record = df[df['id'] == id].to_dict(orient='records')
|
23 |
+
if record:
|
24 |
+
return record[0]
|
25 |
+
else:
|
26 |
+
# print(f"Record with id {id} not found.")
|
27 |
+
return None
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
def format_meta_data(record):
|
32 |
+
|
33 |
+
special_tags, general_tags = extract_special_tags(record['tag_string_general'].split(" "))
|
34 |
+
date = dateutil.parser.parse(record['created_at'])
|
35 |
+
year = date.year
|
36 |
+
# print(record['tag_string_meta'])
|
37 |
+
formatted_record = {
|
38 |
+
"danbooru_id": record["id"],
|
39 |
+
'image_key':f"danbooru_{record['id']}",
|
40 |
+
'image_width': record['image_width'],
|
41 |
+
'image_height': record['image_height'],
|
42 |
+
'tag_count': record['tag_count'],
|
43 |
+
|
44 |
+
"special_tags": special_tags,
|
45 |
+
|
46 |
+
'rating': record['rating'],
|
47 |
+
'rating_tags': [rating_tag(record['rating'])] if rating_tag(record['rating']) else [],
|
48 |
+
|
49 |
+
'created_at': record['created_at'],
|
50 |
+
'year_tags': year_tag(record['created_at']),
|
51 |
+
'year': year,
|
52 |
+
|
53 |
+
"tag_string": record['tag_string'],
|
54 |
+
|
55 |
+
"fav_count": record['fav_count'],
|
56 |
+
'quality_tags': [quality_tag(record['id'], record['fav_count'], record['rating'])] if quality_tag(record['id'], record['fav_count'], record['rating']) else [],
|
57 |
+
|
58 |
+
"created_at": record['created_at'],
|
59 |
+
'tag_string_general': record['tag_string_general'],
|
60 |
+
"gen_tags": general_tags,
|
61 |
+
'tag_string_character': record['tag_string_character'],
|
62 |
+
'character_tags': record['tag_string_character'].split(" "),
|
63 |
+
'tag_string_artist': record['tag_string_artist'],
|
64 |
+
'artist_tags': record['tag_string_artist'].split(" "),
|
65 |
+
'tag_string_meta': record['tag_string_meta'],
|
66 |
+
'meta_tags': meta_tags_filter(record['tag_string_meta'].split(" ")) if record['tag_string_meta']else [],
|
67 |
+
'tag_string_copyright': record['tag_string_copyright'],
|
68 |
+
"copyright_tags": record['tag_string_copyright'].split(" "),
|
69 |
+
}
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
return formatted_record
|
77 |
+
|
78 |
+
def random_list(tags):
|
79 |
+
if isinstance(tags, str):
|
80 |
+
tags = tags.split(",")
|
81 |
+
tags = format_tag_list(tags)
|
82 |
+
if len(tags) == 0:
|
83 |
+
return []
|
84 |
+
else:
|
85 |
+
#reorder the tags
|
86 |
+
tags = random.sample(tags, len(tags))
|
87 |
+
return tags
|
88 |
+
|
89 |
+
def list2str(tags):
|
90 |
+
if len(tags) == 0:
|
91 |
+
return ""
|
92 |
+
else:
|
93 |
+
return ", ".join(tags)
|
94 |
+
|
95 |
+
|
96 |
+
def format_tag(tag_string):
|
97 |
+
if len(tag_string) > 3:
|
98 |
+
tag_string = tag_string.replace("_", " ")
|
99 |
+
|
100 |
+
tag_string = tag_string.strip()
|
101 |
+
return tag_string
|
102 |
+
|
103 |
+
def format_tag_list(tag_list):
|
104 |
+
return [format_tag(tag) for tag in tag_list]
|
105 |
+
|
106 |
+
def add_base_caption(record):
|
107 |
+
caption_base = random_list(record['special_tags']) + random_list(record['character_tags']) + random_list(record["copyright_tags"]) + random_list(record['artist_tags']) + random_list(record['gen_tags']) + random_list(record['meta_tags']) + random_list(record['rating_tags']) + random_list(record['quality_tags'])
|
108 |
+
caption_base = format_tag_list(caption_base)
|
109 |
+
caption_base = list2str(caption_base)
|
110 |
+
return caption_base
|
111 |
+
|
112 |
+
def add_flo2_caption(record, danbooru_flo2_caption_ft_long):
|
113 |
+
|
114 |
+
caption_base = add_base_caption(record)
|
115 |
+
if caption_base:
|
116 |
+
record['caption_base'] = caption_base
|
117 |
+
|
118 |
+
if str(record['danbooru_id']) in danbooru_flo2_caption_ft_long:
|
119 |
+
record['flo2_caption_ft_long'] = danbooru_flo2_caption_ft_long[str(record['danbooru_id'])]
|
120 |
+
|
121 |
+
return record
|
122 |
+
|
123 |
+
else:
|
124 |
+
|
125 |
+
return record
|
126 |
+
|
127 |
+
|
128 |
+
def gen_meta_by_id(df, id, danbooru_flo2_caption_ft_long):
|
129 |
+
record = get_meta_data_by_id(df, id)
|
130 |
+
if record:
|
131 |
+
formatted_record = format_meta_data(record)
|
132 |
+
return add_flo2_caption(formatted_record, danbooru_flo2_caption_ft_long)
|
133 |
+
else:
|
134 |
+
return None
|
135 |
+
|
136 |
+
def format2webui(tag_string):
|
137 |
+
tags = tag_string.split(",")
|
138 |
+
tags = [format_tag(tag) for tag in tags]
|
139 |
+
|
140 |
+
for i, tag in enumerate(tags):
|
141 |
+
tags[i] = tag.replace("(", r"\(").replace(")", r"\)")
|
142 |
+
tags = list2str(tags)
|
143 |
+
return tags
|
144 |
+
|
145 |
+
|
146 |
+
if __name__ == '__main__':
|
147 |
+
start = time.time()
|
148 |
+
# danbooru_parquets_path2 ="/mnt/data/Booru-parquets/danbooru.parquet"
|
149 |
+
danbooru_parquets_path ="/mnt/data/danbooru_newest-all/table.parquet"
|
150 |
+
nlp_path = "/mnt/data/Booru-parquets/danbooru_flo2_caption_ft_long.json"
|
151 |
+
with open(nlp_path, "r") as f:
|
152 |
+
danbooru_flo2_caption_ft_long = json.load(f)
|
153 |
+
|
154 |
+
|
155 |
+
# 读取单个 Parquet 文件
|
156 |
+
# df = pd.read_parquet(danbooru_parquets_path2)
|
157 |
+
df = pd.read_parquet(danbooru_parquets_path)
|
158 |
+
print(f"Time taken to read the Parquet file: {time.time() - start} seconds")
|
159 |
+
|
160 |
+
for column in df.columns:
|
161 |
+
print(f"Column: {column}")
|
162 |
+
print(df[column].head())
|
163 |
+
print("-" * 30)
|
164 |
+
# 主程序逻辑
|
165 |
+
start = time.time()
|
166 |
+
id = 7356649
|
167 |
+
record = get_meta_data_by_id(df, id)
|
168 |
+
print(record)
|
169 |
+
record = gen_meta_by_id(df, id, danbooru_flo2_caption_ft_long)
|
170 |
+
print(record)
|
171 |
+
print(f"Time taken: {time.time() - start} seconds")
|
172 |
+
caption = add_base_caption(record)
|
173 |
+
|
174 |
+
print(format2webui(caption))
|
175 |
+
#get max id
|
176 |
+
max_id = df['id'].max()
|
177 |
+
|
178 |
+
print(f"Max id: {max_id}")
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
+
|