|
import os |
|
import pandas as pd |
|
import time |
|
import json |
|
|
|
from tag_tool import year_tag, rating_tag, quality_tag, meta_tags_filter, extract_special_tags |
|
|
|
import random |
|
import dateutil.parser |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_meta_data_by_id(df, id): |
|
record = df[df['id'] == id].to_dict(orient='records') |
|
if record: |
|
return record[0] |
|
else: |
|
|
|
return None |
|
|
|
|
|
|
|
def format_meta_data(record): |
|
|
|
special_tags, general_tags = extract_special_tags(record['tag_string_general'].split(" ")) |
|
date = dateutil.parser.parse(record['created_at']) |
|
year = date.year |
|
|
|
formatted_record = { |
|
"danbooru_id": record["id"], |
|
'image_key':f"danbooru_{record['id']}", |
|
'image_width': record['image_width'], |
|
'image_height': record['image_height'], |
|
'tag_count': record['tag_count'], |
|
|
|
"special_tags": special_tags, |
|
|
|
'rating': record['rating'], |
|
'rating_tags': [rating_tag(record['rating'])] if rating_tag(record['rating']) else [], |
|
|
|
'created_at': record['created_at'], |
|
'year_tags': year_tag(record['created_at']), |
|
'year': year, |
|
|
|
"tag_string": record['tag_string'], |
|
|
|
"fav_count": record['fav_count'], |
|
'quality_tags': [quality_tag(record['id'], record['fav_count'], record['rating'])] if quality_tag(record['id'], record['fav_count'], record['rating']) else [], |
|
|
|
"created_at": record['created_at'], |
|
'tag_string_general': record['tag_string_general'], |
|
"gen_tags": general_tags, |
|
'tag_string_character': record['tag_string_character'], |
|
'character_tags': record['tag_string_character'].split(" "), |
|
'tag_string_artist': record['tag_string_artist'], |
|
'artist_tags': record['tag_string_artist'].split(" "), |
|
'tag_string_meta': record['tag_string_meta'], |
|
'meta_tags': meta_tags_filter(record['tag_string_meta'].split(" ")) if record['tag_string_meta']else [], |
|
'tag_string_copyright': record['tag_string_copyright'], |
|
"copyright_tags": record['tag_string_copyright'].split(" "), |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
return formatted_record |
|
|
|
def random_list(tags): |
|
if isinstance(tags, str): |
|
tags = tags.split(",") |
|
tags = format_tag_list(tags) |
|
if len(tags) == 0: |
|
return [] |
|
else: |
|
|
|
tags = random.sample(tags, len(tags)) |
|
return tags |
|
|
|
def list2str(tags): |
|
if len(tags) == 0: |
|
return "" |
|
else: |
|
return ", ".join(tags) |
|
|
|
|
|
def format_tag(tag_string): |
|
if len(tag_string) > 3: |
|
tag_string = tag_string.replace("_", " ") |
|
|
|
tag_string = tag_string.strip() |
|
return tag_string |
|
|
|
def format_tag_list(tag_list): |
|
return [format_tag(tag) for tag in tag_list] |
|
|
|
def add_base_caption(record): |
|
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']) |
|
caption_base = format_tag_list(caption_base) |
|
caption_base = list2str(caption_base) |
|
return caption_base |
|
|
|
def add_flo2_caption(record, danbooru_flo2_caption_ft_long): |
|
|
|
caption_base = add_base_caption(record) |
|
if caption_base: |
|
record['caption_base'] = caption_base |
|
|
|
if str(record['danbooru_id']) in danbooru_flo2_caption_ft_long: |
|
record['flo2_caption_ft_long'] = danbooru_flo2_caption_ft_long[str(record['danbooru_id'])] |
|
|
|
return record |
|
|
|
else: |
|
|
|
return record |
|
|
|
|
|
def gen_meta_by_id(df, id, danbooru_flo2_caption_ft_long): |
|
record = get_meta_data_by_id(df, id) |
|
if record: |
|
formatted_record = format_meta_data(record) |
|
return add_flo2_caption(formatted_record, danbooru_flo2_caption_ft_long) |
|
else: |
|
return None |
|
|
|
def format2webui(tag_string): |
|
tags = tag_string.split(",") |
|
tags = [format_tag(tag) for tag in tags] |
|
|
|
for i, tag in enumerate(tags): |
|
tags[i] = tag.replace("(", r"\(").replace(")", r"\)") |
|
tags = list2str(tags) |
|
return tags |
|
|
|
|
|
if __name__ == '__main__': |
|
start = time.time() |
|
|
|
danbooru_parquets_path ="/mnt/data/danbooru_newest-all/table.parquet" |
|
nlp_path = "/mnt/data/Booru-parquets/danbooru_flo2_caption_ft_long.json" |
|
with open(nlp_path, "r") as f: |
|
danbooru_flo2_caption_ft_long = json.load(f) |
|
|
|
|
|
|
|
|
|
df = pd.read_parquet(danbooru_parquets_path) |
|
print(f"Time taken to read the Parquet file: {time.time() - start} seconds") |
|
|
|
for column in df.columns: |
|
print(f"Column: {column}") |
|
print(df[column].head()) |
|
print("-" * 30) |
|
|
|
start = time.time() |
|
id = 7356649 |
|
record = get_meta_data_by_id(df, id) |
|
print(record) |
|
record = gen_meta_by_id(df, id, danbooru_flo2_caption_ft_long) |
|
print(record) |
|
print(f"Time taken: {time.time() - start} seconds") |
|
caption = add_base_caption(record) |
|
|
|
print(format2webui(caption)) |
|
|
|
max_id = df['id'].max() |
|
|
|
print(f"Max id: {max_id}") |
|
|
|
|
|
|
|
|
|
|
|
|