2022-08-29 16:47:45 +00:00
|
|
|
function toBase64(file) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const r = new FileReader();
|
|
|
|
r.readAsDataURL(file);
|
|
|
|
r.onload = () => resolve(r.result);
|
|
|
|
r.onerror = (error) => reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function appendOutput(src, seed, config) {
|
2022-09-05 14:31:59 +00:00
|
|
|
let outputNode = document.createElement("figure");
|
2022-08-29 16:47:45 +00:00
|
|
|
let altText = seed.toString() + " | " + config.prompt;
|
2022-09-05 14:31:59 +00:00
|
|
|
|
|
|
|
const figureContents = `
|
|
|
|
<a href="${src}" target="_blank">
|
|
|
|
<img src="${src}" alt="${altText}" title="${altText}">
|
|
|
|
</a>
|
|
|
|
<figcaption>${seed}</figcaption>
|
|
|
|
`;
|
|
|
|
|
|
|
|
outputNode.innerHTML = figureContents;
|
|
|
|
let figcaption = outputNode.querySelector('figcaption')
|
2022-08-29 16:47:45 +00:00
|
|
|
|
|
|
|
// Reload image config
|
2022-09-05 14:31:59 +00:00
|
|
|
figcaption.addEventListener('click', () => {
|
2022-08-29 16:47:45 +00:00
|
|
|
let form = document.querySelector("#generate-form");
|
|
|
|
for (const [k, v] of new FormData(form)) {
|
2022-09-03 18:10:31 +00:00
|
|
|
if (k == 'initimg') { continue; }
|
|
|
|
form.querySelector(`*[name=${k}]`).value = config[k];
|
2022-08-29 16:47:45 +00:00
|
|
|
}
|
|
|
|
document.querySelector("#seed").value = seed;
|
|
|
|
|
|
|
|
saveFields(document.querySelector("#generate-form"));
|
|
|
|
});
|
|
|
|
|
|
|
|
document.querySelector("#results").prepend(outputNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveFields(form) {
|
|
|
|
for (const [k, v] of new FormData(form)) {
|
|
|
|
if (typeof v !== 'object') { // Don't save 'file' type
|
|
|
|
localStorage.setItem(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadFields(form) {
|
|
|
|
for (const [k, v] of new FormData(form)) {
|
|
|
|
const item = localStorage.getItem(k);
|
|
|
|
if (item != null) {
|
|
|
|
form.querySelector(`*[name=${k}]`).value = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearFields(form) {
|
2022-08-29 15:26:14 +00:00
|
|
|
localStorage.clear();
|
|
|
|
let prompt = form.prompt.value;
|
|
|
|
form.reset();
|
|
|
|
form.prompt.value = prompt;
|
2022-08-29 16:47:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 16:36:48 +00:00
|
|
|
const BLANK_IMAGE_URL = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"/>';
|
2022-08-29 16:47:45 +00:00
|
|
|
async function generateSubmit(form) {
|
|
|
|
const prompt = document.querySelector("#prompt").value;
|
|
|
|
|
|
|
|
// Convert file data to base64
|
|
|
|
let formData = Object.fromEntries(new FormData(form));
|
2022-09-03 18:10:31 +00:00
|
|
|
formData.initimg_name = formData.initimg.name
|
2022-08-29 16:47:45 +00:00
|
|
|
formData.initimg = formData.initimg.name !== '' ? await toBase64(formData.initimg) : null;
|
|
|
|
|
2022-08-31 15:28:11 +00:00
|
|
|
let strength = formData.strength;
|
|
|
|
let totalSteps = formData.initimg ? Math.floor(strength * formData.steps) : formData.steps;
|
2022-08-30 19:36:12 +00:00
|
|
|
|
2022-08-29 16:36:48 +00:00
|
|
|
let progressSectionEle = document.querySelector('#progress-section');
|
|
|
|
progressSectionEle.style.display = 'initial';
|
|
|
|
let progressEle = document.querySelector('#progress-bar');
|
2022-08-30 19:36:12 +00:00
|
|
|
progressEle.setAttribute('max', totalSteps);
|
2022-08-29 16:36:48 +00:00
|
|
|
let progressImageEle = document.querySelector('#progress-image');
|
|
|
|
progressImageEle.src = BLANK_IMAGE_URL;
|
|
|
|
|
|
|
|
progressImageEle.style.display = {}.hasOwnProperty.call(formData, 'progress_images') ? 'initial': 'none';
|
2022-08-29 16:47:45 +00:00
|
|
|
|
|
|
|
// Post as JSON, using Fetch streaming to get results
|
|
|
|
fetch(form.action, {
|
|
|
|
method: form.method,
|
|
|
|
body: JSON.stringify(formData),
|
|
|
|
}).then(async (response) => {
|
|
|
|
const reader = response.body.getReader();
|
|
|
|
|
|
|
|
let noOutputs = true;
|
|
|
|
while (true) {
|
|
|
|
let {value, done} = await reader.read();
|
|
|
|
value = new TextDecoder().decode(value);
|
2022-08-29 16:36:48 +00:00
|
|
|
if (done) {
|
|
|
|
progressSectionEle.style.display = 'none';
|
|
|
|
break;
|
|
|
|
}
|
2022-08-29 16:47:45 +00:00
|
|
|
|
|
|
|
for (let event of value.split('\n').filter(e => e !== '')) {
|
|
|
|
const data = JSON.parse(event);
|
|
|
|
|
2022-08-30 15:55:40 +00:00
|
|
|
if (data.event === 'result') {
|
2022-08-29 16:47:45 +00:00
|
|
|
noOutputs = false;
|
|
|
|
document.querySelector("#no-results-message")?.remove();
|
2022-08-31 04:33:42 +00:00
|
|
|
appendOutput(data.url, data.seed, data.config);
|
2022-08-29 16:36:48 +00:00
|
|
|
progressEle.setAttribute('value', 0);
|
2022-08-30 19:36:12 +00:00
|
|
|
progressEle.setAttribute('max', totalSteps);
|
2022-08-30 15:55:40 +00:00
|
|
|
} else if (data.event === 'upscaling-started') {
|
2022-08-29 16:36:48 +00:00
|
|
|
document.getElementById("processing_cnt").textContent=data.processed_file_cnt;
|
|
|
|
document.getElementById("scaling-inprocess-message").style.display = "block";
|
2022-08-30 15:55:40 +00:00
|
|
|
} else if (data.event === 'upscaling-done') {
|
2022-08-29 16:36:48 +00:00
|
|
|
document.getElementById("scaling-inprocess-message").style.display = "none";
|
2022-08-30 15:55:40 +00:00
|
|
|
} else if (data.event === 'step') {
|
2022-08-29 16:36:48 +00:00
|
|
|
progressEle.setAttribute('value', data.step);
|
|
|
|
if (data.url) {
|
|
|
|
progressImageEle.src = data.url;
|
|
|
|
}
|
2022-08-30 15:55:40 +00:00
|
|
|
} else if (data.event === 'canceled') {
|
|
|
|
// avoid alerting as if this were an error case
|
|
|
|
noOutputs = false;
|
2022-08-29 16:47:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-enable form, remove no-results-message
|
|
|
|
form.querySelector('fieldset').removeAttribute('disabled');
|
|
|
|
document.querySelector("#prompt").value = prompt;
|
|
|
|
document.querySelector('progress').setAttribute('value', '0');
|
|
|
|
|
|
|
|
if (noOutputs) {
|
|
|
|
alert("Error occurred while generating.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Disable form while generating
|
|
|
|
form.querySelector('fieldset').setAttribute('disabled','');
|
|
|
|
document.querySelector("#prompt").value = `Generating: "${prompt}"`;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.onload = () => {
|
|
|
|
document.querySelector("#generate-form").addEventListener('submit', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const form = e.target;
|
|
|
|
|
|
|
|
generateSubmit(form);
|
|
|
|
});
|
|
|
|
document.querySelector("#generate-form").addEventListener('change', (e) => {
|
|
|
|
saveFields(e.target.form);
|
|
|
|
});
|
2022-08-29 15:26:14 +00:00
|
|
|
document.querySelector("#reset-seed").addEventListener('click', (e) => {
|
2022-08-29 16:47:45 +00:00
|
|
|
document.querySelector("#seed").value = -1;
|
|
|
|
saveFields(e.target.form);
|
|
|
|
});
|
|
|
|
document.querySelector("#reset-all").addEventListener('click', (e) => {
|
|
|
|
clearFields(e.target.form);
|
|
|
|
});
|
2022-09-05 14:05:39 +00:00
|
|
|
document.querySelector("#remove-image").addEventListener('click', (e) => {
|
|
|
|
initimg.value=null;
|
|
|
|
});
|
2022-08-29 16:47:45 +00:00
|
|
|
loadFields(document.querySelector("#generate-form"));
|
2022-08-29 15:53:27 +00:00
|
|
|
|
2022-08-30 15:55:40 +00:00
|
|
|
document.querySelector('#cancel-button').addEventListener('click', () => {
|
|
|
|
fetch('/cancel').catch(e => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-29 15:53:27 +00:00
|
|
|
if (!config.gfpgan_model_exists) {
|
|
|
|
document.querySelector("#gfpgan").style.display = 'none';
|
|
|
|
}
|
2022-08-29 16:47:45 +00:00
|
|
|
};
|