"""OpenAI-compatible /v1/images/generations server for Z-Image (Turbo or Base). Env vars (identity only — NOT for tuning generation): MODEL_DIR container path to weights (default /z-image-model) PORT listen port (default 9006) SERVED_MODEL_ID /v1/models id + variant key (default z-image-turbo-6b) Generation defaults are documented per-variant in VARIANT_DEFAULTS below. Query them at runtime via GET /v1/config. Override any value per-request in the /v1/images/generations body. The response echoes the resolved values under "params_used". """ import base64, io, os, time, torch from fastapi import FastAPI, HTTPException from fastapi.responses import JSONResponse from pydantic import BaseModel, Field from typing import Optional import uvicorn MODEL_DIR = os.environ.get("MODEL_DIR", "/z-image-model") PORT = int(os.environ.get("PORT", 9006)) SERVED_MODEL_ID = os.environ.get("SERVED_MODEL_ID", "z-image-turbo-6b") # Per-variant generation defaults. Keys = SERVED_MODEL_ID values. VARIANT_DEFAULTS = { "z-image-turbo-6b": {"num_inference_steps": 9, "guidance_scale": 0.0}, # Turbo MUST be cfg=0 "z-image-6b": {"num_inference_steps": 50, "guidance_scale": 4.0}, } DEFAULTS = VARIANT_DEFAULTS.get(SERVED_MODEL_ID, VARIANT_DEFAULTS["z-image-turbo-6b"]) app = FastAPI() pipe = None def load_model(): global pipe from diffusers import ZImagePipeline print(f"Loading {SERVED_MODEL_ID} from {MODEL_DIR}...", flush=True) pipe = ZImagePipeline.from_pretrained( MODEL_DIR, torch_dtype=torch.bfloat16, low_cpu_mem_usage=False, ) pipe.to("cuda") print("Model ready.", flush=True) class ImageRequest(BaseModel): prompt: str = Field(..., description="Text prompt") n: int = Field(1, description="Number of images to generate") size: str = Field("1024x1024", description="WxH, e.g. 1024x1024") num_inference_steps: int = Field( DEFAULTS["num_inference_steps"], description="Diffusion steps. Turbo: ~9. Base: 28-50. More = higher quality, slower.", ) guidance_scale: float = Field( DEFAULTS["guidance_scale"], description="CFG scale. Turbo MUST be 0.0; Base typically 3.0-5.0.", ) seed: Optional[int] = Field(None, description="Random seed. null = random per call.") response_format: str = Field("b64_json", description="Only b64_json supported.") def _params_doc(): """Introspect ImageRequest into a {name: {default, description}} dict.""" out = {} for name, info in ImageRequest.model_fields.items(): default = info.default if default is ...: default = None out[name] = {"default": default, "description": info.description} return out @app.get("/health") def health(): return {"status": "ok", "model": SERVED_MODEL_ID} @app.get("/v1/models") def models(): return {"object": "list", "data": [{"id": SERVED_MODEL_ID, "object": "model"}]} @app.get("/v1/config") def config(): return {"model": SERVED_MODEL_ID, "params": _params_doc()} @app.post("/v1/images/generations") def generate(req: ImageRequest): if pipe is None: raise HTTPException(503, "Model not loaded") w, h = (int(x) for x in req.size.split("x")) gen = torch.Generator("cuda").manual_seed(req.seed) if req.seed is not None else None t0 = time.time() images = pipe( prompt=req.prompt, width=w, height=h, num_inference_steps=req.num_inference_steps, guidance_scale=req.guidance_scale, num_images_per_prompt=req.n, generator=gen, ).images elapsed = time.time() - t0 print(f"Generated {len(images)} image(s) in {elapsed:.1f}s", flush=True) data = [] for img in images: buf = io.BytesIO() img.save(buf, format="PNG") data.append({"b64_json": base64.b64encode(buf.getvalue()).decode()}) return JSONResponse({ "created": int(time.time()), "data": data, "params_used": { "n": req.n, "size": req.size, "num_inference_steps": req.num_inference_steps, "guidance_scale": req.guidance_scale, "seed": req.seed, }, }) if __name__ == "__main__": load_model() uvicorn.run(app, host="0.0.0.0", port=PORT)