m64_e51ffc16a885 / z_image_server.py
119 lines, 4.3 KBraw
1"""OpenAI-compatible /v1/images/generations server for Z-Image (Turbo or Base).23Env vars (identity only — NOT for tuning generation):4 MODEL_DIR container path to weights (default /z-image-model)5 PORT listen port (default 9006)6 SERVED_MODEL_ID /v1/models id + variant key (default z-image-turbo-6b)78Generation defaults are documented per-variant in VARIANT_DEFAULTS below. Query9them at runtime via GET /v1/config. Override any value per-request in the10/v1/images/generations body. The response echoes the resolved values under11"params_used".12"""13import base64, io, os, time, torch14from fastapi import FastAPI, HTTPException15from fastapi.responses import JSONResponse16from pydantic import BaseModel, Field17from typing import Optional18import uvicorn1920MODEL_DIR = os.environ.get("MODEL_DIR", "/z-image-model")21PORT = int(os.environ.get("PORT", 9006))22SERVED_MODEL_ID = os.environ.get("SERVED_MODEL_ID", "z-image-turbo-6b")2324# Per-variant generation defaults. Keys = SERVED_MODEL_ID values.25VARIANT_DEFAULTS = {26 "z-image-turbo-6b": {"num_inference_steps": 9, "guidance_scale": 0.0}, # Turbo MUST be cfg=027 "z-image-6b": {"num_inference_steps": 50, "guidance_scale": 4.0},28}29DEFAULTS = VARIANT_DEFAULTS.get(SERVED_MODEL_ID, VARIANT_DEFAULTS["z-image-turbo-6b"])3031app = FastAPI()32pipe = None3334def load_model():35 global pipe36 from diffusers import ZImagePipeline37 print(f"Loading {SERVED_MODEL_ID} from {MODEL_DIR}...", flush=True)38 pipe = ZImagePipeline.from_pretrained(39 MODEL_DIR,40 torch_dtype=torch.bfloat16,41 low_cpu_mem_usage=False,42 )43 pipe.to("cuda")44 print("Model ready.", flush=True)4546class ImageRequest(BaseModel):47 prompt: str = Field(..., description="Text prompt")48 n: int = Field(1, description="Number of images to generate")49 size: str = Field("1024x1024", description="WxH, e.g. 1024x1024")50 num_inference_steps: int = Field(51 DEFAULTS["num_inference_steps"],52 description="Diffusion steps. Turbo: ~9. Base: 28-50. More = higher quality, slower.",53 )54 guidance_scale: float = Field(55 DEFAULTS["guidance_scale"],56 description="CFG scale. Turbo MUST be 0.0; Base typically 3.0-5.0.",57 )58 seed: Optional[int] = Field(None, description="Random seed. null = random per call.")59 response_format: str = Field("b64_json", description="Only b64_json supported.")6061def _params_doc():62 """Introspect ImageRequest into a {name: {default, description}} dict."""63 out = {}64 for name, info in ImageRequest.model_fields.items():65 default = info.default66 if default is ...:67 default = None68 out[name] = {"default": default, "description": info.description}69 return out7071@app.get("/health")72def health():73 return {"status": "ok", "model": SERVED_MODEL_ID}7475@app.get("/v1/models")76def models():77 return {"object": "list", "data": [{"id": SERVED_MODEL_ID, "object": "model"}]}7879@app.get("/v1/config")80def config():81 return {"model": SERVED_MODEL_ID, "params": _params_doc()}8283@app.post("/v1/images/generations")84def generate(req: ImageRequest):85 if pipe is None:86 raise HTTPException(503, "Model not loaded")87 w, h = (int(x) for x in req.size.split("x"))88 gen = torch.Generator("cuda").manual_seed(req.seed) if req.seed is not None else None89 t0 = time.time()90 images = pipe(91 prompt=req.prompt,92 width=w, height=h,93 num_inference_steps=req.num_inference_steps,94 guidance_scale=req.guidance_scale,95 num_images_per_prompt=req.n,96 generator=gen,97 ).images98 elapsed = time.time() - t099 print(f"Generated {len(images)} image(s) in {elapsed:.1f}s", flush=True)100 data = []101 for img in images:102 buf = io.BytesIO()103 img.save(buf, format="PNG")104 data.append({"b64_json": base64.b64encode(buf.getvalue()).decode()})105 return JSONResponse({106 "created": int(time.time()),107 "data": data,108 "params_used": {109 "n": req.n,110 "size": req.size,111 "num_inference_steps": req.num_inference_steps,112 "guidance_scale": req.guidance_scale,113 "seed": req.seed,114 },115 })116117if __name__ == "__main__":118 load_model()119 uvicorn.run(app, host="0.0.0.0", port=PORT)