mirabarukaso
update counter
0579f6b
import gzip
import hashlib
import os
import requests
import json
import base64
from io import BytesIO
from PIL import Image
from tag_autocomplete import PromptManager
# Language
LANG_EN = {
"character1": "Character list EN",
"character2": "Character list EN",
"character3": "Character list CN",
"action": "Action list",
"original_character": "Original Character",
}
LANG = LANG_EN
TITLE = "WAI Character Select Preview"
CAT = "WAI_Character_Select"
ENGLISH_CHARACTER_NAME = True
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
json_folder = os.path.join(parent_dir, 'json')
character_list = ''
character_dict = {}
wai_image_dict = {}
character_list_cn = ''
PROMPT_MANAGER = None
total_counter = 0
wai_illustrious_character_select_files = [
{'name': 'wai_character', 'file_path': os.path.join(json_folder, 'wai_characters.csv'), 'url':'https://raw.githubusercontent.com/mirabarukaso/character_select_stand_alone_app/refs/heads/main/json/wai_characters.csv'},
{'name': 'wai_image', 'file_path': os.path.join(json_folder, 'wai_character_thumbs.json'), 'url': 'https://huggingface.co/datasets/flagrantia/character_select_stand_alone_app/resolve/main/wai_character_thumbs.json'},
{'name': 'e621_sfw', 'file_path': os.path.join(json_folder, 'e621_sfw.csv'), 'url': 'https://raw.githubusercontent.com/DominikDoom/a1111-sd-webui-tagcomplete/refs/heads/main/tags/e621_sfw.csv'},
]
def get_md5_hash(input_str):
md5_hash = hashlib.md5()
md5_hash.update(input_str.encode('utf-8'))
return md5_hash.hexdigest()
def base64_to_image(base64_data):
compressed_data = base64.b64decode(base64_data)
webp_data = gzip.decompress(compressed_data)
image = Image.open(BytesIO(webp_data))
return image
def download_file(url, file_path):
response = requests.get(url)
response.raise_for_status()
print(f'[{CAT}]:Downloading... {url}')
with open(file_path, 'wb') as file:
file.write(response.content)
def load_text_file(file_path):
raw_text = ''
if os.path.exists(file_path):
print(f'[{CAT}]:Loading {file_path}')
with open(file_path, 'r', encoding='utf-8') as js_file:
raw_text = js_file.read()
else:
print(f"[{CAT}] ERROR: {file_path} file missing!!!")
return raw_text
def load_jsons():
global character_list
global character_dict
global wai_image_dict
global character_list_cn
global PROMPT_MANAGER
# download file
for item in wai_illustrious_character_select_files:
name = item['name']
file_path = item['file_path']
url = item['url']
if not os.path.exists(file_path):
download_file(url, file_path)
if 'e621_sfw' == name:
PROMPT_MANAGER = PromptManager(file_path)
else:
with open(file_path, 'r', encoding='utf-8') as file:
if 'wai_character' == name:
lines = file.readlines()
for line in lines:
key, value = line.split(',')
character_dict[key.strip()]=value.strip()
elif 'wai_image' == name:
wai_image_dict = json.load(file)
# Create list
character_list = list(character_dict.values())
character_list.insert(0, "none")
character_list_cn = list(character_dict.keys())
character_list_cn.insert(0, "none")
def illustrious_character_select_ex(character = 'random', optimise_tags = True, use_cn=False):
global total_counter
chara = ''
if 'none' == character:
return '', '', None
if not use_cn:
chara = character
else:
chara = character_dict[character]
md5_chara = get_md5_hash(chara.replace('(','\\(').replace(')','\\)'))
thumb_image = Image.new('RGB', (128, 128), (128, 128, 128))
if wai_image_dict.keys().__contains__(md5_chara):
thumb_image = base64_to_image(wai_image_dict.get(md5_chara))
opt_chara = chara
if optimise_tags:
opt_chara = opt_chara.replace('(', '\\(').replace(')', '\\)')
total_counter = total_counter + 1
print(f'{CAT}:{total_counter}:[{chara}]->[{opt_chara}]')
if not opt_chara.endswith(','):
opt_chara = f'{opt_chara},'
return character, opt_chara, thumb_image
def create_prompt_info(rnd_character1, opt_chara1,
rnd_character2, opt_chara2,
rnd_character3, opt_chara3):
info = ''
if '' != opt_chara1:
info += f'Character 1:{rnd_character1}\nPrompt: {opt_chara1}\n\n'
if '' != opt_chara2:
info += f'Character 2:{rnd_character2}\nPrompt: {opt_chara2}\n\n'
if '' != opt_chara3:
info += f'Character 3:{rnd_character3}\nPrompt: {opt_chara3}'
prompt = f'{opt_chara1}{opt_chara2}{opt_chara3}'
return prompt, info
def refresh_character_thumb_image(character1, character2, character3):
thumb_image = []
rnd_character = [''] *3
opt_chara = [''] *3
if 'none' != character1 and 'random' != character1:
rnd_character[0], opt_chara[0], thumb_image1 = illustrious_character_select_ex(character = character1)
thumb_image.append(thumb_image1)
if 'none' != character2 and 'random' != character2:
rnd_character[1], opt_chara[1], thumb_image2 = illustrious_character_select_ex(character = character2)
thumb_image.append(thumb_image2)
if 'none' != character3 and 'random' != character3:
rnd_character[2], opt_chara[2], thumb_image3 = illustrious_character_select_ex(character = character3, use_cn=True)
thumb_image.append(thumb_image3)
_, character_info = create_prompt_info(rnd_character[0], opt_chara[0],
rnd_character[1], opt_chara[1],
rnd_character[2], opt_chara[2])
return thumb_image, character_info
def get_prompt_manager():
return PROMPT_MANAGER
def init():
lib_js_path = os.path.join(current_dir, 'lib.js')
lib_css_path = os.path.join(current_dir, 'lib.css')
load_jsons()
js_script = load_text_file(lib_js_path)
css_script = load_text_file(lib_css_path)
print(f'[{CAT}]:Starting...')
return character_list, character_list_cn, LANG, js_script, css_script