File size: 5,283 Bytes
99fdf40 29d301f 99fdf40 29d301f 99fdf40 29d301f 99fdf40 29d301f 99fdf40 29d301f 99fdf40 29d301f 99fdf40 29d301f 99fdf40 29d301f 99fdf40 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | /* Optional ZeroGPU workflow. Photo pixels stay local; only editable text is sent. */
import { conceptPrompt, detectedConcepts } from "./concepts.mjs";
const promptBox = document.getElementById("flow-prompt");
const conceptsBox = document.getElementById("flow-concepts");
const modelSelect = document.getElementById("flow-model");
const generateButton = document.getElementById("flow-generate");
const status = document.getElementById("flow-status");
const image = document.getElementById("flow-image");
const receipt = document.getElementById("flow-receipt");
const openLink = document.getElementById("flow-open");
const endpoints = Object.freeze({
sdxl: { name: "miyuki17/creative-technology-image-generation", seed: 2026, steps: 2,
url: "https://huggingface.co/spaces/miyuki17/creative-technology-image-generation" },
sana: { name: "miyuki17/creative-technology-sana-sprint", seed: 2026, steps: 1,
url: "https://huggingface.co/spaces/miyuki17/creative-technology-sana-sprint" },
});
const messages = {
waiting: ["Run object detection above to prepare your first prompt.", "请先在上方运行目标检测,生成第一条提示词。"],
none: ["No objects passed the threshold. Try another photo or lower the threshold.", "当前阈值下没有检测到物体。请换图或降低阈值。"],
ready: ["Prompt ready. Edit it, then choose a model and Generate.", "提示词已经准备好。可以先修改,再选择模型生成。"],
running: ["Request sent. The free GPU may take time to start or queue…", "请求已发出。免费 GPU 可能需要启动或排队,请稍候……"],
succeeded: ["New image returned from the selected model. Read the original receipt below.", "已从所选模型收到新图。下方保留模型返回的原始收据。"],
failed: ["The live generator did not return an image. Check the quota, queue or availability; you can open it separately below.", "实时生成没有返回图片。请检查额度、队列或服务状态,也可以单独打开实验室。"],
};
let statusKey = "waiting";
let lastDetections = [];
let busy = false;
function language() { return document.documentElement.lang.startsWith("zh") ? 1 : 0; }
function displayStatus(key) {
statusKey = key;
if (status) status.textContent = messages[key][language()];
}
function updateButton() {
if (!generateButton || !promptBox) return;
generateButton.disabled = busy || !lastDetections.length || !promptBox.value.trim() || promptBox.value.length > 350;
}
function clearResult() {
if (!image || !receipt) return;
image.hidden = true;
image.removeAttribute("src");
receipt.hidden = true;
receipt.textContent = "";
}
export function resetConceptWorkflow() {
lastDetections = [];
if (!promptBox || !conceptsBox) return;
conceptsBox.replaceChildren();
promptBox.value = "";
clearResult();
displayStatus("waiting");
updateButton();
}
export function updateConceptWorkflow(detections) {
if (!promptBox || !conceptsBox) return;
lastDetections = detectedConcepts(detections);
conceptsBox.replaceChildren();
for (const { label, score } of lastDetections) {
const chip = document.createElement("span");
chip.textContent = `${label} · ${Math.round(score * 100)}%`;
conceptsBox.append(chip);
}
promptBox.value = conceptPrompt(detections);
clearResult();
displayStatus(lastDetections.length ? "ready" : "none");
updateButton();
}
function safeImageUrl(file) {
const raw = typeof file === "string" ? file : file?.url;
if (!raw) return null;
try {
const url = new URL(raw);
if (url.protocol !== "https:") return null;
if (url.hostname.endsWith(".hf.space") || url.hostname === "huggingface.co") return url.href;
} catch (_) { /* Invalid provider output. */ }
return null;
}
promptBox?.addEventListener("input", updateButton);
modelSelect?.addEventListener("change", () => {
openLink.href = endpoints[modelSelect.value].url;
});
if (status) new MutationObserver(() => displayStatus(statusKey)).observe(document.documentElement, { attributes:true, attributeFilter:["lang"] });
displayStatus("waiting");
generateButton?.addEventListener("click", async () => {
if (busy || !lastDetections.length || !promptBox.value.trim()) return;
const config = endpoints[modelSelect.value];
const prompt = promptBox.value.trim();
if (!config || prompt.length > 350) return;
busy = true;
updateButton();
clearResult();
displayStatus("running");
try {
// The dynamic import loads on an explicit generation request only.
const { Client } = await import("https://cdn.jsdelivr.net/npm/@gradio/client@2.8.0/+esm");
const app = await Client.connect(config.name);
const result = await app.predict("/generate", {
prompt, seed: config.seed, steps: config.steps,
});
const url = safeImageUrl(result?.data?.[0]);
if (!url || !result?.data?.[1]) throw new Error("Missing image or model receipt");
image.src = url;
image.hidden = false;
receipt.textContent = JSON.stringify(result.data[1], null, 2);
receipt.hidden = false;
displayStatus("succeeded");
} catch (error) {
console.error("WR92 Detect → Create generation failed", error);
displayStatus("failed");
} finally {
busy = false;
updateButton();
}
});
|