Spaces:
Running on Zero
Running on Zero
Serve outputs/ (not tempfiles) + harden frontend URL extraction & logging
Browse filesBackend: revert to saving outputs to outputs/ and returning FileData(path=...).
On ZeroGPU the @spaces.GPU worker can be torn down right after the call,
deleting tempfiles before the browser fetches their URL -> broken image.
outputs/ is persistent in the app dir and Gradio serves it via file protocol.
Frontend: log the raw predict() result and parsed results to console so we
can see the actual data shape / URL. Normalize result extraction (handle
both [images, seed] and object shapes). Add absURL() to resolve relative
Gradio file paths against the origin, and onerror handlers on the preview
image to surface load failures instead of a silent broken icon.
Co-Authored-By: GLM 5.2 <noreply@zai.org>
- app.py +8 -11
- index.html +26 -3
app.py
CHANGED
|
@@ -219,20 +219,17 @@ def infer(
|
|
| 219 |
except Exception:
|
| 220 |
pass
|
| 221 |
|
| 222 |
-
# Persist outputs and wrap as FileData
|
| 223 |
-
#
|
| 224 |
-
#
|
|
|
|
|
|
|
| 225 |
output_files = []
|
| 226 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 227 |
for idx, img in enumerate(images_pil):
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
img.save(persist_path)
|
| 232 |
-
# Temp copy that Gradio serves via the FileData URL.
|
| 233 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 234 |
-
img.save(tmp.name)
|
| 235 |
-
output_files.append(FileData(path=tmp.name))
|
| 236 |
|
| 237 |
return output_files, seed
|
| 238 |
|
|
|
|
| 219 |
except Exception:
|
| 220 |
pass
|
| 221 |
|
| 222 |
+
# Persist outputs to outputs/ and wrap as FileData. Gradio serves the path
|
| 223 |
+
# via its file protocol (same mechanism the original gr.Gallery filepath
|
| 224 |
+
# used). NOT using tempfiles: on ZeroGPU the @spaces.GPU worker can be torn
|
| 225 |
+
# down right after the call, deleting temp files before the browser fetches
|
| 226 |
+
# the URL -> broken image. outputs/ is persistent in the app dir.
|
| 227 |
output_files = []
|
| 228 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 229 |
for idx, img in enumerate(images_pil):
|
| 230 |
+
out_path = f"{OUTPUT_DIR}/output_{seed}_{idx}_{int(time.time() * 1000)}.png"
|
| 231 |
+
img.save(out_path)
|
| 232 |
+
output_files.append(FileData(path=out_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
return output_files, seed
|
| 235 |
|
index.html
CHANGED
|
@@ -463,11 +463,17 @@ function renderPreview() {
|
|
| 463 |
const img = document.getElementById("preview-img");
|
| 464 |
const empty = document.getElementById("canvas-empty");
|
| 465 |
if (state.activeResult) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
img.src = state.activeResult;
|
| 467 |
img.classList.remove("hidden");
|
| 468 |
empty.classList.add("hidden");
|
| 469 |
const im = new Image();
|
| 470 |
im.onload = () => { document.getElementById("st-size").textContent = `output: ${im.naturalWidth}×${im.naturalHeight}`; };
|
|
|
|
| 471 |
im.src = state.activeResult;
|
| 472 |
} else {
|
| 473 |
img.classList.add("hidden");
|
|
@@ -476,6 +482,16 @@ function renderPreview() {
|
|
| 476 |
}
|
| 477 |
}
|
| 478 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
// ---------- progress ----------
|
| 480 |
function showProgress(show) { document.getElementById("progress-card").classList.toggle("hidden", !show); }
|
| 481 |
function setProgress(stage, frac) {
|
|
@@ -558,12 +574,19 @@ async function generate() {
|
|
| 558 |
// No live step fraction in this client build -> indeterminate bar + eta.
|
| 559 |
setProgress("Generating…", null);
|
| 560 |
const final = await state.app.predict("/infer", payload);
|
| 561 |
-
|
| 562 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
state.lastSeed = seed;
|
| 564 |
document.getElementById("st-seed").textContent = "seed: " + seed;
|
| 565 |
|
| 566 |
-
const newResults = images.map((fd) => ({ url: fd.url, seed }));
|
|
|
|
| 567 |
state.results = newResults.concat(state.results);
|
| 568 |
state.activeResult = newResults[0].url;
|
| 569 |
renderResults();
|
|
|
|
| 463 |
const img = document.getElementById("preview-img");
|
| 464 |
const empty = document.getElementById("canvas-empty");
|
| 465 |
if (state.activeResult) {
|
| 466 |
+
console.log("[renderPreview] loading:", state.activeResult);
|
| 467 |
+
img.onerror = () => {
|
| 468 |
+
console.error("[renderPreview] image FAILED to load:", state.activeResult);
|
| 469 |
+
toast("Image URL failed to load — check console.", "err");
|
| 470 |
+
};
|
| 471 |
img.src = state.activeResult;
|
| 472 |
img.classList.remove("hidden");
|
| 473 |
empty.classList.add("hidden");
|
| 474 |
const im = new Image();
|
| 475 |
im.onload = () => { document.getElementById("st-size").textContent = `output: ${im.naturalWidth}×${im.naturalHeight}`; };
|
| 476 |
+
im.onerror = () => { console.error("[renderPreview] size-probe image FAILED:", state.activeResult); };
|
| 477 |
im.src = state.activeResult;
|
| 478 |
} else {
|
| 479 |
img.classList.add("hidden");
|
|
|
|
| 482 |
}
|
| 483 |
}
|
| 484 |
|
| 485 |
+
// Resolve a possibly-relative Gradio file URL/path to an absolute URL the <img>
|
| 486 |
+
// can load. Gradio may return a path like "/file=outputs/foo.png" or a bare path;
|
| 487 |
+
// prefix with the current origin when there's no scheme.
|
| 488 |
+
function absURL(u) {
|
| 489 |
+
if (!u) return u;
|
| 490 |
+
if (/^https?:\/\//i.test(u) || u.startsWith("data:") || u.startsWith("blob:")) return u;
|
| 491 |
+
if (u.startsWith("http")) return u;
|
| 492 |
+
return window.location.origin + (u.startsWith("/") ? "" : "/") + u;
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
// ---------- progress ----------
|
| 496 |
function showProgress(show) { document.getElementById("progress-card").classList.toggle("hidden", !show); }
|
| 497 |
function setProgress(stage, frac) {
|
|
|
|
| 574 |
// No live step fraction in this client build -> indeterminate bar + eta.
|
| 575 |
setProgress("Generating…", null);
|
| 576 |
const final = await state.app.predict("/infer", payload);
|
| 577 |
+
console.log("[infer] raw predict() result:", JSON.stringify(final, null, 2));
|
| 578 |
+
const data = (final && Array.isArray(final.data)) ? final.data : final;
|
| 579 |
+
// data could be [images, seed] or {images, seed}; normalize.
|
| 580 |
+
let images = null, seed = null;
|
| 581 |
+
if (Array.isArray(data)) { [images, seed] = data; }
|
| 582 |
+
else if (data && typeof data === "object") { images = data.images || data.output || null; seed = data.seed; }
|
| 583 |
+
if (seed == null && Array.isArray(images)) seed = images[0]?.seed;
|
| 584 |
+
if (!images || !images.length) throw new Error("No images returned. (see console for raw result)");
|
| 585 |
state.lastSeed = seed;
|
| 586 |
document.getElementById("st-seed").textContent = "seed: " + seed;
|
| 587 |
|
| 588 |
+
const newResults = images.map((fd) => ({ url: absURL(fd.url || fd.path), fd, seed }));
|
| 589 |
+
console.log("[infer] parsed results:", newResults);
|
| 590 |
state.results = newResults.concat(state.results);
|
| 591 |
state.activeResult = newResults[0].url;
|
| 592 |
renderResults();
|