Spaces:
Running
on
A100
Running
on
A100
File size: 13,369 Bytes
94890d6 ec8114e 94890d6 c7af5cd 94890d6 ec8114e 94890d6 ec8114e 94890d6 |
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Real-Time Latent Consistency Model</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/piexifjs@1.0.6/piexif.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<style type="text/tailwindcss">
.button {
@apply bg-gray-700 hover:bg-gray-800 text-white font-normal p-2 rounded disabled:bg-gray-300 dark:disabled:bg-gray-700 disabled:cursor-not-allowed dark:disabled:text-black
}
</style>
<script type="module">
const getValue = (id) => {
const el = document.querySelector(`${id}`)
if (el.type === "checkbox")
return el.checked;
return el.value;
}
const startBtn = document.querySelector("#start");
const stopBtn = document.querySelector("#stop");
const videoEl = document.querySelector("#webcam");
const imageEl = document.querySelector("#player");
const queueSizeEl = document.querySelector("#queue_size");
const errorEl = document.querySelector("#error");
const snapBtn = document.querySelector("#snap");
const paramsEl = document.querySelector("#params");
const promptEl = document.querySelector("#prompt");
paramsEl.addEventListener("submit", (e) => e.preventDefault());
function LCMLive(promptEl, paramsEl, liveImage) {
let websocket;
async function start() {
return new Promise((resolve, reject) => {
const websocketURL = `${window.location.protocol === "https:" ? "wss" : "ws"
}:${window.location.host}/ws`;
const socket = new WebSocket(websocketURL);
socket.onopen = () => {
console.log("Connected to websocket");
};
socket.onclose = () => {
console.log("Disconnected from websocket");
stop();
resolve({ "status": "disconnected" });
};
socket.onerror = (err) => {
console.error(err);
reject(err);
};
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
switch (data.status) {
case "success":
break;
case "start":
const userId = data.userId;
initPromptStream(userId);
break;
case "timeout":
stop();
resolve({ "status": "timeout" });
case "error":
stop();
reject(data.message);
}
};
websocket = socket;
})
}
async function promptUpdateStream(e) {
const [WIDTH, HEIGHT] = [512, 512];
websocket.send(JSON.stringify({
"seed": getValue("#seed"),
"prompt": getValue("#prompt"),
"guidance_scale": getValue("#guidance-scale"),
"steps": getValue("#steps"),
"lcm_steps": getValue("#lcm_steps"),
"width": WIDTH,
"height": HEIGHT,
}));
}
function debouceInput(fn, delay) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
fn(...args);
}, delay);
}
}
const debouncedInput = debouceInput(promptUpdateStream, 200);
function initPromptStream(userId) {
liveImage.src = `/stream/${userId}`;
paramsEl.addEventListener("change", debouncedInput);
promptEl.addEventListener("input", debouncedInput);
}
async function stop() {
websocket.close();
paramsEl.removeEventListener("change", debouncedInput);
promptEl.removeEventListener("input", debouncedInput);
}
return {
start,
stop
}
}
function toggleMessage(type) {
errorEl.hidden = false;
errorEl.scrollIntoView();
switch (type) {
case "error":
errorEl.innerText = "To many users are using the same GPU, please try again later.";
errorEl.classList.toggle("bg-red-300", "text-red-900");
break;
case "success":
errorEl.innerText = "Your session has ended, please start a new one.";
errorEl.classList.toggle("bg-green-300", "text-green-900");
break;
}
setTimeout(() => {
errorEl.hidden = true;
}, 2000);
}
function snapImage() {
try {
const zeroth = {};
const exif = {};
const gps = {};
zeroth[piexif.ImageIFD.Make] = "LCM Text-to-Image";
zeroth[piexif.ImageIFD.ImageDescription] = `prompt: ${getValue("#prompt")} | seed: ${getValue("#seed")} | guidance_scale: ${getValue("#guidance-scale")} | lcm_steps: ${getValue("#lcm_steps")} | steps: ${getValue("#steps")}`;
zeroth[piexif.ImageIFD.Software] = "https://github.com/radames/Real-Time-Latent-Consistency-Model";
exif[piexif.ExifIFD.DateTimeOriginal] = new Date().toISOString();
const exifObj = { "0th": zeroth, "Exif": exif, "GPS": gps };
const exifBytes = piexif.dump(exifObj);
const canvas = document.createElement("canvas");
canvas.width = imageEl.naturalWidth;
canvas.height = imageEl.naturalHeight;
const ctx = canvas.getContext("2d");
ctx.drawImage(imageEl, 0, 0);
const dataURL = canvas.toDataURL("image/jpeg");
const withExif = piexif.insert(exifBytes, dataURL);
const a = document.createElement("a");
a.href = withExif;
a.download = `lcm_txt_2_img${Date.now()}.png`;
a.click();
} catch (err) {
console.log(err);
}
}
const lcmLive = LCMLive(promptEl, paramsEl, imageEl);
startBtn.addEventListener("click", async () => {
try {
startBtn.disabled = true;
snapBtn.disabled = false;
const res = await lcmLive.start();
startBtn.disabled = false;
if (res.status === "timeout")
toggleMessage("success")
} catch (err) {
console.log(err);
toggleMessage("error")
startBtn.disabled = false;
}
});
stopBtn.addEventListener("click", () => {
lcmLive.stop();
});
window.addEventListener("beforeunload", () => {
lcmLive.stop();
});
snapBtn.addEventListener("click", snapImage);
setInterval(() =>
fetch("/queue_size")
.then((res) => res.json())
.then((data) => {
queueSizeEl.innerText = data.queue_size;
})
.catch((err) => {
console.log(err);
})
, 5000);
</script>
</head>
<body class="text-black dark:bg-gray-900 dark:text-white">
<div class="fixed right-2 top-2 p-4 font-bold text-sm rounded-lg max-w-xs text-center" id="error">
</div>
<main class="container mx-auto px-4 py-4 max-w-4xl flex flex-col gap-4">
<article class="text-center max-w-xl mx-auto">
<h1 class="text-3xl font-bold">Real-Time Latent Consistency Model</h1>
<h2 class="text-2xl font-bold mb-4">Text to Image Lora</h2>
<p class="text-sm">
This demo showcases
<a href="https://huggingface.co/SimianLuo/LCM_Dreamshaper_v7" target="_blank"
class="text-blue-500 underline hover:no-underline">LCM</a> Text to Image model
using
<a href="https://github.com/huggingface/diffusers/tree/main/examples/community#latent-consistency-pipeline"
target="_blank" class="text-blue-500 underline hover:no-underline">Diffusers</a> with a MJPEG
stream server. Featuring <a href="https://huggingface.co/wavymulder/Analog-Diffusion" target="_blank"
class="text-blue-500 underline hover:no-underline">Analog Diffusion</a> Model.
</p>
</article>
<div>
<h2 class="font-medium">Prompt</h2>
<p class="text-sm text-gray-500 dark:text-gray-400">
Start your session and type your prompt here, accepts
<a href="https://github.com/damian0815/compel/blob/main/doc/syntax.md" target="_blank"
class="text-blue-500 underline hover:no-underline">Compel</a> syntax.
</p>
<div class="flex text-normal px-1 py-1 border border-gray-700 rounded-md items-center">
<textarea type="text" id="prompt" class="font-light w-full px-3 py-2 mx-1 outline-none dark:text-black"
title=" Start your session and type your prompt here, you can see the result in real-time."
placeholder="Add your prompt here...">Analog style photograph of young Harrison Ford as Han Solo, star wars behind the scenes</textarea>
</div>
</div>
<div class="">
<details>
<summary class="font-medium cursor-pointer">Advanced Options</summary>
<form class="grid grid-cols-3 items-center gap-3 py-3" id="params" action="">
<label class="text-sm font-medium " for="steps">Inference Steps
</label>
<input type="range" id="steps" name="steps" min="1" max="20" value="4"
oninput="this.nextElementSibling.value = Number(this.value)">
<output class="text-xs w-[50px] text-center font-light px-1 py-1 border border-gray-700 rounded-md">
4</output>
<!-- -->
<label class="text-sm font-medium" for="lcm_steps">LCM Inference Steps
</label>
<input type="range" id="lcm_steps" name="lcm_steps" min="2" max="60" value="50"
oninput="this.nextElementSibling.value = Number(this.value)">
<output class="text-xs w-[50px] text-center font-light px-1 py-1 border border-gray-700 rounded-md">
50</output>
<!-- -->
<label class="text-sm font-medium" for="guidance-scale">Guidance Scale
</label>
<input type="range" id="guidance-scale" name="guidance-scale" min="0" max="5" step="0.0001"
value="0.8" oninput="this.nextElementSibling.value = Number(this.value).toFixed(2)">
<output class="text-xs w-[50px] text-center font-light px-1 py-1 border border-gray-700 rounded-md">
8.0</output>
<!-- -->
<label class="text-sm font-medium" for="seed">Seed</label>
<input type="number" id="seed" name="seed" value="299792458"
class="font-light border border-gray-700 text-right rounded-md p-2 dark:text-black">
<button class="button"
onclick="document.querySelector('#seed').value = Math.floor(Math.random() * 1000000000); document.querySelector('#params').dispatchEvent(new Event('change'))">
Rand
</button>
<!-- -->
</form>
</details>
</div>
<div class="flex gap-3">
<button id="start" class="button">
Start
</button>
<button id="stop" class="button">
Stop
</button>
<button id="snap" disabled class="button ml-auto">
Snapshot
</button>
</div>
<div class="relative rounded-lg border border-slate-300 overflow-hidden">
<img id="player" class="w-full aspect-square rounded-lg"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=">
</div>
</main>
</body>
</html> |