Spaces:
Running on Zero
Running on Zero
xlarge GPU + bf16 transformers + upsampling default-on + gr.JSON caption + seed-in-field + non-fatal warmup
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
|
|
| 7 |
_HERE = os.path.dirname(os.path.abspath(__file__))
|
| 8 |
sys.path.insert(0, os.path.join(_HERE, "diffusers_src", "src"))
|
| 9 |
|
|
|
|
| 10 |
import random
|
| 11 |
from typing import List, Literal, Union
|
| 12 |
|
|
@@ -159,7 +160,7 @@ def upsample_prompt(prompt: str, width: int, height: int) -> str:
|
|
| 159 |
)[0].strip()
|
| 160 |
|
| 161 |
|
| 162 |
-
@spaces.GPU(duration=240)
|
| 163 |
def generate(
|
| 164 |
prompt: str,
|
| 165 |
mode: str,
|
|
@@ -179,6 +180,7 @@ def generate(
|
|
| 179 |
gr.Warning("`outlines` is not installed — upsampling without structural constraints.")
|
| 180 |
final_prompt = upsample_prompt(prompt, int(width), int(height))
|
| 181 |
|
|
|
|
| 182 |
generator = torch.Generator(device="cuda").manual_seed(int(seed))
|
| 183 |
preset = MODES.get(mode, MODES["Default · 20 steps"])
|
| 184 |
image = pipe(
|
|
@@ -188,14 +190,19 @@ def generate(
|
|
| 188 |
generator=generator,
|
| 189 |
**preset,
|
| 190 |
).images[0]
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
|
| 194 |
-
@spaces.GPU
|
| 195 |
def _warmup():
|
| 196 |
"""Force the upsampler + pipeline onto GPU and warm their kernels at STARTUP, so request #1
|
| 197 |
isn't slow. On ZeroGPU, module-level loading is CPU-only; GPU placement + JIT warmup otherwise
|
| 198 |
happen on the first request."""
|
|
|
|
| 199 |
try:
|
| 200 |
if ENHANCER is not None:
|
| 201 |
upsample_prompt("a red apple on a wooden table", 1024, 1024)
|
|
@@ -210,7 +217,10 @@ def _warmup():
|
|
| 210 |
print(f"[warmup] pipeline warmup skipped: {e!r}", flush=True)
|
| 211 |
|
| 212 |
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
|
| 216 |
with gr.Blocks(theme=gr.themes.Citrus(), title="Ideogram 4 (NF4) — diffusers preview") as demo:
|
|
@@ -238,7 +248,7 @@ with gr.Blocks(theme=gr.themes.Citrus(), title="Ideogram 4 (NF4) — diffusers p
|
|
| 238 |
with gr.Accordion("Advanced", open=False):
|
| 239 |
enhance = gr.Checkbox(
|
| 240 |
label="Prompt upsampling (Outlines)",
|
| 241 |
-
value=
|
| 242 |
info="Rewrite the prompt into Ideogram's native JSON caption before generating."
|
| 243 |
+ ("" if OUTLINES_AVAILABLE else " ⚠ outlines not installed — runs unconstrained."),
|
| 244 |
)
|
|
@@ -250,16 +260,12 @@ with gr.Blocks(theme=gr.themes.Citrus(), title="Ideogram 4 (NF4) — diffusers p
|
|
| 250 |
randomize = gr.Checkbox(label="Randomize seed", value=True)
|
| 251 |
with gr.Column():
|
| 252 |
out_image = gr.Image(label="Output", type="pil")
|
| 253 |
-
|
| 254 |
-
out_caption = gr.Textbox(
|
| 255 |
-
label="Caption fed to the model (upsampled when enabled)",
|
| 256 |
-
lines=4,
|
| 257 |
-
)
|
| 258 |
|
| 259 |
run.click(
|
| 260 |
generate,
|
| 261 |
inputs=[prompt, mode, enhance, width, height, seed, randomize],
|
| 262 |
-
outputs=[out_image,
|
| 263 |
)
|
| 264 |
|
| 265 |
demo.queue().launch()
|
|
|
|
| 7 |
_HERE = os.path.dirname(os.path.abspath(__file__))
|
| 8 |
sys.path.insert(0, os.path.join(_HERE, "diffusers_src", "src"))
|
| 9 |
|
| 10 |
+
import json
|
| 11 |
import random
|
| 12 |
from typing import List, Literal, Union
|
| 13 |
|
|
|
|
| 160 |
)[0].strip()
|
| 161 |
|
| 162 |
|
| 163 |
+
@spaces.GPU(duration=240, size="xlarge")
|
| 164 |
def generate(
|
| 165 |
prompt: str,
|
| 166 |
mode: str,
|
|
|
|
| 180 |
gr.Warning("`outlines` is not installed — upsampling without structural constraints.")
|
| 181 |
final_prompt = upsample_prompt(prompt, int(width), int(height))
|
| 182 |
|
| 183 |
+
_ensure_bf16_transformers()
|
| 184 |
generator = torch.Generator(device="cuda").manual_seed(int(seed))
|
| 185 |
preset = MODES.get(mode, MODES["Default · 20 steps"])
|
| 186 |
image = pipe(
|
|
|
|
| 190 |
generator=generator,
|
| 191 |
**preset,
|
| 192 |
).images[0]
|
| 193 |
+
try:
|
| 194 |
+
caption = json.loads(final_prompt)
|
| 195 |
+
except Exception:
|
| 196 |
+
caption = {"prompt": final_prompt}
|
| 197 |
+
return image, seed, caption
|
| 198 |
|
| 199 |
|
| 200 |
+
@spaces.GPU(size="xlarge")
|
| 201 |
def _warmup():
|
| 202 |
"""Force the upsampler + pipeline onto GPU and warm their kernels at STARTUP, so request #1
|
| 203 |
isn't slow. On ZeroGPU, module-level loading is CPU-only; GPU placement + JIT warmup otherwise
|
| 204 |
happen on the first request."""
|
| 205 |
+
_ensure_bf16_transformers()
|
| 206 |
try:
|
| 207 |
if ENHANCER is not None:
|
| 208 |
upsample_prompt("a red apple on a wooden table", 1024, 1024)
|
|
|
|
| 217 |
print(f"[warmup] pipeline warmup skipped: {e!r}", flush=True)
|
| 218 |
|
| 219 |
|
| 220 |
+
try:
|
| 221 |
+
_warmup()
|
| 222 |
+
except Exception as e: # a flaky ZeroGPU worker (e.g. ECC) must not take down the Space
|
| 223 |
+
print(f"[warmup] failed (will warm lazily on first request): {e!r}", flush=True)
|
| 224 |
|
| 225 |
|
| 226 |
with gr.Blocks(theme=gr.themes.Citrus(), title="Ideogram 4 (NF4) — diffusers preview") as demo:
|
|
|
|
| 248 |
with gr.Accordion("Advanced", open=False):
|
| 249 |
enhance = gr.Checkbox(
|
| 250 |
label="Prompt upsampling (Outlines)",
|
| 251 |
+
value=True,
|
| 252 |
info="Rewrite the prompt into Ideogram's native JSON caption before generating."
|
| 253 |
+ ("" if OUTLINES_AVAILABLE else " ⚠ outlines not installed — runs unconstrained."),
|
| 254 |
)
|
|
|
|
| 260 |
randomize = gr.Checkbox(label="Randomize seed", value=True)
|
| 261 |
with gr.Column():
|
| 262 |
out_image = gr.Image(label="Output", type="pil")
|
| 263 |
+
out_caption = gr.JSON(label="Caption fed to the model (upsampled when enabled)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
run.click(
|
| 266 |
generate,
|
| 267 |
inputs=[prompt, mode, enhance, width, height, seed, randomize],
|
| 268 |
+
outputs=[out_image, seed, out_caption],
|
| 269 |
)
|
| 270 |
|
| 271 |
demo.queue().launch()
|