script.js 7.7 KB
Newer Older
1 2 3 4 5 6
titles = {
    "Sampling steps": "How many times to imptove the generated image itratively; higher values take longer; very low values can produce bad results",
    "Sampling method": "Which algorithm to use to produce the image",
	"GFPGAN": "Restore low quality faces using GFPGAN neural network",
	"Euler a": "Euler Ancestral - very creative, each can get acompletely different pictures depending on step count, setting seps tohigher than 30-40 does not help",
	"DDIM": "Denoising Diffusion Implicit Models - best at inpainting",
A
AUTOMATIC 已提交
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
	"Batch count": "How many batches of images to create",
	"Batch size": "How many image to create in a single batch",
    "CFG Scale": "Classifier Free Guidance Scale - how strongly the image should conform to prompt - lower values produce more creative results",
    "Seed": "A value that determines the output of random number generator - if you create an image with same parameters and seed as another image, you'll get the same result",

    "Inpaint a part of image": "Draw a mask over an image, and the script will regenerate the masked area with content according to prompt",
    "Loopback": "Process an image, use it as an input, repeat. Batch count determings number of iterations.",
    "SD upscale": "Upscale image normally, split result into tiles, improve each tile using img2img, merge whole image back",

    "Just resize": "Resize image to target resolution. Unless height and width match, you will get incorrect aspect ratio.",
    "Crop and resize": "Resize the image so that entirety of target resolution is filled with the image. Crop parts that stick out.",
    "Resize and fill": "Resize the image so that entirety of image is inside target resolution. Fill empty space with image's colors.",

    "Mask blur": "How much to blur the mask before processing, in pixels.",
    "Masked content": "What to put inside the masked area before processing it with Stable Diffusion.",
    "fill": "fill it with colors of the image",
    "original": "keep whatever was there originally",
    "latent noise": "fill it with latent space noise",
    "latent nothing": "fill it with latent space zeroes",
    "Inpaint at full resolution": "Upscale masked region to target resolution, do inpainting, downscale back and paste into original image",

    "Denoising Strength": "Determines how little respect the algorithm should have for image's content. At 0, nothing will change, and at 1 you'll get an unrelated image.",
A
AUTOMATIC 已提交
30 31 32

    "Interrupt": "Stop processing images and return any results accumulated so far.",
    "Save": "Write image to a directory (default - log/images) and generation parameters into csv file.",
A
AUTOMATIC 已提交
33 34 35 36 37 38

    "X values": "Separate values for X axis using commas.",
    "Y values": "Separate values for Y axis using commas.",

    "None": "Do not do anything special",
    "Prompt matrix": "Separate prompts into parts using vertical pipe character (|) and the script will create a picture for every combination of them (except for the first part, which will be present in all combinations)",
A
scripts  
AUTOMATIC 已提交
39
    "X/Y plot": "Create a grid where images will have different parameters. Use inputs below to specify which parameterswill be shared by columns and rows",
A
AUTOMATIC 已提交
40 41 42
    "Custom code": "Run python code. Advanced user only. Must run program with --allow-code for this to work",

    "Prompt S/R": "Separate a list of words with commas, and the first word will be used as a keyword: script will search for this word in the prompt, and replace it with others",
43 44 45 46

    "Tiling": "Produce an image that can be tiled.",
    "Tile overlap": "For SD upscale, how much overlap in pixels should there be between tiles. Tils overlap so that when they are merged back into one oicture, there is no clearly visible seam.",

A
AUTOMATIC 已提交
47
    "Roll": "Add a random artist to the prompt.",
48 49 50
}

function gradioApp(){
51
    return document.getElementsByTagName('gradio-app')[0].shadowRoot;
52 53
}

A
AUTOMATIC 已提交
54 55
global_progressbar = null

56
function addTitles(root){
A
AUTOMATIC 已提交
57
	root.querySelectorAll('span, button, select').forEach(function(span){
58
		tooltip = titles[span.textContent];
A
AUTOMATIC 已提交
59 60 61 62 63

		if(!tooltip){
		    tooltip = titles[span.value];
		}

64 65 66 67
		if(tooltip){
			span.title = tooltip;
		}
	})
A
AUTOMATIC 已提交
68 69 70 71 72 73 74 75

	root.querySelectorAll('select').forEach(function(select){
	    if (select.onchange != null) return;

	    select.onchange = function(){
            select.title = titles[select.value] || "";
	    }
	})
A
AUTOMATIC 已提交
76 77 78 79 80 81

	progressbar = root.getElementById('progressbar')
	if(progressbar!= null && progressbar != global_progressbar){
	    global_progressbar = progressbar

        var mutationObserver = new MutationObserver(function(m){
A
AUTOMATIC 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
            txt2img_preview = gradioApp().getElementById('txt2img_preview')
            txt2img_gallery = gradioApp().getElementById('txt2img_gallery')

            img2img_preview = gradioApp().getElementById('img2img_preview')
            img2img_gallery = gradioApp().getElementById('img2img_gallery')

            if(txt2img_preview != null && txt2img_gallery != null){
                txt2img_preview.style.width = txt2img_gallery.clientWidth + "px"
                txt2img_preview.style.height = txt2img_gallery.clientHeight + "px"
            }

            if(img2img_preview != null && img2img_gallery != null){
                img2img_preview.style.width = img2img_gallery.clientWidth + "px"
                img2img_preview.style.height = img2img_gallery.clientHeight + "px"
            }


A
AUTOMATIC 已提交
99 100 101 102 103
            window.setTimeout(requestProgress, 500)
        });
        mutationObserver.observe( progressbar, { childList:true, subtree:true })
	}

104 105
}

106 107 108
tabNames =  {"txt2img": 1, "img2img": 1, "Extras": 1, "PNG Info": 1, "Settings": 1}
processedTabs = {}

109 110
document.addEventListener("DOMContentLoaded", function() {
    var mutationObserver = new MutationObserver(function(m){
111
        addTitles(gradioApp());
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

        // fix for gradio breaking when you switch away from tab with mask
        gradioApp().querySelectorAll('button').forEach(function(button){
            title = button.textContent.trim()
            if(processedTabs[title]) return
            if(tabNames[button.textContent.trim()]==null) return;
            processedTabs[title]=1

            button.onclick = function(){
                mask_buttons = gradioApp().querySelectorAll('#img2maskimg button');
                if(mask_buttons.length == 2){
                    mask_buttons[1].click();
                }
            }
        })
127
    });
128
    mutationObserver.observe( gradioApp(), { childList:true, subtree:true })
129 130


131
});
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

function selected_gallery_index(){
    var gr = gradioApp()
    var buttons = gradioApp().querySelectorAll(".gallery-item")
    var button = gr.querySelector(".gallery-item.\\!ring-2")

    var result = -1
    buttons.forEach(function(v, i){ if(v==button) { result = i } })

    return result
}

function extract_image_from_gallery(gallery){
    if(gallery.length == 1){
        return gallery[0]
    }

    index = selected_gallery_index()

    if (index < 0 || index >= gallery.length){
        return []
    }

    return gallery[index];
}
A
AUTOMATIC 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173


function requestProgress(){
    btn = gradioApp().getElementById("check_progress");
    if(btn==null) return;

    btn.click();
}

function submit(){
    window.setTimeout(requestProgress, 500)

    res = []
    for(var i=0;i<arguments.length;i++){
        res.push(arguments[i])
    }
    return res
174
}
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

window.addEventListener('paste', e => {
    const files = e.clipboardData.files;
    if (!files || files.length !== 1) {
        return;
    }
    if (!['image/png', 'image/gif', 'image/jpeg'].includes(files[0].type)) {
        return;
    }
    [...gradioApp().querySelectorAll('input[type=file][accept="image/x-png,image/gif,image/jpeg"]')]
        .filter(input => !input.matches('.\\!hidden input[type=file]'))
        .forEach(input => {
            input.files = files;
            input.dispatchEvent(new Event('change'))
        });
});