Commit 124e44cf authored by yfszzx's avatar yfszzx
Browse files

remove browser to extension

parent 6a9ea40d
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -29,5 +29,4 @@ notification.mp3
/textual_inversion
.vscode
/extensions
 /inspiration

javascript/images_history.js

deleted100644 → 0
+0 −200
Original line number Diff line number Diff line
var images_history_click_image = function(){
    if (!this.classList.contains("transform")){        
        var gallery = images_history_get_parent_by_class(this, "images_history_cantainor");
        var buttons = gallery.querySelectorAll(".gallery-item");
        var i = 0;
        var hidden_list = [];
        buttons.forEach(function(e){
            if (e.style.display == "none"){
                hidden_list.push(i);
            }
            i += 1;
        })
        if (hidden_list.length > 0){
            setTimeout(images_history_hide_buttons, 10, hidden_list, gallery);
        }        
    }    
    images_history_set_image_info(this); 
}

function images_history_disabled_del(){
    gradioApp().querySelectorAll(".images_history_del_button").forEach(function(btn){
        btn.setAttribute('disabled','disabled');
    }); 
}

function images_history_get_parent_by_class(item, class_name){
    var parent = item.parentElement;
    while(!parent.classList.contains(class_name)){
        parent = parent.parentElement;
    }
    return parent;  
}

function images_history_get_parent_by_tagname(item, tagname){
    var parent = item.parentElement;
    tagname = tagname.toUpperCase()
    while(parent.tagName != tagname){
        parent = parent.parentElement;
    }  
    return parent;
}

function images_history_hide_buttons(hidden_list, gallery){
    var buttons = gallery.querySelectorAll(".gallery-item");
    var num = 0;
    buttons.forEach(function(e){
        if (e.style.display == "none"){
            num += 1;
        }
    });
    if (num == hidden_list.length){
        setTimeout(images_history_hide_buttons, 10, hidden_list, gallery);
    } 
    for( i in hidden_list){
        buttons[hidden_list[i]].style.display = "none";
    }    
}

function images_history_set_image_info(button){
    var buttons = images_history_get_parent_by_tagname(button, "DIV").querySelectorAll(".gallery-item");
    var index = -1;
    var i = 0;
    buttons.forEach(function(e){
        if(e == button){
            index = i;
        }
        if(e.style.display != "none"){
            i += 1;
        }        
    });
    var gallery = images_history_get_parent_by_class(button, "images_history_cantainor");
    var set_btn = gallery.querySelector(".images_history_set_index");
    var curr_idx = set_btn.getAttribute("img_index", index);  
    if (curr_idx != index) {
        set_btn.setAttribute("img_index", index);        
        images_history_disabled_del();
    }
    set_btn.click();
    
}

function images_history_get_current_img(tabname, img_index, files){
    return [
        tabname, 
        gradioApp().getElementById(tabname + '_images_history_set_index').getAttribute("img_index"),         
        files
    ];
}

function images_history_delete(del_num, tabname, image_index){
    image_index = parseInt(image_index);
    var tab = gradioApp().getElementById(tabname + '_images_history');
    var set_btn = tab.querySelector(".images_history_set_index");
    var buttons = [];
    tab.querySelectorAll(".gallery-item").forEach(function(e){
        if (e.style.display != 'none'){
            buttons.push(e);
        }
    });    
    var img_num = buttons.length / 2;
    del_num = Math.min(img_num - image_index, del_num)    
    if (img_num <= del_num){
        setTimeout(function(tabname){
            gradioApp().getElementById(tabname + '_images_history_renew_page').click();
        }, 30, tabname); 
    } else {
        var next_img  
        for (var i = 0; i < del_num; i++){
            buttons[image_index + i].style.display = 'none';
            buttons[image_index + i + img_num].style.display = 'none';
            next_img = image_index + i + 1
        }
        var bnt;
        if (next_img  >= img_num){
            btn = buttons[image_index - 1];
        } else {            
            btn = buttons[next_img];          
        } 
        setTimeout(function(btn){btn.click()}, 30, btn);
    }
    images_history_disabled_del();  
   
}

function images_history_turnpage(tabname){
    gradioApp().getElementById(tabname + '_images_history_del_button').setAttribute('disabled','disabled');
    var buttons = gradioApp().getElementById(tabname + '_images_history').querySelectorAll(".gallery-item");
    buttons.forEach(function(elem) {
        elem.style.display = 'block';
    })   
}

function images_history_enable_del_buttons(){
    gradioApp().querySelectorAll(".images_history_del_button").forEach(function(btn){
        btn.removeAttribute('disabled');
    })
}

function images_history_init(){ 
    var tabnames = gradioApp().getElementById("images_history_tabnames_list")   
    if (tabnames){  
        images_history_tab_list = tabnames.querySelector("textarea").value.split(",")    
        for (var i in images_history_tab_list ){
            var tab = images_history_tab_list[i];
            gradioApp().getElementById(tab + '_images_history').classList.add("images_history_cantainor");
            gradioApp().getElementById(tab + '_images_history_set_index').classList.add("images_history_set_index");
            gradioApp().getElementById(tab + '_images_history_del_button').classList.add("images_history_del_button");
            gradioApp().getElementById(tab + '_images_history_gallery').classList.add("images_history_gallery");  
             gradioApp().getElementById(tab + "_images_history_start").setAttribute("style","padding:20px;font-size:25px");           
        }

        //preload
        if (gradioApp().getElementById("images_history_preload").querySelector("input").checked ){
            var tabs_box = gradioApp().getElementById("tab_images_history").querySelector("div").querySelector("div").querySelector("div");
            tabs_box.setAttribute("id", "images_history_tab");        
            var tab_btns = tabs_box.querySelectorAll("button"); 
            for (var i in images_history_tab_list){               
                var tabname = images_history_tab_list[i]
                tab_btns[i].setAttribute("tabname", tabname);
                tab_btns[i].addEventListener('click', function(){
                    var tabs_box = gradioApp().getElementById("images_history_tab");
                    if (!tabs_box.classList.contains(this.getAttribute("tabname"))) {
                        gradioApp().getElementById(this.getAttribute("tabname") + "_images_history_start").click();
                        tabs_box.classList.add(this.getAttribute("tabname"))
                    }                
                });
            }            
            tab_btns[0].click()
        }
    } else {
        setTimeout(images_history_init, 500);
    } 
}

var images_history_tab_list = "";
setTimeout(images_history_init, 500);
document.addEventListener("DOMContentLoaded", function() {
    var mutationObserver = new MutationObserver(function(m){
        if (images_history_tab_list != ""){
            for (var i in images_history_tab_list ){
                let tabname = images_history_tab_list[i]
                var buttons = gradioApp().querySelectorAll('#' + tabname + '_images_history .gallery-item');
                buttons.forEach(function(bnt){    
                    bnt.addEventListener('click', images_history_click_image, true);
                });

                var cls_btn = gradioApp().getElementById(tabname + '_images_history_gallery').querySelector("svg");
                if (cls_btn){
                    cls_btn.addEventListener('click', function(){
                        gradioApp().getElementById(tabname + '_images_history_renew_page').click();
                    }, false);
                }

            }     
        }
    });
    mutationObserver.observe(gradioApp(), { childList:true, subtree:true });
});

javascript/inspiration.js

deleted100644 → 0
+0 −48
Original line number Diff line number Diff line
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 });
});

modules/images_history.py

deleted100644 → 0
+0 −424

File deleted.

Preview size limit exceeded, changes collapsed.

modules/inspiration.py

deleted100644 → 0
+0 −193
Original line number Diff line number Diff line
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("""
                <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 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>
                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 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>
                and restart webui, and enjoy the joy of creation!<br></div>
                """)      
            return inspiration
        if not os.path.exists(inspiration_system_path):
            os.mkdir(inspiration_system_path)
        with gr.Row():
            with gr.Column(scale=2):                
                inspiration_gallery = gr.Gallery(show_label=False, elem_id="inspiration_gallery").style(grid=opts.inspiration_cols_num, height='auto')
            with gr.Column(scale=1):
                types = gr.CheckboxGroup(choices=types, value=types)
                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_img2img = gr.Button('to img2img')
                    collect = gr.Button('Collect')     
                    give_up = gr.Button("Don't show again")
                    moveout = gr.Button("Move out", visible=False)
                warning = gr.HTML()
                style_gallery = gr.Gallery(show_label=False).style(grid=2, height='auto') 
                    
               
                
        with gr.Row(visible=False):
            select_button = gr.Button('set button', elem_id="inspiration_select_button")
            name_list = gr.State()
        
        get_inspiration.click(get_inspiration_images, inputs=[source, types, keyword], outputs=[inspiration_gallery, name_list])
        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])
        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])
        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_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_img2img.click(None, _js="switch_to_img2img_img2img", inputs=None, outputs=None)
    return inspiration
Loading