Download workflow.js from wr92-course/object-detection-lab: direct link, hf CLI and curl.
- Browser
- Download file 5.28 kB
-
https://huggingface.co/spaces/wr92-course/object-detection-lab/resolve/main/workflow.js
- Command line
-
hf download hf://spaces/wr92-course/object-detection-lab/workflow.js
-
curl -L -o workflow.js https://huggingface.co/spaces/wr92-course/object-detection-lab/resolve/main/workflow.js
5.28 kB
| /* 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(); | |
| } | |
| }); | |