Merge pull request #39 from Kalekki/model_switching

Model switching, samplers from api

Former-commit-id: 122e518141f05bc6f139f23bf259fe40ca815cb7
This commit is contained in:
tim h 2022-11-23 14:45:15 -06:00 committed by GitHub
commit 8e3ef9162e
2 changed files with 106 additions and 22 deletions

View file

@ -30,28 +30,13 @@
<!-- SD section -->
<button type="button" class="collapsible">Stable Diffusion settings</button>
<div class="content">
<label for="models">Model:</label>
<select id="models" onchange="changeModel()"></select><br>
<label for="samplerSelect">Sampler:</label>
<select id="samplerSelect" onchange="changeSampler()"></select><br />
<label for="seed">Seed (-1 for random):</label> <br>
<input type="number" id="seed" onchange="changeSeed()" min="1" max="9999999999" value="-1"
step="1" /><br />
<label for="samplerSelect">Sampler:</label>
<select id="samplerSelect" onchange="changeSampler()">
<option value="DDIM">DDIM</option>
<option value="Euler a">Euler A</option>
<option value="Euler">Euler</option>
<option value="LMS">LMS</option>
<option value="Heun">Heun</option>
<option value="DPM2">DPM2</option>
<option value="DPM2 a">DPM2a</option>
<option value="DPM++ 2S a">DPM++2Sa</option>
<option value="DPM++ 2m">DPM++2m</option>
<option value="DPM fast">DPM fast</option>
<option value="DPM adaptive">DPM adaptive</option>
<option value="LMS Karras">LMS Karras</option>
<option value="DPM2 Karras">DPM2 Karras</option>
<option value="DPM2 a Karras">DPM2a Karras</option>
<option value="DPM++ 2S a Karras">DPM++2Sa Karras</option>
<option value="DPM++ 2M Karras">DPM++2M Karras</option>
</select><br />
<label for="steps">Steps: <input type="number" id="stepsTxt"></label><br />
<input type="range" id="steps" name="steps" min="1" max="50" /><br />
<label for="cfgScale">CFG scale: <input type="number" id="cfgScaleTxt"></label>

View file

@ -155,7 +155,9 @@ const bgCtx = bgCanvas.getContext("2d");
function startup() {
checkIfWebuiIsRunning();
loadSettings();
getSamplers();
getUpscalers();
getModels();
drawBackground();
changeScaleFactor();
changePaintMode();
@ -782,10 +784,14 @@ function changeEnableErasing() {
}
function changeSampler() {
if (!document.getElementById("samplerSelect").value == "") {
// must be done, since before getSamplers is done, the options are empty
console.log(document.getElementById("samplerSelect").value == "");
stableDiffusionData.sampler_index =
document.getElementById("samplerSelect").value;
localStorage.setItem("sampler", stableDiffusionData.sampler_index);
}
}
const changeCfgScale = sliderChangeHandlerFactory(
"cfgScale",
@ -1070,6 +1076,99 @@ function getUpscalers() {
*/
}
async function getModels() {
var modelSelect = document.getElementById("models");
var url = document.getElementById("host").value + "/sdapi/v1/sd-models";
await fetch(url)
.then((response) => response.json())
.then((data) => {
//console.log(data); All models
for (var i = 0; i < data.length; i++) {
var option = document.createElement("option");
option.text = data[i].model_name;
option.value = data[i].title;
modelSelect.add(option);
}
});
/* To get the current model, we might need to call /config/ which returns a json file with EVERYTHING from the webui, 25k lines of json... i havent figured out any other way to get the model thats loaded
response >> components >> second component(quicksettings with checkpoint chooser as default) >> value = the current model
The current model we get only updates on full restart of WebUI, so if we change the model, and then refresh the page, it will still show the old model.
We could just not show the current model, but i think it would be nice to show it.
*/
await fetch(document.getElementById("host").value + "/config/")
.then((response) => response.json())
.then((data) => {
//console.log(data)
var model = data.components[1].props.value;
console.log("Current model: " + model);
modelSelect.value = model;
});
}
function changeModel() {
// change the model
console.log("changing model to " + document.getElementById("models").value);
var model_title = document.getElementById("models").value;
var payload = {
sd_model_checkpoint: model_title,
};
var url = document.getElementById("host").value + "/sdapi/v1/options/";
fetch(url, {
method: "POST",
mode: "cors", // no-cors, *cors, same-origin
cache: "default", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, *same-origin, omit
redirect: "follow", // manual, *follow, error
referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
})
.then((response) => response.json())
.then(() => {
alert("Model changed to " + model_title);
})
.catch((error) => {
alert(
"Error changing model, please check console for additional info\n" +
error
);
});
}
function getSamplers() {
var samplerSelect = document.getElementById("samplerSelect");
var url = document.getElementById("host").value + "/sdapi/v1/samplers";
fetch(url)
.then((response) => response.json())
.then((data) => {
//console.log(data); All samplers
for (var i = 0; i < data.length; i++) {
// PLMS SAMPLER DOES NOT WORK FOR ANY IMAGES BEYOND FOR THE INITIAL IMAGE (for me at least), GIVES ASGI Exception; AttributeError: 'PLMSSampler' object has no attribute 'stochastic_encode'
var option = document.createElement("option");
option.text = data[i].name;
option.value = data[i].name;
samplerSelect.add(option);
}
if (localStorage.getItem("sampler") != null) {
samplerSelect.value = localStorage.getItem("sampler");
} else {
// needed now, as hardcoded sampler cant be guaranteed to be in the list
samplerSelect.value = data[0].name;
localStorage.setItem("sampler", samplerSelect.value);
}
})
.catch((error) => {
alert(
"Error getting samplers, please check console for additional info\n" +
error
);
});
}
async function upscaleAndDownload() {
// Future improvements: some upscalers take a while to upscale, so we should show a loading bar or something, also a slider for the upscale amount