Spaces:
Running
Running
mirabarukaso
commited on
Commit
·
85226b0
1
Parent(s):
3b57b01
character preview
Browse files- .gitignore +1 -0
- app.py +58 -0
- json/.gitattributes +1 -0
- json/original_character.json +9 -0
- json/wai_character_thumbs.json +3 -0
- json/wai_characters.csv +0 -0
- scripts/__init__.py +1 -0
- scripts/lib.py +200 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
/scripts/__pycache__
|
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import sys
|
3 |
+
sys.path.append("scripts/")
|
4 |
+
from lib import init, refresh_character_thumb_image
|
5 |
+
from lib import JAVA_SCRIPT, CSS_SCRIPT, TITLE
|
6 |
+
|
7 |
+
if __name__ == '__main__':
|
8 |
+
character_list, original_character_list, LANG = init()
|
9 |
+
|
10 |
+
with gr.Blocks(js=JAVA_SCRIPT, css=CSS_SCRIPT, title=TITLE) as ui:
|
11 |
+
with gr.Row():
|
12 |
+
character1 = gr.Dropdown(
|
13 |
+
choices=character_list,
|
14 |
+
label=LANG["character1"],
|
15 |
+
value='none',
|
16 |
+
allow_custom_value=False,
|
17 |
+
)
|
18 |
+
|
19 |
+
character2 = gr.Dropdown(
|
20 |
+
choices=character_list,
|
21 |
+
label=LANG["character2"],
|
22 |
+
value='none',
|
23 |
+
allow_custom_value=False,
|
24 |
+
)
|
25 |
+
|
26 |
+
character3 = gr.Dropdown(
|
27 |
+
choices=character_list,
|
28 |
+
label=LANG["character3"],
|
29 |
+
value='none',
|
30 |
+
allow_custom_value=False,
|
31 |
+
)
|
32 |
+
'''
|
33 |
+
original_character = gr.Dropdown(
|
34 |
+
choices=original_character_list,
|
35 |
+
label=LANG["original_character"],
|
36 |
+
value='none',
|
37 |
+
allow_custom_value=False,
|
38 |
+
)
|
39 |
+
'''
|
40 |
+
|
41 |
+
with gr.Row(elem_classes='main_row'):
|
42 |
+
with gr.Column(elem_classes='column_prompts'):
|
43 |
+
thumb_image = gr.Gallery(type="pil", columns=3, show_download_button=False, object_fit='contain', height=512, label="Thumb")
|
44 |
+
|
45 |
+
with gr.Row():
|
46 |
+
gr.Markdown(f"<a href='https://github.com/mirabarukaso/character_select_stand_alone_app'>Character Select SAA</a>")
|
47 |
+
|
48 |
+
character1.change(fn=refresh_character_thumb_image,
|
49 |
+
inputs=[character1,character2,character3],
|
50 |
+
outputs=[thumb_image])
|
51 |
+
character2.change(fn=refresh_character_thumb_image,
|
52 |
+
inputs=[character1,character2,character3],
|
53 |
+
outputs=[thumb_image])
|
54 |
+
character3.change(fn=refresh_character_thumb_image,
|
55 |
+
inputs=[character1,character2,character3],
|
56 |
+
outputs=[thumb_image])
|
57 |
+
|
58 |
+
ui.launch()
|
json/.gitattributes
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
wai_character_thumbs.json filter=lfs diff=lfs merge=lfs -text
|
json/original_character.json
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Fla": "two-tone fur, (light blue body fur:1.1), white cheek fur, cyan hair, white fur on cat tail front, orange eyes, slit pupils, furry, miqo'te, petite, ahoge, whiskers, long hair, hair between eyes, ear fluff, glowing eyes, (4 fingers:1.2), claws",
|
3 |
+
"Kaura": "two-tone fur, (silver grey body fur:1.1), (tiger striped:1.15), white cheek fur, grey hair, red eyes, slit pupils, furry, miqo'te, petite, ahoge, whiskers, long hair, hair between eyes, ear fluff, glowing eyes, (4 fingers:1.2), claws",
|
4 |
+
"Caelitis": "silver scales, dragon tail, fluffy tail, (blonde hair, long hair, blue eyes, dragon horn, curved horn, white horn:1.05), pointy ears, (feathered wings:1.05), petite, ahoge, fangs, slit pupils, glowing eyes, dragon humanoid, 5 fingers",
|
5 |
+
"Colus": "black scales, dragon tail, membranous tail, (darker blonde hair, short hair, gold eyes, dragon horn, curved horn, black horn:1.05), pointy ears, (membranous wings:1.05), petite, ahoge, fangs, slit pupils, glowing eyes, dragon humanoid, 5 fingers",
|
6 |
+
"Aura": "two-tone scales, (red scales:1.1), red hair, purple eyes, ahoge, short hair, dragon horn, dragon tail, pointy ears, membranous wings, spread wings, anthro dragon, dragon humanoid, petite, fangs, no pupils, (hair slicked back:1.1), (4 fingers:1.2), claws, glowing eyes, white cheek",
|
7 |
+
"Caerulea": "two-tone scales, (blue scales:1.1), cyan hair, black sclera, gold eyes, shark tail, (skin-covered dragon horn:1.1), arm fins, ear fins, gills, (mohawk:0.8), sidelocks, anthro dragon, dragon humanoid, petite, fangs, no pupils, (hair slicked back:1.1), (4 fingers:1.2), claws, glowing eyes, white cheek",
|
8 |
+
"Foxie": "two-tone fur, orange body fur, white cheek fur, red hair, green eyes, (multiple tails:1.1), fox tail,anthro kyuubi, petite, (fangs:0.7), whiskers, hair between eyes, long hair, glowing eyes, ear fluff, claws, (4 fingers:1.2)"
|
9 |
+
}
|
json/wai_character_thumbs.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:184e32e34ca9807614122651f9555ede639a19d7471ac0968ac0cbef22b98c62
|
3 |
+
size 135382738
|
json/wai_characters.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
scripts/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# This file is intentionally left blank to mark the directory as a Python package.
|
scripts/lib.py
ADDED
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datetime
|
2 |
+
import gzip
|
3 |
+
import hashlib
|
4 |
+
import os
|
5 |
+
import glob
|
6 |
+
import textwrap
|
7 |
+
import gradio as gr
|
8 |
+
import numpy as np
|
9 |
+
import requests
|
10 |
+
import json
|
11 |
+
import base64
|
12 |
+
import webbrowser
|
13 |
+
from io import BytesIO
|
14 |
+
from PIL import Image
|
15 |
+
from PIL import PngImagePlugin
|
16 |
+
import random
|
17 |
+
import argparse
|
18 |
+
|
19 |
+
# Language
|
20 |
+
LANG_EN = {
|
21 |
+
"character1": "Character list 1",
|
22 |
+
"character2": "Character list 2",
|
23 |
+
"character3": "Character list 3",
|
24 |
+
"action": "Action list",
|
25 |
+
"original_character": "Original Character",
|
26 |
+
}
|
27 |
+
|
28 |
+
LANG = LANG_EN
|
29 |
+
|
30 |
+
# JavaScript
|
31 |
+
JAVA_SCRIPT = """
|
32 |
+
function refresh() {
|
33 |
+
const url = new URL(window.location);
|
34 |
+
|
35 |
+
if (url.searchParams.get('__theme') !== 'dark') {
|
36 |
+
url.searchParams.set('__theme', 'dark');
|
37 |
+
window.location.href = url.href;
|
38 |
+
}
|
39 |
+
}
|
40 |
+
"""
|
41 |
+
|
42 |
+
# CSS
|
43 |
+
CSS_SCRIPT = """
|
44 |
+
#custom_prompt_text textarea {color: darkorange}
|
45 |
+
#positive_prompt_text textarea {color: greenyellow}
|
46 |
+
#negative_prompt_text textarea {color: red}
|
47 |
+
#ai_prompt_text textarea {color: hotpink}
|
48 |
+
#prompt_ban_text textarea {color: Khaki}
|
49 |
+
"""
|
50 |
+
|
51 |
+
TITLE = "WAI Character Select Preview"
|
52 |
+
CAT = "WAI_Character_Select"
|
53 |
+
ENGLISH_CHARACTER_NAME = True
|
54 |
+
|
55 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
56 |
+
parent_dir = os.path.dirname(current_dir)
|
57 |
+
json_folder = os.path.join(parent_dir, 'json')
|
58 |
+
|
59 |
+
character_list = ''
|
60 |
+
character_dict = {}
|
61 |
+
original_character_list = ''
|
62 |
+
original_character_dict = {}
|
63 |
+
wai_image_dict = {}
|
64 |
+
|
65 |
+
wai_illustrious_character_select_files = [
|
66 |
+
{'name': 'original_character', 'file_path': os.path.join(json_folder, 'original_character.json'), 'url': 'local'},
|
67 |
+
{'name': 'wai_character', 'file_path': os.path.join(json_folder, 'wai_characters.csv'), 'url': 'local'},
|
68 |
+
{'name': 'wai_image', 'file_path': os.path.join(json_folder, 'wai_character_thumbs.json'), 'url': 'local'},
|
69 |
+
]
|
70 |
+
def get_md5_hash(input_str):
|
71 |
+
md5_hash = hashlib.md5()
|
72 |
+
md5_hash.update(input_str.encode('utf-8'))
|
73 |
+
return md5_hash.hexdigest()
|
74 |
+
|
75 |
+
def base64_to_image(base64_data):
|
76 |
+
compressed_data = base64.b64decode(base64_data)
|
77 |
+
webp_data = gzip.decompress(compressed_data)
|
78 |
+
image = Image.open(BytesIO(webp_data))
|
79 |
+
return image
|
80 |
+
|
81 |
+
def load_jsons():
|
82 |
+
global character_list
|
83 |
+
global character_dict
|
84 |
+
global original_character_dict
|
85 |
+
global original_character_list
|
86 |
+
global wai_image_dict
|
87 |
+
|
88 |
+
# download file
|
89 |
+
for item in wai_illustrious_character_select_files:
|
90 |
+
name = item['name']
|
91 |
+
file_path = item['file_path']
|
92 |
+
url = item['url']
|
93 |
+
|
94 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
95 |
+
if 'original_character' == name:
|
96 |
+
original_character_dict.update(json.load(file))
|
97 |
+
elif 'wai_character' == name:
|
98 |
+
lines = file.readlines()
|
99 |
+
for line in lines:
|
100 |
+
key, value = line.split(',')
|
101 |
+
character_dict[key.strip()]=value.strip()
|
102 |
+
elif 'wai_image' == name:
|
103 |
+
wai_image_dict = json.load(file)
|
104 |
+
|
105 |
+
# Create list
|
106 |
+
|
107 |
+
character_list = list(character_dict.values())
|
108 |
+
|
109 |
+
character_list.insert(0, "none")
|
110 |
+
|
111 |
+
original_character_list = list(original_character_dict.keys())
|
112 |
+
original_character_list.insert(0, "none")
|
113 |
+
|
114 |
+
def illustrious_character_select_ex(character = 'random', optimise_tags = True, random_action_seed = 1):
|
115 |
+
chara = ''
|
116 |
+
rnd_character = ''
|
117 |
+
|
118 |
+
if 'none' == character:
|
119 |
+
return '', '', None
|
120 |
+
|
121 |
+
if 'random' == character:
|
122 |
+
index = random_action_seed % len(character_list)
|
123 |
+
rnd_character = character_list[index]
|
124 |
+
if 'random' == rnd_character:
|
125 |
+
rnd_character = character_list[index+2]
|
126 |
+
elif 'none' == rnd_character:
|
127 |
+
rnd_character = character_list[index+1]
|
128 |
+
else:
|
129 |
+
rnd_character = character
|
130 |
+
if ENGLISH_CHARACTER_NAME:
|
131 |
+
chara = rnd_character
|
132 |
+
else:
|
133 |
+
chara = character_dict[rnd_character]
|
134 |
+
|
135 |
+
md5_chara = get_md5_hash(chara.replace('(','\\(').replace(')','\\)'))
|
136 |
+
thumb_image = Image.new('RGB', (128, 128), (128, 128, 128))
|
137 |
+
if wai_image_dict.keys().__contains__(md5_chara):
|
138 |
+
thumb_image = base64_to_image(wai_image_dict.get(md5_chara))
|
139 |
+
|
140 |
+
opt_chara = chara
|
141 |
+
if optimise_tags:
|
142 |
+
opt_chara = opt_chara.replace('(', '\\(').replace(')', '\\)')
|
143 |
+
#print(f'{CAT}:Optimise Tags:[{chara}]->[{opt_chara}]')
|
144 |
+
|
145 |
+
if not opt_chara.endswith(','):
|
146 |
+
opt_chara = f'{opt_chara},'
|
147 |
+
|
148 |
+
return rnd_character, opt_chara, thumb_image
|
149 |
+
|
150 |
+
def original_character_select_ex(character = 'random', random_action_seed = 1):
|
151 |
+
chara = ''
|
152 |
+
rnd_character = ''
|
153 |
+
|
154 |
+
if 'none' == character:
|
155 |
+
return '', ''
|
156 |
+
|
157 |
+
if 'random' == character:
|
158 |
+
index = random_action_seed % len(original_character_list)
|
159 |
+
rnd_character = original_character_list[index]
|
160 |
+
if 'random' == rnd_character:
|
161 |
+
rnd_character = original_character_list[index+2]
|
162 |
+
elif 'none' == rnd_character:
|
163 |
+
rnd_character = original_character_list[index+1]
|
164 |
+
else:
|
165 |
+
rnd_character = character
|
166 |
+
chara = original_character_dict[rnd_character]
|
167 |
+
|
168 |
+
opt_chara = chara
|
169 |
+
if not opt_chara.endswith(','):
|
170 |
+
opt_chara = f'{opt_chara},'
|
171 |
+
|
172 |
+
return rnd_character, opt_chara
|
173 |
+
|
174 |
+
def refresh_character_thumb_image(character1, character2, character3):
|
175 |
+
thumb_image = []
|
176 |
+
if 'none' != character1 and 'random' != character1:
|
177 |
+
_, _, thumb_image1 = illustrious_character_select_ex(character = character1, random_action_seed=42)
|
178 |
+
thumb_image.append(thumb_image1)
|
179 |
+
|
180 |
+
if 'none' != character2 and 'random' != character2:
|
181 |
+
_, _, thumb_image2 = illustrious_character_select_ex(character = character2, random_action_seed=42)
|
182 |
+
thumb_image.append(thumb_image2)
|
183 |
+
if 'none' != character3 and 'random' != character3:
|
184 |
+
_, _, thumb_image3 = illustrious_character_select_ex(character = character3, random_action_seed=42)
|
185 |
+
thumb_image.append(thumb_image3)
|
186 |
+
return thumb_image
|
187 |
+
|
188 |
+
def init():
|
189 |
+
global ENGLISH_CHARACTER_NAME
|
190 |
+
global LANG
|
191 |
+
|
192 |
+
ENGLISH_CHARACTER_NAME = True
|
193 |
+
if ENGLISH_CHARACTER_NAME:
|
194 |
+
print(f'[{CAT}]:Use tags as Character Name')
|
195 |
+
LANG = LANG_EN
|
196 |
+
|
197 |
+
load_jsons()
|
198 |
+
print(f'[{CAT}]:Starting...')
|
199 |
+
|
200 |
+
return character_list, original_character_list, LANG
|