Skip to content

Commit 56f155c

Browse files
jroxendalJohan Roxendallstein
authored
added support for parsing run log and displaying images in the frontend init state (#410)
Co-authored-by: Johan Roxendal <[email protected]> Co-authored-by: Lincoln Stein <[email protected]>
1 parent 4168774 commit 56f155c

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

ldm/dream/server.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ def do_GET(self):
3131
'gfpgan_model_exists': gfpgan_model_exists
3232
}
3333
self.wfile.write(bytes("let config = " + json.dumps(config) + ";\n", "utf-8"))
34+
elif self.path == "/run_log.json":
35+
self.send_response(200)
36+
self.send_header("Content-type", "application/json")
37+
self.end_headers()
38+
output = []
39+
40+
log_file = os.path.join(self.outdir, "dream_web_log.txt")
41+
if os.path.exists(log_file):
42+
with open(log_file, "r") as log:
43+
for line in log:
44+
url, config = line.split(": {", maxsplit=1)
45+
config = json.loads("{" + config)
46+
config["url"] = url.lstrip(".")
47+
if os.path.exists(url):
48+
output.append(config)
49+
50+
self.wfile.write(bytes(json.dumps({"run_log": output}), "utf-8"))
3451
elif self.path == "/cancel":
3552
self.canceled.set()
3653
self.send_response(200)

static/dream_web/index.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ header h1 {
8686
cursor: pointer;
8787
}
8888
#results img {
89-
height: 30vh;
9089
border-radius: 5px;
90+
object-fit: cover;
9191
}
9292
#fieldset-config {
9393
line-height:2em;
@@ -138,3 +138,7 @@ label {
138138
background-color: #F5F5F5;
139139
}
140140

141+
#no-results-message:not(:only-child) {
142+
display: none;
143+
}
144+

static/dream_web/index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ function appendOutput(src, seed, config) {
1111
let outputNode = document.createElement("figure");
1212
let altText = seed.toString() + " | " + config.prompt;
1313

14+
// img needs width and height for lazy loading to work
1415
const figureContents = `
1516
<a href="${src}" target="_blank">
16-
<img src="${src}" alt="${altText}" title="${altText}">
17+
<img src="${src}"
18+
alt="${altText}"
19+
title="${altText}"
20+
loading="lazy"
21+
width="256"
22+
height="256">
1723
</a>
1824
<figcaption>${seed}</figcaption>
1925
`;
@@ -117,7 +123,6 @@ async function generateSubmit(form) {
117123

118124
if (data.event === 'result') {
119125
noOutputs = false;
120-
document.querySelector("#no-results-message")?.remove();
121126
appendOutput(data.url, data.seed, data.config);
122127
progressEle.setAttribute('value', 0);
123128
progressEle.setAttribute('max', totalSteps);
@@ -153,7 +158,19 @@ async function generateSubmit(form) {
153158
document.querySelector("#prompt").value = `Generating: "${prompt}"`;
154159
}
155160

156-
window.onload = () => {
161+
async function fetchRunLog() {
162+
try {
163+
let response = await fetch('/run_log.json')
164+
const data = await response.json();
165+
for(let item of data.run_log) {
166+
appendOutput(item.url, item.seed, item);
167+
}
168+
} catch (e) {
169+
console.error(e);
170+
}
171+
}
172+
173+
window.onload = async () => {
157174
document.querySelector("#prompt").addEventListener("keydown", (e) => {
158175
if (e.key === "Enter" && !e.shiftKey) {
159176
const form = e.target.form;
@@ -196,4 +213,5 @@ window.onload = () => {
196213
if (!config.gfpgan_model_exists) {
197214
document.querySelector("#gfpgan").style.display = 'none';
198215
}
216+
await fetchRunLog()
199217
};

0 commit comments

Comments
 (0)