Daankular commited on
Commit
b83fdde
·
verified ·
1 Parent(s): 3704c95

Use Redcraft checkpoint and public AoTI kernels

Browse files
Files changed (1) hide show
  1. app.py +29 -37
app.py CHANGED
@@ -6,7 +6,7 @@ import sys
6
  import spaces
7
  import torch
8
  import gradio as gr
9
- from huggingface_hub import login
10
 
11
  if os.environ.get("HF_TOKEN"):
12
  login(token=os.environ["HF_TOKEN"])
@@ -14,44 +14,37 @@ if os.environ.get("HF_TOKEN"):
14
  from diffusers import Krea2Pipeline
15
 
16
  DTYPE = torch.bfloat16
17
- RAW_REPO = "krea/Krea-2-Raw"
18
- TURBO_REPO = "krea/Krea-2-Turbo"
 
 
19
  MAX_SEED = 2**31 - 1
20
 
21
- # Both models are loaded at global scope. They share the architecture, so Turbo
22
- # reuses Raw's text encoder / tokenizer / VAE and only loads its own transformer.
23
- pipe_raw = Krea2Pipeline.from_pretrained(RAW_REPO, torch_dtype=DTYPE)
24
- pipe_turbo = Krea2Pipeline.from_pretrained(
25
- TURBO_REPO,
26
- text_encoder=pipe_raw.text_encoder,
27
- tokenizer=pipe_raw.tokenizer,
28
- vae=pipe_raw.vae,
29
- torch_dtype=DTYPE,
30
  )
31
- pipe_raw.to("cuda")
32
- pipe_turbo.to("cuda")
33
 
34
 
35
 
36
  def _load_aoti():
37
- # One compiled Krea2TransformerBlock (kernels only, weights stay live) serves
38
- # both pipelines since their transformer configs are identical. Mirrors
39
- # spaces.aoti_blocks_load, but downloads from the private artifact dataset
40
- # with an explicit write token instead of ambient model-repo auth.
41
- from huggingface_hub import hf_hub_download
42
  from spaces.zero.torch.aoti import LazyAOTIModel
43
 
44
  pt2 = hf_hub_download(
45
- repo_id="multimodalart/Krea-2-aoti",
46
- filename="Krea2TransformerBlock/package.pt2",
47
  repo_type="dataset",
48
- token=os.environ.get("HF_WRITE_TOKEN"),
49
  )
50
  aoti_model = LazyAOTIModel(pt2)
51
- for pipe in (pipe_raw, pipe_turbo):
52
- for block in pipe.transformer.modules():
53
- if block.__class__.__name__ == "Krea2TransformerBlock":
54
- spaces.aoti_patch(block, aoti_model)
55
 
56
 
57
  try:
@@ -60,10 +53,9 @@ try:
60
  except Exception as e:
61
  print(f"AoTI load skipped ({e}); running eager.")
62
 
63
- PIPES = {"Raw": pipe_raw, "Turbo": pipe_turbo}
64
  DEFAULTS = {
65
- "Raw": {"steps": 28, "guidance": 4.5},
66
- "Turbo": {"steps": 8, "guidance": 0.0},
67
  }
68
 
69
  # Resolution presets. The model renders up to 2K, but the compiled transformer
@@ -84,7 +76,7 @@ Krea 2 is tuned for natural language. Describe the image the way you would descr
84
  - To render text in the image, wrap the words in quotes, for example: a storefront window with a neon sign that reads "open late".
85
  - The model can render up to 2K, but very high resolutions may run out of GPU memory on this Space. 1024 is the reliable default.
86
 
87
- Want help writing longer prompts? An `expansion.txt` system prompt is provided in the [model repo](https://huggingface.co/krea/Krea-2-Turbo) for use with any LLM.
88
  """
89
 
90
  # Drawn from the official Krea 2 prompt guide. These demonstrate the
@@ -191,7 +183,7 @@ def _duration(prompt, negative_prompt, model, steps, guidance, width, height, se
191
  def generate(
192
  prompt,
193
  negative_prompt="",
194
- model="Turbo",
195
  steps=8,
196
  guidance=0,
197
  width=1024,
@@ -369,11 +361,11 @@ with gr.Blocks(title="Krea 2") as demo:
369
  <header id="krea-header">
370
  <div class="eyebrow">KREA · TEXT-TO-IMAGE</div>
371
  <h1>Krea 2</h1>
372
- <p class="subtitle">Generate images from natural language. Pick Raw for CFG-guided control or Turbo for fast, few-step results.</p>
373
  <div class="meta">
374
  <div class="badges">
375
- <span class="badge">Raw · CFG</span>
376
- <span class="badge">Turbo · few-step</span>
377
  </div>
378
  <div class="links">
379
  <a href="https://www.krea.ai/blog/krea-2-technical-report" target="_blank" rel="noopener">Technical report ↗</a>
@@ -393,7 +385,7 @@ with gr.Blocks(title="Krea 2") as demo:
393
  show_label=True,
394
  autofocus=True,
395
  )
396
- model = gr.Radio(["Turbo", "Raw"], value="Turbo", label="Model")
397
  run = gr.Button("Generate", variant="primary", elem_id="generate-btn")
398
 
399
  with gr.Accordion("Prompting tips", open=False):
@@ -410,7 +402,7 @@ with gr.Blocks(title="Krea 2") as demo:
410
  label="Negative prompt",
411
  lines=1,
412
  interactive=False,
413
- info="Available with Raw, where guidance is above 0.",
414
  )
415
  steps = gr.Slider(1, 50, value=8, step=1, label="Steps")
416
  guidance = gr.Slider(0.0, 10.0, value=0.0, step=0.1, label="Guidance scale")
@@ -460,4 +452,4 @@ with gr.Blocks(title="Krea 2") as demo:
460
  run.click(generate, inputs, [output, seed])
461
  prompt.submit(generate, inputs, [output, seed])
462
 
463
- demo.launch(theme=theme, css=CSS)
 
6
  import spaces
7
  import torch
8
  import gradio as gr
9
+ from huggingface_hub import hf_hub_download, login
10
 
11
  if os.environ.get("HF_TOKEN"):
12
  login(token=os.environ["HF_TOKEN"])
 
14
  from diffusers import Krea2Pipeline
15
 
16
  DTYPE = torch.bfloat16
17
+ MODEL_REPO = "Daankular/redcraft-krea2-fp8"
18
+ MODEL_FILE = "redcraftKREA2RedMix_krea2Edition.safetensors"
19
+ AOTI_REPO = "multimodalart/krea2-aoti-kernels"
20
+ AOTI_FILE = "Krea2TransformerBlock-lora-r64/package.pt2"
21
  MAX_SEED = 2**31 - 1
22
 
23
+ checkpoint_path = hf_hub_download(
24
+ repo_id=MODEL_REPO,
25
+ filename=MODEL_FILE,
26
+ token=os.environ.get("HF_TOKEN"),
 
 
 
 
 
27
  )
28
+ pipe_redcraft = Krea2Pipeline.from_single_file(checkpoint_path, torch_dtype=DTYPE)
29
+ pipe_redcraft.to("cuda")
30
 
31
 
32
 
33
  def _load_aoti():
34
+ # The compiled Krea2TransformerBlock kernels are public and separate from
35
+ # the checkpoint weights. If they cannot be loaded, the Space falls back to
36
+ # eager mode.
 
 
37
  from spaces.zero.torch.aoti import LazyAOTIModel
38
 
39
  pt2 = hf_hub_download(
40
+ repo_id=AOTI_REPO,
41
+ filename=AOTI_FILE,
42
  repo_type="dataset",
 
43
  )
44
  aoti_model = LazyAOTIModel(pt2)
45
+ for block in pipe_redcraft.transformer.modules():
46
+ if block.__class__.__name__ == "Krea2TransformerBlock":
47
+ spaces.aoti_patch(block, aoti_model)
 
48
 
49
 
50
  try:
 
53
  except Exception as e:
54
  print(f"AoTI load skipped ({e}); running eager.")
55
 
56
+ PIPES = {"Redcraft": pipe_redcraft}
57
  DEFAULTS = {
58
+ "Redcraft": {"steps": 8, "guidance": 0.0},
 
59
  }
60
 
61
  # Resolution presets. The model renders up to 2K, but the compiled transformer
 
76
  - To render text in the image, wrap the words in quotes, for example: a storefront window with a neon sign that reads "open late".
77
  - The model can render up to 2K, but very high resolutions may run out of GPU memory on this Space. 1024 is the reliable default.
78
 
79
+ This Space uses the Redcraft checkpoint from [Daankular/redcraft-krea2-fp8](https://huggingface.co/Daankular/redcraft-krea2-fp8).
80
  """
81
 
82
  # Drawn from the official Krea 2 prompt guide. These demonstrate the
 
183
  def generate(
184
  prompt,
185
  negative_prompt="",
186
+ model="Redcraft",
187
  steps=8,
188
  guidance=0,
189
  width=1024,
 
361
  <header id="krea-header">
362
  <div class="eyebrow">KREA · TEXT-TO-IMAGE</div>
363
  <h1>Krea 2</h1>
364
+ <p class="subtitle">Generate images from natural language with the Redcraft Krea 2 checkpoint.</p>
365
  <div class="meta">
366
  <div class="badges">
367
+ <span class="badge">Redcraft</span>
368
+ <span class="badge">Public AoTI kernels</span>
369
  </div>
370
  <div class="links">
371
  <a href="https://www.krea.ai/blog/krea-2-technical-report" target="_blank" rel="noopener">Technical report ↗</a>
 
385
  show_label=True,
386
  autofocus=True,
387
  )
388
+ model = gr.Radio(["Redcraft"], value="Redcraft", label="Model")
389
  run = gr.Button("Generate", variant="primary", elem_id="generate-btn")
390
 
391
  with gr.Accordion("Prompting tips", open=False):
 
402
  label="Negative prompt",
403
  lines=1,
404
  interactive=False,
405
+ info="Enabled when guidance is above 0.",
406
  )
407
  steps = gr.Slider(1, 50, value=8, step=1, label="Steps")
408
  guidance = gr.Slider(0.0, 10.0, value=0.0, step=0.1, label="Guidance scale")
 
452
  run.click(generate, inputs, [output, seed])
453
  prompt.submit(generate, inputs, [output, seed])
454
 
455
+ demo.launch(theme=theme, css=CSS)