一.准备工作
首先,开启api保存格式。
其次,保存现有工作流
然后,结果自动下载
看下保存的内容:
3
inputs
seed 563090841476318
steps 40
cfg 8
sampler_name "dpmpp_2m"
scheduler "karras"
denoise 1
model
0 "25"
1 0
positive
0 "13"
1 0
negative
0 "15"
1 0
latent_image
0 "5"
1 0
class_type "KSampler"
_meta
title "KSampler"
5
inputs
width 704
height 960
batch_size 4
class_type "EmptyLatentImage"
_meta
title "Empty Latent Image"
8
inputs
samples
0 "3"
1 0
vae
0 "36"
1 2
class_type "VAEDecode"
_meta
title "VAE Decode"
13
inputs
from_translate "chinese"
to_translate "english"
text "海边 ,只有一个人\n\n"
clip
0 "36"
1 1
class_type "ArgosTranslateCLIPTextEncodeNode"
_meta
title "Argos Translate CLIP Text Encode Node"
14
inputs
text
0 "13"
1 1
PreviewTextNode_0 "By the sea. Naked beauty, full breasts, walking. Only one."
class_type "PreviewTextNode"
_meta
title "Preview Text Node"
15
inputs
text ""
clip
0 "36"
1 1
class_type "CLIPTextEncode"
_meta
title "CLIP Text Encode (Prompt)"
19
inputs
images
0 "8"
1 0
class_type "PreviewImage"
_meta
title "Preview Image"
25
inputs
unet_name "绪儿-写实 帅哥美女模型_东方v4.safetensors"
weight_dtype "fp8_e4m3fn"
class_type "UNETLoader"
_meta
title "Load Diffusion Model"
36
inputs
ckpt_name "213.safetensors"
class_type "CheckpointLoaderSimple"
_meta
title "Load Checkpoint"
二.学习例题
安装时提供了三个例子
先学习第一个:代码如下。
第一个:
import json
from urllib import request, parse
import random
#This is the ComfyUI api prompt format.
#If you want it for a specific workflow you can "enable dev mode options"
#in the settings of the UI (gear beside the "Queue Size: ") this will enable
#a button on the UI to save workflows in api format.
#keep in mind ComfyUI is pre alpha software so this format will change a bit.
#this is the one for the default workflow
prompt_text = """
{
"3": {
"class_type": "KSampler",
"inputs": {
"cfg": 8,
"denoise": 1,
"latent_image": [
"5",
0
],
"model": [
"4",
0
],
"negative": [
"7",
0
],
"positive": [
"6",
0
],
"sampler_name": "euler",
"scheduler": "normal",
"seed": 8566257,
"steps": 20
}
},
"4": {
"class_type": "CheckpointLoaderSimple",
"inputs": {
"ckpt_name": "v1-5-pruned-emaonly.safetensors"
}
},
"5": {
"class_type": "EmptyLatentImage",
"inputs": {
"batch_size": 1,
"height": 512,
"width": 512
}
},
"6": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"4",
1
],
"text": "masterpiece best quality girl"
}
},
"7": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"4",
1
],
"text": "bad hands"
}
},
"8": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"3",
0
],
"vae": [
"4",
2
]
}
},
"9": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"8",
0
]
}
}
}
"""
def queue_prompt(prompt):
p = {"prompt": prompt}
data = json.dumps(p).encode('utf-8')
req = request.Request("http://127.0.0.1:8188/prompt", data=data)
request.urlopen(req)
prompt = json.loads(prompt_text)
#set the text prompt for our positive CLIPTextEncode
prompt["6"]["inputs"]["text"] = "masterpiece best quality man"
#set the seed for our KSampler node
prompt["3"]["inputs"]["seed"] = 5
queue_prompt(prompt)
替换,我们的工作流的内容。 及必要修改的内容。也可以通过引入的方法。
整体如下:
import json
from urllib import request, parse
import random
#This is the ComfyUI api prompt format.
#If you want it for a specific workflow you can "enable dev mode options"
#in the settings of the UI (gear beside the "Queue Size: ") this will enable
#a button on the UI to save workflows in api format.
#keep in mind ComfyUI is pre alpha software so this format will change a bit.
#this is the one for the default workflow
# 这是 ComfyUI API 的提示格式。
# 如果你想要特定工作流的格式,可以在 UI 设置中启用“开发模式选项”
# (队列大小旁边的齿轮图标),这将启用一个按钮,允许你保存工作流为 API 格式。
# 请注意,ComfyUI 是预 alpha 软件,所以这个格式可能会有一些变化。
prompt_text = """
{
{
"3": {
"inputs": {
"seed": 563090841476318,
"steps": 40,
"cfg": 8,
"sampler_name": "dpmpp_2m",
"scheduler": "karras",
"denoise": 1,
"model": [
"25",
0
],
"positive": [
"13",
0
],
"negative": [
"15",
0
],
"latent_image": [
"5",
0
]
},
"class_type": "KSampler",
"_meta": {
"title": "KSampler"
}
},
"5": {
"inputs": {
"width": 704,
"height": 960,
"batch_size": 4
},
"class_type": "EmptyLatentImage",
"_meta": {
"title": "Empty Latent Image"
}
},
"8": {
"inputs": {
"samples": [
"3",
0
],
"vae": [
"36",
2
]
},
"class_type": "VAEDecode",
"_meta": {
"title": "VAE Decode"
}
},
"13": {
"inputs": {
"from_translate": "chinese",
"to_translate": "english",
"text": "这里是本次要替换的内容",
"clip": [
"36",
1
]
},
"class_type": "ArgosTranslateCLIPTextEncodeNode",
"_meta": {
"title": "Argos Translate CLIP Text Encode Node"
}
},
"14": {
"inputs": {
"text": [
"13",
1
],
"PreviewTextNode_0": "By the sea. Naked beauty, full breasts, walking. Only one."
},
"class_type": "PreviewTextNode",
"_meta": {
"title": "Preview Text Node"
}
},
"15": {
"inputs": {
"text": "",
"clip": [
"36",
1
]
},
"class_type": "CLIPTextEncode",
"_meta": {
"title": "CLIP Text Encode (Prompt)"
}
},
"19": {
"inputs": {
"images": [
"8",
0
]
},
"class_type": "PreviewImage",
"_meta": {
"title": "Preview Image"
}
},
"25": {
"inputs": {
"unet_name": "绪儿-写实 帅哥美女模型_东方v4.safetensors",
"weight_dtype": "fp8_e4m3fn"
},
"class_type": "UNETLoader",
"_meta": {
"title": "Load Diffusion Model"
}
},
"36": {
"inputs": {
"ckpt_name": "213.safetensors"
},
"class_type": "CheckpointLoaderSimple",
"_meta": {
"title": "Load Checkpoint"
}
}
}
"""
def queue_prompt(prompt):
p = {"prompt": prompt}
data = json.dumps(p).encode('utf-8')
req = request.Request("http://127.0.0.1:8188/prompt", data=data)
request.urlopen(req)
prompt = json.loads(prompt_text)
#set the text prompt for our positive CLIPTextEncode
prompt["13"]["inputs"]["text"] = "这里是你要替换的内容"
# #set the seed for our KSampler node
# prompt["3"]["inputs"]["seed"] = 5
queue_prompt(prompt)
运行出错:
python script_examples/basic_api_learn.py
Traceback (most recent call last):
File "/home/duyicheng/gitee/ComfyUI/script_examples/basic_api_learn.py", line 163, in <module>
prompt = json.loads(prompt_text)
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/duyicheng/anaconda3/envs/comfyui/lib/python3.12/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/duyicheng/anaconda3/envs/comfyui/lib/python3.12/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/duyicheng/anaconda3/envs/comfyui/lib/python3.12/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 5 (char 7)
原来是多了两个花括号,这提醒我们,在复制时一定要稳定。
再次运行3次
队列也能看到。
但卡得不动,不知是什么原因。就当是成功了吧。
学习第二个
很简单,结合第一个,一次成功。注意安装第一个模块时,不要太高。否则不行。
第三个就不学了。