mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
Merge branch 'main' of https://github.com/TesseractCat/stable-diffusion into TesseractCat-main
This commit is contained in:
commit
79add5f0b6
88
scripts/dream_web.py
Normal file
88
scripts/dream_web.py
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import json
|
||||||
|
import base64
|
||||||
|
import os
|
||||||
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||||
|
|
||||||
|
print("Loading model...")
|
||||||
|
from ldm.simplet2i import T2I
|
||||||
|
model = T2I()
|
||||||
|
|
||||||
|
class DreamServer(BaseHTTPRequestHandler):
|
||||||
|
def do_GET(self):
|
||||||
|
if self.path == "/":
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header("Content-type", "text/html")
|
||||||
|
self.end_headers()
|
||||||
|
with open("./scripts/static/index.html", "rb") as content:
|
||||||
|
self.wfile.write(content.read())
|
||||||
|
elif os.path.exists("." + self.path):
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header("Content-type", "image/png")
|
||||||
|
self.end_headers()
|
||||||
|
with open("." + self.path, "rb") as content:
|
||||||
|
self.wfile.write(content.read())
|
||||||
|
else:
|
||||||
|
self.send_response(404)
|
||||||
|
|
||||||
|
def do_POST(self):
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header("Content-type", "application/json")
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
|
content_length = int(self.headers['Content-Length'])
|
||||||
|
post_data = json.loads(self.rfile.read(content_length))
|
||||||
|
prompt = post_data['prompt']
|
||||||
|
initimg = post_data['initimg']
|
||||||
|
batch = int(post_data['batch'])
|
||||||
|
steps = int(post_data['steps'])
|
||||||
|
width = int(post_data['width'])
|
||||||
|
height = int(post_data['height'])
|
||||||
|
cfgscale = float(post_data['cfgscale'])
|
||||||
|
seed = None if int(post_data['seed']) == -1 else int(post_data['seed'])
|
||||||
|
|
||||||
|
print(f"Request to generate with prompt: {prompt}")
|
||||||
|
|
||||||
|
outputs = []
|
||||||
|
if initimg is None:
|
||||||
|
# Run txt2img
|
||||||
|
outputs = model.txt2img(prompt,
|
||||||
|
batch_size = batch,
|
||||||
|
cfg_scale = cfgscale,
|
||||||
|
width = width,
|
||||||
|
height = height,
|
||||||
|
seed = seed,
|
||||||
|
steps = steps)
|
||||||
|
else:
|
||||||
|
# Decode initimg as base64 to temp file
|
||||||
|
with open("./img2img-tmp.png", "wb") as f:
|
||||||
|
initimg = initimg.split(",")[1] # Ignore mime type
|
||||||
|
f.write(base64.b64decode(initimg))
|
||||||
|
|
||||||
|
# Run img2img
|
||||||
|
outputs = model.img2img(prompt,
|
||||||
|
init_img = "./img2img-tmp.png",
|
||||||
|
batch_size = batch,
|
||||||
|
cfg_scale = cfgscale,
|
||||||
|
seed = seed,
|
||||||
|
steps = steps)
|
||||||
|
# Remove the temp file
|
||||||
|
os.remove("./img2img-tmp.png")
|
||||||
|
|
||||||
|
print(f"Prompt generated with output: {outputs}")
|
||||||
|
|
||||||
|
post_data['initimg'] = '' # Don't send init image back
|
||||||
|
outputs = [x + [post_data] for x in outputs] # Append config to each output
|
||||||
|
result = {'outputs': outputs}
|
||||||
|
self.wfile.write(bytes(json.dumps(result), "utf-8"))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
dream_server = ThreadingHTTPServer(("0.0.0.0", 9090), DreamServer)
|
||||||
|
print("Started Stable Diffusion dream server!")
|
||||||
|
|
||||||
|
try:
|
||||||
|
dream_server.serve_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
|
||||||
|
dream_server.server_close()
|
||||||
|
|
194
scripts/static/index.html
Normal file
194
scripts/static/index.html
Normal file
@ -0,0 +1,194 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Stable Diffusion WebUI</title>
|
||||||
|
<link rel="icon" href="data:,">
|
||||||
|
<style>
|
||||||
|
* {
|
||||||
|
font-family: 'Arial';
|
||||||
|
}
|
||||||
|
#header {
|
||||||
|
text-decoration: dotted underline;
|
||||||
|
}
|
||||||
|
#search {
|
||||||
|
margin-top: 20vh;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
fieldset {
|
||||||
|
border: none;
|
||||||
|
}
|
||||||
|
#prompt {
|
||||||
|
width: 500px;
|
||||||
|
border-radius: 20px 0px 0px 20px;
|
||||||
|
padding: 5px 10px 5px 10px;
|
||||||
|
border: 1px solid black;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
#submit {
|
||||||
|
border-radius: 0px 20px 20px 0px;
|
||||||
|
padding: 5px 10px 5px 10px;
|
||||||
|
border: 1px solid black;
|
||||||
|
}
|
||||||
|
#results {
|
||||||
|
text-align: center;
|
||||||
|
padding-left: 20vw;
|
||||||
|
padding-right: 20vw;
|
||||||
|
padding-top: 10px;
|
||||||
|
}
|
||||||
|
img {
|
||||||
|
cursor: pointer;
|
||||||
|
height: 30vh;
|
||||||
|
border-radius: 5px;
|
||||||
|
margin: 10px;
|
||||||
|
}
|
||||||
|
#generate-config {
|
||||||
|
line-height:2em;
|
||||||
|
}
|
||||||
|
input[type="number"] {
|
||||||
|
width: 60px;
|
||||||
|
}
|
||||||
|
#seed {
|
||||||
|
width: 150px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<script>
|
||||||
|
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(output) {
|
||||||
|
let outputNode = document.createElement("img");
|
||||||
|
outputNode.src = output[0];
|
||||||
|
|
||||||
|
let outputConfig = output[2];
|
||||||
|
let altText = output[1].toString() + " | " + outputConfig.prompt;
|
||||||
|
outputNode.alt = altText;
|
||||||
|
outputNode.title = altText;
|
||||||
|
|
||||||
|
// Reload image config
|
||||||
|
outputNode.addEventListener('click', () => {
|
||||||
|
let form = document.querySelector("#generate-form");
|
||||||
|
for (const [k, v] of new FormData(form)) {
|
||||||
|
form.querySelector(`*[name=${k}]`).value = outputConfig[k];
|
||||||
|
}
|
||||||
|
document.querySelector("#seed").value = output[1];
|
||||||
|
|
||||||
|
saveFields(document.querySelector("#generate-form"));
|
||||||
|
});
|
||||||
|
|
||||||
|
document.querySelector("#results").prepend(outputNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
function appendOutputs(outputs) {
|
||||||
|
for (const output of outputs) {
|
||||||
|
appendOutput(output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function generateSubmit(form) {
|
||||||
|
const prompt = document.querySelector("#prompt").value;
|
||||||
|
|
||||||
|
// Convert file data to base64
|
||||||
|
let formData = Object.fromEntries(new FormData(form));
|
||||||
|
formData.initimg = formData.initimg.name !== '' ? await toBase64(formData.initimg) : null;
|
||||||
|
|
||||||
|
// Post as JSON
|
||||||
|
fetch(form.action, {
|
||||||
|
method: form.method,
|
||||||
|
body: JSON.stringify(formData),
|
||||||
|
}).then(async (result) => {
|
||||||
|
let data = await result.json();
|
||||||
|
|
||||||
|
// Re-enable form, remove no-results-message
|
||||||
|
form.querySelector('fieldset').removeAttribute('disabled');
|
||||||
|
document.querySelector("#prompt").value = prompt;
|
||||||
|
|
||||||
|
if (data.outputs.length != 0) {
|
||||||
|
document.querySelector("#no-results-message")?.remove();
|
||||||
|
appendOutputs(data.outputs);
|
||||||
|
} else {
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
document.querySelector("#reset").addEventListener('click', (e) => {
|
||||||
|
document.querySelector("#seed").value = -1;
|
||||||
|
saveFields(e.target.form);
|
||||||
|
});
|
||||||
|
loadFields(document.querySelector("#generate-form"));
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="search">
|
||||||
|
<h2 id="header">Stable Diffusion</h2>
|
||||||
|
|
||||||
|
<form id="generate-form" method="post" action="#">
|
||||||
|
<fieldset>
|
||||||
|
<input type="text" id="prompt" name="prompt">
|
||||||
|
<input type="submit" id="submit" value="Generate">
|
||||||
|
</fieldset>
|
||||||
|
<fieldset id="generate-config">
|
||||||
|
<label for="batch">Batch Size:</label>
|
||||||
|
<input value="1" type="number" id="batch" name="batch">
|
||||||
|
<label for="steps">Steps:</label>
|
||||||
|
<input value="50" type="number" id="steps" name="steps">
|
||||||
|
<label for="cfgscale">Cfg Scale:</label>
|
||||||
|
<input value="7.5" type="number" id="cfgscale" name="cfgscale" step="any">
|
||||||
|
<span>•</span>
|
||||||
|
<label title="Set to multiple of 64" for="width">Width:</label>
|
||||||
|
<input value="512" type="number" id="width" name="width">
|
||||||
|
<label title="Set to multiple of 64" for="height">Height:</label>
|
||||||
|
<input value="512" type="number" id="height" name="height">
|
||||||
|
<br>
|
||||||
|
<label title="Upload an image to use img2img" for="initimg">Img2Img Init:</label>
|
||||||
|
<input type="file" id="initimg" name="initimg" accept=".jpg, .jpeg, .png">
|
||||||
|
<label title="Set to -1 for random seed" for="seed">Seed:</label>
|
||||||
|
<input value="-1" type="number" id="seed" name="seed">
|
||||||
|
<button type="button" id="reset">↺</button>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<hr style="width: 200px">
|
||||||
|
<div id="results">
|
||||||
|
<div id="no-results-message">
|
||||||
|
<i><p>No results...</p></i>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user