PAI ArtLab 开发者中心使用说明

本文介绍如何使用开发者中心进行ComfyUI API集成验证。

点击登录PAI ArtLab控制台

ComfyUI API集成验证

API

ComfyUI API版本主要⽤于多ComfyUI实例下的API调⽤, ⾯向开发者集成使⽤,⽆Web图形界⾯。 由于ComfyUI是属于有状态服务,因此多ComfyUI实例情况下,需要通过EAS异步队列服务才能正常调⽤ComfyUI服务。

前提条件

已搭建Python环境,并安装EAS SDK依赖(异步队列依赖):pip install eas_prediction

操作步骤

  1. 登录PAI ArtLab控制台,在开发者中心页面的自定义服务页签,单击创建自定义服务

    其中,服务版本选择ComfyUI(API版)

  2. 服务状态变为运行中,单击调用信息,在公网输出请求地址页签获取请求URL、Token、Endpoint和服务ID。

    image

  3. 使用已获得的调用信息替换代码中的对应内容,然后运行代码。

    代码示例

    import requests
    
    ##### 根据调试信息,替换以下参数 #####
    input_url = "http://115************.cn-shanghai.pai-eas.aliyuncs.com/api/predict/ai4d_comfyuiapi_1u38************"
    token = "Yjha************"
    endpoint = "115************.cn-shanghai.pai-eas.aliyuncs.com"
    service_name = "ai4d_comfyuiapi_1u38************/sink"
    ################################
    
    session = requests.session()
    session.headers.update({"Authorization": f"{token}"})
    
    work_flow = {
        "3": {
            "inputs": {
                "seed": 156680208700286,
                "steps": 20,
                "cfg": 8,
                "sampler_name": "euler",
                "scheduler": "normal",
                "denoise": 1,
                "model": [
                    "4",
                    0
                ],
                "positive": [
                    "6",
                    0
                ],
                "negative": [
                    "7",
                    0
                ],
                "latent_image": [
                    "5",
                    0
                ]
            },
            "class_type": "KSampler",
            "_meta": {
                "title": "K采样器"
            }
        },
        "4": {
            "inputs": {
                "ckpt_name": "3dAnimationDiffusion_v10.safetensors"
            },
            "class_type": "CheckpointLoaderSimple",
            "_meta": {
                "title": "Checkpoint加载器(简易)"
            }
        },
        "5": {
            "inputs": {
                "width": 512,
                "height": 512,
                "batch_size": 1
            },
            "class_type": "EmptyLatentImage",
            "_meta": {
                "title": "空Latent"
            }
        },
        "6": {
            "inputs": {
                "text": "beautiful scenery nature glass bottle landscape, , purple galaxy bottle,",
                "clip": [
                    "4",
                    1
                ]
            },
            "class_type": "CLIPTextEncode",
            "_meta": {
                "title": "CLIP⽂本编码器"
            }
        },
        "7": {
            "inputs": {
                "text": "text, watermark",
                "clip": [
                    "4",
                    1
                ]
            },
            "class_type": "CLIPTextEncode",
            "_meta": {
                "title": "CLIP⽂本编码器"
            }
        },
        "8": {
            "inputs": {
                "samples": [
                    "3",
                    0
                ],
                "vae": [
                    "4",
                    2
                ]
            },
            "class_type": "VAEDecode",
            "_meta": {
                "title": "VAE解码"
            }
        },
        "9": {
            "inputs": {
                "filename_prefix": "ComfyUI",
                "images": [
                    "8",
                    0
                ]
            },
            "class_type": "SaveImage",
            "_meta": {
                "title": "保存图像"
            }
        }
    }
    
    for i in range(1):
        payload = work_flow
        response = session.post(url=f'{input_url}/api_prompt?task_id=txt2img_test', json=payload)
        if response.status_code != 200:
            exit(f"send request error:{response.content}, response code:{response.status_code}")
        else:
            print(f"send {i} success, index is {response.content}")
    
    
    from eas_prediction import QueueClient
    
    sink_queue = QueueClient(f'{endpoint}', f'{service_name}')
    sink_queue.set_token(f'{token}')
    sink_queue.init()
    
    watcher = sink_queue.watch(0, 1, auto_commit=False)
    for x in watcher.run():
        if 'task_id' in x.tags:
            print('index {} task_id is {}'.format(x.index, x.tags['task_id']))
            print(f'index {x.index} data is {x.data}')
            sink_queue.commit(x.index)
    
  4. 运行完后,在生成结果中获取文件名称。

    image

  5. PAI ArtLab页面,鼠标悬停右上角image,单击储存,访问OSS Bucket,获取储存路径。

    image

  6. 拼接OSS图片地址并访问,格式为:OSS Bucket路径/output/文件名称

    例如,26****/data-115****************/output/ComfyUI_00000000_174427782695938_599ffc33-edea-4e64-bb60-28e834940f5c_.png

专享版

ComfyUI专享版同时⽀持API调⽤和Web调试, 测试验证流程参考API版。

前提条件

已搭建Python环境。

操作步骤

  1. 登录PAI ArtLab控制台,在开发者中心页面的自定义服务页签,单击创建自定义服务

    其中,服务版本选择ComfyUI(专享版)

  2. 服务状态变为运行中,单击调用信息,在公网地址调用页签获取请求URLToken。

    image

  3. 使用已获得的调用信息替换代码中的对应内容,然后运行代码。

    代码示例

    import requests
    
    ##### 根据调试信息,替换以下参数 #####
    url = "http://ai4d_comfyuiapi_1u38************.115************.cn-shanghai.pai-eas.aliyuncs.com/"
    token = "Yjha************"
    ################################
    
    payload = {
        "prompt": {
            "3": {
                "inputs": {
                    "seed": 156680208700286,
                    "steps": 20,
                    "cfg": 8,
                    "sampler_name": "euler",
                    "scheduler": "normal",
                    "denoise": 1,
                    "model": [
                        "4",
                        0
                    ],
                    "positive": [
                        "6",
                        0
                    ],
                    "negative": [
                        "7",
                        0
                    ],
                    "latent_image": [
                        "5",
                        0
                    ]
                },
                "class_type": "KSampler",
                "_meta": {
                    "title": "K采样器"
                }
            },
            "4": {
                "inputs": {
                    "ckpt_name": "3dAnimationDiffusion_v10.safetensors"
                },
                "class_type": "CheckpointLoaderSimple",
                "_meta": {
                    "title": "Checkpoint加载器(简易)"
                }
            },
            "5": {
                "inputs": {
                    "width": 512,
                    "height": 512,
                    "batch_size": 1
                },
                "class_type": "EmptyLatentImage",
                "_meta": {
                    "title": "空Latent"
                }
            },
            "6": {
                "inputs": {
                    "text": "beautiful scenery nature glass bottle landscape, , purple galaxy bottle,",
                    "clip": [
                        "4",
                        1
                    ]
                },
                "class_type": "CLIPTextEncode",
                "_meta": {
                    "title": "CLIP⽂本编码器"
                }
            },
            "7": {
                "inputs": {
                    "text": "text, watermark",
                    "clip": [
                        "4",
                        1
                    ]
                },
                "class_type": "CLIPTextEncode",
                "_meta": {
                    "title": "CLIP⽂本编码器"
                }
            },
            "8": {
                "inputs": {
                    "samples": [
                        "3",
                        0
                    ],
                    "vae": [
                        "4",
                        2
                    ]
                },
                "class_type": "VAEDecode",
                "_meta": {
                    "title": "VAE解码"
                }
            },
            "9": {
                "inputs": {
                    "filename_prefix": "ComfyUI",
                    "images": [
                        "8",
                        0
                    ]
                },
                "class_type": "SaveImage",
                "_meta": {
                    "title": "保存图像"
                }
            }
        }
    }
    
    # 发起请求
    session = requests.session()
    session.headers.update({"Authorization": f"{token}"})
    prompt_url = url + "prompt"
    
    response = session.post(url=f'{prompt_url}', json=payload)
    if response.status_code != 200:
        raise Exception(response.content)
    
    data = response.json()
    prompt_id = data['prompt_id']
    print(f"get data: {data}, get prompt id: {prompt_id}")
    
    
    # 获取结果
    # 构造请求URL
    history_url = f"{url}history/{prompt_id}"
    
    session = requests.session()
    session.headers.update({"Authorization": f"{token}"})
    
    response = session.get(url=f'{history_url}')
    
    if response.status_code != 200:
        raise Exception(response.content)
    
    data = response.json()
    print(data)
    
    # 解析 JSON 数据,获取所有 outputs 的图片信息
    for prompt_id, prompt_data in data.items():
        outputs = prompt_data.get("outputs", {})
        for node_id, node_data in outputs.items():
            images = node_data.get("images", [])
            for image in images:
                filename = image.get("filename")
                print(f"节点号: {node_id}, 图⽚⽂件名: {filename}")
    
  4. 运行完后,在生成结果中获取文件名称。

    image

  5. PAI ArtLab页面,鼠标悬停右上角image,单击储存,访问OSS Bucket,获取储存路径。

    image

  6. 拼接OSS图片地址并访问,格式为:OSS Bucket路径/output/文件名称

    例如,26****/data-115****************/output/ComfyUI_00000000_174427782695938_599ffc33-edea-4e64-bb60-28e834940f5c_.png