AI编程-用Trae开发 驱动Comfyui进行AI绘画(基Flux.1模型): 注明:以下的代码目前只是基础版本,只能够驱动Comfyui进行启动,而无法通过代码进行调整ComfyuI绘画参数……
哈喽!伙伴们,我是小智,你们的AI向导。欢迎来到每日的AI学习时间。今天,我们将一起深入AI的奇妙世界,探索“AI编程-用Trae开发 驱动Comfyui进行AI绘画(基Flux.1模型)”,并学会本篇文章中所讲的全部知识点。还是那句话“不必远征未知,只需唤醒你的潜能!”跟着小智的步伐,我们终将学有所成,学以致用,并发现自身的更多可能性。话不多说,现在就让我们开始这场激发潜能的AI学习之旅吧。
AI编程-用Trae开发 驱动Comfyui进行AI绘画(基Flux.1模型):
注明:以下的代码目前只是基础版本,只能够驱动Comfyui进行启动,而无法通过代码进行调整ComfyuI绘画参数,后期会迭代优化
用到的工作流文件:
{
“6”: {
“inputs”: {
“text”: “This image is a digitally edited photograph depicting a large crowd of people walking in formation towards the central sunlit area amidst a hilly, rural landscape. The people are dressed in white robes or garments, creating a stark visual contrast with the warm orange and gold hues of the sunlight seeping through a cloudy sky. The sunlight forms a bright, almost divine, pathway over the heads of the crowd, guiding their direction towards a distant point and emphasizing the sense of unity and purpose.nnThe photograph appears to be a digitally enhanced and stylized version, utilizing filters and lighting effects to evoke a timeless, almost biblical or celestial quality. The hills and buildings in the distance include a mix of stone and wooden structures with red roofs, likely indicative of a rural European or Mediterranean setting. The horizon stretches out, blending into the clouds and sky, which is painted in varying shades of blue and grey with golden beams breaking through.nnThe crowd is composed of diverse individuals, but they are uniform in their attire and movement, contributing to the sense of collective spirituality or journey”,
“clip”: [
“30”,
1
]
},
“class_type”: “CLIPTextEncode”,
“_meta”: {
“title”: “CLIP Text Encode (Positive Prompt)”
}
},
“8”: {
“inputs”: {
“samples”: [
“31”,
0
],
“vae”: [
“30”,
2
]
},
“class_type”: “VAEDecode”,
“_meta”: {
“title”: “VAE解码”
}
},
“9”: {
“inputs”: {
“filename_prefix”: “ComfyUI”,
“images”: [
“8”,
0
]
},
“class_type”: “SaveImage”,
“_meta”: {
“title”: “保存图像”
}
},
“27”: {
“inputs”: {
“width”: 720,
“height”: 1280,
“batch_size”: 1
},
“class_type”: “EmptySD3LatentImage”,
“_meta”: {
“title”: “空Latent图像(SD3)”
}
},
“30”: {
“inputs”: {
“ckpt_name”: “flux1-dev-fp8.safetensors”
},
“class_type”: “CheckpointLoaderSimple”,
“_meta”: {
“title”: “Checkpoint加载器(简易)”
}
},
“31”: {
“inputs”: {
“seed”: -1,
“steps”: 25,
“cfg”: 0.5,
“sampler_name”: “euler”,
“scheduler”: “simple”,
“denoise”: 1,
“model”: [
“30”,
0
],
“positive”: [
“35”,
0
],
“negative”: [
“33”,
0
],
“latent_image”: [
“27”,
0
]
},
“class_type”: “KSampler”,
“_meta”: {
“title”: “K采样器”
}
},
“33”: {
“inputs”: {
“text”: “”,
“clip”: [
“30”,
1
]
},
“class_type”: “CLIPTextEncode”,
“_meta”: {
“title”: “CLIP Text Encode (Negative Prompt)”
}
},
“35”: {
“inputs”: {
“guidance”: 3,
“conditioning”: [
“6”,
0
]
},
“class_type”: “FluxGuidance”,
“_meta”: {
“title”: “Flux引导”
}
}
}
工作流详情图如下
本地调用comfyui生图参考初始版本代码(不推荐拷贝,测试用)
import requests
import json
import time
# 这个改成本地Comfyui启动端的IP地址以及端口号
server_url = “http://26.55.222.77:8188″
def queue_prompt(prompt):
response = requests.post(f”{server_url}/prompt”, json={“prompt”: prompt})
return response.json()
with open(“flux_dev.json”, “r”, encoding=”utf-8″) as f:
prompt_data = json.load(f)
result = queue_prompt(prompt_data)
prompt_id = result.get(‘prompt_id’)
print(f”[状态] 任务已提交,Prompt ID: {prompt_id}n”)
# 添加超时和计时器
start_time = time.time()
timeout = 1000 # 最长等待300秒(5分钟)
poll_interval = 1 # 轮询间隔(秒)
while True:
try:
# 获取任务状态
response = requests.get(f”{server_url}/history/{prompt_id}”)
history = response.json()
# 计算已等待时间
elapsed_time = int(time.time() – start_time)
if prompt_id in history:
outputs = history[prompt_id][‘outputs’]
print(f”n[成功] 生成完成!耗时: {elapsed_time}秒”)
# 提取图片信息
if ‘9’ in outputs: # 对应SaveImage节点
images = outputs[‘9’][‘images’]
for img in images:
print(f” → 图片路径: {img[‘filename’]}”)
print(f” → 尺寸: {img[‘width’]}x{img[‘height’]}”)
print(f” → 类型: {img[‘type’]}n”)
else:
print(“[警告] 未找到图片输出,请检查SaveImage节点配置”)
break
else:
# 实时输出等待状态
print(f”[等待中] 已等待 {elapsed_time}秒…”, end=’r’) # r实现原地刷新
# 超时检测
if time.time() – start_time > timeout:
print(“n[错误] 生成超时,请检查ComfyUI日志或调整超时时间”)
break
time.sleep(poll_interval)
本地调用comfyui生图参考最终版本代码(推荐拷贝)
直接可用,需要改一下Comfyui启动的IP地址
import requests
import json
import time
# 这个改成本地Comfyui启动端的IP地址以及端口号
server_url = “http://26.55.222.77:8188″
def queue_prompt(prompt):
response = requests.post(f”{server_url}/prompt”, json={“prompt”: prompt})
return response.json()
with open(“flux_dev.json”, “r”, encoding=”utf-8″) as f:
prompt_data = json.load(f)
result = queue_prompt(prompt_data)
prompt_id = result.get(‘prompt_id’)
print(f”[状态] 任务已提交,Prompt ID: {prompt_id}n”)
# 添加超时和计时器
start_time = time.time()
timeout = 1000 # 最长等待300秒(5分钟)
poll_interval = 1 # 轮询间隔(秒)
while True:
try:
# 获取任务状态
response = requests.get(f”{server_url}/history/{prompt_id}”)
history = response.json()
# 计算已等待时间
elapsed_time = int(time.time() – start_time)
if prompt_id in history:
outputs = history[prompt_id][‘outputs’]
print(f”n[成功] 生成完成!耗时: {elapsed_time}秒”)
# 提取图片信息
if ‘9’ in outputs: # 对应SaveImage节点
images = outputs[‘9’][‘images’]
for img in images:
print(f” → 图片路径: {img[‘filename’]}”)
print(f” → 尺寸: {img[‘width’]}x{img[‘height’]}”)
嘿,伙伴们,今天我们的AI探索之旅已经圆满结束。关于“AI编程-用Trae开发 驱动Comfyui进行AI绘画(基Flux.1模型)”的内容已经分享给大家了。感谢你们的陪伴,希望这次旅程让你对AI能够更了解、更喜欢。谨记,精准提问是解锁AI潜能的钥匙哦!如果有小伙伴想要了解学习更多的AI知识,请关注我们的官网“AI智研社”,保证让你收获满满呦!
还没有评论呢,快来抢沙发~