Spaces:
Running on Zero
Running on Zero
update app
Browse files
app.py
CHANGED
|
@@ -16,39 +16,34 @@ from gradio.themes.utils import colors, fonts, sizes
|
|
| 16 |
|
| 17 |
# ── Theme ─────────────────────────────────────────────────────────────────────
|
| 18 |
|
| 19 |
-
colors.
|
| 20 |
-
name="
|
| 21 |
-
c50="#
|
| 22 |
-
c100="#
|
| 23 |
-
c200="#
|
| 24 |
-
c300="#
|
| 25 |
-
c400="#
|
| 26 |
-
c500="#
|
| 27 |
-
c600="#
|
| 28 |
-
c700="#
|
| 29 |
-
c800="#
|
| 30 |
-
c900="#
|
| 31 |
-
c950="#
|
| 32 |
)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
class FluxTheme(Soft):
|
| 36 |
def __init__(
|
| 37 |
self,
|
| 38 |
*,
|
| 39 |
-
primary_hue: colors.Color | str = colors.
|
| 40 |
-
secondary_hue: colors.Color | str = colors.
|
| 41 |
neutral_hue: colors.Color | str = colors.slate,
|
| 42 |
text_size: sizes.Size | str = sizes.text_lg,
|
| 43 |
font: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 44 |
-
fonts.GoogleFont("
|
| 45 |
-
"Arial",
|
| 46 |
-
"sans-serif",
|
| 47 |
),
|
| 48 |
font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 49 |
-
fonts.GoogleFont("
|
| 50 |
-
"ui-monospace",
|
| 51 |
-
"monospace",
|
| 52 |
),
|
| 53 |
):
|
| 54 |
super().__init__(
|
|
@@ -87,8 +82,7 @@ class FluxTheme(Soft):
|
|
| 87 |
block_label_background_fill="*primary_200",
|
| 88 |
)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
flux_theme = FluxTheme()
|
| 92 |
|
| 93 |
# ── Config ────────────────────────────────────────────────────────────────────
|
| 94 |
|
|
@@ -97,7 +91,6 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 97 |
|
| 98 |
MAX_SEED = np.iinfo(np.int32).max
|
| 99 |
MAX_IMAGE_SIZE = 1024
|
| 100 |
-
EXAMPLES_DIR = Path("examples")
|
| 101 |
|
| 102 |
# ── Models ────────────────────────────────────────────────────────────────────
|
| 103 |
|
|
@@ -152,27 +145,6 @@ def update_dimensions_from_image(image_list):
|
|
| 152 |
return new_width, new_height
|
| 153 |
|
| 154 |
|
| 155 |
-
def get_example_items():
|
| 156 |
-
example_prompts = {
|
| 157 |
-
"1.jpg": "Change the weather to stormy.",
|
| 158 |
-
"2.jpg": "Transform the scene into a snowy winter day while preserving the original subject identity, framing, and composition.",
|
| 159 |
-
"3.jpg": "Relight the image with soft golden sunset lighting while keeping all structures and subject details consistent.",
|
| 160 |
-
"4.jpg": "Make the texture high-resolution.",
|
| 161 |
-
}
|
| 162 |
-
items = []
|
| 163 |
-
if EXAMPLES_DIR.exists():
|
| 164 |
-
for name in sorted(os.listdir(EXAMPLES_DIR)):
|
| 165 |
-
if name.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
|
| 166 |
-
items.append({
|
| 167 |
-
"file": name,
|
| 168 |
-
"path": str(EXAMPLES_DIR / name),
|
| 169 |
-
"prompt": example_prompts.get(
|
| 170 |
-
name, "Edit this image while preserving composition."
|
| 171 |
-
),
|
| 172 |
-
})
|
| 173 |
-
return items
|
| 174 |
-
|
| 175 |
-
|
| 176 |
def parse_input_images(input_images):
|
| 177 |
"""Safely parse gallery / filepath / PIL inputs → list[PIL.Image] or None."""
|
| 178 |
if input_images is None:
|
|
@@ -261,12 +233,18 @@ def infer(
|
|
| 261 |
return out_standard, out_small, seed
|
| 262 |
|
| 263 |
|
| 264 |
-
# Text-only wrapper used by gr.Examples (avoids Gallery type issues entirely)
|
| 265 |
@spaces.GPU(duration=120)
|
| 266 |
-
def infer_example(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
out_std, out_small, seed_used = infer(
|
| 268 |
prompt=prompt,
|
| 269 |
-
input_images=
|
| 270 |
seed=0,
|
| 271 |
randomize_seed=True,
|
| 272 |
width=1024,
|
|
@@ -277,16 +255,12 @@ def infer_example(prompt):
|
|
| 277 |
return out_std, out_small, seed_used
|
| 278 |
|
| 279 |
|
| 280 |
-
# ── Load examples ─────────────────────────────────────────────────────────────
|
| 281 |
-
|
| 282 |
-
EXAMPLE_ITEMS = get_example_items()
|
| 283 |
-
|
| 284 |
# ── CSS ───────────────────────────────────────────────────────────────────────
|
| 285 |
|
| 286 |
css = """
|
| 287 |
#col-container {
|
| 288 |
margin: 0 auto;
|
| 289 |
-
max-width:
|
| 290 |
}
|
| 291 |
#main-title h1 {
|
| 292 |
font-size: 2.4em !important;
|
|
@@ -309,16 +283,12 @@ with gr.Blocks() as demo:
|
|
| 309 |
with gr.Column(elem_id="col-container"):
|
| 310 |
|
| 311 |
gr.Markdown(
|
| 312 |
-
"#
|
| 313 |
elem_id="main-title",
|
| 314 |
)
|
| 315 |
gr.Markdown(
|
| 316 |
-
"Compare **FLUX.2-klein-4B** side-by-side with two VAE decoders — "
|
| 317 |
-
"
|
| 318 |
-
"🟦 **Standard VAE** vs 🟩 **Small Decoder VAE** "
|
| 319 |
-
"([FLUX.2-small-decoder](https://huggingface.co/black-forest-labs/FLUX.2-small-decoder)) · "
|
| 320 |
-
"[[model](https://huggingface.co/black-forest-labs/FLUX.2-klein-4B)] · "
|
| 321 |
-
"[[blog](https://bfl.ai/blog/flux-2)]"
|
| 322 |
)
|
| 323 |
|
| 324 |
# ── Main two-column row ───────────────────────────────────────────────
|
|
@@ -328,17 +298,17 @@ with gr.Blocks() as demo:
|
|
| 328 |
with gr.Column():
|
| 329 |
input_images = gr.Gallery(
|
| 330 |
label="Input Image(s) for Editing (optional)",
|
| 331 |
-
type="
|
| 332 |
columns=2,
|
| 333 |
rows=1,
|
| 334 |
-
height=
|
| 335 |
allow_preview=True,
|
| 336 |
)
|
| 337 |
|
| 338 |
prompt = gr.Text(
|
| 339 |
-
label="Prompt",
|
| 340 |
show_label=True,
|
| 341 |
-
placeholder="e.g.,
|
| 342 |
)
|
| 343 |
|
| 344 |
run_button = gr.Button("⚡ Run Comparison", variant="primary")
|
|
@@ -349,7 +319,7 @@ with gr.Blocks() as demo:
|
|
| 349 |
with gr.Column():
|
| 350 |
gr.HTML(
|
| 351 |
'<span class="vae-badge" '
|
| 352 |
-
'style="background:#
|
| 353 |
'🟦 Standard VAE</span>'
|
| 354 |
)
|
| 355 |
result_standard = gr.Image(
|
|
@@ -363,7 +333,7 @@ with gr.Blocks() as demo:
|
|
| 363 |
with gr.Column():
|
| 364 |
gr.HTML(
|
| 365 |
'<span class="vae-badge" '
|
| 366 |
-
'style="background:#
|
| 367 |
'🟩 Small Decoder VAE</span>'
|
| 368 |
)
|
| 369 |
result_small = gr.Image(
|
|
@@ -374,9 +344,8 @@ with gr.Blocks() as demo:
|
|
| 374 |
height=280,
|
| 375 |
)
|
| 376 |
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 380 |
seed = gr.Slider(
|
| 381 |
label="Seed",
|
| 382 |
minimum=0,
|
|
@@ -418,50 +387,20 @@ with gr.Blocks() as demo:
|
|
| 418 |
value=1.0,
|
| 419 |
)
|
| 420 |
|
| 421 |
-
# ──
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
examples
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
# ── Visual image cards (click to load image + prompt) ─────────────────
|
| 436 |
-
if EXAMPLE_ITEMS:
|
| 437 |
-
gr.Markdown("#### 🖼️ Image Editing Examples — click to load")
|
| 438 |
-
with gr.Row():
|
| 439 |
-
for item in EXAMPLE_ITEMS:
|
| 440 |
-
with gr.Column(scale=1, min_width=180):
|
| 441 |
-
gr.Image(
|
| 442 |
-
value=item["path"],
|
| 443 |
-
show_label=False,
|
| 444 |
-
interactive=False,
|
| 445 |
-
height=150,
|
| 446 |
-
)
|
| 447 |
-
card_btn = gr.Button(
|
| 448 |
-
(item["prompt"][:48] + "…")
|
| 449 |
-
if len(item["prompt"]) > 48
|
| 450 |
-
else item["prompt"],
|
| 451 |
-
size="sm",
|
| 452 |
-
)
|
| 453 |
-
|
| 454 |
-
def _make_loader(p, path):
|
| 455 |
-
def _load():
|
| 456 |
-
pil = Image.open(path).convert("RGB")
|
| 457 |
-
return p, [(pil, None)]
|
| 458 |
-
return _load
|
| 459 |
-
|
| 460 |
-
card_btn.click(
|
| 461 |
-
fn=_make_loader(item["prompt"], item["path"]),
|
| 462 |
-
inputs=[],
|
| 463 |
-
outputs=[prompt, input_images],
|
| 464 |
-
)
|
| 465 |
|
| 466 |
gr.Markdown(
|
| 467 |
"[*](https://huggingface.co/black-forest-labs/FLUX.2-klein-4B) "
|
|
@@ -495,7 +434,7 @@ with gr.Blocks() as demo:
|
|
| 495 |
if __name__ == "__main__":
|
| 496 |
demo.queue(max_size=20).launch(
|
| 497 |
css=css,
|
| 498 |
-
theme=
|
| 499 |
ssr_mode=False,
|
| 500 |
show_error=True,
|
| 501 |
)
|
|
|
|
| 16 |
|
| 17 |
# ── Theme ─────────────────────────────────────────────────────────────────────
|
| 18 |
|
| 19 |
+
colors.orange_red = colors.Color(
|
| 20 |
+
name="orange_red",
|
| 21 |
+
c50="#FFF0E5",
|
| 22 |
+
c100="#FFE0CC",
|
| 23 |
+
c200="#FFC299",
|
| 24 |
+
c300="#FFA366",
|
| 25 |
+
c400="#FF8533",
|
| 26 |
+
c500="#FF4500",
|
| 27 |
+
c600="#E63E00",
|
| 28 |
+
c700="#CC3700",
|
| 29 |
+
c800="#B33000",
|
| 30 |
+
c900="#992900",
|
| 31 |
+
c950="#802200",
|
| 32 |
)
|
| 33 |
|
| 34 |
+
class OrangeRedTheme(Soft):
|
|
|
|
| 35 |
def __init__(
|
| 36 |
self,
|
| 37 |
*,
|
| 38 |
+
primary_hue: colors.Color | str = colors.gray,
|
| 39 |
+
secondary_hue: colors.Color | str = colors.orange_red,
|
| 40 |
neutral_hue: colors.Color | str = colors.slate,
|
| 41 |
text_size: sizes.Size | str = sizes.text_lg,
|
| 42 |
font: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 43 |
+
fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
|
|
|
|
|
|
|
| 44 |
),
|
| 45 |
font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 46 |
+
fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
|
|
|
|
|
|
|
| 47 |
),
|
| 48 |
):
|
| 49 |
super().__init__(
|
|
|
|
| 82 |
block_label_background_fill="*primary_200",
|
| 83 |
)
|
| 84 |
|
| 85 |
+
orange_red_theme = OrangeRedTheme()
|
|
|
|
| 86 |
|
| 87 |
# ── Config ────────────────────────────────────────────────────────────────────
|
| 88 |
|
|
|
|
| 91 |
|
| 92 |
MAX_SEED = np.iinfo(np.int32).max
|
| 93 |
MAX_IMAGE_SIZE = 1024
|
|
|
|
| 94 |
|
| 95 |
# ── Models ────────────────────────────────────────────────────────────────────
|
| 96 |
|
|
|
|
| 145 |
return new_width, new_height
|
| 146 |
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
def parse_input_images(input_images):
|
| 149 |
"""Safely parse gallery / filepath / PIL inputs → list[PIL.Image] or None."""
|
| 150 |
if input_images is None:
|
|
|
|
| 233 |
return out_standard, out_small, seed
|
| 234 |
|
| 235 |
|
|
|
|
| 236 |
@spaces.GPU(duration=120)
|
| 237 |
+
def infer_example(images, prompt):
|
| 238 |
+
if not images:
|
| 239 |
+
images_list = None
|
| 240 |
+
elif isinstance(images, str):
|
| 241 |
+
images_list = [images]
|
| 242 |
+
else:
|
| 243 |
+
images_list = images
|
| 244 |
+
|
| 245 |
out_std, out_small, seed_used = infer(
|
| 246 |
prompt=prompt,
|
| 247 |
+
input_images=images_list,
|
| 248 |
seed=0,
|
| 249 |
randomize_seed=True,
|
| 250 |
width=1024,
|
|
|
|
| 255 |
return out_std, out_small, seed_used
|
| 256 |
|
| 257 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
# ── CSS ───────────────────────────────────────────────────────────────────────
|
| 259 |
|
| 260 |
css = """
|
| 261 |
#col-container {
|
| 262 |
margin: 0 auto;
|
| 263 |
+
max-width: 1000px;
|
| 264 |
}
|
| 265 |
#main-title h1 {
|
| 266 |
font-size: 2.4em !important;
|
|
|
|
| 283 |
with gr.Column(elem_id="col-container"):
|
| 284 |
|
| 285 |
gr.Markdown(
|
| 286 |
+
"# **Flux.2-4B-Encoder-Comparator**",
|
| 287 |
elem_id="main-title",
|
| 288 |
)
|
| 289 |
gr.Markdown(
|
| 290 |
+
"Compare **FLUX.2-klein-4B** side-by-side with two VAE decoders — generated **simultaneously** from the **same seed**. \n"
|
| 291 |
+
"🟦 **Standard VAE** vs 🟩 **Small Decoder VAE** ([FLUX.2-small-decoder](https://huggingface.co/black-forest-labs/FLUX.2-small-decoder)) · [[model](https://huggingface.co/black-forest-labs/FLUX.2-klein-4B)]"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
)
|
| 293 |
|
| 294 |
# ── Main two-column row ───────────────────────────────────────────────
|
|
|
|
| 298 |
with gr.Column():
|
| 299 |
input_images = gr.Gallery(
|
| 300 |
label="Input Image(s) for Editing (optional)",
|
| 301 |
+
type="filepath",
|
| 302 |
columns=2,
|
| 303 |
rows=1,
|
| 304 |
+
height=300,
|
| 305 |
allow_preview=True,
|
| 306 |
)
|
| 307 |
|
| 308 |
prompt = gr.Text(
|
| 309 |
+
label="Edit Prompt",
|
| 310 |
show_label=True,
|
| 311 |
+
placeholder="e.g., Transform the scene into a snowy winter day...",
|
| 312 |
)
|
| 313 |
|
| 314 |
run_button = gr.Button("⚡ Run Comparison", variant="primary")
|
|
|
|
| 319 |
with gr.Column():
|
| 320 |
gr.HTML(
|
| 321 |
'<span class="vae-badge" '
|
| 322 |
+
'style="background:#FFE0CC;color:#CC3700;">'
|
| 323 |
'🟦 Standard VAE</span>'
|
| 324 |
)
|
| 325 |
result_standard = gr.Image(
|
|
|
|
| 333 |
with gr.Column():
|
| 334 |
gr.HTML(
|
| 335 |
'<span class="vae-badge" '
|
| 336 |
+
'style="background:#FFF0E5;color:#E63E00;">'
|
| 337 |
'🟩 Small Decoder VAE</span>'
|
| 338 |
)
|
| 339 |
result_small = gr.Image(
|
|
|
|
| 344 |
height=280,
|
| 345 |
)
|
| 346 |
|
| 347 |
+
with gr.Accordion("Advanced Settings", open=False, visible=False):
|
| 348 |
+
seed_output = gr.Number(label="Seed Used", precision=0)
|
|
|
|
| 349 |
seed = gr.Slider(
|
| 350 |
label="Seed",
|
| 351 |
minimum=0,
|
|
|
|
| 387 |
value=1.0,
|
| 388 |
)
|
| 389 |
|
| 390 |
+
# ── Standard App-Style Examples ───────────────────────────────────────
|
| 391 |
+
gr.Examples(
|
| 392 |
+
examples=[
|
| 393 |
+
[["examples/1.jpg"], "Change the weather to stormy."],
|
| 394 |
+
[["examples/2.jpg"], "Transform the scene into a snowy winter day while preserving the original subject identity, framing, and composition."],
|
| 395 |
+
[["examples/3.jpg"], "Relight the image with soft golden sunset lighting while keeping all structures and subject details consistent."],
|
| 396 |
+
[["examples/4.jpg"], "Make the texture high-resolution."],
|
| 397 |
+
],
|
| 398 |
+
inputs=[input_images, prompt],
|
| 399 |
+
outputs=[result_standard, result_small, seed_output],
|
| 400 |
+
fn=infer_example,
|
| 401 |
+
cache_examples=False,
|
| 402 |
+
label="Examples"
|
| 403 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
gr.Markdown(
|
| 406 |
"[*](https://huggingface.co/black-forest-labs/FLUX.2-klein-4B) "
|
|
|
|
| 434 |
if __name__ == "__main__":
|
| 435 |
demo.queue(max_size=20).launch(
|
| 436 |
css=css,
|
| 437 |
+
theme=orange_red_theme,
|
| 438 |
ssr_mode=False,
|
| 439 |
show_error=True,
|
| 440 |
)
|