multimodalart HF Staff commited on
Commit
406e0b0
·
verified ·
1 Parent(s): e801310

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +67 -64
app.py CHANGED
@@ -4,22 +4,25 @@ import spaces
4
  import torch
5
  import gradio as gr
6
  from huggingface_hub import hf_hub_download
7
- from diffusers import Krea2Pipeline, AutoencoderKLQwenImage
 
8
 
9
  TURBO_REPO = "krea/Krea-2-Turbo"
10
  HD_VAE_REPO = "wikeeyang/Krea2-Turbo-HD-V1"
11
  MAX_SEED = 2**31 - 1
12
 
13
- # Load the Krea-2-Turbo pipeline (full diffusers format) and swap in the HD VAE
14
- # from wikeeyang/Krea2-Turbo-HD-V1 for enhanced detail, clarity, and contrast.
 
 
 
15
  pipe = Krea2Pipeline.from_pretrained(TURBO_REPO, torch_dtype=torch.bfloat16)
16
 
17
- # Load and replace the VAE with the HD-optimized version
18
  hd_vae_path = hf_hub_download(HD_VAE_REPO, "Krea2-HD-vae.safetensors")
19
- hd_vae = AutoencoderKLQwenImage.from_single_file(
20
- hd_vae_path, torch_dtype=torch.bfloat16
21
- )
22
- pipe.vae = hd_vae
23
 
24
  pipe.to("cuda")
25
 
@@ -79,51 +82,6 @@ CSS = """
79
  .dark .gradio-container { color: var(--body-text-color); }
80
  """
81
 
82
- with gr.Blocks(theme=gr.themes.Citrus(), css=CSS) as demo:
83
- with gr.Column(elem_id="col-container"):
84
- gr.Markdown(
85
- "# Krea 2 Turbo HD\n"
86
- "Text-to-image generation with the HD-optimized VAE from "
87
- "[wikeeyang/Krea2-Turbo-HD-V1](https://huggingface.co/wikeeyang/Krea2-Turbo-HD-V1), "
88
- "built on [krea/Krea-2-Turbo](https://huggingface.co/krea/Krea-2-Turbo). "
89
- "The fine-tuned VAE enhances detail rendering, clarity, and contrast."
90
- )
91
-
92
- with gr.Row():
93
- prompt = gr.Textbox(
94
- show_label=False,
95
- placeholder=PLACEHOLDER,
96
- container=False,
97
- scale=4,
98
- lines=3,
99
- autofocus=True,
100
- )
101
- run = gr.Button("Generate", variant="primary", scale=1)
102
-
103
- output = gr.Image(label="Result", format="png")
104
-
105
- with gr.Accordion("Advanced settings", open=False):
106
- with gr.Row():
107
- width = gr.Slider(512, 2048, value=1024, step=16, label="Width")
108
- height = gr.Slider(512, 2048, value=1024, step=16, label="Height")
109
- steps = gr.Slider(1, 50, value=8, step=1, label="Steps")
110
- with gr.Row():
111
- seed = gr.Slider(0, MAX_SEED, value=42, step=1, label="Seed")
112
- randomize = gr.Checkbox(value=True, label="Randomize seed")
113
-
114
- with gr.Accordion("Prompting tips", open=False):
115
- gr.Markdown(PROMPT_TIPS)
116
-
117
- gr.Examples(
118
- examples=EXAMPLE_PROMPTS,
119
- inputs=[prompt],
120
- outputs=output,
121
- fn=lambda p: generate(p),
122
- cache_examples=True,
123
- cache_mode="lazy",
124
- label="Example prompts",
125
- )
126
-
127
 
128
  def _duration(prompt, width, height, steps, seed, randomize):
129
  """Estimate GPU time based on steps and pixel area."""
@@ -174,16 +132,61 @@ def generate(
174
  return image, seed
175
 
176
 
177
- run.click(
178
- generate,
179
- inputs=[prompt, width, height, steps, seed, randomize],
180
- outputs=[output, seed],
181
- api_name="generate",
182
- )
183
- prompt.submit(
184
- generate,
185
- inputs=[prompt, width, height, steps, seed, randomize],
186
- outputs=[output, seed],
187
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
  demo.launch(mcp_server=True)
 
4
  import torch
5
  import gradio as gr
6
  from huggingface_hub import hf_hub_download
7
+ from safetensors.torch import load_file as load_safetensors
8
+ from diffusers import Krea2Pipeline
9
 
10
  TURBO_REPO = "krea/Krea-2-Turbo"
11
  HD_VAE_REPO = "wikeeyang/Krea2-Turbo-HD-V1"
12
  MAX_SEED = 2**31 - 1
13
 
14
+ # Load the Krea-2-Turbo pipeline (full diffusers format) then swap in the
15
+ # HD-optimized VAE from wikeeyang/Krea2-Turbo-HD-V1 for enhanced detail,
16
+ # clarity, and contrast. The HD VAE is a single safetensors checkpoint of the
17
+ # same AutoencoderKLQwenImage architecture, so we load the base pipeline's VAE
18
+ # (which has the proper config) and overwrite its weights.
19
  pipe = Krea2Pipeline.from_pretrained(TURBO_REPO, torch_dtype=torch.bfloat16)
20
 
21
+ # Load the HD VAE weights and replace the pipeline's VAE state dict
22
  hd_vae_path = hf_hub_download(HD_VAE_REPO, "Krea2-HD-vae.safetensors")
23
+ hd_vae_state = load_safetensors(hd_vae_path)
24
+ pipe.vae.load_state_dict(hd_vae_state, strict=True)
25
+ print("HD VAE weights loaded successfully.")
 
26
 
27
  pipe.to("cuda")
28
 
 
82
  .dark .gradio-container { color: var(--body-text-color); }
83
  """
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  def _duration(prompt, width, height, steps, seed, randomize):
87
  """Estimate GPU time based on steps and pixel area."""
 
132
  return image, seed
133
 
134
 
135
+ with gr.Blocks(theme=gr.themes.Citrus(), css=CSS) as demo:
136
+ with gr.Column(elem_id="col-container"):
137
+ gr.Markdown(
138
+ "# Krea 2 Turbo HD\n"
139
+ "Text-to-image generation with the HD-optimized VAE from "
140
+ "[wikeeyang/Krea2-Turbo-HD-V1](https://huggingface.co/wikeeyang/Krea2-Turbo-HD-V1), "
141
+ "built on [krea/Krea-2-Turbo](https://huggingface.co/krea/Krea-2-Turbo). "
142
+ "The fine-tuned VAE enhances detail rendering, clarity, and contrast."
143
+ )
144
+
145
+ with gr.Row():
146
+ prompt = gr.Textbox(
147
+ show_label=False,
148
+ placeholder=PLACEHOLDER,
149
+ container=False,
150
+ scale=4,
151
+ lines=3,
152
+ autofocus=True,
153
+ )
154
+ run = gr.Button("Generate", variant="primary", scale=1)
155
+
156
+ output = gr.Image(label="Result", format="png")
157
+
158
+ with gr.Accordion("Advanced settings", open=False):
159
+ with gr.Row():
160
+ width = gr.Slider(512, 2048, value=1024, step=16, label="Width")
161
+ height = gr.Slider(512, 2048, value=1024, step=16, label="Height")
162
+ steps = gr.Slider(1, 50, value=8, step=1, label="Steps")
163
+ with gr.Row():
164
+ seed = gr.Slider(0, MAX_SEED, value=42, step=1, label="Seed")
165
+ randomize = gr.Checkbox(value=True, label="Randomize seed")
166
+
167
+ with gr.Accordion("Prompting tips", open=False):
168
+ gr.Markdown(PROMPT_TIPS)
169
+
170
+ gr.Examples(
171
+ examples=EXAMPLE_PROMPTS,
172
+ inputs=[prompt],
173
+ outputs=[output, seed],
174
+ fn=generate,
175
+ cache_examples=True,
176
+ cache_mode="lazy",
177
+ label="Example prompts",
178
+ )
179
+
180
+ run.click(
181
+ generate,
182
+ inputs=[prompt, width, height, steps, seed, randomize],
183
+ outputs=[output, seed],
184
+ api_name="generate",
185
+ )
186
+ prompt.submit(
187
+ generate,
188
+ inputs=[prompt, width, height, steps, seed, randomize],
189
+ outputs=[output, seed],
190
+ )
191
 
192
  demo.launch(mcp_server=True)