OzzyGT HF Staff commited on
Commit
40dd94b
·
1 Parent(s): 106319b

Add Krea 2 prediction-preview Space

Browse files
Files changed (4) hide show
  1. .gitignore +2 -0
  2. README.md +24 -5
  3. app.py +301 -0
  4. requirements.txt +10 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __pycache__/
2
+ .ruff_cache/
README.md CHANGED
@@ -1,13 +1,32 @@
1
  ---
2
- title: Krea2 Preview
3
- emoji: 🔥
4
- colorFrom: pink
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 6.27.0
8
  python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Krea 2 Preview
3
+ emoji: 👁️
4
+ colorFrom: indigo
5
  colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 6.20.0
8
  python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
11
+ short_description: Krea 2's prediction preview images
12
  ---
13
 
14
+ # Krea 2 -- prediction previews
15
+
16
+ A UI built as a demo for [`OzzyGT/krea2_preview_blocks`](https://huggingface.co/OzzyGT/krea2_preview_blocks)
17
+ modular blocks. At every step the model predicts the image it is heading for; this streams *that* prediction
18
+ rather than the noisy latents the loop is holding, so there is a whole picture from step one which then
19
+ sharpens, instead of static that resolves near the end.
20
+
21
+ Two decoders, both cheap next to a transformer forward: TAEW 2.1, an 11.3M-parameter tiny autoencoder
22
+ (~21 ms for a full 1024x1024 preview, 22 MB fetched on first use), or a 16x3 linear projection of the
23
+ latent channels (under 1 ms, blocky).
24
+
25
+ ## Notes
26
+
27
+ `pipe(...)` blocks and the preview callback fires inside it, so the generation runs on a worker thread and
28
+ pushes previews onto a queue that the Gradio generator drains. Stopping the run closes that generator, which
29
+ sets a cancel flag the callback raises on -- the exception unwinds out of `pipe(...)` and the pipeline stays
30
+ usable.
31
+
32
+ On ZeroGPU the whole generation has to fit inside one `@spaces.GPU(duration=...)` call, previews included.
app.py ADDED
@@ -0,0 +1,301 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gc
2
+ import os
3
+ import queue
4
+ import sys
5
+ import threading
6
+
7
+
8
+ IS_ZERO_GPU = os.environ.get("SPACES_ZERO_GPU") is not None
9
+
10
+ # `spaces` must be imported before any CUDA-initializing package: keep this above the torch imports.
11
+ if IS_ZERO_GPU:
12
+ import spaces
13
+ else:
14
+
15
+ class _NoSpaces:
16
+ @staticmethod
17
+ def GPU(*args, **kwargs):
18
+ if len(args) == 1 and callable(args[0]) and not kwargs:
19
+ return args[0]
20
+
21
+ def deco(fn):
22
+ return fn
23
+
24
+ return deco
25
+
26
+ spaces = _NoSpaces()
27
+
28
+
29
+ import gradio as gr # noqa: E402
30
+ import torch # noqa: E402
31
+
32
+ from diffusers.modular_pipelines import ( # noqa: E402
33
+ ComponentsManager,
34
+ ModularPipelineBlocks,
35
+ )
36
+
37
+
38
+ BLOCK_REPO = os.environ.get("KREA2_PREVIEW_BLOCKS", "OzzyGT/krea2_preview_blocks")
39
+
40
+ MODELS = {
41
+ "Krea 2 Turbo — SDNQ int8 (~18GB)": {
42
+ "repo": "OzzyGT/Krea_2_Turbo_sdnq_dynamic_8bit",
43
+ "distilled": True,
44
+ "steps": 8,
45
+ },
46
+ "Krea 2 Turbo — SDNQ int4 (~10GB)": {
47
+ "repo": "OzzyGT/Krea_2_Turbo_sdnq_dynamic_4bit",
48
+ "distilled": True,
49
+ "steps": 8,
50
+ },
51
+ }
52
+ DEFAULT_MODEL = "Krea 2 Turbo — SDNQ int8 (~18GB)"
53
+
54
+ PREVIEW_MODES = {
55
+ "Tiny VAE (TAEW 2.1)": "taew",
56
+ "Tiny VAE, quarter size": "taew_fast",
57
+ "Latent RGB projection": "rgb",
58
+ }
59
+ DEFAULT_PREVIEW_MODE = "Tiny VAE (TAEW 2.1)"
60
+
61
+ COMPONENTS = ["text_encoder", "tokenizer", "transformer", "vae", "scheduler"]
62
+ MAX_SEQUENCE_LENGTH = 512
63
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
64
+
65
+
66
+ class Cancelled(Exception):
67
+ """Raised inside the preview callback to abort a run the user walked away from."""
68
+
69
+
70
+ # ----------------------------------------------------------------------------- pipeline lifecycle
71
+ # One pipeline resident at a time; switching the checkpoint reloads.
72
+ _current = {"id": None, "pipe": None}
73
+
74
+
75
+ def _resolve(choice: str) -> str:
76
+ """Map a dropdown label to its repo id; anything else is used verbatim."""
77
+ preset = MODELS.get(choice)
78
+ return preset["repo"] if preset else choice
79
+
80
+
81
+ def _load(checkpoint: str, distilled: bool):
82
+ import sdnq # noqa: F401 registers the SDNQ backend with transformers so quantized checkpoints load
83
+
84
+ # `auto_map` names the distilled blockset, so the CFG one is pulled off the loaded module by name.
85
+ # The two are not interchangeable: the distilled blockset carries no guider and ignores negative_prompt.
86
+ blocks = ModularPipelineBlocks.from_pretrained(BLOCK_REPO, trust_remote_code=True)
87
+ if not distilled:
88
+ blocks = getattr(sys.modules[type(blocks).__module__], "Krea2PreviewAutoBlocks")()
89
+ manager = ComponentsManager()
90
+ # Subfolders come from the block repo's modular index; `load_components` overrides the repo.
91
+ pipe = blocks.init_pipeline(BLOCK_REPO, components_manager=manager)
92
+ pipe.load_components(names=COMPONENTS, pretrained_model_name_or_path=checkpoint, dtype=torch.bfloat16)
93
+
94
+ if IS_ZERO_GPU:
95
+ # ZeroGPU holds a large GPU for the whole call, so offloading would only cost time.
96
+ pipe.to(DEVICE)
97
+ elif DEVICE == "cuda":
98
+ manager.enable_auto_cpu_offload(device=DEVICE)
99
+ return pipe
100
+
101
+
102
+ def get_pipe(choice: str, distilled: bool):
103
+ checkpoint = _resolve(choice)
104
+ if _current["id"] != (checkpoint, distilled):
105
+ _current.update(id=None, pipe=None)
106
+ gc.collect()
107
+ if torch.cuda.is_available():
108
+ torch.cuda.empty_cache()
109
+ _current.update(id=(checkpoint, distilled), pipe=_load(checkpoint, distilled))
110
+ print(f"loaded {checkpoint} ({'distilled' if distilled else 'CFG'} blockset)")
111
+ return _current["pipe"]
112
+
113
+
114
+ # Preloading is free on ZeroGPU. Locally nothing loads until the Load button, so running this Space does
115
+ # not take VRAM from whatever else is on the card.
116
+ if IS_ZERO_GPU:
117
+ get_pipe(DEFAULT_MODEL, MODELS[DEFAULT_MODEL]["distilled"])
118
+
119
+
120
+ @spaces.GPU(duration=120)
121
+ def load_model(choice, distilled):
122
+ get_pipe(choice, distilled)
123
+ return f"✅ Loaded: {_resolve(choice)}"
124
+
125
+
126
+ def unload_model():
127
+ _current.update(id=None, pipe=None)
128
+ gc.collect()
129
+ if torch.cuda.is_available():
130
+ torch.cuda.empty_cache()
131
+ return "⚪ No model loaded"
132
+
133
+
134
+ # ----------------------------------------------------------------------------- generate
135
+ @spaces.GPU(duration=180)
136
+ def generate(
137
+ model_choice,
138
+ distilled,
139
+ prompt,
140
+ preview_mode_label,
141
+ preview_every,
142
+ width,
143
+ height,
144
+ steps,
145
+ seed,
146
+ randomize_seed,
147
+ ):
148
+ """Stream the model's own prediction, step by step, then the decoded result.
149
+
150
+ `pipe(...)` blocks and the preview callback fires inside it, so the generation runs on a worker thread
151
+ and pushes each preview onto a queue that this generator drains.
152
+
153
+ No `gr.Progress`, and `gr.skip()` for the image on the status-only update: anything that re-renders the
154
+ image component between steps makes the preview blink.
155
+ """
156
+ yield gr.skip(), "Loading model…", gr.skip()
157
+ pipe = get_pipe(model_choice, distilled)
158
+
159
+ if randomize_seed:
160
+ seed = int(torch.randint(0, 2**31 - 1, (1,)).item())
161
+
162
+ kwargs = {
163
+ "prompt": prompt or "",
164
+ "height": int(height),
165
+ "width": int(width),
166
+ "num_inference_steps": int(steps),
167
+ "max_sequence_length": MAX_SEQUENCE_LENGTH,
168
+ "generator": torch.Generator("cpu").manual_seed(int(seed)),
169
+ "output_type": "pil",
170
+ "preview_mode": PREVIEW_MODES[preview_mode_label],
171
+ "preview_every": int(preview_every),
172
+ }
173
+
174
+ updates: queue.Queue = queue.Queue()
175
+ cancel = threading.Event()
176
+
177
+ def on_preview(step, total, image):
178
+ # The only point where a stopped run can be noticed: raising unwinds out of `pipe(...)`.
179
+ if cancel.is_set():
180
+ raise Cancelled
181
+ updates.put(("preview", step, total, image))
182
+
183
+ def worker():
184
+ try:
185
+ images = pipe(preview_callback=on_preview, **kwargs).values["images"]
186
+ updates.put(("done", images[0]))
187
+ except Cancelled:
188
+ updates.put(("cancelled", None))
189
+ except Exception as exc: # surface worker failures on the UI thread
190
+ updates.put(("error", exc))
191
+
192
+ threading.Thread(target=worker, daemon=True).start()
193
+
194
+ try:
195
+ while True:
196
+ kind, *payload = updates.get()
197
+ if kind == "preview":
198
+ step, total, image = payload
199
+ yield image, f"Step {step + 1}/{total} — the model's prediction", int(seed)
200
+ elif kind == "done":
201
+ yield payload[0], "Done", int(seed)
202
+ return
203
+ elif kind == "cancelled":
204
+ return
205
+ else:
206
+ raise gr.Error(str(payload[0]))
207
+ finally:
208
+ # Gradio closes this generator when the user stops; the worker thread is still denoising.
209
+ cancel.set()
210
+
211
+
212
+ # ----------------------------------------------------------------------------- UI
213
+ with gr.Blocks(title="Krea 2 — prediction previews") as demo:
214
+ gr.Markdown(
215
+ "# Krea 2 — prediction previews\n"
216
+ "Every step, the model predicts the image it is heading for. This shows *that*, rather than the "
217
+ "noisy latents it currently holds — so there is a whole picture from step one, which then sharpens. "
218
+ f"Built on the [`{BLOCK_REPO}`](https://huggingface.co/{BLOCK_REPO}) modular blocks."
219
+ )
220
+
221
+ with gr.Row():
222
+ with gr.Column(scale=1):
223
+ prompt = gr.Textbox(label="Prompt", lines=3, placeholder="a photograph of ...")
224
+
225
+ preview_mode = gr.Radio(
226
+ choices=list(PREVIEW_MODES),
227
+ value=DEFAULT_PREVIEW_MODE,
228
+ label="Preview decoder",
229
+ info="The tiny VAE costs ~21 ms a step at 1024². The RGB projection is under 1 ms, and blocky.",
230
+ )
231
+ preview_every = gr.Slider(
232
+ 1, 5, value=1, step=1, label="Preview every N steps", info="The last step is always shown."
233
+ )
234
+
235
+ with gr.Accordion("Advanced", open=False):
236
+ with gr.Row():
237
+ width = gr.Slider(512, 2048, value=1024, step=32, label="Width")
238
+ height = gr.Slider(512, 2048, value=1024, step=32, label="Height")
239
+ with gr.Row():
240
+ steps = gr.Slider(1, 60, value=MODELS[DEFAULT_MODEL]["steps"], step=1, label="Steps")
241
+ with gr.Row():
242
+ seed = gr.Number(value=0, precision=0, label="Seed")
243
+ randomize_seed = gr.Checkbox(value=True, label="Randomize")
244
+
245
+ with gr.Column(scale=1):
246
+ model_choice = gr.Dropdown(
247
+ choices=list(MODELS),
248
+ value=DEFAULT_MODEL,
249
+ label="Model",
250
+ allow_custom_value=True,
251
+ )
252
+ distilled = gr.Checkbox(
253
+ value=MODELS[DEFAULT_MODEL]["distilled"],
254
+ label="Distilled (Turbo) checkpoint",
255
+ info="Picks the blockset: distilled runs few-step without CFG. Presets set this for you.",
256
+ )
257
+ with gr.Row(visible=not IS_ZERO_GPU):
258
+ load_btn = gr.Button("Load model")
259
+ unload_btn = gr.Button("Unload")
260
+ status = gr.Markdown("🟢 Ready (ZeroGPU)" if IS_ZERO_GPU else "⚪ No model loaded")
261
+
262
+ run = gr.Button("Generate", variant="primary")
263
+ image = gr.Image(label="Prediction → result", type="pil", format="png", interactive=False)
264
+ step_status = gr.Markdown("idle")
265
+ used_seed = gr.Number(label="Seed used", precision=0, interactive=False)
266
+
267
+ def on_model_change(choice):
268
+ """A preset carries its own schedule and blockset; a custom repo leaves both alone to be set."""
269
+ preset = MODELS.get(choice)
270
+ if preset is None:
271
+ return gr.update(), gr.update()
272
+ return gr.update(value=preset["steps"]), gr.update(value=preset["distilled"])
273
+
274
+ model_choice.change(on_model_change, inputs=[model_choice], outputs=[steps, distilled])
275
+
276
+ load_btn.click(load_model, inputs=[model_choice, distilled], outputs=[status])
277
+ unload_btn.click(unload_model, outputs=[status])
278
+ run.click(
279
+ generate,
280
+ inputs=[
281
+ model_choice,
282
+ distilled,
283
+ prompt,
284
+ preview_mode,
285
+ preview_every,
286
+ width,
287
+ height,
288
+ steps,
289
+ seed,
290
+ randomize_seed,
291
+ ],
292
+ outputs=[image, step_status, used_seed],
293
+ # One pipeline object, and `BlockState` is not reentrant: concurrent runs corrupt each other.
294
+ concurrency_limit=1,
295
+ # Anything but "hidden" blanks the image and draws a bar over it on every yield.
296
+ show_progress="hidden",
297
+ )
298
+
299
+
300
+ if __name__ == "__main__":
301
+ demo.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ diffusers @ git+https://github.com/huggingface/diffusers.git
2
+ transformers>=5.12
3
+ torchvision
4
+ accelerate
5
+ safetensors
6
+ pillow
7
+ numpy
8
+ spaces
9
+ gradio>=6.14,<7
10
+ sdnq>=0.2.1