Topic6APIG / app.js
miyuki17's picture
feat(course): release public no-GPU image generation student lab
3b5f917 verified
Raw History Blame Contribute Delete
3.14 kB
import { MODEL_SPEC, buildPlan, noiseRGBA } from "./logic.mjs";
const $ = (q) => document.querySelector(q);
const form = $("#lab-form");
const model = $("#model");
const mode = $("#mode");
const seed = $("#seed");
const prompt = $("#prompt");
const alternative = $("#alternative");
const promptFields = $("#prompt-fields");
const altField = $("#alt-field");
const feedback = $("#feedback");
const result = $("#plan-output");
const copy = $("#copy-plan");
let currentPlan = null;
function updateModel() {
const spec = MODEL_SPEC[model.value];
$("#model-description").textContent = spec.notes;
if (!spec.acceptsPrompt) {
mode.value = "seed";
for (const option of mode.options) option.disabled = option.value !== "seed";
promptFields.hidden = true;
} else {
for (const option of mode.options) option.disabled = false;
promptFields.hidden = false;
}
updateMode();
}
function updateMode() {
altField.hidden = mode.value !== "refine";
}
function updateNoise() {
const canvas = $("#noise-canvas");
try {
const ctx = canvas.getContext("2d");
if (!ctx) return;
const pixels = noiseRGBA(canvas.width, canvas.height, seed.value);
ctx.putImageData(new ImageData(pixels, canvas.width, canvas.height), 0, 0);
} catch (_) { /* Invalid user input handled by the form when submitted. */ }
}
function resetResult() {
currentPlan = null;
copy.disabled = true;
result.textContent = "Select parameters, then build a plan.";
updateNoise();
}
model.addEventListener("change", () => { updateModel(); resetResult(); });
mode.addEventListener("change", () => { updateMode(); resetResult(); });
seed.addEventListener("input", resetResult);
prompt.addEventListener("input", resetResult);
alternative.addEventListener("input", resetResult);
$("#next-seed").addEventListener("click", () => {
const n = Number(seed.value);
seed.value = Number.isSafeInteger(n) && n >= 0 && n <= 2147483647
? String((n + 1) % 2147483648) : "2026";
resetResult();
});
for (const button of document.querySelectorAll("[data-prompt]")) {
button.addEventListener("click", () => {prompt.value=button.dataset.prompt;resetResult();});
}
form.addEventListener("submit", (event) => {
event.preventDefault();
try {
currentPlan = buildPlan({
model: model.value, mode: mode.value, seed: seed.value,
prompt: prompt.value, alternative: alternative.value
});
result.textContent = JSON.stringify(currentPlan, null, 2);
feedback.textContent = "Plan built. No inference has run and no GPU time was consumed.";
copy.disabled = false;
} catch (err) {
feedback.textContent = err.message;
result.textContent = "Correct the input to build a plan.";
copy.disabled = true; currentPlan = null;
}
});
copy.addEventListener("click", async () => {
if (!currentPlan) return;
try {
await navigator.clipboard.writeText(JSON.stringify(currentPlan,null,2));
feedback.textContent = "Experiment JSON copied.";
} catch (_) {
feedback.textContent = "Copy is unavailable here. Select the JSON text above to copy it.";
result.focus();
}
});
updateModel(); updateNoise();