diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index a1c288f..0000000 --- a/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -inspiration.rar filter=lfs diff=lfs merge=lfs -text -artist.zip filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f52302c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +inspiration \ No newline at end of file diff --git a/inspiration.js b/inspiration.js new file mode 100644 index 0000000..3984454 --- /dev/null +++ b/inspiration.js @@ -0,0 +1,48 @@ +function public_image_index_in_gallery(item, gallery){ + var imgs = gallery.querySelectorAll("img.h-full") + var index; + var i = 0; + imgs.forEach(function(e){ + if (e == item) + index = i; + i += 1; + }); + var all_imgs = gallery.querySelectorAll("img") + if (all_imgs.length > imgs.length){ + var num = imgs.length / 2 + index = (index < num) ? index : (index - num) + } + return index; +} + +function inspiration_selected(name, name_list){ + var btn = gradioApp().getElementById("inspiration_select_button") + return [gradioApp().getElementById("inspiration_select_button").getAttribute("img-index")]; +} + +function inspiration_click_get_button(){ + gradioApp().getElementById("inspiration_get_button").click(); +} + +var inspiration_image_click = function(){ + var index = public_image_index_in_gallery(this, gradioApp().getElementById("inspiration_gallery")); + var btn = gradioApp().getElementById("inspiration_select_button"); + btn.setAttribute("img-index", index); + setTimeout(function(btn){btn.click();}, 10, btn); +} + +document.addEventListener("DOMContentLoaded", function() { + var mutationObserver = new MutationObserver(function(m){ + var gallery = gradioApp().getElementById("inspiration_gallery") + if (gallery) { + var node = gallery.querySelector(".absolute.backdrop-blur.h-full") + if (node) { + node.style.display = "None"; + } + gallery.querySelectorAll('img').forEach(function(e){ + e.onclick = inspiration_image_click + }); + } + }); + mutationObserver.observe( gradioApp(), { childList:true, subtree:true }); +}); diff --git a/scripts/inspiration.py b/scripts/inspiration.py new file mode 100644 index 0000000..ace10d4 --- /dev/null +++ b/scripts/inspiration.py @@ -0,0 +1,194 @@ +import os +import random +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): + if not os.path.exists(file): + return [] + ret = [] + f = open(file, "r") + line = f.readline() + while len(line) > 0: + line = line.rstrip("\n") + if types is not None: + dirname = os.path.split(line) + if dirname[0] in types and keyword in dirname[1].lower(): + ret.append(line) + else: + ret.append(line) + line = f.readline() + return ret + +def save_name_list(file, name): + name_list = read_name_list(file) + if name not in name_list: + with open(file, "a") as f: + f.write(name + "\n") + +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): + keyword = keyword.strip(" ").lower() + get_num = int(opts.inspiration_rows_num * opts.inspiration_cols_num) + if source == "Favorites": + 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 + elif source == "Abandoned": + names = read_name_list(os.path.join(inspiration_system_path, "abandoned.txt"), types, keyword) + 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.lower()] + + 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 + else: + 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.lower()] + names = random.sample(all_names, get_num) if len(all_names) > get_num else all_names + image_list = [] + for a in names: + image_path = os.path.join(opts.inspiration_dir, a) + images = os.listdir(image_path) + if len(images) > 0: + image_list.append((os.path.join(image_path, random.choice(images)), a)) + else: + print(image_path) + return image_list, names + +def select_click(index, name_list): + name = name_list[int(index)] + path = os.path.join(opts.inspiration_dir, name) + images = os.listdir(path) + return name, [os.path.join(path, x) for x in images], "" + +def give_up_click(name): + file = os.path.join(inspiration_system_path, "abandoned.txt") + save_name_list(file, name) + return "Added to abandoned list" + +def collect_click(name): + file = os.path.join(inspiration_system_path, "faverites.txt") + save_name_list(file, name) + return "Added to faverite list" + +def moveout_click(name, source): + if source == "Abandoned": + file = os.path.join(inspiration_system_path, "abandoned.txt") + elif source == "Favorites": + file = os.path.join(inspiration_system_path, "faverites.txt") + else: + return None + name_list = read_name_list(file) + os.remove(file) + with open(file, "a") as f: + for a in name_list: + if a != name: + f.write(a + "\n") + return f"Moved out {name} from {source} list" + +def source_change(source): + if source in ["Abandoned", "Favorites"]: + return gradio.update(visible=True), [] + else: + return gradio.update(visible=False), [] +def add_to_prompt(name, prompt): + name = os.path.basename(name) + return prompt + "," + name + +def clear_keyword(): + return "" + +def ui(gr, opts, txt2img_prompt, img2img_prompt): + with gr.Blocks(analytics_enabled=False) as inspiration: + flag = os.path.exists(opts.inspiration_dir) + if flag: + types = get_types_list() + flag = len(types) > 0 + else: + os.makedirs(opts.inspiration_dir) + if not flag: + gr.HTML(""" +