"""ComfyUI をヘッドレスモードまたは代替フロントエンドで実行する際に API ノードを使用する
API キーをプロンプトに含めることで、API ノードを含む ComfyUI ワークフローを実行できます。
API キーは、ペイロードの `extra_data` フィールドに追加する必要があります。
以下に、その方法の例を示します。
詳細情報:
- API ノードの概要:https://docs.comfy.org/tutorials/partner-nodes/overview
- API キーを生成するには、こちらにログインしてください:https://platform.comfy.org/login
"""
import json
from urllib import request
SERVER_URL = "http://127.0.0.1:8188"
# API ノードを含むプロンプト/ジョブ(「API 形式」のワークフロー)があります。
workflow_with_api_nodes = """{
"11": {
"inputs": {
"prompt": "A dreamy, surreal half-body portrait of a young woman meditating. She has a short, straight bob haircut dyed in pastel pink, with soft bangs covering her forehead. Her eyes are gently closed, and her hands are raised in a calm, open-palmed meditative pose, fingers slightly curved, as if levitating or in deep concentration. She wears a colorful dress made of patchwork-like pastel tiles, featuring clouds, stars, and rainbows. Around her float translucent, iridescent soap bubbles reflecting the rainbow hues. The background is a fantastical sky filled with cotton-candy clouds and vivid rainbow waves, giving the entire scene a magical, dreamlike atmosphere. Emphasis on youthful serenity, whimsical ambiance, and vibrant soft lighting.",
"prompt_upsampling": false,
"seed": 589991183902375,
"aspect_ratio": "1:1",
"raw": false,
"image_prompt_strength": 0.4000000000000001,
"image_prompt": [
"14",
0
]
},
"class_type": "FluxProUltraImageNode",
"_meta": {
"title": "Flux 1.1 [pro] Ultra Image"
}
},
"12": {
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"11",
0
]
},
"class_type": "SaveImage",
"_meta": {
"title": "Save Image"
}
},
"14": {
"inputs": {
"image": "example.png"
},
"class_type": "LoadImage",
"_meta": {
"title": "Load Image"
}
}
}"""
prompt = json.loads(workflow_with_api_nodes)
payload = {
"prompt": prompt,
# `api_key_comfy_org` をペイロードに追加します。
# 複数のクライアントを処理する場合は、まず関連するユーザーからキーを取得できます。
"extra_data": {
"api_key_comfy_org": "comfyui-87d01e28d*******************************************************" # 実際のキーに置き換えてください
},
}
data = json.dumps(payload).encode("utf-8")
req = request.Request(f"{SERVER_URL}/prompt", data=data)
request.urlopen(req)