Spaces:
Running
Running
File size: 6,606 Bytes
85226b0 264c7c2 85226b0 e60fe6a 85226b0 8650fd3 85226b0 8650fd3 e60fe6a 85226b0 0579f6b 85226b0 9618844 264c7c2 85226b0 9618844 901270b 85226b0 8650fd3 e60fe6a 85226b0 9618844 85226b0 264c7c2 e60fe6a 264c7c2 85226b0 9618844 85226b0 8650fd3 85226b0 b0fc198 0579f6b 85226b0 b0fc198 8650fd3 b0fc198 85226b0 b0fc198 85226b0 0579f6b 85226b0 b0fc198 85226b0 b0fc198 85226b0 b0fc198 85226b0 b0fc198 85226b0 b0fc198 85226b0 b0fc198 85226b0 e60fe6a 901270b 85226b0 901270b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
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
|