|
首先comfyui使用websockt实现请求在script_examples内存在示例py文件那么如何在代码端对comfyui实现api请求步骤:1.在安装comfyui时,有一个python编码文件夹2.在cmd使用文件夹的python执行pipinstallwebsocket-client3.利用编写官方提供的脚本importwebsocket importuuidimportjsonimporturllib.requestimporturllib.parseserver_address="127.0.0.1:8188"#看自己服务器是否是8188这个端口client_id=str(uuid.uuid4())#创建对应的iddefqueue_prompt(prompt):#函数返回对应prompt_id 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) returnjson.loads(urllib.request.urlopen(req).read())defget_image(filename,subfolder,folder_type):#获取对应图片 data={"filename":filename,"subfolder":subfolder,"type":folder_type} url_values=urllib.parse.urlencode(data) withurllib.request.urlopen("http://{}/view?{}".format(server_address,url_values))asresponse: returnresponse.read()defget_history(prompt_id): withurllib.request.urlopen("http://{}/history/{}".format(server_address,prompt_id))asresponse: returnjson.loads(response.read())defget_images(ws,prompt):#返回图篇 prompt_id=queue_prompt(prompt)['prompt_id'] output_images={} whileTrue: out=ws.recv() ifisinstance(out,str): message=json.loads(out) ifmessage['type']=='executing': data=message['data'] ifdata['node']isNoneanddata['prompt_id']==prompt_id: break#Executionisdone else: continue#previewsarebinarydata history=get_history(prompt_id)[prompt_id] foroinhistory['outputs']: fornode_idinhistory['outputs']: node_output=history['outputs'][node_id] if'images'innode_output: images_output=[] forimageinnode_output['images']: image_data=get_image(image['filename'],image['subfolder'],image['type']) images_output.append(image_data) output_images[node_id]=images_output returnoutput_images#这个prompt_text可以使用在工作流里面保存api开发,使用open(“地址”,“r”,“编码格式”)来读取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":"masterpiecebestqualitygirl" } }, "7":{ "class_type":"CLIPTextEncode", "inputs":{ "clip":[ "4", 1 ], "text":"badhands" } }, "8":{ "class_type":"VAEDecode", "inputs":{ "samples":[ "3", 0 ], "vae":[ "4", 2 ] } }, "9":{ "class_type":"SaveImage", "inputs":{ "filename_prefix":"ComfyUI", "images":[ "8", 0 ] } }}"""prompt=json.loads(prompt_text)prompt["6"]["inputs"]["text"]="masterpiecebestqualityman"#这个是通过json里面的结构进行修改。#settheseedforourKSamplernodeprompt["3"]["inputs"]["seed"]=5ws=websocket.WebSocket()ws.connect("ws://{}/ws?clientId={}".format(server_address,client_id))images=get_images(ws,prompt)#Commentedoutcodetodisplaytheoutputimages:fornode_idinimages: forimage_datainimages[node_id]: fromPILimportImage importio image=Image.open(io.BytesIO(image_data)) image.show()4.在对应编写的文件里面使用cmd,在使用刚刚下载对应客户端的python.exe执行编写好的文件就可以实现的在代码端调用api对工作流执行
|
|