"""Custom handler for a Hugging Face Inference Endpoint serving a Qwen-Image edit/generate model. Upload this file + requirements.txt to the root of your model repo (or a copy/fork of it). Request format (matches the ComfyUI node): {"inputs": "prompt", "parameters": {"steps": 40, "seed": 0, "size": "1024x1024", "neg": "...", "cfg": 4.0, "input_images": ["", ...]}} Response: a PIL image (served as image/png when the request sends Accept: image/png). """ import base64 import io import torch from diffusers import DiffusionPipeline from PIL import Image class EndpointHandler: def __init__(self, path=""): # `path` is the local snapshot of the repo the endpoint was created from. self.pipe = DiffusionPipeline.from_pretrained(path, torch_dtype=torch.bfloat16) self.pipe.to("cuda") self.pipe.set_progress_bar_config(disable=True) def __call__(self, data): prompt = data["inputs"] params = data.get("parameters", {}) or {} kwargs = { "prompt": prompt, "num_inference_steps": int(params.get("steps", 40)), "generator": torch.Generator("cuda").manual_seed(int(params.get("seed", 0))), } images = [ Image.open(io.BytesIO(base64.b64decode(b))).convert("RGB") for b in params.get("input_images", []) ] if images: kwargs["image"] = images if len(images) > 1 else images[0] size = params.get("size") if size: width, height = (int(v) for v in size.lower().split("x")) kwargs["width"], kwargs["height"] = width, height neg = params.get("neg") if neg: kwargs["negative_prompt"] = neg # Qwen-Image pipelines use true_cfg_scale (only active with a negative prompt) kwargs["true_cfg_scale"] = float(params.get("cfg", 4.0)) with torch.inference_mode(): return self.pipe(**kwargs).images[0]