mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
Add bare bones web UI
This commit is contained in:
parent
7ea168227c
commit
0efc3bf780
62
scripts/dream_web.py
Normal file
62
scripts/dream_web.py
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||||
|
|
||||||
|
print("Loading model...")
|
||||||
|
from ldm.simplet2i import T2I
|
||||||
|
model = T2I()
|
||||||
|
|
||||||
|
class DreamServer(BaseHTTPRequestHandler):
|
||||||
|
def do_GET(self):
|
||||||
|
self.send_response(200)
|
||||||
|
if self.path == "/":
|
||||||
|
self.send_header("Content-type", "text/html")
|
||||||
|
self.end_headers()
|
||||||
|
with open("./scripts/static/index.html", "rb") as content:
|
||||||
|
self.wfile.write(content.read())
|
||||||
|
else:
|
||||||
|
self.send_header("Content-type", "image/png")
|
||||||
|
self.end_headers()
|
||||||
|
with open("." + self.path, "rb") as content:
|
||||||
|
self.wfile.write(content.read())
|
||||||
|
|
||||||
|
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']
|
||||||
|
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 data: {post_data}")
|
||||||
|
outputs = model.txt2img(prompt,
|
||||||
|
batch_size = batch,
|
||||||
|
cfg_scale = cfgscale,
|
||||||
|
width = width,
|
||||||
|
height = height,
|
||||||
|
seed = seed,
|
||||||
|
steps = steps);
|
||||||
|
print(f"Prompt generated with output: {outputs}")
|
||||||
|
|
||||||
|
outputs = [x + [prompt] for x in outputs] # Append prompt to each output
|
||||||
|
result = {'outputs': outputs}
|
||||||
|
self.wfile.write(bytes(json.dumps(result), "utf-8"))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
dream_server = HTTPServer(("0.0.0.0", 9090), DreamServer)
|
||||||
|
print("Started Stable Diffusion dream server!")
|
||||||
|
|
||||||
|
try:
|
||||||
|
dream_server.serve_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
|
||||||
|
dream_server.server_close()
|
||||||
|
|
148
scripts/static/index.html
Normal file
148
scripts/static/index.html
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
<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 {
|
||||||
|
height:30vh;
|
||||||
|
border-radius:5px;
|
||||||
|
margin:10px;
|
||||||
|
}
|
||||||
|
input[type="number"] {
|
||||||
|
width:60px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<script>
|
||||||
|
function append_output(output) {
|
||||||
|
let output_node = document.createElement("img");
|
||||||
|
output_node.src = output[0];
|
||||||
|
|
||||||
|
let alt_text = output[1].toString() + " | " + output[2];
|
||||||
|
output_node.alt = alt_text;
|
||||||
|
output_node.title = alt_text;
|
||||||
|
|
||||||
|
document.querySelector("#results").prepend(output_node);
|
||||||
|
}
|
||||||
|
|
||||||
|
function append_outputs(outputs) {
|
||||||
|
for (const output of outputs) {
|
||||||
|
append_output(output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function save_fields(form) {
|
||||||
|
for (const [k, v] of new FormData(form)) {
|
||||||
|
localStorage.setItem(k, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function load_fields(form) {
|
||||||
|
for (const [k, v] of new FormData(form)) {
|
||||||
|
const item = localStorage.getItem(k);
|
||||||
|
if (item != null) {
|
||||||
|
form.querySelector(`*[name=${k}]`).value = item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.onload = () => {
|
||||||
|
document.querySelector("#generate_form").addEventListener('submit', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const form = e.target;
|
||||||
|
|
||||||
|
// Post as JSON
|
||||||
|
fetch(form.action, {
|
||||||
|
method: form.method,
|
||||||
|
body: JSON.stringify(Object.fromEntries(new FormData(form))),
|
||||||
|
}).then((result) => {
|
||||||
|
result.json().then((data) => {
|
||||||
|
// Re-enable form, remove no-results-message
|
||||||
|
form.querySelector('fieldset').removeAttribute('disabled');
|
||||||
|
document.querySelector("#prompt").value = '';
|
||||||
|
save_fields(form);
|
||||||
|
|
||||||
|
if (data.outputs.length != 0) {
|
||||||
|
document.querySelector("#no_results_message")?.remove();
|
||||||
|
append_outputs(data.outputs);
|
||||||
|
} else {
|
||||||
|
alert("Error occurred while generating.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Disable form
|
||||||
|
form.querySelector('fieldset').setAttribute('disabled','');
|
||||||
|
document.querySelector("#prompt").value = 'Generating...';
|
||||||
|
});
|
||||||
|
document.querySelector("#generate_form").addEventListener('change', (e) => {
|
||||||
|
save_fields(e.target.form);
|
||||||
|
});
|
||||||
|
load_fields(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>
|
||||||
|
<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">
|
||||||
|
<span>•</span>
|
||||||
|
<label title="Set to -1 for random seed" for="seed">Seed:</label>
|
||||||
|
<input value="-1" type="number" id="seed" name="seed">
|
||||||
|
</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…
Reference in New Issue
Block a user