2022-10-20 15:58:52 +00:00
|
|
|
import os
|
|
|
|
import random
|
2022-10-21 17:23:00 +00:00
|
|
|
import gradio
|
|
|
|
from modules.shared import opts
|
|
|
|
inspiration_system_path = os.path.join(opts.inspiration_dir, "system")
|
|
|
|
def read_name_list(file, types=None, keyword=None):
|
2022-10-20 15:58:52 +00:00
|
|
|
if not os.path.exists(file):
|
|
|
|
return []
|
|
|
|
ret = []
|
2022-10-21 17:23:00 +00:00
|
|
|
f = open(file, "r")
|
2022-10-20 15:58:52 +00:00
|
|
|
line = f.readline()
|
|
|
|
while len(line) > 0:
|
|
|
|
line = line.rstrip("\n")
|
2022-10-21 17:23:00 +00:00
|
|
|
if types is not None:
|
|
|
|
dirname = os.path.split(line)
|
|
|
|
if dirname[0] in types and keyword in dirname[1]:
|
|
|
|
ret.append(line)
|
|
|
|
else:
|
|
|
|
ret.append(line)
|
|
|
|
line = f.readline()
|
2022-10-20 15:58:52 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def save_name_list(file, name):
|
2022-10-21 17:23:00 +00:00
|
|
|
with open(file, "a") as f:
|
|
|
|
f.write(name + "\n")
|
2022-10-20 15:58:52 +00:00
|
|
|
|
2022-10-21 17:23:00 +00:00
|
|
|
def get_types_list():
|
|
|
|
files = os.listdir(opts.inspiration_dir)
|
|
|
|
types = []
|
|
|
|
for x in files:
|
|
|
|
path = os.path.join(opts.inspiration_dir, x)
|
|
|
|
if x[0] == ".":
|
|
|
|
continue
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
continue
|
|
|
|
if path == inspiration_system_path:
|
|
|
|
continue
|
|
|
|
types.append(x)
|
|
|
|
return types
|
|
|
|
|
|
|
|
def get_inspiration_images(source, types, keyword):
|
|
|
|
get_num = int(opts.inspiration_rows_num * opts.inspiration_cols_num)
|
2022-10-20 15:58:52 +00:00
|
|
|
if source == "Favorites":
|
2022-10-21 17:23:00 +00:00
|
|
|
names = read_name_list(os.path.join(inspiration_system_path, "faverites.txt"), types, keyword)
|
|
|
|
names = random.sample(names, get_num) if len(names) > get_num else names
|
2022-10-20 15:58:52 +00:00
|
|
|
elif source == "Abandoned":
|
2022-10-21 17:23:00 +00:00
|
|
|
names = read_name_list(os.path.join(inspiration_system_path, "abandoned.txt"), types, keyword)
|
|
|
|
print(names)
|
|
|
|
names = random.sample(names, get_num) if len(names) > get_num else names
|
|
|
|
elif source == "Exclude abandoned":
|
|
|
|
abandoned = read_name_list(os.path.join(inspiration_system_path, "abandoned.txt"), types, keyword)
|
|
|
|
all_names = []
|
|
|
|
for tp in types:
|
|
|
|
name_list = os.listdir(os.path.join(opts.inspiration_dir, tp))
|
|
|
|
all_names += [os.path.join(tp, x) for x in name_list if keyword in x]
|
|
|
|
|
|
|
|
if len(all_names) > get_num:
|
|
|
|
names = []
|
|
|
|
while len(names) < get_num:
|
|
|
|
name = random.choice(all_names)
|
|
|
|
if name not in abandoned:
|
|
|
|
names.append(name)
|
|
|
|
else:
|
|
|
|
names = all_names
|
2022-10-20 15:58:52 +00:00
|
|
|
else:
|
2022-10-21 17:23:00 +00:00
|
|
|
all_names = []
|
|
|
|
for tp in types:
|
|
|
|
name_list = os.listdir(os.path.join(opts.inspiration_dir, tp))
|
|
|
|
all_names += [os.path.join(tp, x) for x in name_list if keyword in x]
|
|
|
|
names = random.sample(all_names, get_num) if len(all_names) > get_num else all_names
|
2022-10-20 15:58:52 +00:00
|
|
|
image_list = []
|
|
|
|
for a in names:
|
2022-10-21 17:23:00 +00:00
|
|
|
image_path = os.path.join(opts.inspiration_dir, a)
|
2022-10-20 15:58:52 +00:00
|
|
|
images = os.listdir(image_path)
|
2022-10-21 17:23:00 +00:00
|
|
|
image_list.append((os.path.join(image_path, random.choice(images)), a))
|
|
|
|
return image_list, names, ""
|
2022-10-20 15:58:52 +00:00
|
|
|
|
2022-10-21 17:23:00 +00:00
|
|
|
def select_click(index, name_list):
|
2022-10-20 15:58:52 +00:00
|
|
|
name = name_list[int(index)]
|
2022-10-21 17:23:00 +00:00
|
|
|
path = os.path.join(opts.inspiration_dir, name)
|
2022-10-20 15:58:52 +00:00
|
|
|
images = os.listdir(path)
|
2022-10-21 17:23:00 +00:00
|
|
|
return name, [os.path.join(path, x) for x in images], ""
|
2022-10-20 15:58:52 +00:00
|
|
|
|
2022-10-21 17:23:00 +00:00
|
|
|
def give_up_click(name):
|
|
|
|
file = os.path.join(inspiration_system_path, "abandoned.txt")
|
2022-10-20 15:58:52 +00:00
|
|
|
name_list = read_name_list(file)
|
|
|
|
if name not in name_list:
|
|
|
|
save_name_list(file, name)
|
2022-10-21 17:23:00 +00:00
|
|
|
return "Added to abandoned list"
|
2022-10-20 15:58:52 +00:00
|
|
|
|
2022-10-21 17:23:00 +00:00
|
|
|
def collect_click(name):
|
|
|
|
file = os.path.join(inspiration_system_path, "faverites.txt")
|
2022-10-20 15:58:52 +00:00
|
|
|
name_list = read_name_list(file)
|
|
|
|
if name not in name_list:
|
|
|
|
save_name_list(file, name)
|
2022-10-21 17:23:00 +00:00
|
|
|
return "Added to faverite list"
|
2022-10-20 15:58:52 +00:00
|
|
|
|
2022-10-21 17:23:00 +00:00
|
|
|
def moveout_click(name, source):
|
|
|
|
if source == "Abandoned":
|
|
|
|
file = os.path.join(inspiration_system_path, "abandoned.txt")
|
|
|
|
if source == "Favorites":
|
|
|
|
file = os.path.join(inspiration_system_path, "faverites.txt")
|
|
|
|
else:
|
|
|
|
return None
|
2022-10-20 15:58:52 +00:00
|
|
|
name_list = read_name_list(file)
|
2022-10-21 17:23:00 +00:00
|
|
|
os.remove(file)
|
|
|
|
with open(file, "a") as f:
|
|
|
|
for a in name_list:
|
|
|
|
if a != name:
|
|
|
|
f.write(a)
|
|
|
|
return "Moved out {name} from {source} list"
|
2022-10-20 15:58:52 +00:00
|
|
|
|
|
|
|
def source_change(source):
|
2022-10-21 17:23:00 +00:00
|
|
|
if source in ["Abandoned", "Favorites"]:
|
|
|
|
return gradio.update(visible=True), []
|
2022-10-20 15:58:52 +00:00
|
|
|
else:
|
2022-10-21 17:23:00 +00:00
|
|
|
return gradio.update(visible=False), []
|
|
|
|
def add_to_prompt(name, prompt):
|
|
|
|
print(name, prompt)
|
|
|
|
name = os.path.basename(name)
|
|
|
|
return prompt + "," + name
|
2022-10-20 15:58:52 +00:00
|
|
|
|
2022-10-21 17:23:00 +00:00
|
|
|
def ui(gr, opts, txt2img_prompt, img2img_prompt):
|
2022-10-20 15:58:52 +00:00
|
|
|
with gr.Blocks(analytics_enabled=False) as inspiration:
|
2022-10-21 17:23:00 +00:00
|
|
|
flag = os.path.exists(opts.inspiration_dir)
|
2022-10-20 15:58:52 +00:00
|
|
|
if flag:
|
2022-10-21 17:23:00 +00:00
|
|
|
types = get_types_list()
|
2022-10-20 15:58:52 +00:00
|
|
|
flag = len(types) > 0
|
2022-10-21 17:23:00 +00:00
|
|
|
else:
|
|
|
|
os.makedirs(opts.inspiration_dir)
|
|
|
|
if not flag:
|
2022-10-20 15:58:52 +00:00
|
|
|
gr.HTML("""
|
2022-10-21 17:23:00 +00:00
|
|
|
<div align='center' width="50%"><h2>To activate inspiration function, you need get "inspiration" images first. </h2><br>
|
|
|
|
You can create these images by run "Create inspiration images" script in txt2img page, <br> you can get the artists or art styles list from here<br>
|
|
|
|
<a>https://github.com/pharmapsychotic/clip-interrogator/tree/main/data</a><br>
|
|
|
|
download these files, and select these files in the "Create inspiration images" script UI<br>
|
|
|
|
There about 6000 artists and art styles in these files. <br>This takes server hours depending on your GPU type and how many pictures you generate for each artist/style
|
|
|
|
<br>I suggest at least four images for each<br><br><br>
|
|
|
|
<h2>You can also download generated pictures from here:</h2><br>
|
|
|
|
<a>https://huggingface.co/datasets/yfszzx/inspiration</a><br>
|
|
|
|
unzip the file to the project directory of webui<br>
|
|
|
|
and restart webui, and enjoy the joy of creation!<br></div>
|
2022-10-20 15:58:52 +00:00
|
|
|
""")
|
|
|
|
return inspiration
|
|
|
|
if not os.path.exists(inspiration_system_path):
|
|
|
|
os.mkdir(inspiration_system_path)
|
|
|
|
with gr.Row():
|
|
|
|
with gr.Column(scale=2):
|
2022-10-21 17:23:00 +00:00
|
|
|
inspiration_gallery = gr.Gallery(show_label=False, elem_id="inspiration_gallery").style(grid=opts.inspiration_cols_num, height='auto')
|
2022-10-20 15:58:52 +00:00
|
|
|
with gr.Column(scale=1):
|
2022-10-21 17:23:00 +00:00
|
|
|
print(types)
|
|
|
|
types = gr.CheckboxGroup(choices=types, value=types)
|
|
|
|
keyword = gr.Textbox("", label="Key word")
|
2022-10-20 15:58:52 +00:00
|
|
|
with gr.Row():
|
|
|
|
source = gr.Dropdown(choices=["All", "Favorites", "Exclude abandoned", "Abandoned"], value="Exclude abandoned", label="Source")
|
2022-10-21 17:23:00 +00:00
|
|
|
get_inspiration = gr.Button("Get inspiration", elem_id="inspiration_get_button")
|
2022-10-20 15:58:52 +00:00
|
|
|
name = gr.Textbox(show_label=False, interactive=False)
|
|
|
|
with gr.Row():
|
|
|
|
send_to_txt2img = gr.Button('to txt2img')
|
|
|
|
send_to_img2img = gr.Button('to img2img')
|
2022-10-21 17:23:00 +00:00
|
|
|
style_gallery = gr.Gallery(show_label=False).style(grid=2, height='auto')
|
2022-10-20 15:58:52 +00:00
|
|
|
collect = gr.Button('Collect')
|
2022-10-21 17:23:00 +00:00
|
|
|
give_up = gr.Button("Don't show again")
|
2022-10-20 15:58:52 +00:00
|
|
|
moveout = gr.Button("Move out", visible=False)
|
2022-10-21 17:23:00 +00:00
|
|
|
warning = gr.HTML()
|
|
|
|
with gr.Row(visible=False):
|
2022-10-20 15:58:52 +00:00
|
|
|
select_button = gr.Button('set button', elem_id="inspiration_select_button")
|
2022-10-21 17:23:00 +00:00
|
|
|
name_list = gr.State()
|
|
|
|
|
|
|
|
get_inspiration.click(get_inspiration_images, inputs=[source, types, keyword], outputs=[inspiration_gallery, name_list, keyword])
|
|
|
|
source.change(source_change, inputs=[source], outputs=[moveout, style_gallery])
|
|
|
|
source.change(fn=None, _js="inspiration_click_get_button", inputs=None, outputs=None)
|
|
|
|
keyword.submit(fn=None, _js="inspiration_click_get_button", inputs=None, outputs=None)
|
|
|
|
select_button.click(select_click, _js="inspiration_selected", inputs=[name, name_list], outputs=[name, style_gallery, warning])
|
|
|
|
give_up.click(give_up_click, inputs=[name], outputs=[warning])
|
|
|
|
collect.click(collect_click, inputs=[name], outputs=[warning])
|
|
|
|
moveout.click(moveout_click, inputs=[name, source], outputs=[warning])
|
|
|
|
send_to_txt2img.click(add_to_prompt, inputs=[name, txt2img_prompt], outputs=[txt2img_prompt])
|
|
|
|
send_to_img2img.click(add_to_prompt, inputs=[name, img2img_prompt], outputs=[img2img_prompt])
|
|
|
|
send_to_txt2img.click(None, _js='switch_to_txt2img', inputs=None, outputs=None)
|
|
|
|
send_to_img2img.click(None, _js="switch_to_img2img_img2img", inputs=None, outputs=None)
|
2022-10-20 15:58:52 +00:00
|
|
|
return inspiration
|