#!/usr/bin/env python """Generate an image with FLUX.2 [klein] 4B and the DWD head (no VAE). python scripts/generate.py --prompt "a red fox in fresh snow" --out fox.png --tail takes an exported tail file, a directory holding dwd_tail.safetensors, or a Hugging Face repo id (default: the released tail, PrunaAI/P-Flux-2-Klein-4B-DWD). Width and height must be multiples of 16; the tail was trained at 1024x1024, 1152x896, 896x1152, 1344x768 and 768x1344. """ import argparse import os import sys import time try: from dwd_klein.pipeline import DEFAULT_REPO, DWDKleinPipeline except ImportError: # running from a source checkout without installing the package sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src")) from dwd_klein.pipeline import DEFAULT_REPO, DWDKleinPipeline def main(argv=None) -> int: ap = argparse.ArgumentParser(description="FLUX.2 [klein] 4B + DWD head text-to-image (no VAE)") ap.add_argument("--tail", default=DEFAULT_REPO, help=f"exported tail (.safetensors), a directory holding it, or a repo id (default {DEFAULT_REPO})") ap.add_argument("--prompt", required=True, action="append", help="text prompt; repeat for several images (seeds seed, seed + 1, ...)") ap.add_argument("--width", type=int, default=1024) ap.add_argument("--height", type=int, default=1024) ap.add_argument("--seed", type=int, default=0) ap.add_argument("--steps", type=int, default=4, help="sampler steps (the tail was trained for 4)") ap.add_argument("--out", default="dwd.png", help="output PNG; with several prompts, _ is added to the name") ap.add_argument("--base", default="black-forest-labs/FLUX.2-klein-4B", help="base model repo id or local dir") ap.add_argument("--device", default=None, help="default: cuda if available, else cpu") ap.add_argument("--compile", nargs="?", const="default", default=None, choices=["default", "reduce-overhead"], help="torch.compile the transformer and head (first image per size is slow)") a = ap.parse_args(argv) if a.width % 16 or a.height % 16: ap.error("--width and --height must be multiples of 16") t0 = time.perf_counter() compile_arg = False if a.compile is None else (True if a.compile == "default" else a.compile) pipe = DWDKleinPipeline.from_pretrained(a.tail, base=a.base, device=a.device, compile=compile_arg) print(f"loaded in {time.perf_counter() - t0:.1f} s", flush=True) t1 = time.perf_counter() images = pipe(a.prompt, width=a.width, height=a.height, seed=a.seed, steps=a.steps) print(f"generated {len(images)} image(s) in {time.perf_counter() - t1:.2f} s", flush=True) root, ext = os.path.splitext(a.out) os.makedirs(os.path.dirname(os.path.abspath(a.out)), exist_ok=True) for i, img in enumerate(images): path = a.out if len(images) == 1 else f"{root}_{i}{ext or '.png'}" img.save(path) print(f"wrote {path}") return 0 if __name__ == "__main__": sys.exit(main())