"use strict" // Opt in to a restricted variant of JavaScript const SOUND_ENABLED_KEY = "soundEnabled" const SAVE_TO_DISK_KEY = "saveToDisk" const USE_CPU_KEY = "useCPU" const USE_FULL_PRECISION_KEY = "useFullPrecision" const USE_TURBO_MODE_KEY = "useTurboMode" const DISK_PATH_KEY = "diskPath" const ADVANCED_PANEL_OPEN_KEY = "advancedPanelOpen" const MODIFIERS_PANEL_OPEN_KEY = "modifiersPanelOpen" const USE_FACE_CORRECTION_KEY = "useFaceCorrection" const USE_UPSCALING_KEY = "useUpscaling" const SHOW_ONLY_FILTERED_IMAGE_KEY = "showOnlyFilteredImage" const STREAM_IMAGE_PROGRESS_KEY = "streamImageProgress" const OUTPUT_FORMAT_KEY = "outputFormat" const HEALTH_PING_INTERVAL = 5 // seconds const MAX_INIT_IMAGE_DIMENSION = 768 const INPAINTING_EDITOR_SIZE = 450 const IMAGE_REGEX = new RegExp('data:image/[A-Za-z]+;base64') let sessionId = Date.now() let promptField = document.querySelector('#prompt') let promptsFromFileSelector = document.querySelector('#prompt_from_file') let promptsFromFileBtn = document.querySelector('#promptsFromFileBtn') let negativePromptField = document.querySelector('#negative_prompt') let numOutputsTotalField = document.querySelector('#num_outputs_total') let numOutputsParallelField = document.querySelector('#num_outputs_parallel') let numInferenceStepsField = document.querySelector('#num_inference_steps') let guidanceScaleSlider = document.querySelector('#guidance_scale_slider') let guidanceScaleField = document.querySelector('#guidance_scale') let randomSeedField = document.querySelector("#random_seed") let seedField = document.querySelector('#seed') let widthField = document.querySelector('#width') let heightField = document.querySelector('#height') let initImageSelector = document.querySelector("#init_image") let initImagePreview = document.querySelector("#init_image_preview") let maskImageSelector = document.querySelector("#mask") let maskImagePreview = document.querySelector("#mask_preview") let turboField = document.querySelector('#turbo') let useCPUField = document.querySelector('#use_cpu') let useFullPrecisionField = document.querySelector('#use_full_precision') let saveToDiskField = document.querySelector('#save_to_disk') let diskPathField = document.querySelector('#diskPath') // let allowNSFWField = document.querySelector("#allow_nsfw") let useBetaChannelField = document.querySelector("#use_beta_channel") let promptStrengthSlider = document.querySelector('#prompt_strength_slider') let promptStrengthField = document.querySelector('#prompt_strength') let samplerField = document.querySelector('#sampler') let samplerSelectionContainer = document.querySelector("#samplerSelection") let useFaceCorrectionField = document.querySelector("#use_face_correction") let useUpscalingField = document.querySelector("#use_upscale") let upscaleModelField = document.querySelector("#upscale_model") let stableDiffusionModelField = document.querySelector('#stable_diffusion_model') let outputFormatField = document.querySelector('#output_format') let showOnlyFilteredImageField = document.querySelector("#show_only_filtered_image") let updateBranchLabel = document.querySelector("#updateBranchLabel") let streamImageProgressField = document.querySelector("#stream_image_progress") let makeImageBtn = document.querySelector('#makeImage') let stopImageBtn = document.querySelector('#stopImage') let imagesContainer = document.querySelector('#current-images') let initImagePreviewContainer = document.querySelector('#init_image_preview_container') let initImageClearBtn = document.querySelector('.init_image_clear') let promptStrengthContainer = document.querySelector('#prompt_strength_container') let initialText = document.querySelector("#initial-text") let previewTools = document.querySelector("#preview-tools") let clearAllPreviewsBtn = document.querySelector("#clear-all-previews") // let maskSetting = document.querySelector('#editor-inputs-mask_setting') // let maskImagePreviewContainer = document.querySelector('#mask_preview_container') // let maskImageClearBtn = document.querySelector('#mask_clear') let maskSetting = document.querySelector('#enable_mask') let editorModifierEntries = document.querySelector('#editor-modifiers-entries') let editorModifierTagsList = document.querySelector('#editor-inputs-tags-list') let editorTagsContainer = document.querySelector('#editor-inputs-tags-container') let imagePreview = document.querySelector("#preview") let previewImageField = document.querySelector('#preview-image') previewImageField.onchange = () => changePreviewImages(previewImageField.value) let modifierCardSizeSlider = document.querySelector('#modifier-card-size-slider') modifierCardSizeSlider.onchange = () => resizeModifierCards(modifierCardSizeSlider.value) // let previewPrompt = document.querySelector('#preview-prompt') let showConfigToggle = document.querySelector('#configToggleBtn') // let configBox = document.querySelector('#config') // let outputMsg = document.querySelector('#outputMsg') // let progressBar = document.querySelector("#progressBar") let soundToggle = document.querySelector('#sound_toggle') let serverStatusColor = document.querySelector('#server-status-color') let serverStatusMsg = document.querySelector('#server-status-msg') let advancedPanelHandle = document.querySelector("#editor-settings .collapsible") let modifiersPanelHandle = document.querySelector("#editor-modifiers .collapsible") let inpaintingEditorContainer = document.querySelector('#inpaintingEditor') let inpaintingEditor = new DrawingBoard.Board('inpaintingEditor', { color: "#ffffff", background: false, size: 30, webStorage: false, controls: [{'DrawingMode': {'filler': false}}, 'Size', 'Navigation'] }) let inpaintingEditorCanvasBackground = document.querySelector('.drawing-board-canvas-wrapper') document.querySelector('.drawing-board-control-navigation-back').innerHTML = '' document.querySelector('.drawing-board-control-navigation-forward').innerHTML = '' let maskResetButton = document.querySelector('.drawing-board-control-navigation-reset') maskResetButton.innerHTML = 'Clear' maskResetButton.style.fontWeight = 'normal' maskResetButton.style.fontSize = '10pt' let serverState = {'status': 'Offline'} let activeTags = [] let modifiers = [] let lastPromptUsed = '' let bellPending = false let taskQueue = [] let currentTask = null const modifierThumbnailPath = 'media/modifier-thumbnails' const activeCardClass = 'modifier-card-active' function getLocalStorageItem(key, fallback) { let item = localStorage.getItem(key) if (item === null) { return fallback } return item } function getLocalStorageBoolItem(key, fallback) { let item = localStorage.getItem(key) if (item === null) { return fallback } return (item === 'true' ? true : false) } function handleBoolSettingChange(key) { return function(e) { localStorage.setItem(key, e.target.checked.toString()) } } function handleStringSettingChange(key) { return function(e) { localStorage.setItem(key, e.target.value.toString()) } } function isSoundEnabled() { return getLocalStorageBoolItem(SOUND_ENABLED_KEY, true) } function isFaceCorrectionEnabled() { return getLocalStorageBoolItem(USE_FACE_CORRECTION_KEY, false) } function isUpscalingEnabled() { return getLocalStorageBoolItem(USE_UPSCALING_KEY, false) } function isShowOnlyFilteredImageEnabled() { return getLocalStorageBoolItem(SHOW_ONLY_FILTERED_IMAGE_KEY, true) } function isSaveToDiskEnabled() { return getLocalStorageBoolItem(SAVE_TO_DISK_KEY, false) } function isUseCPUEnabled() { return getLocalStorageBoolItem(USE_CPU_KEY, false) } function isUseFullPrecisionEnabled() { return getLocalStorageBoolItem(USE_FULL_PRECISION_KEY, false) } function isUseTurboModeEnabled() { return getLocalStorageBoolItem(USE_TURBO_MODE_KEY, true) } function getSavedDiskPath() { return getLocalStorageItem(DISK_PATH_KEY, '') } function isAdvancedPanelOpenEnabled() { return getLocalStorageBoolItem(ADVANCED_PANEL_OPEN_KEY, false) } function isModifiersPanelOpenEnabled() { return getLocalStorageBoolItem(MODIFIERS_PANEL_OPEN_KEY, false) } function isStreamImageProgressEnabled() { return getLocalStorageBoolItem(STREAM_IMAGE_PROGRESS_KEY, false) } function getOutputFormat() { return getLocalStorageItem(OUTPUT_FORMAT_KEY, 'jpeg') } function setStatus(statusType, msg, msgType) { } function setServerStatus(msgType, msg) { switch(msgType) { case 'online': serverStatusColor.style.color = 'green' serverStatusMsg.style.color = 'green' serverStatusMsg.innerText = 'Stable Diffusion is ' + msg break case 'busy': serverStatusColor.style.color = 'yellow' serverStatusMsg.style.color = 'yellow' serverStatusMsg.innerText = 'Stable Diffusion is ' + msg break case 'error': serverStatusColor.style.color = 'red' serverStatusMsg.style.color = 'red' serverStatusMsg.innerText = 'Stable Diffusion has stopped' break } } function isServerAvailable() { if (typeof serverState !== 'object') { return false } switch (serverState.status) { case 'LoadingModel': case 'Rendering': case 'Online': return true default: return false } } function logMsg(msg, level, outputMsg) { if (outputMsg.hasChildNodes()) { outputMsg.appendChild(document.createElement('br')) } if (level === 'error') { outputMsg.innerHTML += 'Error: ' + msg + '' } else if (level === 'warn') { outputMsg.innerHTML += 'Warning: ' + msg + '' } else { outputMsg.innerText += msg } console.log(level, msg) } function logError(msg, res, outputMsg) { logMsg(msg, 'error', outputMsg) console.log('request error', res) setStatus('request', 'error', 'error') } function asyncDelay(timeout) { return new Promise(function(resolve, reject) { setTimeout(resolve, timeout, true) }) } function playSound() { const audio = new Audio('/media/ding.mp3') audio.volume = 0.2 audio.play() } async function healthCheck() { try { let res = undefined if (sessionId) { res = await fetch('/ping?session_id=' + sessionId) } else { res = await fetch('/ping') } serverState = await res.json() // Set status switch(serverState.status) { case 'Init': // Wait for init to complete before updating status. break case 'Online': setServerStatus('online', 'ready') break case 'LoadingModel': setServerStatus('busy', 'loading model') break case 'Rendering': setServerStatus('busy', 'rendering') break default: // Unavailable setServerStatus('error', serverState.status.toLowerCase()) break } serverState.time = Date.now() } catch (e) { setServerStatus('error', 'offline') } } function resizeInpaintingEditor() { if (!maskSetting.checked) { return } let widthValue = parseInt(widthField.value) let heightValue = parseInt(heightField.value) if (widthValue === heightValue) { widthValue = INPAINTING_EDITOR_SIZE heightValue = INPAINTING_EDITOR_SIZE } else if (widthValue > heightValue) { heightValue = (heightValue / widthValue) * INPAINTING_EDITOR_SIZE widthValue = INPAINTING_EDITOR_SIZE } else { widthValue = (widthValue / heightValue) * INPAINTING_EDITOR_SIZE heightValue = INPAINTING_EDITOR_SIZE } if (inpaintingEditor.opts.aspectRatio === (widthValue / heightValue).toFixed(3)) { // Same ratio, don't reset the canvas. return } inpaintingEditor.opts.aspectRatio = (widthValue / heightValue).toFixed(3) inpaintingEditorContainer.style.width = widthValue + 'px' inpaintingEditorContainer.style.height = heightValue + 'px' inpaintingEditor.opts.enlargeYourContainer = true inpaintingEditor.opts.size = inpaintingEditor.ctx.lineWidth inpaintingEditor.resize() inpaintingEditor.ctx.lineCap = "round" inpaintingEditor.ctx.lineJoin = "round" inpaintingEditor.ctx.lineWidth = inpaintingEditor.opts.size inpaintingEditor.setColor(inpaintingEditor.opts.color) } function showImages(reqBody, res, outputContainer, livePreview) { let imageItemElements = outputContainer.querySelectorAll('.imgItem') if(typeof res != 'object') return res.output.reverse() res.output.forEach((result, index) => { const imageData = result?.data || result?.path + '?t=' + Date.now() const imageWidth = reqBody.width const imageHeight = reqBody.height if (!imageData.includes('/')) { // res contained no data for the image, stop execution setStatus('request', 'invalid image', 'error') return } let imageItemElem = (index < imageItemElements.length ? imageItemElements[index] : null) if(!imageItemElem) { imageItemElem = document.createElement('div') imageItemElem.className = 'imgItem' imageItemElem.innerHTML = `
StepUpdate: ${JSON.stringify(stepUpdate, undefined, 4)}` } logError(msg, res, outputMsg) return false } if (typeof stepUpdate !== 'object' || !res || res.status != 200) { if (!isServerAvailable()) { logError("Stable Diffusion is still starting up, please wait. If this goes on beyond a few minutes, Stable Diffusion has probably crashed. Please check the error message in the command-line window.", res, outputMsg) } else if (typeof res === 'object') { let msg = 'Stable Diffusion had an error reading the response: ' try { // 'Response': body stream already read msg += 'Read: ' + await res.text() } catch(e) { msg += 'Unexpected end of stream. ' } if (finalJSON) { msg += 'Buffered data: ' + finalJSON } logError(msg, res, outputMsg) } else { let msg = `Unexpected Read Error:
Response: ${res}` logError(msg, res, outputMsg) } progressBar.style.display = 'none' return false } lastPromptUsed = reqBody['prompt'] showImages(reqBody, stepUpdate, outputContainer, false) } catch (e) { console.log('request error', e) logError('Stable Diffusion had an error. Please check the logs in the command-line window.
StepUpdate: ${typeof stepUpdate === 'object' ? JSON.stringify(stepUpdate, undefined, 4) : stepUpdate}
' + e.stack + '', res, outputMsg) setStatus('request', 'error', 'error') progressBar.style.display = 'none' return false } return true } async function checkTasks() { if (taskQueue.length === 0) { setStatus('request', 'done', 'success') setTimeout(checkTasks, 500) stopImageBtn.style.display = 'none' makeImageBtn.innerHTML = 'Make Image' currentTask = null if (bellPending) { if (isSoundEnabled()) { playSound() } bellPending = false } return } setStatus('request', 'fetching..') stopImageBtn.style.display = 'block' makeImageBtn.innerHTML = 'Enqueue Next Image' bellPending = true previewTools.style.display = 'block' let task = taskQueue.pop() currentTask = task let time = Date.now() let successCount = 0 task.isProcessing = true task['stopTask'].innerHTML = ' Stop' task['taskStatusLabel'].innerText = "Starting" task['taskStatusLabel'].classList.add('waitingTaskLabel') const genSeeds = Boolean(typeof task.reqBody.seed !== 'number' || (task.reqBody.seed === task.seed && task.numOutputsTotal > 1)) const startSeed = task.reqBody.seed || task.seed for (let i = 0; i < task.batchCount; i++) { let newTask = task; if (task.batchCount > 1) { // Each output render batch needs it's own task instance to avoid altering the other runs after they are completed. newTask = Object.assign({}, task, { reqBody: Object.assign({}, task.reqBody) }) } if (genSeeds) { newTask.reqBody.seed = startSeed + (i * newTask.reqBody.num_outputs) newTask.seed = newTask.reqBody.seed } else if (newTask.seed !== newTask.reqBody.seed) { newTask.seed = newTask.reqBody.seed } let success = await doMakeImage(newTask) task.batchesDone++ if (!task.isProcessing || !success) { break } if (success) { successCount++ } } task.isProcessing = false task['stopTask'].innerHTML = ' Remove' task['taskStatusLabel'].style.display = 'none' time = Date.now() - time time /= 1000 if (successCount === task.batchCount) { task.outputMsg.innerText = 'Processed ' + task.numOutputsTotal + ' images in ' + time + ' seconds' // setStatus('request', 'done', 'success') } else { if (task.outputMsg.innerText.toLowerCase().indexOf('error') === -1) { task.outputMsg.innerText = 'Task ended after ' + time + ' seconds' } } if (randomSeedField.checked) { seedField.value = task.seed } currentTask = null if (typeof requestIdleCallback === 'function') { requestIdleCallback(checkTasks, { timeout: 30 * 1000 }) } else { setTimeout(checkTasks, 500) } } if (typeof requestIdleCallback === 'function') { requestIdleCallback(checkTasks, { timeout: 30 * 1000 }) } else { setTimeout(checkTasks, 10) } function getCurrentUserRequest() { const numOutputsTotal = parseInt(numOutputsTotalField.value) const numOutputsParallel = parseInt(numOutputsParallelField.value) const seed = (randomSeedField.checked ? Math.floor(Math.random() * 10000000) : parseInt(seedField.value)) const newTask = { isProcessing: false, stopped: false, batchesDone: 0, numOutputsTotal: numOutputsTotal, batchCount: Math.ceil(numOutputsTotal / numOutputsParallel), seed, reqBody: { session_id: sessionId, seed, negative_prompt: negativePromptField.value.trim(), num_outputs: numOutputsParallel, num_inference_steps: numInferenceStepsField.value, guidance_scale: guidanceScaleField.value, width: widthField.value, height: heightField.value, // allow_nsfw: allowNSFWField.checked, turbo: turboField.checked, use_cpu: useCPUField.checked, use_full_precision: useFullPrecisionField.checked, use_stable_diffusion_model: stableDiffusionModelField.value, stream_progress_updates: true, stream_image_progress: (numOutputsTotal > 50 ? false : streamImageProgressField.checked), show_only_filtered_image: showOnlyFilteredImageField.checked, output_format: outputFormatField.value } } if (IMAGE_REGEX.test(initImagePreview.src)) { newTask.reqBody.init_image = initImagePreview.src newTask.reqBody.prompt_strength = promptStrengthField.value // if (IMAGE_REGEX.test(maskImagePreview.src)) { // newTask.reqBody.mask = maskImagePreview.src // } if (maskSetting.checked) { newTask.reqBody.mask = inpaintingEditor.getImg() } newTask.reqBody.sampler = 'ddim' } else { newTask.reqBody.sampler = samplerField.value } if (saveToDiskField.checked && diskPathField.value.trim() !== '') { newTask.reqBody.save_to_disk_path = diskPathField.value.trim() } if (useFaceCorrectionField.checked) { newTask.reqBody.use_face_correction = 'GFPGANv1.3' } if (useUpscalingField.checked) { newTask.reqBody.use_upscale = upscaleModelField.value } return newTask } function makeImage() { if (!isServerAvailable()) { alert('The server is not available.') return } const taskTemplate = getCurrentUserRequest() const newTaskRequests = [] getPrompts().forEach((prompt) => newTaskRequests.push(Object.assign({}, taskTemplate, { reqBody: Object.assign({ prompt: prompt }, taskTemplate.reqBody) }))) newTaskRequests.forEach(createTask) initialText.style.display = 'none' } function createTask(task) { let taskConfig = `Seed: ${task.seed}, Sampler: ${task.reqBody.sampler}, Inference Steps: ${task.reqBody.num_inference_steps}, Guidance Scale: ${task.reqBody.guidance_scale}, Model: ${task.reqBody.use_stable_diffusion_model}` if (negativePromptField.value.trim() !== '') { taskConfig += `, Negative Prompt: ${task.reqBody.negative_prompt}` } if (task.reqBody.init_image !== undefined) { taskConfig += `, Prompt Strength: ${task.reqBody.prompt_strength}` } if (task.reqBody.use_face_correction) { taskConfig += `, Fix Faces: ${task.reqBody.use_face_correction}` } if (task.reqBody.use_upscale) { taskConfig += `, Upscale: ${task.reqBody.use_upscale}` } let taskEntry = document.createElement('div') taskEntry.className = 'imageTaskContainer' taskEntry.innerHTML = `