360軟件商店衡陽seo優(yōu)化報價
來進行知識接力了:
首先了解下ComfiUI的API
stable diffusion comfyui的api使用教程-CSDN博客
對于ComfiUI,接口比較簡單。查詢接口比較容易看明白。
對于發(fā)起prompt的請求,如果需要圖片的,則需預先上傳圖片給ComfiUI,獲取返回的文件名進行調用。
?
發(fā)起調用后,實際上是加入隊列,獲得一個任務ID,通過ws實時獲取對應的執(zhí)行狀態(tài),進度等數(shù)據(jù)。當未執(zhí)行時,則查詢任務獲得圖片。
AI的解釋
?
這段代碼是一個使用websockets API的Python示例,用于檢測提示執(zhí)行何時完成。一旦提示執(zhí)行完成,它將使用/history端點下載圖像。
首先,導入所需的庫,如websocket(需要安裝websocket-client),uuid,json,urllib.request和urllib.parse。設置服務器地址和客戶端ID。
定義幾個函數(shù):
- queue_prompt(prompt):向服務器發(fā)送一個提示,并返回包含prompt_id的響應。
- get_image(filename, subfolder, folder_type):從服務器下載指定的圖像數(shù)據(jù)。
- get_history(prompt_id):獲取與給定prompt_id關聯(lián)的歷史記錄。
- get_images(ws, prompt):通過websocket接收消息,直到提示執(zhí)行完成。然后獲取歷史記錄并下載所有相關圖像。
在主程序中,首先定義了一個JSON字符串prompt_text,表示要使用的提示。然后解析這個字符串為Python字典,并根據(jù)需要修改其中的文本提示和種子值。
接下來,創(chuàng)建一個WebSocket連接到服務器,并調用get_images()函數(shù)來獲取圖像。最后,顯示輸出圖像(這部分已注釋掉)。
簡而言之,這段代碼的主要目的是使用websockets API與服務器通信,以執(zhí)行提示并在提示完成后下載生成的圖像。
AI加上注釋的代碼
# 導入所需庫
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())# 定義向服務器發(fā)送提示的函數(shù)
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())# 定義從服務器下載圖像數(shù)據(jù)的函數(shù)
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()# 定義獲取歷史記錄的函數(shù)
def get_history(prompt_id):with urllib.request.urlopen("http://{}/history/{}".format(server_address, prompt_id)) as response:return json.loads(response.read())# 定義通過WebSocket接收消息并下載圖像的函數(shù)
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 # 執(zhí)行完成else:continue # 預覽是二進制數(shù)據(jù)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_outputreturn 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字典,并根據(jù)需要修改其中的文本提示和種子值
prompt = json.loads(prompt_text)
prompt["6"]["inputs"]["text"] = "masterpiece best quality man"
prompt["3"]["inputs"]["seed"] = 5# 創(chuàng)建一個WebSocket連接到服務器
ws = websocket.WebSocket()
ws.connect("ws://{}/ws?clientId={}".format(server_address, client_id))# 調用get_images()函數(shù)來獲取圖像
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()
代碼尚未深入,就目前的知識進度做一下記錄和分享