Spaces:
Running on Zero
Running on Zero
Build prompt-free diffusion drawing canvas
Browse files- README.md +89 -86
- app.py +409 -449
- brand.css +293 -0
- brand_theme.py +61 -0
- hf_login.py +24 -4
README.md
CHANGED
|
@@ -1,86 +1,89 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Automatic Drawing
|
| 3 |
-
emoji: 🎨
|
| 4 |
-
colorFrom: green
|
| 5 |
-
colorTo: gray
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 6.32.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
python_version: '3.12'
|
| 10 |
-
pinned: false
|
| 11 |
-
hf_oauth: true
|
| 12 |
-
---
|
| 13 |
-
|
| 14 |
-
# Automatic Drawing
|
| 15 |
-
|
| 16 |
-
A prompt-free evolving diffusion canvas. The welcome page contains Hugging Face
|
| 17 |
-
sign-in, model selection, and Start drawing
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
the
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
the
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Automatic Drawing
|
| 3 |
+
emoji: 🎨
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 6.32.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
python_version: '3.12'
|
| 10 |
+
pinned: false
|
| 11 |
+
hf_oauth: true
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# Automatic Drawing
|
| 15 |
+
|
| 16 |
+
A prompt-free evolving diffusion canvas. The welcome page contains Hugging Face
|
| 17 |
+
sign-in, model selection, and Start drawing, and follows the browser's light or
|
| 18 |
+
dark setting. Starting hides the welcome page and shows the canvas. The drawing
|
| 19 |
+
stage fills the viewport. The look is shared with Let It Flow and Token Visualiser
|
| 20 |
+
through the shared `brand.css` and `brand_theme.py`:
|
| 21 |
+
midnight stage, amber accents, serif heading, and a floating pale mint dock that
|
| 22 |
+
looks the same in both colour modes. Resume, Pause, and step controls stay visible
|
| 23 |
+
in the dock. Other controls, history, and downloads are inside Settings, collapsed
|
| 24 |
+
by default. The explanation is in an expandable About this drawing panel on the
|
| 25 |
+
generation page. New start image immediately starts from fresh random noise using
|
| 26 |
+
the same model. The round ↺ button at the bottom right (Change model / sign in)
|
| 27 |
+
pauses and returns to the welcome page.
|
| 28 |
+
|
| 29 |
+
Each cycle runs in a short ZeroGPU allocation. Its actual latent states are
|
| 30 |
+
captured without changing random draws and decoded using the model VAE. After
|
| 31 |
+
inference, stages play on the main canvas at the selected pace outside the GPU
|
| 32 |
+
allocation. A browser timer requests the next cycle automatically. Queue delays
|
| 33 |
+
and account GPU quotas still apply. Closing the page stops new cycle requests.
|
| 34 |
+
|
| 35 |
+
Drift and playback speed apply on the next cycle. Default drift is 0.75 and
|
| 36 |
+
default stage duration is 0.5 seconds. There are no periodic full-noise resets.
|
| 37 |
+
Every call uses an empty string as its prompt, with no hidden subject, style
|
| 38 |
+
prompt, negative prompt, or language model.
|
| 39 |
+
|
| 40 |
+
Entering the canvas displays the actual initial latent noise decoded by the model
|
| 41 |
+
VAE before denoising begins. Its raw latent tensor and RNG state are retained, so
|
| 42 |
+
the first generated image uses exactly that noise. Change noise replaces it with
|
| 43 |
+
a new random sample and pauses; Resume generates from that sample. New start
|
| 44 |
+
image also samples new noise but resumes automatically. Noise preparation uses a
|
| 45 |
+
short ZeroGPU allocation, so there can be a queue delay before it appears.
|
| 46 |
+
|
| 47 |
+
Step back and Step forward pause playback and browse up to 64 actual captured
|
| 48 |
+
noise/denoising stages across cycles. These controls do not rerun inference or
|
| 49 |
+
alter the generation RNG or completed image. Resume continues from the latest
|
| 50 |
+
completed image, not from the inspected stage. Old snapshots are discarded as
|
| 51 |
+
the bounded stage history fills. New noise resets both histories.
|
| 52 |
+
|
| 53 |
+
The last 32 completed images are retained in the collapsed history drawer. PNG
|
| 54 |
+
and GIF exports are available there. Refreshing the page clears session state.
|
| 55 |
+
Start/Pause/Reset control state stays in the application process; version checks
|
| 56 |
+
prevent an old inference result from overwriting a paused or reset session.
|
| 57 |
+
|
| 58 |
+
`feedback_test.py` is the separate command-line experiment. `check_space.py`
|
| 59 |
+
checks deployed streaming, 13 cycles, resume, exports, and privacy.
|
| 60 |
+
`deploy_space.py` enforces private visibility and uploads an explicit allowlist.
|
| 61 |
+
|
| 62 |
+
Sign-in uses the shared TopLevelLoginButton (`hf_login.py`), the same component
|
| 63 |
+
as Let It Flow, with one visible button and
|
| 64 |
+
Gradio's built-in session-status handling. Both Spaces open sign-in in the same tab
|
| 65 |
+
(`_top`) on the canonical hf.space hostname, for both login and logout. Local runs
|
| 66 |
+
use the page's own origin, which reaches Gradio's local mocked sign-in. There is no
|
| 67 |
+
second sign-in control, hidden registration button, progress panel, or fallback
|
| 68 |
+
link. Only basic profile identity is requested.
|
| 69 |
+
ZeroGPU quota is attributed by Hugging Face to the authenticated visitor, not a
|
| 70 |
+
shared owner API key. Sign-in does not override private Space access restrictions.
|
| 71 |
+
The Space does not use your computer's GPU.
|
| 72 |
+
|
| 73 |
+
Two compatible models are preloaded at startup: SD-Turbo and SDXL-Turbo. Each
|
| 74 |
+
session selects its own model. Switching pauses and resets that session's canvas;
|
| 75 |
+
it does not change another visitor's selection. Both use an empty prompt, four
|
| 76 |
+
configured steps, and the same noise visualization. Arbitrary model repositories
|
| 77 |
+
are not supported because schedulers and pipeline architectures differ.
|
| 78 |
+
|
| 79 |
+
Models: https://huggingface.co/stabilityai/sd-turbo and
|
| 80 |
+
https://huggingface.co/stabilityai/sdxl-turbo
|
| 81 |
+
|
| 82 |
+
The dock uses one Start/Pause toggle. Its label follows generation state after
|
| 83 |
+
entry, history navigation, resets, noise changes, and generation errors. Numeric
|
| 84 |
+
fields and labels have explicit light-panel colors; rows wrap within the dock
|
| 85 |
+
and horizontal overflow is prevented. Stage history and playback skip repeated
|
| 86 |
+
pictures, including the completed/next-before pair and last-step/completed pair.
|
| 87 |
+
Near-identical decoder rounding is ignored using a mean pixel difference below
|
| 88 |
+
0.5 on a 128 by 128 comparison. This affects inspection/playback only, not model
|
| 89 |
+
sampling or the image fed into the next cycle.
|
app.py
CHANGED
|
@@ -1,449 +1,409 @@
|
|
| 1 |
-
"""Prompt-free diffusion canvas for a private Hugging Face Space."""
|
| 2 |
-
import spaces
|
| 3 |
-
import os
|
| 4 |
-
import secrets
|
| 5 |
-
import tempfile
|
| 6 |
-
import time
|
| 7 |
-
import threading
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
from PIL import ImageChops, ImageStat
|
| 10 |
-
|
| 11 |
-
import gradio as gr
|
| 12 |
-
from hf_login import TopLevelLoginButton
|
| 13 |
-
import
|
| 14 |
-
|
| 15 |
-
from diffusers import
|
| 16 |
-
from diffusers
|
| 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 |
-
"stabilityai/
|
| 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 |
-
kwargs["
|
| 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 |
-
self.
|
| 141 |
-
self.
|
| 142 |
-
self.
|
| 143 |
-
self.
|
| 144 |
-
self.
|
| 145 |
-
self.
|
| 146 |
-
self.
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
session.
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
session.
|
| 161 |
-
session.
|
| 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 |
-
session
|
| 188 |
-
session.
|
| 189 |
-
session.
|
| 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 |
-
session.
|
| 215 |
-
session.
|
| 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 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
frames
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
#
|
| 313 |
-
#
|
| 314 |
-
|
| 315 |
-
#
|
| 316 |
-
|
| 317 |
-
#
|
| 318 |
-
#
|
| 319 |
-
#
|
| 320 |
-
#
|
| 321 |
-
#
|
| 322 |
-
#
|
| 323 |
-
#
|
| 324 |
-
#
|
| 325 |
-
#
|
| 326 |
-
#
|
| 327 |
-
#
|
| 328 |
-
#
|
| 329 |
-
#
|
| 330 |
-
#
|
| 331 |
-
#
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
#
|
| 335 |
-
#
|
| 336 |
-
#
|
| 337 |
-
#
|
| 338 |
-
#
|
| 339 |
-
#
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
#
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
#
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
""
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
timer =
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
following = gr.Button("Step forward")
|
| 411 |
-
with gr.Accordion("Options", open=False, elem_id="options"):
|
| 412 |
-
with gr.Row():
|
| 413 |
-
fresh = gr.Button("New start image")
|
| 414 |
-
noise_button = gr.Button("Change noise")
|
| 415 |
-
back = gr.Button("Change model / sign in")
|
| 416 |
-
with gr.Row():
|
| 417 |
-
drift = gr.Slider(.25, 1, value=.75, step=.25, label="Drift",
|
| 418 |
-
info="Lower values preserve more of the previous image; higher values allow bigger changes. Applies next cycle.")
|
| 419 |
-
pace = gr.Slider(.15, 1.5, value=.5, step=.05, label="Seconds per stage",
|
| 420 |
-
info="How long each stage stays on screen. This changes playback speed.")
|
| 421 |
-
gr.Markdown('**Change noise** picks a new random starting noise and pauses so you can inspect it; press Start to generate from it. **New start image** starts from new noise and runs automatically. **Step back / Step forward** pause playback and browse the last 64 visually distinct noise and denoising stages. Browsing does not change generation; Start continues from the latest completed image. **Change model / sign in** returns to the start page and pauses generation.')
|
| 422 |
-
gallery = gr.Gallery(label="Last 32 completed images", columns=8, height=220)
|
| 423 |
-
download = gr.Button("Prepare downloads")
|
| 424 |
-
with gr.Row():
|
| 425 |
-
png = gr.File(label="Current image")
|
| 426 |
-
gif = gr.File(label="Evolution animation")
|
| 427 |
-
# Hidden API controls share the same state; the visible transport has one toggle.
|
| 428 |
-
api_start = gr.Button(visible=False)
|
| 429 |
-
api_pause = gr.Button(visible=False)
|
| 430 |
-
api_start.click(start_session, [state], [timer, status], queue=False, api_name="start").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 431 |
-
api_pause.click(pause_session, [state], [timer, status], queue=False, api_name="pause").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 432 |
-
outputs = [canvas, gallery, status]
|
| 433 |
-
begin.click(enter_canvas, [state, model],
|
| 434 |
-
[welcome, experience, canvas, gallery, status, timer, png, gif],
|
| 435 |
-
concurrency_limit=1, concurrency_id="drawing", api_name="begin").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 436 |
-
playback.click(toggle_playback, [state], [timer, status, playback], queue=False, api_name="toggle")
|
| 437 |
-
fresh.click(new_start_image, [state], [canvas, gallery, status, timer, png, gif],
|
| 438 |
-
concurrency_limit=1, concurrency_id="drawing", api_name="new_start_image").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 439 |
-
noise_button.click(change_noise, [state], [canvas, gallery, status, timer, png, gif],
|
| 440 |
-
concurrency_limit=1, concurrency_id="drawing", api_name="change_noise").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 441 |
-
previous.click(step_back, [state], [canvas, status, timer], queue=False, api_name="step_back").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 442 |
-
following.click(step_forward, [state], [canvas, status, timer], queue=False, api_name="step_forward").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 443 |
-
back.click(back_to_start, [state], [welcome, experience, timer], queue=False, api_name="back")
|
| 444 |
-
timer.tick(animate, [state, drift, pace], outputs, concurrency_limit=1,
|
| 445 |
-
concurrency_id="drawing", trigger_mode="once", api_name="cycle").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 446 |
-
download.click(export, [state], [png, gif], api_name="export")
|
| 447 |
-
|
| 448 |
-
if __name__ == "__main__":
|
| 449 |
-
demo.queue(max_size=8).launch(css=CSS, theme=THEME)
|
|
|
|
| 1 |
+
"""Prompt-free diffusion canvas for a private Hugging Face Space."""
|
| 2 |
+
import spaces
|
| 3 |
+
import os
|
| 4 |
+
import secrets
|
| 5 |
+
import tempfile
|
| 6 |
+
import time
|
| 7 |
+
import threading
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from PIL import ImageChops, ImageStat
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
from hf_login import TopLevelLoginButton
|
| 13 |
+
from brand_theme import brand_theme
|
| 14 |
+
import torch
|
| 15 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
| 16 |
+
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
|
| 17 |
+
from diffusers.utils.torch_utils import randn_tensor
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class CaptureStart:
|
| 22 |
+
"""Observe the exact prepared latents without drawing extra random numbers."""
|
| 23 |
+
def prepare_latents(self, *args, **kwargs):
|
| 24 |
+
latents = super().prepare_latents(*args, **kwargs)
|
| 25 |
+
observer = getattr(self, "latent_observer", None)
|
| 26 |
+
if observer is not None:
|
| 27 |
+
observer(latents)
|
| 28 |
+
return latents
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class DrawingPipeline(CaptureStart, StableDiffusionPipeline):
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class DrawingFeedbackPipeline(CaptureStart, StableDiffusionImg2ImgPipeline):
|
| 36 |
+
pass
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class DrawingXLPipeline(CaptureStart, StableDiffusionXLPipeline):
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class DrawingXLFeedbackPipeline(CaptureStart, StableDiffusionXLImg2ImgPipeline):
|
| 44 |
+
pass
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
MODELS = {
|
| 48 |
+
"stabilityai/sd-turbo": (DrawingPipeline, DrawingFeedbackPipeline),
|
| 49 |
+
"stabilityai/sdxl-turbo": (DrawingXLPipeline, DrawingXLFeedbackPipeline),
|
| 50 |
+
}
|
| 51 |
+
PIPELINES = {}
|
| 52 |
+
for repo, (text_cls, image_cls) in MODELS.items():
|
| 53 |
+
print(f"Preloading {repo}", flush=True)
|
| 54 |
+
text_pipe = text_cls.from_pretrained(
|
| 55 |
+
repo, torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
| 56 |
+
text_pipe.to("cuda" if os.environ.get("SPACES_ZERO_GPU") or torch.cuda.is_available() else "cpu")
|
| 57 |
+
PIPELINES[repo] = (text_pipe, image_cls(**text_pipe.components))
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def decode_preview(pipeline, latents):
|
| 61 |
+
with torch.inference_mode():
|
| 62 |
+
original_dtype = pipeline.vae.dtype
|
| 63 |
+
upcast = original_dtype == torch.float16 and getattr(pipeline.vae.config, "force_upcast", False)
|
| 64 |
+
if upcast:
|
| 65 |
+
pipeline.vae.to(dtype=torch.float32)
|
| 66 |
+
try:
|
| 67 |
+
pixels = pipeline.vae.decode(
|
| 68 |
+
latents.detach().to(pipeline.vae.dtype) / pipeline.vae.config.scaling_factor,
|
| 69 |
+
return_dict=False)[0]
|
| 70 |
+
finally:
|
| 71 |
+
if upcast:
|
| 72 |
+
pipeline.vae.to(dtype=original_dtype)
|
| 73 |
+
return pipeline.image_processor.postprocess(pixels, output_type="pil")[0]
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@spaces.GPU(duration=15)
|
| 77 |
+
def prepare_noise(model, seed):
|
| 78 |
+
pipeline = PIPELINES[model][0]
|
| 79 |
+
device = pipeline._execution_device
|
| 80 |
+
pipeline.scheduler.set_timesteps(4, device=device)
|
| 81 |
+
generator = torch.Generator("cpu").manual_seed(seed)
|
| 82 |
+
raw = randn_tensor((1, pipeline.unet.config.in_channels, 64, 64),
|
| 83 |
+
generator=generator, device=device, dtype=pipeline.unet.dtype)
|
| 84 |
+
preview = decode_preview(pipeline, raw * pipeline.scheduler.init_noise_sigma)
|
| 85 |
+
return dict(seed=seed, start_latents=raw.cpu(), initial_rng_state=generator.get_state()), preview
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
@spaces.GPU(duration=30)
|
| 89 |
+
def evolve(state, drift, model):
|
| 90 |
+
if model not in PIPELINES:
|
| 91 |
+
raise gr.Error("Choose a supported model.")
|
| 92 |
+
pipe, feedback = PIPELINES[model]
|
| 93 |
+
state = dict(state or {})
|
| 94 |
+
seed = state.get("seed", secrets.randbelow(2**31))
|
| 95 |
+
image = state.get("image")
|
| 96 |
+
frame = state.get("frame", -1)
|
| 97 |
+
history = list(state.get("history", []))
|
| 98 |
+
for _ in range(1):
|
| 99 |
+
frame += 1
|
| 100 |
+
started = time.perf_counter()
|
| 101 |
+
kwargs = dict(prompt="", guidance_scale=0.0, num_inference_steps=4,
|
| 102 |
+
generator=torch.Generator("cpu").manual_seed(seed + frame))
|
| 103 |
+
if image is None and "start_latents" in state:
|
| 104 |
+
kwargs["latents"] = state["start_latents"].to(pipe._execution_device)
|
| 105 |
+
kwargs["generator"].set_state(state["initial_rng_state"])
|
| 106 |
+
effective_drift = float(drift)
|
| 107 |
+
active = pipe if image is None else feedback
|
| 108 |
+
stages = [] if image is None else [(image.copy(), "Before noise")]
|
| 109 |
+
|
| 110 |
+
def capture_start(latents):
|
| 111 |
+
label = "Starting noise (decoded latent)" if image is None else "After noise added (decoded latent)"
|
| 112 |
+
stages.append((decode_preview(active, latents), label))
|
| 113 |
+
|
| 114 |
+
def capture_step(pipeline, step, timestep, callback_kwargs):
|
| 115 |
+
stages.append((decode_preview(pipeline, callback_kwargs["latents"]),
|
| 116 |
+
f"Denoising step {step + 1} · t={int(timestep)}"))
|
| 117 |
+
return callback_kwargs
|
| 118 |
+
|
| 119 |
+
active.latent_observer = capture_start
|
| 120 |
+
try:
|
| 121 |
+
with torch.inference_mode():
|
| 122 |
+
kwargs.update(callback_on_step_end=capture_step,
|
| 123 |
+
callback_on_step_end_tensor_inputs=["latents"])
|
| 124 |
+
if image is None:
|
| 125 |
+
result = pipe(height=512, width=512, **kwargs)
|
| 126 |
+
else:
|
| 127 |
+
result = feedback(image=image, strength=effective_drift, **kwargs)
|
| 128 |
+
finally:
|
| 129 |
+
active.latent_observer = None
|
| 130 |
+
image = result.images[0]
|
| 131 |
+
history = (history + [(image.copy(), f"Frame {frame}")])[-32:]
|
| 132 |
+
state = dict(seed=seed, image=image, frame=frame, history=history)
|
| 133 |
+
stages.append((image.copy(), "Completed image"))
|
| 134 |
+
elapsed = time.perf_counter()-started
|
| 135 |
+
return state, stages, f"{model.split('/')[-1]} | Frame {frame} | seed {seed} | drift {effective_drift:g} | {elapsed:.2f}s | empty prompt"
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class Session:
|
| 139 |
+
def __init__(self):
|
| 140 |
+
self.data = {}
|
| 141 |
+
self.running = False
|
| 142 |
+
self.revision = 0
|
| 143 |
+
self.wake = threading.Event()
|
| 144 |
+
self.lock = threading.Lock()
|
| 145 |
+
self.model = "stabilityai/sd-turbo"
|
| 146 |
+
self.stages = []
|
| 147 |
+
self.cursor = -1
|
| 148 |
+
|
| 149 |
+
def __deepcopy__(self, memo):
|
| 150 |
+
return Session()
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def start_session(session):
|
| 154 |
+
session.running = True
|
| 155 |
+
session.wake.clear()
|
| 156 |
+
return gr.Timer(active=True), "Evolving continuously..."
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def pause_session(session):
|
| 160 |
+
session.running = False
|
| 161 |
+
session.revision += 1
|
| 162 |
+
session.wake.set()
|
| 163 |
+
return gr.Timer(active=False), "Paused. Start continues from the latest completed image."
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def playback_button(session):
|
| 167 |
+
return gr.update(value="Pause" if session.running else "Start")
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def toggle_playback(session):
|
| 171 |
+
timer, message = pause_session(session) if session.running else start_session(session)
|
| 172 |
+
return timer, message, playback_button(session)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def same_picture(first, second):
|
| 176 |
+
if first is second:
|
| 177 |
+
return True
|
| 178 |
+
if not hasattr(first, "resize") or not hasattr(second, "resize"):
|
| 179 |
+
return first == second
|
| 180 |
+
# Ignore sub-visible decoder rounding differences as well as exact repeats.
|
| 181 |
+
difference = ImageChops.difference(first.convert("RGB").resize((128, 128)),
|
| 182 |
+
second.convert("RGB").resize((128, 128)))
|
| 183 |
+
return sum(ImageStat.Stat(difference).mean) / 3 < .5
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def reset(session):
|
| 187 |
+
pause_session(session)
|
| 188 |
+
session.data = {}
|
| 189 |
+
session.stages = []
|
| 190 |
+
session.cursor = -1
|
| 191 |
+
return None, [], "Ready. Start a new drawing from random noise.", gr.Timer(active=False)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def select_model(session, model):
|
| 195 |
+
if model not in MODELS:
|
| 196 |
+
raise gr.Error("Choose a supported model.")
|
| 197 |
+
result = reset(session)
|
| 198 |
+
session.model = model
|
| 199 |
+
return result
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def enter_canvas(session, model):
|
| 203 |
+
select_model(session, model)
|
| 204 |
+
noise, _ = initialize_noise(session)
|
| 205 |
+
timer, message = start_session(session)
|
| 206 |
+
return gr.update(visible=False), gr.update(visible=True), noise, [], "Starting noise. " + message, timer, None, None
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def initialize_noise(session):
|
| 210 |
+
revision = session.revision
|
| 211 |
+
data, noise = prepare_noise(session.model, secrets.randbelow(2**31))
|
| 212 |
+
if revision != session.revision:
|
| 213 |
+
raise gr.Error("Noise preparation was interrupted. Please try again.")
|
| 214 |
+
session.data = data
|
| 215 |
+
session.stages = [(noise, "Starting noise")]
|
| 216 |
+
session.cursor = 0
|
| 217 |
+
return noise, "Starting noise. Press Start to watch it become an image."
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def new_start_image(session):
|
| 221 |
+
reset(session)
|
| 222 |
+
noise, _ = initialize_noise(session)
|
| 223 |
+
timer, _ = start_session(session)
|
| 224 |
+
return noise, [], "New starting noise. Evolving continuously...", timer, None, None
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
def change_noise(session):
|
| 228 |
+
reset(session)
|
| 229 |
+
noise, message = initialize_noise(session)
|
| 230 |
+
return noise, [], message, gr.Timer(active=False), None, None
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
def inspect_stage(session, direction):
|
| 234 |
+
pause_session(session)
|
| 235 |
+
if not session.stages:
|
| 236 |
+
return gr.skip(), "No changed stages recorded yet.", gr.Timer(active=False)
|
| 237 |
+
current = session.stages[session.cursor][0]
|
| 238 |
+
candidate = session.cursor + direction
|
| 239 |
+
while 0 <= candidate < len(session.stages):
|
| 240 |
+
if not same_picture(current, session.stages[candidate][0]):
|
| 241 |
+
session.cursor = candidate
|
| 242 |
+
break
|
| 243 |
+
candidate += direction
|
| 244 |
+
image, label = session.stages[session.cursor]
|
| 245 |
+
return image, f"Paused | Stage {session.cursor+1}/{len(session.stages)} | {label}", gr.Timer(active=False)
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def step_back(session):
|
| 249 |
+
return inspect_stage(session, -1)
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
def step_forward(session):
|
| 253 |
+
return inspect_stage(session, 1)
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
def back_to_start(session):
|
| 257 |
+
pause_session(session)
|
| 258 |
+
return gr.update(visible=True), gr.update(visible=False), gr.Timer(active=False)
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def animate(session, drift, pace):
|
| 262 |
+
if not session.running or not session.lock.acquire(blocking=False):
|
| 263 |
+
return
|
| 264 |
+
revision = session.revision
|
| 265 |
+
try:
|
| 266 |
+
data, stages, message = evolve(session.data, drift, session.model)
|
| 267 |
+
if revision != session.revision or not session.running:
|
| 268 |
+
return
|
| 269 |
+
session.data = data
|
| 270 |
+
# Keep bounded actual snapshots; browsing never reruns inference or changes RNG.
|
| 271 |
+
changed = []
|
| 272 |
+
last = session.stages[-1][0] if session.stages else None
|
| 273 |
+
for preview, label in stages:
|
| 274 |
+
if last is not None and same_picture(last, preview):
|
| 275 |
+
continue
|
| 276 |
+
changed.append((preview, label))
|
| 277 |
+
last = preview
|
| 278 |
+
labeled = [(image, f"Cycle {data['frame']+1} | {label}") for image, label in changed]
|
| 279 |
+
session.stages = (session.stages + labeled)[-64:]
|
| 280 |
+
offset = len(session.stages) - len(changed)
|
| 281 |
+
for index, (preview, label) in enumerate(changed):
|
| 282 |
+
if revision != session.revision or not session.running:
|
| 283 |
+
return
|
| 284 |
+
session.cursor = offset + index
|
| 285 |
+
yield preview, gr.skip(), f"{message} | {label}"
|
| 286 |
+
if session.wake.wait(float(pace)):
|
| 287 |
+
return
|
| 288 |
+
yield data["image"], data["history"], message
|
| 289 |
+
except Exception:
|
| 290 |
+
session.running = False
|
| 291 |
+
raise
|
| 292 |
+
finally:
|
| 293 |
+
session.lock.release()
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def export(session):
|
| 297 |
+
state = session.data
|
| 298 |
+
if not state or not state.get("history"):
|
| 299 |
+
raise gr.Error("Generate a drawing first.")
|
| 300 |
+
folder = Path(tempfile.mkdtemp(prefix="drawing-"))
|
| 301 |
+
png = folder / "drawing.png"
|
| 302 |
+
gif = folder / "evolution.gif"
|
| 303 |
+
state["image"].save(png)
|
| 304 |
+
frames = [item[0] for item in state["history"]]
|
| 305 |
+
frames[0].save(gif, save_all=True, append_images=frames[1:], duration=650, loop=0)
|
| 306 |
+
return str(png), str(gif)
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
# Shared look (brand.css, from shared-ui/) first, then this app's layout.
|
| 310 |
+
CSS = Path(__file__).with_name("brand.css").read_text(encoding="utf-8") + """
|
| 311 |
+
.gradio-container {max-width:none !important;min-height:100dvh !important;--color-accent:#f4bf67;--color-accent-soft:#f4bf6726}
|
| 312 |
+
#experience {position:fixed !important;inset:0 !important;width:100% !important;max-width:100% !important;height:100dvh !important;margin:0 !important;padding:0 !important;gap:0 !important;overflow:hidden !important;background:var(--brand-stage);z-index:50;--brand-return-label:"Change model / sign in"}
|
| 313 |
+
#canvas {position:absolute !important;inset:0 !important;width:100% !important;height:100% !important;min-height:0 !important;border:0 !important;border-radius:0 !important;background:var(--brand-stage) !important;padding:64px 16px 165px !important;box-sizing:border-box}
|
| 314 |
+
#canvas > div {height:100% !important;min-height:0 !important;background:transparent !important}
|
| 315 |
+
#canvas img {width:100% !important;height:100% !important;object-fit:contain !important;border-radius:0 !important}
|
| 316 |
+
#drawing-caption {position:absolute !important;top:18px;left:clamp(16px,4vw,60px);right:72px;z-index:2;background:transparent;border:0;padding:0}
|
| 317 |
+
#drawing-caption .prose p {white-space:nowrap;overflow:hidden;text-overflow:ellipsis;margin:0}
|
| 318 |
+
#about-drawing {position:absolute !important;top:48px;left:clamp(16px,4vw,60px);width:min(440px,calc(100vw - 32px)) !important;max-height:55dvh;overflow:auto;z-index:3;background:#091622ed !important;border:1px solid #78949d45 !important;border-radius:14px;backdrop-filter:blur(14px);color:#d5e3e6;--block-title-text-color:#d5e3e6;--accordion-text-color:#d5e3e6;--body-text-color:#d5e3e6;--block-background-fill:transparent;--background-fill-primary:transparent}
|
| 319 |
+
#about-drawing .label-wrap, #about-drawing .label-wrap span {color:#d5e3e6 !important}
|
| 320 |
+
#about-drawing .prose {color:#d5e3e6 !important;font-size:13px;line-height:1.65}
|
| 321 |
+
#about-drawing h3 {color:var(--brand-amber) !important;font-family:var(--brand-serif);font-weight:400}
|
| 322 |
+
#control-dock {position:absolute !important;bottom:max(16px,env(safe-area-inset-bottom));left:50%;transform:translateX(-50%);width:min(940px,calc(100vw - 120px)) !important;max-width:calc(100% - 24px) !important;max-height:65dvh;overflow-x:hidden !important;overflow-y:auto;z-index:5;padding:12px !important;gap:8px !important}
|
| 323 |
+
#control-dock *,#control-dock *::before,#control-dock *::after {box-sizing:border-box;min-width:0 !important}
|
| 324 |
+
#control-dock > div,#options,#options > div {max-width:100% !important;min-width:0 !important}
|
| 325 |
+
#control-dock .row {flex-wrap:wrap !important;min-width:0 !important}
|
| 326 |
+
#control-dock .prose {overflow-wrap:anywhere}
|
| 327 |
+
#control-dock button {min-height:44px}
|
| 328 |
+
#transport {display:grid !important;grid-template-columns:minmax(0,1.3fr) repeat(2,minmax(0,1fr));width:100% !important;gap:8px !important;position:sticky;top:0;z-index:6;background:var(--brand-mint-solid)}
|
| 329 |
+
#transport button {width:100% !important;white-space:normal}
|
| 330 |
+
#resume-drawing {font-weight:600}
|
| 331 |
+
#options {background:transparent !important;border:0 !important}
|
| 332 |
+
@media(max-width:640px){
|
| 333 |
+
#canvas {padding:70px 8px 150px !important}
|
| 334 |
+
#drawing-caption {left:12px;top:12px;right:12px}
|
| 335 |
+
#drawing-caption .prose p {font-size:11px !important}
|
| 336 |
+
#about-drawing {left:12px;top:44px;width:calc(100vw - 24px) !important;max-height:45dvh}
|
| 337 |
+
#control-dock {bottom:12px;left:12px;transform:none;width:calc(100vw - 72px) !important;padding:8px !important;max-height:60dvh}
|
| 338 |
+
#transport {gap:5px !important}
|
| 339 |
+
#transport button {font-size:12px;padding:8px 5px}
|
| 340 |
+
}
|
| 341 |
+
"""
|
| 342 |
+
# Start page follows the browser's light/dark setting; the workspace sets its own colours.
|
| 343 |
+
THEME = brand_theme()
|
| 344 |
+
with gr.Blocks(title="Automatic Drawing") as demo:
|
| 345 |
+
state = gr.State(Session())
|
| 346 |
+
timer = gr.Timer(.5, active=False)
|
| 347 |
+
with gr.Column(visible=True, elem_id="welcome", elem_classes=["brand-start"]) as welcome:
|
| 348 |
+
gr.HTML('<header class="brand-header"><div class="eyebrow">AN EXPERIMENT IN AUTOMATIC DRAWING</div><h1 class="brand-title">Automatic Drawing<span class="title-stop">.</span></h1><p class="brand-intro">Inspired by automatic drawing, this experiment lets an image unfold without a written prompt. Begin with noise, watch forms emerge, then see the image become the starting point for something new.</p></header>', padding=False)
|
| 349 |
+
TopLevelLoginButton(elem_id="hf-login")
|
| 350 |
+
gr.Markdown("Sign in to use your Hugging Face ZeroGPU quota. Sign-in opens in this tab and brings you back here.",
|
| 351 |
+
elem_classes=["signin-note"])
|
| 352 |
+
model = gr.Dropdown(choices=[("SD-Turbo · faster", "stabilityai/sd-turbo"),
|
| 353 |
+
("SDXL-Turbo · larger model", "stabilityai/sdxl-turbo")],
|
| 354 |
+
value="stabilityai/sd-turbo", label="Model")
|
| 355 |
+
begin = gr.Button("Start drawing", variant="primary", size="lg", elem_id="begin-drawing",
|
| 356 |
+
elem_classes=["brand-primary"])
|
| 357 |
+
gr.Markdown(elem_classes=["brand-note"], value="**What you will see:** noise appears, forms emerge as noise is removed, then the finished image is reinterpreted. There are no hidden subject or style instructions. The model's learned visual patterns guide what appears.")
|
| 358 |
+
with gr.Column(visible=False, elem_id="experience") as experience:
|
| 359 |
+
canvas = gr.Image(label="Living canvas", show_label=False, interactive=False,
|
| 360 |
+
height=640, elem_id="canvas", type="pil")
|
| 361 |
+
status = gr.Markdown("**Automatic Drawing.** No written prompt. Just the next image.", elem_id="drawing-caption",
|
| 362 |
+
elem_classes=["brand-caption"])
|
| 363 |
+
with gr.Accordion("About this drawing", open=False, elem_id="about-drawing"):
|
| 364 |
+
gr.Markdown('### What is happening?\nThe first image begins as random noise. The model removes noise in a few steps, drawing on patterns learned during training. Each later cycle takes the previous image, adds fresh noise, and removes it again to produce a variation. Every text prompt is empty.\n\nThe canvas shows decoded views of the actual internal image states: before noise, after noise is added, and after each denoising step. Stages play after a cycle has been computed. Brief holds between cycles come from processing and the shared GPU queue. Generation continues until you pause or reach your GPU quota.', elem_id="explanation")
|
| 365 |
+
with gr.Column(elem_id="control-dock", elem_classes=["brand-dock"]):
|
| 366 |
+
with gr.Row(elem_id="transport"):
|
| 367 |
+
playback = gr.Button("Start", variant="primary", elem_id="resume-drawing")
|
| 368 |
+
previous = gr.Button("Step back")
|
| 369 |
+
following = gr.Button("Step forward")
|
| 370 |
+
with gr.Accordion("Settings", open=False, elem_id="options"):
|
| 371 |
+
with gr.Row():
|
| 372 |
+
fresh = gr.Button("New start image")
|
| 373 |
+
noise_button = gr.Button("Change noise")
|
| 374 |
+
with gr.Row():
|
| 375 |
+
drift = gr.Slider(.25, 1, value=.75, step=.25, label="Drift",
|
| 376 |
+
info="Lower values preserve more of the previous image; higher values allow bigger changes. Applies next cycle.")
|
| 377 |
+
pace = gr.Slider(.15, 1.5, value=.5, step=.05, label="Seconds per stage",
|
| 378 |
+
info="How long each stage stays on screen. This changes playback speed.")
|
| 379 |
+
gr.Markdown('**Change noise** picks a new random starting noise and pauses so you can inspect it; press Start to generate from it. **New start image** starts from new noise and runs automatically. **Step back / Step forward** pause playback and browse the last 64 visually distinct noise and denoising stages. Browsing does not change generation; Start continues from the latest completed image. The round **↺** button at the bottom right pauses generation and returns to the start page, where you can change model or sign in.')
|
| 380 |
+
gallery = gr.Gallery(label="Last 32 completed images", columns=8, height=220)
|
| 381 |
+
download = gr.Button("Prepare downloads")
|
| 382 |
+
with gr.Row():
|
| 383 |
+
png = gr.File(label="Current image")
|
| 384 |
+
gif = gr.File(label="Evolution animation")
|
| 385 |
+
# Outside the dock: its transform would otherwise trap position:fixed.
|
| 386 |
+
back = gr.Button("Change model / sign in", elem_id="return-home", elem_classes=["return-home"])
|
| 387 |
+
# Hidden API controls share the same state; the visible transport has one toggle.
|
| 388 |
+
api_start = gr.Button(visible=False)
|
| 389 |
+
api_pause = gr.Button(visible=False)
|
| 390 |
+
api_start.click(start_session, [state], [timer, status], queue=False, api_name="start").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 391 |
+
api_pause.click(pause_session, [state], [timer, status], queue=False, api_name="pause").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 392 |
+
outputs = [canvas, gallery, status]
|
| 393 |
+
begin.click(enter_canvas, [state, model],
|
| 394 |
+
[welcome, experience, canvas, gallery, status, timer, png, gif],
|
| 395 |
+
concurrency_limit=1, concurrency_id="drawing", api_name="begin").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 396 |
+
playback.click(toggle_playback, [state], [timer, status, playback], queue=False, api_name="toggle")
|
| 397 |
+
fresh.click(new_start_image, [state], [canvas, gallery, status, timer, png, gif],
|
| 398 |
+
concurrency_limit=1, concurrency_id="drawing", api_name="new_start_image").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 399 |
+
noise_button.click(change_noise, [state], [canvas, gallery, status, timer, png, gif],
|
| 400 |
+
concurrency_limit=1, concurrency_id="drawing", api_name="change_noise").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 401 |
+
previous.click(step_back, [state], [canvas, status, timer], queue=False, api_name="step_back").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 402 |
+
following.click(step_forward, [state], [canvas, status, timer], queue=False, api_name="step_forward").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 403 |
+
back.click(back_to_start, [state], [welcome, experience, timer], queue=False, api_name="back")
|
| 404 |
+
timer.tick(animate, [state, drift, pace], outputs, concurrency_limit=1,
|
| 405 |
+
concurrency_id="drawing", trigger_mode="once", api_name="cycle").then(playback_button, [state], [playback], queue=False, api_name=False)
|
| 406 |
+
download.click(export, [state], [png, gif], api_name="export")
|
| 407 |
+
|
| 408 |
+
if __name__ == "__main__":
|
| 409 |
+
demo.queue(max_size=8).launch(css=CSS, theme=THEME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
brand.css
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* ==========================================================================
|
| 2 |
+
Shared look for the Hugging Face Spaces in this folder:
|
| 3 |
+
Automatic Drawing, Stream of Consciousness (Let It Flow), Token Visualiser.
|
| 4 |
+
|
| 5 |
+
Canonical copy: shared-ui/brand.css. Edit it there, then run
|
| 6 |
+
`python shared-ui/sync.py` to copy it into each project.
|
| 7 |
+
|
| 8 |
+
- Start page: follows the browser's light/dark setting.
|
| 9 |
+
- Workspace: a dark stage with a pale mint control panel, identical in
|
| 10 |
+
both modes.
|
| 11 |
+
========================================================================== */
|
| 12 |
+
|
| 13 |
+
:root {
|
| 14 |
+
/* Workspace: the same in light and dark browser modes. */
|
| 15 |
+
--brand-stage: #080f18;
|
| 16 |
+
--brand-stage-raised: #112530;
|
| 17 |
+
--brand-stage-line: #577f89;
|
| 18 |
+
--brand-stage-text: #eef3f5;
|
| 19 |
+
--brand-stage-caption: #c5e2dd;
|
| 20 |
+
--brand-amber: #f4bf67;
|
| 21 |
+
--brand-amber-hover: #e7aa45;
|
| 22 |
+
--brand-amber-deep: #b77a20;
|
| 23 |
+
--brand-on-amber: #17242b;
|
| 24 |
+
--brand-mint: #eaf2eff2;
|
| 25 |
+
--brand-mint-solid: #eaf2ef;
|
| 26 |
+
--brand-mint-surface: #f4f8f6;
|
| 27 |
+
--brand-mint-text: #203c3a;
|
| 28 |
+
--brand-mint-label: #29433f;
|
| 29 |
+
--brand-mint-subdued: #526b68;
|
| 30 |
+
--brand-mint-line: #b3c7bd;
|
| 31 |
+
--brand-mint-border: #6d9f9b66;
|
| 32 |
+
--brand-shadow: 0 12px 50px #0006;
|
| 33 |
+
--brand-radius-panel: 18px;
|
| 34 |
+
--brand-radius-control: 10px;
|
| 35 |
+
--brand-serif: Georgia, 'Times New Roman', serif;
|
| 36 |
+
--brand-sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
|
| 37 |
+
|
| 38 |
+
/* Start page, light browser mode. */
|
| 39 |
+
--brand-page: #f4f8f6;
|
| 40 |
+
--brand-surface: #ffffff;
|
| 41 |
+
--brand-heading: #17242b;
|
| 42 |
+
--brand-text: #203c3a;
|
| 43 |
+
--brand-subdued: #526b68;
|
| 44 |
+
--brand-eyebrow: #92601c;
|
| 45 |
+
--brand-link: #92601c;
|
| 46 |
+
--brand-line: #b3c7bd;
|
| 47 |
+
--brand-input: #ffffff;
|
| 48 |
+
--brand-input-hover: #f4f8f6;
|
| 49 |
+
--brand-focus: #b77a20;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
/* Start page, dark browser mode. Gradio puts .dark on an ancestor of the app and
|
| 53 |
+
honours ?__theme=. Streamlit follows the system setting, hence the media query. */
|
| 54 |
+
.dark {
|
| 55 |
+
--brand-page: #080f18;
|
| 56 |
+
--brand-surface: #091622;
|
| 57 |
+
--brand-heading: #eef3f5;
|
| 58 |
+
--brand-text: #d5e3e6;
|
| 59 |
+
--brand-subdued: #a8bdc4;
|
| 60 |
+
--brand-eyebrow: #f4bf67;
|
| 61 |
+
--brand-link: #f4bf67;
|
| 62 |
+
--brand-line: #577f89;
|
| 63 |
+
--brand-input: #112530;
|
| 64 |
+
--brand-input-hover: #193442;
|
| 65 |
+
--brand-focus: #f4bf67;
|
| 66 |
+
}
|
| 67 |
+
@media (prefers-color-scheme: dark) {
|
| 68 |
+
.stApp {
|
| 69 |
+
--brand-page: #080f18;
|
| 70 |
+
--brand-surface: #091622;
|
| 71 |
+
--brand-heading: #eef3f5;
|
| 72 |
+
--brand-text: #d5e3e6;
|
| 73 |
+
--brand-subdued: #a8bdc4;
|
| 74 |
+
--brand-eyebrow: #f4bf67;
|
| 75 |
+
--brand-link: #f4bf67;
|
| 76 |
+
--brand-line: #577f89;
|
| 77 |
+
--brand-input: #112530;
|
| 78 |
+
--brand-input-hover: #193442;
|
| 79 |
+
--brand-focus: #f4bf67;
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
/* ---- Start page --------------------------------------------------------- */
|
| 84 |
+
/* Order: eyebrow, title, intro, sign-in + note, model, primary action, note. */
|
| 85 |
+
/* .st-key-start_page is Token Visualiser's Streamlit start-page container. */
|
| 86 |
+
.brand-start, .st-key-start_page {
|
| 87 |
+
box-sizing: border-box;
|
| 88 |
+
width: calc(100% - 24px) !important;
|
| 89 |
+
max-width: 680px !important;
|
| 90 |
+
margin: 7vh auto !important;
|
| 91 |
+
padding: 24px !important;
|
| 92 |
+
gap: 18px !important;
|
| 93 |
+
}
|
| 94 |
+
/* Gradio wraps fields in a bordered card; keep the start page flat (label above field),
|
| 95 |
+
as in Token Visualiser. */
|
| 96 |
+
.brand-start .form { border: 0 !important; background: transparent !important; box-shadow: none !important; }
|
| 97 |
+
.brand-start .form > .block {
|
| 98 |
+
padding: 0 !important;
|
| 99 |
+
border: 0 !important;
|
| 100 |
+
background: transparent !important;
|
| 101 |
+
box-shadow: none !important;
|
| 102 |
+
}
|
| 103 |
+
.brand-start .form > .block [data-testid="block-info"] {
|
| 104 |
+
padding-left: 0 !important;
|
| 105 |
+
padding-right: 0 !important;
|
| 106 |
+
font-weight: 600;
|
| 107 |
+
color: var(--brand-text) !important;
|
| 108 |
+
}
|
| 109 |
+
/* Gradio pads HTML blocks; align the title with the controls below it. */
|
| 110 |
+
.brand-start .html-container { padding-left: 0 !important; padding-right: 0 !important; }
|
| 111 |
+
.brand-header { padding: 12px 0 6px; }
|
| 112 |
+
.eyebrow {
|
| 113 |
+
font: 600 11px/1.5 var(--brand-sans);
|
| 114 |
+
letter-spacing: .18em;
|
| 115 |
+
text-transform: uppercase;
|
| 116 |
+
color: var(--brand-eyebrow) !important;
|
| 117 |
+
}
|
| 118 |
+
.brand-title {
|
| 119 |
+
margin: 14px 0 18px !important;
|
| 120 |
+
padding: 0 !important;
|
| 121 |
+
border: 0 !important;
|
| 122 |
+
font: 400 clamp(38px, 6vw, 62px)/1.06 var(--brand-serif) !important;
|
| 123 |
+
letter-spacing: -.04em !important;
|
| 124 |
+
color: var(--brand-heading) !important;
|
| 125 |
+
}
|
| 126 |
+
.title-stop { color: var(--brand-amber) !important; }
|
| 127 |
+
.brand-intro {
|
| 128 |
+
margin: 0 0 12px !important;
|
| 129 |
+
font: 16px/1.7 var(--brand-sans) !important;
|
| 130 |
+
color: var(--brand-text) !important;
|
| 131 |
+
}
|
| 132 |
+
.brand-note, .brand-note p, .brand-note li,
|
| 133 |
+
.signin-note, .signin-note p {
|
| 134 |
+
margin: 0;
|
| 135 |
+
font: 14px/1.6 var(--brand-sans) !important;
|
| 136 |
+
color: var(--brand-subdued) !important;
|
| 137 |
+
}
|
| 138 |
+
.brand-note strong, .signin-note strong { color: var(--brand-text) !important; }
|
| 139 |
+
.brand-note a, .signin-note a, .brand-header a { color: var(--brand-link) !important; }
|
| 140 |
+
/* .st-key-hf_load_preset is Token Visualiser's "Load model" button. */
|
| 141 |
+
.brand-primary, .st-key-hf_load_preset button {
|
| 142 |
+
min-height: 48px !important;
|
| 143 |
+
border: 0 !important;
|
| 144 |
+
border-radius: var(--brand-radius-control) !important;
|
| 145 |
+
background: var(--brand-amber) !important;
|
| 146 |
+
color: var(--brand-on-amber) !important;
|
| 147 |
+
font: 600 16px/1.4 var(--brand-sans) !important;
|
| 148 |
+
}
|
| 149 |
+
.brand-primary:hover, .st-key-hf_load_preset button:hover { background: var(--brand-amber-hover) !important; }
|
| 150 |
+
.brand-primary:focus-visible, .st-key-hf_load_preset button:focus-visible {
|
| 151 |
+
outline: 3px solid var(--brand-focus) !important;
|
| 152 |
+
outline-offset: 2px;
|
| 153 |
+
}
|
| 154 |
+
@media (max-width: 640px) {
|
| 155 |
+
.brand-start, .st-key-start_page { margin: 3vh auto !important; padding: 18px !important; }
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
/* ---- Workspace ---------------------------------------------------------- */
|
| 159 |
+
/* Floating mint panel. A light island: readable in both browser modes. */
|
| 160 |
+
.brand-dock {
|
| 161 |
+
box-sizing: border-box;
|
| 162 |
+
border: 1px solid var(--brand-mint-border) !important;
|
| 163 |
+
border-radius: var(--brand-radius-panel) !important;
|
| 164 |
+
background: var(--brand-mint) !important;
|
| 165 |
+
backdrop-filter: blur(18px);
|
| 166 |
+
box-shadow: var(--brand-shadow);
|
| 167 |
+
color: var(--brand-mint-text);
|
| 168 |
+
color-scheme: light;
|
| 169 |
+
/* Start-page tokens revert to their light values inside the panel. */
|
| 170 |
+
--brand-heading: #17242b;
|
| 171 |
+
--brand-text: #203c3a;
|
| 172 |
+
--brand-subdued: #526b68;
|
| 173 |
+
--brand-eyebrow: #92601c;
|
| 174 |
+
--brand-link: #92601c;
|
| 175 |
+
--brand-line: #b3c7bd;
|
| 176 |
+
--brand-input: #ffffff;
|
| 177 |
+
--brand-input-hover: #f4f8f6;
|
| 178 |
+
--brand-focus: #b77a20;
|
| 179 |
+
/* Gradio theme variables, so the panel ignores Gradio's dark mode. */
|
| 180 |
+
--body-text-color: #203c3a;
|
| 181 |
+
--body-text-color-subdued: #526b68;
|
| 182 |
+
--block-background-fill: #f4f8f6;
|
| 183 |
+
--background-fill-primary: #f4f8f6;
|
| 184 |
+
--background-fill-secondary: #e3ece7;
|
| 185 |
+
--block-label-background-fill: #f4f8f6;
|
| 186 |
+
--block-label-text-color: #29433f;
|
| 187 |
+
--block-title-text-color: #203c3a;
|
| 188 |
+
--block-title-background-fill: transparent;
|
| 189 |
+
--block-title-border-color: transparent;
|
| 190 |
+
--block-info-text-color: #526b68;
|
| 191 |
+
--block-border-color: #bdcdc5;
|
| 192 |
+
--border-color-primary: #b3c7bd;
|
| 193 |
+
--input-background-fill: #ffffff;
|
| 194 |
+
--input-background-fill-hover: #f4f8f6;
|
| 195 |
+
--input-background-fill-focus: #ffffff;
|
| 196 |
+
--input-border-color: #b3c7bd;
|
| 197 |
+
--input-text-color: #1c3536;
|
| 198 |
+
--input-placeholder-color: #526b68;
|
| 199 |
+
--accordion-text-color: #203c3a;
|
| 200 |
+
--checkbox-background-color: #ffffff;
|
| 201 |
+
--checkbox-border-color: #8aa49c;
|
| 202 |
+
--checkbox-label-background-fill: #f4f8f6;
|
| 203 |
+
--checkbox-label-text-color: #203c3a;
|
| 204 |
+
--button-secondary-background-fill: #f4f8f6;
|
| 205 |
+
--button-secondary-background-fill-hover: #dce9e1;
|
| 206 |
+
--button-secondary-text-color: #203c3a;
|
| 207 |
+
--button-secondary-text-color-hover: #17242b;
|
| 208 |
+
--button-secondary-border-color: #b3c7bd;
|
| 209 |
+
--link-text-color: #725020;
|
| 210 |
+
--table-even-background-fill: #ffffff;
|
| 211 |
+
--table-odd-background-fill: #f4f8f6;
|
| 212 |
+
}
|
| 213 |
+
.brand-dock .prose, .brand-dock .prose strong { color: var(--brand-mint-text) !important; }
|
| 214 |
+
.brand-dock label, .brand-dock .label-wrap, .brand-dock .label-wrap span {
|
| 215 |
+
color: var(--brand-mint-label) !important;
|
| 216 |
+
background: transparent !important;
|
| 217 |
+
}
|
| 218 |
+
.brand-dock input[type="number"], .brand-dock input[type="text"], .brand-dock textarea {
|
| 219 |
+
max-width: 100%;
|
| 220 |
+
color: #1c3536 !important;
|
| 221 |
+
-webkit-text-fill-color: #1c3536 !important;
|
| 222 |
+
background: #ffffff !important;
|
| 223 |
+
}
|
| 224 |
+
.brand-dock .block-info { color: var(--brand-mint-subdued) !important; }
|
| 225 |
+
.brand-dock button:focus-visible { outline: 3px solid var(--brand-amber-deep) !important; outline-offset: 2px; }
|
| 226 |
+
|
| 227 |
+
/* Model and status line at the top left of the stage. */
|
| 228 |
+
.brand-caption, .brand-caption .prose, .brand-caption p {
|
| 229 |
+
color: var(--brand-stage-caption) !important;
|
| 230 |
+
font: 13px/1.5 var(--brand-sans) !important;
|
| 231 |
+
}
|
| 232 |
+
.brand-caption strong { color: var(--brand-amber) !important; font-weight: 600; }
|
| 233 |
+
|
| 234 |
+
/* Round "change model" button, bottom right, outside the panel.
|
| 235 |
+
Its text stays as the accessible name; the ↺ glyph is drawn instead.
|
| 236 |
+
.st-key-clear_model is the Streamlit wrapper of Token Visualiser's button. */
|
| 237 |
+
.return-home, .st-key-clear_model button {
|
| 238 |
+
position: fixed !important;
|
| 239 |
+
right: max(16px, env(safe-area-inset-right));
|
| 240 |
+
bottom: max(16px, env(safe-area-inset-bottom));
|
| 241 |
+
z-index: 60;
|
| 242 |
+
display: flex !important;
|
| 243 |
+
align-items: center;
|
| 244 |
+
justify-content: center;
|
| 245 |
+
box-sizing: border-box;
|
| 246 |
+
width: 40px !important;
|
| 247 |
+
height: 40px !important;
|
| 248 |
+
min-width: 40px !important;
|
| 249 |
+
min-height: 40px !important;
|
| 250 |
+
padding: 0 !important;
|
| 251 |
+
border: 1px solid #86b6b366 !important;
|
| 252 |
+
border-radius: 50% !important;
|
| 253 |
+
background: #112530ed !important;
|
| 254 |
+
color: var(--brand-stage-caption) !important;
|
| 255 |
+
font-size: 0 !important;
|
| 256 |
+
box-shadow: 0 3px 16px #0004;
|
| 257 |
+
cursor: pointer;
|
| 258 |
+
}
|
| 259 |
+
.return-home::before, .st-key-clear_model button::before {
|
| 260 |
+
content: "\21ba";
|
| 261 |
+
font: 22px/1 var(--brand-sans);
|
| 262 |
+
color: var(--brand-stage-caption);
|
| 263 |
+
}
|
| 264 |
+
.return-home::after, .st-key-clear_model button::after {
|
| 265 |
+
content: var(--brand-return-label, "Change model");
|
| 266 |
+
position: absolute;
|
| 267 |
+
top: 50%;
|
| 268 |
+
right: 50px;
|
| 269 |
+
transform: translateY(-50%);
|
| 270 |
+
padding: 5px 9px;
|
| 271 |
+
border: 1px solid #78949d66;
|
| 272 |
+
border-radius: 7px;
|
| 273 |
+
background: #07121ff5;
|
| 274 |
+
color: #e2edf0;
|
| 275 |
+
font: 12px/1.4 var(--brand-sans);
|
| 276 |
+
white-space: nowrap;
|
| 277 |
+
opacity: 0;
|
| 278 |
+
pointer-events: none;
|
| 279 |
+
transition: opacity .15s;
|
| 280 |
+
}
|
| 281 |
+
.return-home:hover, .st-key-clear_model button:hover { background: #23434d !important; }
|
| 282 |
+
.return-home:hover::after, .return-home:focus-visible::after,
|
| 283 |
+
.st-key-clear_model button:hover::after, .st-key-clear_model button:focus-visible::after { opacity: 1; }
|
| 284 |
+
.return-home:focus-visible, .st-key-clear_model button:focus-visible {
|
| 285 |
+
outline: 2px solid var(--brand-amber) !important;
|
| 286 |
+
outline-offset: 3px;
|
| 287 |
+
}
|
| 288 |
+
@media (max-width: 640px) {
|
| 289 |
+
.return-home, .st-key-clear_model button { right: 12px; bottom: 12px; }
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
/* Gradio's "Use via API · Built with Gradio" footer. */
|
| 293 |
+
footer { display: none !important; }
|
brand_theme.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Gradio theme shared by the Spaces in this folder.
|
| 2 |
+
|
| 3 |
+
Canonical copy: shared-ui/brand_theme.py. Edit it there, then run
|
| 4 |
+
`python shared-ui/sync.py` to copy it into each project.
|
| 5 |
+
|
| 6 |
+
The start page follows the browser's light/dark setting: plain keys hold the
|
| 7 |
+
light palette and `*_dark` keys the dark one. The workspace sets its own colours
|
| 8 |
+
(see brand.css), so it looks the same in both modes.
|
| 9 |
+
"""
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
LIGHT = {
|
| 13 |
+
"body_background_fill": "#f4f8f6", "body_text_color": "#203c3a",
|
| 14 |
+
"body_text_color_subdued": "#526b68", "background_fill_primary": "#f4f8f6",
|
| 15 |
+
"background_fill_secondary": "#eaf2ef", "block_background_fill": "#ffffff",
|
| 16 |
+
"block_border_color": "#cfdcd6", "block_label_background_fill": "#ffffff",
|
| 17 |
+
"block_label_text_color": "#29433f", "block_title_background_fill": "transparent",
|
| 18 |
+
"block_title_text_color": "#203c3a", "block_info_text_color": "#526b68",
|
| 19 |
+
"panel_background_fill": "#eaf2ef", "input_background_fill": "#ffffff",
|
| 20 |
+
"input_background_fill_hover": "#f4f8f6", "input_background_fill_focus": "#ffffff",
|
| 21 |
+
"input_border_color": "#b3c7bd", "input_placeholder_color": "#6b827e",
|
| 22 |
+
"accordion_text_color": "#203c3a", "border_color_primary": "#b3c7bd",
|
| 23 |
+
"button_secondary_background_fill": "#eaf2ef", "button_secondary_background_fill_hover": "#dce9e1",
|
| 24 |
+
"button_secondary_text_color": "#203c3a", "button_secondary_text_color_hover": "#17242b",
|
| 25 |
+
"link_text_color": "#92601c", "link_text_color_hover": "#b77a20",
|
| 26 |
+
}
|
| 27 |
+
DARK = {
|
| 28 |
+
"body_background_fill": "#080f18", "body_text_color": "#eef3f5",
|
| 29 |
+
"body_text_color_subdued": "#a8bdc4", "background_fill_primary": "#091622",
|
| 30 |
+
"background_fill_secondary": "#112530", "block_background_fill": "#091622",
|
| 31 |
+
"block_border_color": "#577f89", "block_label_background_fill": "#091622",
|
| 32 |
+
"block_label_text_color": "#eef3f5", "block_title_background_fill": "transparent",
|
| 33 |
+
"block_title_text_color": "#eef3f5", "block_info_text_color": "#a8bdc4",
|
| 34 |
+
"panel_background_fill": "#091622", "input_background_fill": "#112530",
|
| 35 |
+
"input_background_fill_hover": "#193442", "input_background_fill_focus": "#112530",
|
| 36 |
+
"input_border_color": "#577f89", "input_placeholder_color": "#a8bdc4",
|
| 37 |
+
"accordion_text_color": "#eef3f5", "border_color_primary": "#577f89",
|
| 38 |
+
"button_secondary_background_fill": "#193442", "button_secondary_background_fill_hover": "#577f89",
|
| 39 |
+
"button_secondary_text_color": "#eef3f5", "button_secondary_text_color_hover": "#ffffff",
|
| 40 |
+
"link_text_color": "#f4bf67", "link_text_color_hover": "#f8d49a",
|
| 41 |
+
}
|
| 42 |
+
# Same system font stack as brand.css (--brand-sans) and Token Visualiser's Streamlit theme.
|
| 43 |
+
FONT = ["system-ui", "-apple-system", "Segoe UI", "Roboto", "sans-serif"]
|
| 44 |
+
# Amber primary actions and plain bordered inputs (as in Token Visualiser) in both modes.
|
| 45 |
+
PRIMARY = {
|
| 46 |
+
"input_border_width": "1px", "input_shadow": "none",
|
| 47 |
+
"input_shadow_focus": "0 0 0 2px #f4bf6733", "input_border_color_focus": "#f4bf67",
|
| 48 |
+
"button_primary_background_fill": "#f4bf67", "button_primary_background_fill_hover": "#e7aa45",
|
| 49 |
+
"button_primary_text_color": "#17242b", "button_primary_text_color_hover": "#17242b",
|
| 50 |
+
"button_primary_border_color": "#f4bf67", "button_primary_border_color_hover": "#e7aa45",
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Settings Gradio applies to both modes at once (no *_dark variant).
|
| 55 |
+
BOTH_MODES = {"input_radius": "10px"}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def brand_theme():
|
| 59 |
+
values = {**LIGHT, **PRIMARY, **BOTH_MODES}
|
| 60 |
+
values.update({key + "_dark": value for key, value in {**DARK, **PRIMARY}.items()})
|
| 61 |
+
return gr.themes.Soft(primary_hue="amber", neutral_hue="stone", font=FONT).set(**values)
|
hf_login.py
CHANGED
|
@@ -1,20 +1,38 @@
|
|
| 1 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
import re
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def canonical_space_origin():
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
raise ValueError("Expected the canonical hf.space hostname for OAuth.")
|
| 12 |
return "https://" + host
|
| 13 |
|
| 14 |
|
| 15 |
def login_redirect_js(default_value):
|
|
|
|
|
|
|
| 16 |
return """(buttonValue) => {
|
| 17 |
-
const origin = ORIGIN;
|
| 18 |
const route = buttonValue === DEFAULT ? '/login/huggingface' : '/logout';
|
| 19 |
const url = new URL(route, origin);
|
| 20 |
url.searchParams.set('_target_url', '/');
|
|
@@ -23,6 +41,8 @@ def login_redirect_js(default_value):
|
|
| 23 |
|
| 24 |
|
| 25 |
class TopLevelLoginButton(gr.LoginButton):
|
|
|
|
|
|
|
| 26 |
def get_block_name(self):
|
| 27 |
return "button"
|
| 28 |
|
|
|
|
| 1 |
+
"""Hugging Face sign-in shared by the Gradio Spaces in this folder.
|
| 2 |
+
|
| 3 |
+
Canonical copy: shared-ui/hf_login.py. Edit it there, then run
|
| 4 |
+
`python shared-ui/sync.py` to copy it into each project.
|
| 5 |
+
|
| 6 |
+
The button opens the OAuth route of the Space's canonical *.hf.space origin in
|
| 7 |
+
the same tab (target `_top`). The cookie-based round trip therefore happens
|
| 8 |
+
outside the huggingface.co iframe and then returns to the app. Gradio's own
|
| 9 |
+
OAuth state validation, callback, expiry checks and logout stay in use.
|
| 10 |
+
Only basic profile identity is requested.
|
| 11 |
+
"""
|
| 12 |
import json
|
| 13 |
import os
|
| 14 |
import re
|
| 15 |
+
|
| 16 |
import gradio as gr
|
| 17 |
|
| 18 |
+
HOST_PATTERN = re.compile(r"[a-z0-9][a-z0-9-]*\.hf\.space")
|
| 19 |
+
|
| 20 |
|
| 21 |
def canonical_space_origin():
|
| 22 |
+
"""The Space's canonical https origin, or None when not running on Spaces."""
|
| 23 |
+
host = os.environ.get("SPACE_HOST", "").split(",", 1)[0].strip().lower()
|
| 24 |
+
if not host:
|
| 25 |
+
return None
|
| 26 |
+
if not HOST_PATTERN.fullmatch(host):
|
| 27 |
raise ValueError("Expected the canonical hf.space hostname for OAuth.")
|
| 28 |
return "https://" + host
|
| 29 |
|
| 30 |
|
| 31 |
def login_redirect_js(default_value):
|
| 32 |
+
# Outside Spaces (local runs) the page's own origin is used, which reaches
|
| 33 |
+
# Gradio's local mocked sign-in.
|
| 34 |
return """(buttonValue) => {
|
| 35 |
+
const origin = ORIGIN || window.location.origin;
|
| 36 |
const route = buttonValue === DEFAULT ? '/login/huggingface' : '/logout';
|
| 37 |
const url = new URL(route, origin);
|
| 38 |
url.searchParams.set('_target_url', '/');
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
class TopLevelLoginButton(gr.LoginButton):
|
| 44 |
+
"""Gradio's LoginButton, rendered as a normal button, signing in in the same tab."""
|
| 45 |
+
|
| 46 |
def get_block_name(self):
|
| 47 |
return "button"
|
| 48 |
|