import argparse import json import subprocess import sys import time from pathlib import Path ap = argparse.ArgumentParser() ap.add_argument("--input", action="append", default=[]) ap.add_argument("--outdir", required=True) ap.add_argument("--params", default="{}") a = ap.parse_args() p = json.loads(a.params) if not p.get("prompt"): print("ERROR: prompt is required") sys.exit(1) model = p.get("model", "flux2-klein-4b") steps = int(p.get("steps", 4)) outdir = Path(a.outdir) out_png = outdir / f"flux_{model.replace('/', '_')}_s{p.get('seed', 42)}.png" bindir = Path(sys.executable).parent if model.startswith("flux2-"): cli = bindir / "mflux-generate-flux2" model_args = ["--model", model] quant_args = [] # klein is small; skip quantization flags guidance_args = [] # distilled klein: guidance fixed at 1.0 elif model == "schnell-4bit": cli = bindir / "mflux-generate" # ungated community pre-quantized weights (~10GB) — no HF license wall model_args = ["--model", "dhairyashil/FLUX.1-schnell-mflux-4bit", "--base-model", "schnell"] quant_args = [] guidance_args = [] else: cli = bindir / "mflux-generate" model_args = ["--model", model] quant = str(p.get("quantize", "8")) quant_args = ["--quantize", quant] if quant in ("4", "8") else [] guidance_args = (["--guidance", str(p.get("guidance", 3.5))] if model in ("dev", "krea-dev") else []) if not cli.exists(): print(f"ERROR: mflux not installed ({cli} missing). Run scripts/install_mflux.sh") sys.exit(1) cmd = [str(cli), *model_args, "--prompt", p["prompt"], "--steps", str(steps), "--width", str(p.get("width", 1024)), "--height", str(p.get("height", 1024)), "--seed", str(p.get("seed", 42)), "--output", str(out_png), *quant_args, *guidance_args] print("+", " ".join(cmd), flush=True) print("(first run per model downloads weights from HuggingFace)", flush=True) t0 = time.time() res = subprocess.run(cmd, stdout=sys.stdout, stderr=subprocess.STDOUT) if res.returncode != 0: print("HINT: gated-repo/auth errors mean this model needs an HF license accept " "+ token (Settings → HuggingFace token). flux2-klein-4b and schnell-4bit " "are ungated and need nothing.") sys.exit(res.returncode) elapsed = round(time.time() - t0, 1) if not out_png.exists(): pngs = sorted(outdir.glob("*.png")) if not pngs: print("ERROR: no image produced") sys.exit(1) out_png = pngs[-1] (outdir / "result.json").write_text(json.dumps({"outputs": [ {"path": out_png.name, "meta": {"tool": "mflux", "model": model, "steps": steps, "seed": p.get("seed", 42), "seconds": elapsed}}]})) print(f"done in {elapsed}s: {out_png.name}", flush=True)