yingzhac commited on
Commit
74862c5
·
verified ·
1 Parent(s): f5b9cdf

Create Krea 2 Turbo demo

Browse files

Use the Z-Image demo control layout with Krea2Pipeline and Krea 2 Turbo defaults. Hardware still needs ZeroGPU because namespace quota is full.

Files changed (3) hide show
  1. README.md +12 -8
  2. app.py +233 -0
  3. requirements.txt +5 -0
README.md CHANGED
@@ -1,13 +1,17 @@
1
  ---
2
- title: Krea 2 Demo
3
- emoji: 🔥
4
- colorFrom: yellow
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 6.19.0
8
- python_version: '3.13'
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 Turbo Demo
3
+ emoji: 🎨
4
+ colorFrom: indigo
5
+ colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 6.15.1
 
8
  app_file: app.py
9
+ python_version: "3.12"
10
+ startup_duration_timeout: 1h
11
+ pinned: true
12
+ short_description: Krea 2 Turbo text-to-image demo with the Z-Image UI layout
13
+ models:
14
+ - krea/Krea-2-Turbo
15
  ---
16
 
17
+ Krea 2 Turbo text-to-image demo using the same compact control layout as the Z-Image demo.
app.py ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ import torch
4
+ from diffusers import Krea2Pipeline
5
+
6
+
7
+ MODEL_ID = "krea/Krea-2-Turbo"
8
+ DTYPE = torch.bfloat16
9
+ MAX_SEED = 2**31 - 1
10
+
11
+ pipe = None
12
+
13
+
14
+ def get_pipe():
15
+ global pipe
16
+ if pipe is None:
17
+ if not torch.cuda.is_available():
18
+ raise RuntimeError("CUDA is not available. Set this Space hardware to ZeroGPU before generating.")
19
+ print(f"Loading {MODEL_ID} pipeline...")
20
+ pipe = Krea2Pipeline.from_pretrained(MODEL_ID, torch_dtype=DTYPE).to("cuda")
21
+ print("Pipeline loaded!")
22
+ return pipe
23
+
24
+
25
+ if torch.cuda.is_available():
26
+ get_pipe()
27
+ else:
28
+ print("CUDA is not available at startup. The UI will load, but generation requires ZeroGPU hardware.")
29
+
30
+
31
+ def gpu_duration(
32
+ prompt,
33
+ negative_prompt,
34
+ height,
35
+ width,
36
+ num_inference_steps,
37
+ guidance_scale,
38
+ seed,
39
+ randomize_seed,
40
+ progress=None,
41
+ ):
42
+ megapixels = max(1.0, (int(width) * int(height)) / (1024 * 1024))
43
+ return min(300, int(int(num_inference_steps) * 8 * megapixels * 4 + 180))
44
+
45
+
46
+ @spaces.GPU(duration=gpu_duration, size="xlarge")
47
+ def generate_image(
48
+ prompt,
49
+ negative_prompt,
50
+ height,
51
+ width,
52
+ num_inference_steps,
53
+ guidance_scale,
54
+ seed,
55
+ randomize_seed,
56
+ progress=gr.Progress(track_tqdm=True),
57
+ ):
58
+ """Generate 4 Krea 2 Turbo images with seeds: seed, 2x, 3x, 4x."""
59
+ if not prompt or not str(prompt).strip():
60
+ raise gr.Error("Enter a prompt to generate images.")
61
+ if not torch.cuda.is_available():
62
+ raise gr.Error("CUDA is not available. Set this Space hardware to ZeroGPU before generating.")
63
+
64
+ pipe = get_pipe()
65
+ if randomize_seed:
66
+ seed = torch.randint(0, MAX_SEED, (1,)).item()
67
+
68
+ base_seed = int(seed) % MAX_SEED
69
+ seeds = [(base_seed * i) % MAX_SEED for i in range(1, 5)]
70
+
71
+ guidance = float(guidance_scale)
72
+ neg_prompt = None
73
+ if guidance > 0 and isinstance(negative_prompt, str) and negative_prompt.strip():
74
+ neg_prompt = negative_prompt
75
+
76
+ images = []
77
+ try:
78
+ for current_seed in seeds:
79
+ generator = torch.Generator("cuda").manual_seed(int(current_seed))
80
+ image = pipe(
81
+ prompt=str(prompt).strip(),
82
+ negative_prompt=neg_prompt,
83
+ height=int(height),
84
+ width=int(width),
85
+ num_inference_steps=int(num_inference_steps),
86
+ guidance_scale=guidance,
87
+ generator=generator,
88
+ ).images[0]
89
+ images.append(image)
90
+ except RuntimeError as exc:
91
+ torch.cuda.empty_cache()
92
+ raise gr.Error(
93
+ f"Generation failed at {int(width)}x{int(height)}. Try 1024x1024, fewer images, or fewer steps."
94
+ ) from exc
95
+
96
+ return images, ", ".join(str(s) for s in seeds)
97
+
98
+
99
+ examples = [
100
+ [
101
+ "A russet harvest mouse clinging to a branch, macro photograph, shallow depth of field, "
102
+ "creamy green bokeh, soft natural light"
103
+ ],
104
+ [
105
+ 'A quiet city bookstore at night, rain on the windows, a neon sign that reads "open late", '
106
+ "cinematic warm lighting, detailed reflections"
107
+ ],
108
+ [
109
+ "A fashion editorial portrait of a model wearing sculptural silver fabric, clean studio backdrop, "
110
+ "softbox lighting, high-end magazine photography"
111
+ ],
112
+ [
113
+ "A whimsical hand-drawn village built inside a giant teacup, watercolor texture, cozy evening light"
114
+ ],
115
+ ]
116
+
117
+
118
+ with gr.Blocks(title="Krea 2 Turbo Demo") as demo:
119
+ gr.Markdown(
120
+ """
121
+ # 🎨 Krea 2 Turbo Demo
122
+
123
+ Generate images with [krea/Krea-2-Turbo](https://huggingface.co/krea/Krea-2-Turbo).
124
+ Turbo is the fast distilled Krea 2 checkpoint; the recommended default is 8 steps with CFG 0.0.
125
+ """
126
+ )
127
+
128
+ with gr.Row():
129
+ with gr.Column(scale=1):
130
+ prompt = gr.Textbox(
131
+ label="Prompt",
132
+ placeholder='Describe the image in natural language. Wrap rendered text in quotes, e.g. a sign that reads "open late".',
133
+ lines=4,
134
+ )
135
+
136
+ negative_prompt = gr.Textbox(
137
+ label="Negative Prompt",
138
+ placeholder="Only used when CFG Guidance Scale is above 0.",
139
+ lines=3,
140
+ )
141
+
142
+ with gr.Row():
143
+ height = gr.Slider(
144
+ minimum=512,
145
+ maximum=1536,
146
+ value=1024,
147
+ step=16,
148
+ label="Height",
149
+ )
150
+ width = gr.Slider(
151
+ minimum=512,
152
+ maximum=1536,
153
+ value=1024,
154
+ step=16,
155
+ label="Width",
156
+ )
157
+
158
+ with gr.Row():
159
+ num_inference_steps = gr.Slider(
160
+ minimum=1,
161
+ maximum=28,
162
+ value=8,
163
+ step=1,
164
+ label="Inference Steps",
165
+ info="Krea 2 Turbo is designed for 8 steps.",
166
+ )
167
+
168
+ guidance_scale = gr.Slider(
169
+ minimum=0.0,
170
+ maximum=5.0,
171
+ value=0.0,
172
+ step=0.1,
173
+ label="CFG Guidance Scale",
174
+ info="Turbo default is 0.0. Negative prompt is ignored when CFG is 0.",
175
+ )
176
+
177
+ with gr.Row():
178
+ seed = gr.Number(
179
+ label="Seed",
180
+ value=42,
181
+ precision=0,
182
+ )
183
+ randomize_seed = gr.Checkbox(
184
+ label="Randomize Seed",
185
+ value=False,
186
+ )
187
+
188
+ generate_btn = gr.Button("🚀 Generate", variant="primary", size="lg")
189
+
190
+ with gr.Column(scale=1):
191
+ output_images = gr.Gallery(
192
+ label="Generated Images",
193
+ columns=2,
194
+ rows=2,
195
+ preview=True,
196
+ )
197
+ used_seeds = gr.Textbox(
198
+ label="Seeds Used (base, 2x, 3x, 4x)",
199
+ interactive=False,
200
+ )
201
+
202
+ gr.Markdown("### 💡 Example Prompts")
203
+ gr.Examples(
204
+ examples=examples,
205
+ inputs=[prompt],
206
+ cache_examples=False,
207
+ )
208
+
209
+ gr.Markdown(
210
+ "Model by [Krea](https://huggingface.co/krea). "
211
+ "This Space follows the Krea 2 Community License and uses the Turbo checkpoint for demo inference."
212
+ )
213
+
214
+ inputs = [prompt, negative_prompt, height, width, num_inference_steps, guidance_scale, seed, randomize_seed]
215
+ outputs = [output_images, used_seeds]
216
+
217
+ generate_btn.click(
218
+ fn=generate_image,
219
+ inputs=inputs,
220
+ outputs=outputs,
221
+ api_name="generate_image",
222
+ )
223
+
224
+ prompt.submit(
225
+ fn=generate_image,
226
+ inputs=inputs,
227
+ outputs=outputs,
228
+ api_name="generate_image_submit",
229
+ )
230
+
231
+
232
+ if __name__ == "__main__":
233
+ demo.launch(mcp_server=True, show_error=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio[mcp]==6.15.1
2
+ transformers>=4.57.0
3
+ accelerate
4
+ sentencepiece
5
+ git+https://github.com/huggingface/diffusers.git