ComfiUI API调用随记

news/2024/7/10 20:57:26 标签: python, comfiui, stable diffusion, api

来进行知识接力了:

首先了解下ComfiUI的API
stable diffusion comfyui的api使用教程-CSDN博客

对于ComfiUI,接口比较简单。查询接口比较容易看明白。
对于发起prompt的请求,如果需要图片的,则需预先上传图片给ComfiUI,获取返回的文件名进行调用。
 

发起调用后,实际上是加入队列,获得一个任务ID,通过ws实时获取对应的执行状态,进度等数据。当未执行时,则查询任务获得图片。

AI的解释
 

这段代码是一个使用websockets API的Python示例,用于检测提示执行何时完成。一旦提示执行完成,它将使用/history端点下载图像。

首先,导入所需的库,如websocket(需要安装websocket-client),uuid,json,urllib.request和urllib.parse。设置服务器地址和客户端ID。

定义几个函数:

  1. queue_prompt(prompt):向服务器发送一个提示,并返回包含prompt_id的响应。
  2. get_image(filename, subfolder, folder_type):从服务器下载指定的图像数据。
  3. get_history(prompt_id):获取与给定prompt_id关联的历史记录。
  4. get_images(ws, prompt):通过websocket接收消息,直到提示执行完成。然后获取历史记录并下载所有相关图像。

在主程序中,首先定义了一个JSON字符串prompt_text,表示要使用的提示。然后解析这个字符串为Python字典,并根据需要修改其中的文本提示和种子值。

接下来,创建一个WebSocket连接到服务器,并调用get_images()函数来获取图像。最后,显示输出图像(这部分已注释掉)。

简而言之,这段代码的主要目的是使用websockets API与服务器通信,以执行提示并在提示完成后下载生成的图像。

AI加上注释的代码

python"># 导入所需库
import websocket # NOTE: 需要安装websocket-client (https://github.com/websocket-client/websocket-client)
import uuid
import json
import urllib.request
import urllib.parse

# 设置服务器地址和客户端ID
server_address = "127.0.0.1:8188"
client_id = str(uuid.uuid4())

# 定义向服务器发送提示的函数
def queue_prompt(prompt):
    p = {"prompt": prompt, "client_id": client_id}
    data = json.dumps(p).encode('utf-8')
    req =  urllib.request.Request("http://{}/prompt".format(server_address), data=data)
    return json.loads(urllib.request.urlopen(req).read())

# 定义从服务器下载图像数据的函数
def get_image(filename, subfolder, folder_type):
    data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
    url_values = urllib.parse.urlencode(data)
    with urllib.request.urlopen("http://{}/view?{}".format(server_address, url_values)) as response:
        return response.read()

# 定义获取历史记录的函数
def get_history(prompt_id):
    with urllib.request.urlopen("http://{}/history/{}".format(server_address, prompt_id)) as response:
        return json.loads(response.read())

# 定义通过WebSocket接收消息并下载图像的函数
def get_images(ws, prompt):
    prompt_id = queue_prompt(prompt)['prompt_id']
    output_images = {}
    while True:
        out = ws.recv()
        if isinstance(out, str):
            message = json.loads(out)
            if message['type'] == 'executing':
                data = message['data']
                if data['node'] is None and data['prompt_id'] == prompt_id:
                    break # 执行完成
        else:
            continue # 预览是二进制数据

    history = get_history(prompt_id)[prompt_id]
    for o in history['outputs']:
        for node_id in history['outputs']:
            node_output = history['outputs'][node_id]
            if 'images' in node_output:
                images_output = []
                for image in node_output['images']:
                    image_data = get_image(image['filename'], image['subfolder'], image['type'])
                    images_output.append(image_data)
            output_images[node_id] = images_output

    return output_images

# 示例JSON字符串,表示要使用的提示
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.ckpt"
        }
    },
    "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
            ]
        }
    }
}
"""

# 将示例JSON字符串解析为Python字典,并根据需要修改其中的文本提示和种子值
prompt = json.loads(prompt_text)
prompt["6"]["inputs"]["text"] = "masterpiece best quality man"
prompt["3"]["inputs"]["seed"] = 5

# 创建一个WebSocket连接到服务器
ws = websocket.WebSocket()
ws.connect("ws://{}/ws?clientId={}".format(server_address, client_id))

# 调用get_images()函数来获取图像
images = get_images(ws, prompt)

# 显示输出图像(这部分已注释掉)
#Commented out code to display the output images:

# for node_id in images:
#     for image_data in images[node_id]:
#         from PIL import Image
#         import io
#         image = Image.open(io.BytesIO(image_data))
#         image.show()

代码尚未深入,就目前的知识进度做一下记录和分享


http://www.niftyadmin.cn/n/5237831.html

相关文章

Python Selenium 图片资源自动搜索保存 项目实践

实现访问首页 from os.path import dirnamefrom selenium import webdriverclass ImageAutoSearchAndSave:"""图片自动搜索保存"""def __init__(self):"""初始化"""self.driver webdriver.Chrome(executable_pa…

认知觉醒(三)

认知觉醒(三) 第二节 焦虑:焦虑的根源 焦虑肯定是你的老朋友了,它总像背景音乐一样伴随着你,我们虽对它极为熟悉,却从来不知道它究竟是谁。我也是默默忍受多年之后,终于在某天鼓足气力和它对视了一番,从…

电子取证--windows下的volatility分析与讲解

1.volatility的安装 提示:我用的是2.6版本(windows),如果直接下载的出现问题,用迅雷就可以解决 下载地址:Volatility 2.volatility的使用 1.进入终端,查看镜像的系统信息: volati…

初识动态规划算法(题目加解析)

文章目录 什么是动态规划正文力扣题第 N 个泰波那契数三步问题使用最小花费爬楼梯 总结 什么是动态规划 线性动态规划:是可以用一个dp表来存储内容,并且找到规律存储,按照规律存储。让第i个位置的值等于题目要求的答案 >dp表:dp表就是用一…

仅 CSS 阅读进度条

为了构建一个阅读进度条,即显示用户向下滚动时阅读文章的进度,很难不考虑 JavaScript。但是,事实证明,您也可以使用纯 CSS 构建阅读进度条。 从本质上讲,一个名为 animation-timeline 的新实验性 CSS 属性可以让你指定…

质量小议35 -- SQL注入

已经记不得上次用到SQL注入是什么时候了,一些概念和操作已经模糊。 最近与人聊起SQL注入,重新翻阅,暂记于此。 重点:敏感信息、权限过大、未脱敏的输入/输出、协议、框架、数据包、明文、安全意识 SQL - Structured Query La…

【hacker送书第8期】Java从入门到精通(第7版)

第8期图书推荐 内容简介编辑推荐作者简介图书目录参与方式 内容简介 《Java从入门到精通(第7版)》从初学者角度出发,通过通俗易懂的语言、丰富多彩的实例,详细讲解了使用Java语言进行程序开发需要掌握的知识。全书分为4篇共24章&a…

【自用数据结构】—将链表中的奇数全部移动到偶数前面

void move(LNode*& L){ LNode* pL; while(p!null){ if((p->next->data)%21) //判断该元素是否为奇函数 LNode *s p->next; p->nexts->next; s->nextL->next; L->nexts; else pp->next; } }