inspiration perfected

This commit is contained in:
yfszzx 2022-10-22 10:16:22 +08:00
parent 58ee008f0f
commit 40ddb6df61
2 changed files with 49 additions and 41 deletions

View File

@ -1,5 +1,5 @@
function public_image_index_in_gallery(item, gallery){ function public_image_index_in_gallery(item, gallery){
var imgs = gallery.querySelectorAll("img.h-full") var imgs = gallery.querySelectorAll("img.h-full")
var index; var index;
var i = 0; var i = 0;
imgs.forEach(function(e){ imgs.forEach(function(e){
@ -7,18 +7,23 @@ function public_image_index_in_gallery(item, gallery){
index = i; index = i;
i += 1; i += 1;
}); });
var num = imgs.length / 2 var all_imgs = gallery.querySelectorAll("img")
index = (index < num) ? index : (index - num) if (all_imgs.length > imgs.length){
var num = imgs.length / 2
index = (index < num) ? index : (index - num)
}
return index; return index;
} }
function inspiration_selected(name, name_list){ function inspiration_selected(name, name_list){
var btn = gradioApp().getElementById("inspiration_select_button") var btn = gradioApp().getElementById("inspiration_select_button")
return [gradioApp().getElementById("inspiration_select_button").getAttribute("img-index")]; return [gradioApp().getElementById("inspiration_select_button").getAttribute("img-index")];
} }
function inspiration_click_get_button(){ function inspiration_click_get_button(){
gradioApp().getElementById("inspiration_get_button").click(); gradioApp().getElementById("inspiration_get_button").click();
} }
var inspiration_image_click = function(){ var inspiration_image_click = function(){
var index = public_image_index_in_gallery(this, gradioApp().getElementById("inspiration_gallery")); var index = public_image_index_in_gallery(this, gradioApp().getElementById("inspiration_gallery"));
var btn = gradioApp().getElementById("inspiration_select_button"); var btn = gradioApp().getElementById("inspiration_select_button");
@ -32,16 +37,12 @@ document.addEventListener("DOMContentLoaded", function() {
if (gallery) { if (gallery) {
var node = gallery.querySelector(".absolute.backdrop-blur.h-full") var node = gallery.querySelector(".absolute.backdrop-blur.h-full")
if (node) { if (node) {
node.style.display = "None"; //parentNode.removeChild(node) node.style.display = "None";
} }
gallery.querySelectorAll('img').forEach(function(e){ gallery.querySelectorAll('img').forEach(function(e){
e.onclick = inspiration_image_click e.onclick = inspiration_image_click
}); });
} }
}); });
mutationObserver.observe( gradioApp(), { childList:true, subtree:true }); mutationObserver.observe( gradioApp(), { childList:true, subtree:true });
}); });

View File

@ -13,7 +13,7 @@ def read_name_list(file, types=None, keyword=None):
line = line.rstrip("\n") line = line.rstrip("\n")
if types is not None: if types is not None:
dirname = os.path.split(line) dirname = os.path.split(line)
if dirname[0] in types and keyword in dirname[1]: if dirname[0] in types and keyword in dirname[1].lower():
ret.append(line) ret.append(line)
else: else:
ret.append(line) ret.append(line)
@ -21,8 +21,10 @@ def read_name_list(file, types=None, keyword=None):
return ret return ret
def save_name_list(file, name): def save_name_list(file, name):
with open(file, "a") as f: name_list = read_name_list(file)
f.write(name + "\n") if name not in name_list:
with open(file, "a") as f:
f.write(name + "\n")
def get_types_list(): def get_types_list():
files = os.listdir(opts.inspiration_dir) files = os.listdir(opts.inspiration_dir)
@ -39,20 +41,20 @@ def get_types_list():
return types return types
def get_inspiration_images(source, types, keyword): def get_inspiration_images(source, types, keyword):
keyword = keyword.strip(" ").lower()
get_num = int(opts.inspiration_rows_num * opts.inspiration_cols_num) get_num = int(opts.inspiration_rows_num * opts.inspiration_cols_num)
if source == "Favorites": if source == "Favorites":
names = read_name_list(os.path.join(inspiration_system_path, "faverites.txt"), types, keyword) 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 names = random.sample(names, get_num) if len(names) > get_num else names
elif source == "Abandoned": elif source == "Abandoned":
names = read_name_list(os.path.join(inspiration_system_path, "abandoned.txt"), types, keyword) 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 names = random.sample(names, get_num) if len(names) > get_num else names
elif source == "Exclude abandoned": elif source == "Exclude abandoned":
abandoned = read_name_list(os.path.join(inspiration_system_path, "abandoned.txt"), types, keyword) abandoned = read_name_list(os.path.join(inspiration_system_path, "abandoned.txt"), types, keyword)
all_names = [] all_names = []
for tp in types: for tp in types:
name_list = os.listdir(os.path.join(opts.inspiration_dir, tp)) 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] all_names += [os.path.join(tp, x) for x in name_list if keyword in x.lower()]
if len(all_names) > get_num: if len(all_names) > get_num:
names = [] names = []
@ -66,14 +68,14 @@ def get_inspiration_images(source, types, keyword):
all_names = [] all_names = []
for tp in types: for tp in types:
name_list = os.listdir(os.path.join(opts.inspiration_dir, tp)) 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] all_names += [os.path.join(tp, x) for x in name_list if keyword in x.lower()]
names = random.sample(all_names, get_num) if len(all_names) > get_num else all_names names = random.sample(all_names, get_num) if len(all_names) > get_num else all_names
image_list = [] image_list = []
for a in names: for a in names:
image_path = os.path.join(opts.inspiration_dir, a) image_path = os.path.join(opts.inspiration_dir, a)
images = os.listdir(image_path) images = os.listdir(image_path)
image_list.append((os.path.join(image_path, random.choice(images)), a)) image_list.append((os.path.join(image_path, random.choice(images)), a))
return image_list, names, "" return image_list, names
def select_click(index, name_list): def select_click(index, name_list):
name = name_list[int(index)] name = name_list[int(index)]
@ -83,22 +85,18 @@ def select_click(index, name_list):
def give_up_click(name): def give_up_click(name):
file = os.path.join(inspiration_system_path, "abandoned.txt") file = os.path.join(inspiration_system_path, "abandoned.txt")
name_list = read_name_list(file) save_name_list(file, name)
if name not in name_list:
save_name_list(file, name)
return "Added to abandoned list" return "Added to abandoned list"
def collect_click(name): def collect_click(name):
file = os.path.join(inspiration_system_path, "faverites.txt") file = os.path.join(inspiration_system_path, "faverites.txt")
name_list = read_name_list(file) save_name_list(file, name)
if name not in name_list:
save_name_list(file, name)
return "Added to faverite list" return "Added to faverite list"
def moveout_click(name, source): def moveout_click(name, source):
if source == "Abandoned": if source == "Abandoned":
file = os.path.join(inspiration_system_path, "abandoned.txt") file = os.path.join(inspiration_system_path, "abandoned.txt")
if source == "Favorites": elif source == "Favorites":
file = os.path.join(inspiration_system_path, "faverites.txt") file = os.path.join(inspiration_system_path, "faverites.txt")
else: else:
return None return None
@ -107,8 +105,8 @@ def moveout_click(name, source):
with open(file, "a") as f: with open(file, "a") as f:
for a in name_list: for a in name_list:
if a != name: if a != name:
f.write(a) f.write(a + "\n")
return "Moved out {name} from {source} list" return f"Moved out {name} from {source} list"
def source_change(source): def source_change(source):
if source in ["Abandoned", "Favorites"]: if source in ["Abandoned", "Favorites"]:
@ -116,10 +114,12 @@ def source_change(source):
else: else:
return gradio.update(visible=False), [] return gradio.update(visible=False), []
def add_to_prompt(name, prompt): def add_to_prompt(name, prompt):
print(name, prompt)
name = os.path.basename(name) name = os.path.basename(name)
return prompt + "," + name return prompt + "," + name
def clear_keyword():
return ""
def ui(gr, opts, txt2img_prompt, img2img_prompt): def ui(gr, opts, txt2img_prompt, img2img_prompt):
with gr.Blocks(analytics_enabled=False) as inspiration: with gr.Blocks(analytics_enabled=False) as inspiration:
flag = os.path.exists(opts.inspiration_dir) flag = os.path.exists(opts.inspiration_dir)
@ -132,15 +132,15 @@ def ui(gr, opts, txt2img_prompt, img2img_prompt):
gr.HTML(""" gr.HTML("""
<div align='center' width="50%"><h2>To activate inspiration function, you need get "inspiration" images first. </h2><br> <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> 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> <a href="https://github.com/pharmapsychotic/clip-interrogator/tree/main/data">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> 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 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> <br>I suggest at least four images for each<br><br><br>
<h2>You can also download generated pictures from here:</h2><br> <h2>You can also download generated pictures from here:</h2><br>
<a>https://huggingface.co/datasets/yfszzx/inspiration</a><br> <a href="https://huggingface.co/datasets/yfszzx/inspiration">https://huggingface.co/datasets/yfszzx/inspiration</a><br>
unzip the file to the project directory of webui<br> unzip the file to the project directory of webui<br>
and restart webui, and enjoy the joy of creation!<br></div> and restart webui, and enjoy the joy of creation!<br></div>
""") """)
return inspiration return inspiration
if not os.path.exists(inspiration_system_path): if not os.path.exists(inspiration_system_path):
os.mkdir(inspiration_system_path) os.mkdir(inspiration_system_path)
@ -148,35 +148,42 @@ def ui(gr, opts, txt2img_prompt, img2img_prompt):
with gr.Column(scale=2): with gr.Column(scale=2):
inspiration_gallery = gr.Gallery(show_label=False, elem_id="inspiration_gallery").style(grid=opts.inspiration_cols_num, height='auto') inspiration_gallery = gr.Gallery(show_label=False, elem_id="inspiration_gallery").style(grid=opts.inspiration_cols_num, height='auto')
with gr.Column(scale=1): with gr.Column(scale=1):
print(types)
types = gr.CheckboxGroup(choices=types, value=types) types = gr.CheckboxGroup(choices=types, value=types)
keyword = gr.Textbox("", label="Key word")
with gr.Row():
source = gr.Dropdown(choices=["All", "Favorites", "Exclude abandoned", "Abandoned"], value="Exclude abandoned", label="Source")
get_inspiration = gr.Button("Get inspiration", elem_id="inspiration_get_button")
name = gr.Textbox(show_label=False, interactive=False)
with gr.Row(): with gr.Row():
source = gr.Dropdown(choices=["All", "Favorites", "Exclude abandoned", "Abandoned"], value="Exclude abandoned", label="Source")
keyword = gr.Textbox("", label="Key word")
get_inspiration = gr.Button("Get inspiration", elem_id="inspiration_get_button")
name = gr.Textbox(show_label=False, interactive=False)
with gr.Row():
send_to_txt2img = gr.Button('to txt2img') send_to_txt2img = gr.Button('to txt2img')
send_to_img2img = gr.Button('to img2img') send_to_img2img = gr.Button('to img2img')
style_gallery = gr.Gallery(show_label=False).style(grid=2, height='auto') style_gallery = gr.Gallery(show_label=False).style(grid=2, height='auto')
collect = gr.Button('Collect')
give_up = gr.Button("Don't show again")
moveout = gr.Button("Move out", visible=False)
warning = gr.HTML() warning = gr.HTML()
with gr.Row():
collect = gr.Button('Collect')
give_up = gr.Button("Don't show again")
moveout = gr.Button("Move out", visible=False)
with gr.Row(visible=False): with gr.Row(visible=False):
select_button = gr.Button('set button', elem_id="inspiration_select_button") select_button = gr.Button('set button', elem_id="inspiration_select_button")
name_list = gr.State() name_list = gr.State()
get_inspiration.click(get_inspiration_images, inputs=[source, types, keyword], outputs=[inspiration_gallery, name_list, keyword]) get_inspiration.click(get_inspiration_images, inputs=[source, types, keyword], outputs=[inspiration_gallery, name_list])
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) keyword.submit(fn=None, _js="inspiration_click_get_button", inputs=None, outputs=None)
source.change(source_change, inputs=[source], outputs=[moveout, style_gallery])
source.change(fn=clear_keyword, _js="inspiration_click_get_button", inputs=None, outputs=[keyword])
types.change(fn=clear_keyword, _js="inspiration_click_get_button", inputs=None, outputs=[keyword])
select_button.click(select_click, _js="inspiration_selected", inputs=[name, name_list], outputs=[name, style_gallery, warning]) 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]) give_up.click(give_up_click, inputs=[name], outputs=[warning])
collect.click(collect_click, inputs=[name], outputs=[warning]) collect.click(collect_click, inputs=[name], outputs=[warning])
moveout.click(moveout_click, inputs=[name, source], outputs=[warning]) moveout.click(moveout_click, inputs=[name, source], outputs=[warning])
moveout.click(fn=None, _js="inspiration_click_get_button", inputs=None, outputs=None)
send_to_txt2img.click(add_to_prompt, inputs=[name, txt2img_prompt], outputs=[txt2img_prompt]) 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_img2img.click(add_to_prompt, inputs=[name, img2img_prompt], outputs=[img2img_prompt])
send_to_txt2img.click(collect_click, inputs=[name], outputs=[warning])
send_to_img2img.click(collect_click, inputs=[name], outputs=[warning])
send_to_txt2img.click(None, _js='switch_to_txt2img', inputs=None, outputs=None) 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) send_to_img2img.click(None, _js="switch_to_img2img_img2img", inputs=None, outputs=None)
return inspiration return inspiration