Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -73,13 +73,14 @@ DEFAULTS = {
|
|
| 73 |
"Turbo": {"steps": 8, "guidance": 0.0},
|
| 74 |
}
|
| 75 |
|
| 76 |
-
# Resolution presets. The
|
| 77 |
-
#
|
|
|
|
| 78 |
RESOLUTIONS = {
|
| 79 |
"Square · 1024": (1024, 1024),
|
|
|
|
|
|
|
| 80 |
"Square · 2K": (2048, 2048),
|
| 81 |
-
"Portrait · 2K": (1536, 2048),
|
| 82 |
-
"Landscape · 2K": (2048, 1536),
|
| 83 |
}
|
| 84 |
|
| 85 |
PROMPT_TIPS = """\
|
|
@@ -88,7 +89,7 @@ Krea 2 is tuned for natural language. Describe the image the way you would descr
|
|
| 88 |
- Write in full sentences or rich phrases. Longer, more specific prompts give the best results, but short prompts work too.
|
| 89 |
- Name the things that matter: subject, setting, lighting, color, framing, medium, and mood.
|
| 90 |
- To render text in the image, wrap the words in quotes, for example: a storefront window with a neon sign that reads "open late".
|
| 91 |
-
-
|
| 92 |
|
| 93 |
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.
|
| 94 |
"""
|
|
@@ -162,7 +163,10 @@ PLACEHOLDER = (
|
|
| 162 |
|
| 163 |
|
| 164 |
def _duration(prompt, negative_prompt, model, steps, guidance, width, height, seed, randomize, progress=None):
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
|
| 168 |
@spaces.GPU(duration=_duration, size="xlarge")
|
|
@@ -185,15 +189,25 @@ def generate(
|
|
| 185 |
seed = int(seed)
|
| 186 |
generator = torch.Generator("cuda").manual_seed(seed)
|
| 187 |
pipe = PIPES[model]
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
return image, seed
|
| 198 |
|
| 199 |
|
|
@@ -279,7 +293,15 @@ CSS = """
|
|
| 279 |
margin: 0;
|
| 280 |
max-width: 60ch;
|
| 281 |
}
|
| 282 |
-
#krea-header .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
#krea-header .badge {
|
| 284 |
font-family: 'JetBrains Mono', ui-monospace, monospace;
|
| 285 |
font-size: 10px;
|
|
@@ -290,10 +312,17 @@ CSS = """
|
|
| 290 |
border-radius: 999px;
|
| 291 |
padding: 4px 10px;
|
| 292 |
}
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
#generate-btn { font-weight: 600; letter-spacing: 0.01em; }
|
| 299 |
|
|
@@ -310,10 +339,16 @@ with gr.Blocks(theme=theme, css=CSS, title="Krea 2", fill_height=True) as demo:
|
|
| 310 |
<header id="krea-header">
|
| 311 |
<div class="eyebrow">KREA · TEXT-TO-IMAGE</div>
|
| 312 |
<h1>Krea 2</h1>
|
| 313 |
-
<p class="subtitle">Generate images from natural language. Pick Raw for CFG-guided control or Turbo for fast, few-step results
|
| 314 |
-
<div class="
|
| 315 |
-
<
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
</div>
|
| 318 |
</header>
|
| 319 |
"""
|
|
@@ -336,7 +371,7 @@ with gr.Blocks(theme=theme, css=CSS, title="Krea 2", fill_height=True) as demo:
|
|
| 336 |
|
| 337 |
resolution = gr.Radio(
|
| 338 |
list(RESOLUTIONS.keys()),
|
| 339 |
-
value="Square ·
|
| 340 |
label="Resolution",
|
| 341 |
)
|
| 342 |
|
|
@@ -350,8 +385,8 @@ with gr.Blocks(theme=theme, css=CSS, title="Krea 2", fill_height=True) as demo:
|
|
| 350 |
steps = gr.Slider(1, 50, value=8, step=1, label="Steps")
|
| 351 |
guidance = gr.Slider(0.0, 10.0, value=0.0, step=0.1, label="Guidance scale")
|
| 352 |
with gr.Row():
|
| 353 |
-
width = gr.Slider(512, 2048, value=
|
| 354 |
-
height = gr.Slider(512, 2048, value=
|
| 355 |
with gr.Row():
|
| 356 |
seed = gr.Slider(0, MAX_SEED, value=0, step=1, label="Seed")
|
| 357 |
randomize = gr.Checkbox(value=True, label="Randomize seed")
|
|
|
|
| 73 |
"Turbo": {"steps": 8, "guidance": 0.0},
|
| 74 |
}
|
| 75 |
|
| 76 |
+
# Resolution presets. The model renders up to 2K, but the compiled transformer
|
| 77 |
+
# block can exceed this Space's GPU memory above 1024, so 1024 is the default
|
| 78 |
+
# and larger sizes are opt-in (see the OOM guard in generate).
|
| 79 |
RESOLUTIONS = {
|
| 80 |
"Square · 1024": (1024, 1024),
|
| 81 |
+
"Portrait · 1024": (832, 1216),
|
| 82 |
+
"Landscape · 1024": (1216, 832),
|
| 83 |
"Square · 2K": (2048, 2048),
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
PROMPT_TIPS = """\
|
|
|
|
| 89 |
- Write in full sentences or rich phrases. Longer, more specific prompts give the best results, but short prompts work too.
|
| 90 |
- Name the things that matter: subject, setting, lighting, color, framing, medium, and mood.
|
| 91 |
- To render text in the image, wrap the words in quotes, for example: a storefront window with a neon sign that reads "open late".
|
| 92 |
+
- 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.
|
| 93 |
|
| 94 |
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.
|
| 95 |
"""
|
|
|
|
| 163 |
|
| 164 |
|
| 165 |
def _duration(prompt, negative_prompt, model, steps, guidance, width, height, seed, randomize, progress=None):
|
| 166 |
+
# Scale the GPU reservation by step count and pixel area so larger renders
|
| 167 |
+
# are not killed before they finish.
|
| 168 |
+
megapixels = max(1.0, (int(width) * int(height)) / (1024 * 1024))
|
| 169 |
+
return int(int(steps) * 2 * megapixels + 25)
|
| 170 |
|
| 171 |
|
| 172 |
@spaces.GPU(duration=_duration, size="xlarge")
|
|
|
|
| 189 |
seed = int(seed)
|
| 190 |
generator = torch.Generator("cuda").manual_seed(seed)
|
| 191 |
pipe = PIPES[model]
|
| 192 |
+
try:
|
| 193 |
+
image = pipe(
|
| 194 |
+
prompt=prompt,
|
| 195 |
+
negative_prompt=(negative_prompt or None) if guidance > 0 else None,
|
| 196 |
+
height=int(height),
|
| 197 |
+
width=int(width),
|
| 198 |
+
num_inference_steps=int(steps),
|
| 199 |
+
guidance_scale=float(guidance),
|
| 200 |
+
generator=generator,
|
| 201 |
+
).images[0]
|
| 202 |
+
except RuntimeError as exc:
|
| 203 |
+
# At high resolution the compiled transformer block can exhaust GPU
|
| 204 |
+
# memory, which surfaces as a CUDA allocation / AOTI runtime error.
|
| 205 |
+
# Recover the worker and tell the user how to fix it.
|
| 206 |
+
torch.cuda.empty_cache()
|
| 207 |
+
raise gr.Error(
|
| 208 |
+
f"Generation failed at {int(width)}x{int(height)}. This is usually the GPU running "
|
| 209 |
+
"out of memory at high resolution. Try 1024x1024 or a smaller size."
|
| 210 |
+
) from exc
|
| 211 |
return image, seed
|
| 212 |
|
| 213 |
|
|
|
|
| 293 |
margin: 0;
|
| 294 |
max-width: 60ch;
|
| 295 |
}
|
| 296 |
+
#krea-header .meta {
|
| 297 |
+
margin-top: 18px;
|
| 298 |
+
display: flex;
|
| 299 |
+
justify-content: space-between;
|
| 300 |
+
align-items: center;
|
| 301 |
+
flex-wrap: wrap;
|
| 302 |
+
gap: 12px;
|
| 303 |
+
}
|
| 304 |
+
#krea-header .badges { display: flex; gap: 8px; }
|
| 305 |
#krea-header .badge {
|
| 306 |
font-family: 'JetBrains Mono', ui-monospace, monospace;
|
| 307 |
font-size: 10px;
|
|
|
|
| 312 |
border-radius: 999px;
|
| 313 |
padding: 4px 10px;
|
| 314 |
}
|
| 315 |
+
#krea-header .links { display: flex; gap: 16px; }
|
| 316 |
+
#krea-header .links a {
|
| 317 |
+
font-family: 'JetBrains Mono', ui-monospace, monospace;
|
| 318 |
+
font-size: 11px;
|
| 319 |
+
letter-spacing: 0.08em;
|
| 320 |
+
text-transform: uppercase;
|
| 321 |
+
color: #737373;
|
| 322 |
+
text-decoration: none;
|
| 323 |
+
transition: color 0.15s ease;
|
| 324 |
+
}
|
| 325 |
+
#krea-header .links a:hover { color: #f5f5f5; }
|
| 326 |
|
| 327 |
#generate-btn { font-weight: 600; letter-spacing: 0.01em; }
|
| 328 |
|
|
|
|
| 339 |
<header id="krea-header">
|
| 340 |
<div class="eyebrow">KREA · TEXT-TO-IMAGE</div>
|
| 341 |
<h1>Krea 2</h1>
|
| 342 |
+
<p class="subtitle">Generate images from natural language. Pick Raw for CFG-guided control or Turbo for fast, few-step results.</p>
|
| 343 |
+
<div class="meta">
|
| 344 |
+
<div class="badges">
|
| 345 |
+
<span class="badge">Raw · CFG</span>
|
| 346 |
+
<span class="badge">Turbo · few-step</span>
|
| 347 |
+
</div>
|
| 348 |
+
<div class="links">
|
| 349 |
+
<a href="https://www.krea.ai/blog/krea-2-technical-report" target="_blank" rel="noopener">Technical report ↗</a>
|
| 350 |
+
<a href="https://github.com/krea-ai/krea-2" target="_blank" rel="noopener">GitHub ↗</a>
|
| 351 |
+
</div>
|
| 352 |
</div>
|
| 353 |
</header>
|
| 354 |
"""
|
|
|
|
| 371 |
|
| 372 |
resolution = gr.Radio(
|
| 373 |
list(RESOLUTIONS.keys()),
|
| 374 |
+
value="Square · 1024",
|
| 375 |
label="Resolution",
|
| 376 |
)
|
| 377 |
|
|
|
|
| 385 |
steps = gr.Slider(1, 50, value=8, step=1, label="Steps")
|
| 386 |
guidance = gr.Slider(0.0, 10.0, value=0.0, step=0.1, label="Guidance scale")
|
| 387 |
with gr.Row():
|
| 388 |
+
width = gr.Slider(512, 2048, value=1024, step=16, label="Width")
|
| 389 |
+
height = gr.Slider(512, 2048, value=1024, step=16, label="Height")
|
| 390 |
with gr.Row():
|
| 391 |
seed = gr.Slider(0, MAX_SEED, value=0, step=1, label="Seed")
|
| 392 |
randomize = gr.Checkbox(value=True, label="Randomize seed")
|