File size: 2,019 Bytes
617ab30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""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": ["<b64 png>", ...]}}
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]