-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomfy_generate.py
executable file
·139 lines (111 loc) · 4.63 KB
/
comfy_generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import websocket #NOTE: websocket-client (https://github.com/websocket-client/websocket-client)
import uuid
import json
from urllib import request, parse
from PIL import Image
import io
import workflow
server_url = ""
busy = False
progress_value = 0
progress_max = 0
# Generate a random client id
client_id = str(uuid.uuid4())
# open websocket connection
ws = websocket.WebSocket()
# Load the workflow from file
#comfy_workflow = json.load(open('workflow_api_2.json'))
comfy_workflow = json.loads(workflow.workflow)
# Functions to interact with the server
def connect(url):
global server_url
server_url = url
print ("Connecting to server at", server_url)
ws.connect("ws://{}/ws?clientId={}".format(server_url, client_id))
def queue():
p = {"prompt": comfy_workflow, "client_id": client_id}
data = json.dumps(p).encode('utf-8')
req = request.Request("http://{}/prompt".format(server_url), data=data)
return json.loads(request.urlopen(req).read())
def get_image(filename, subfolder, folder_type):
data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
url_values = parse.urlencode(data)
with request.urlopen("http://{}/view?{}".format(server_url, url_values)) as response:
return response.read()
def get_history(prompt_id):
with request.urlopen("http://{}/history/{}".format(server_url, prompt_id)) as response:
return json.loads(response.read())
def get_images(fun):
print ("Sending prompt to ComfyUI.")
prompt_id = queue()['prompt_id']
output_images = {}
busy = True
while True:
out = ws.recv()
if isinstance(out, str):
message = json.loads(out)
# print(message)
# type -> progress will have 'value' and 'max' in the data, for a progress bar.
if message['type'] == 'progress':
progress_value = message['data']['value']
progress_max = message['data']['max']
#print(f'{progress_value}/{progress_max}')
fun(progress_value/progress_max, "Generating...")
elif message['type'] == 'executing':
data = message['data']
if data['node'] is None and data['prompt_id'] == prompt_id:
break #Execution is done
else:
continue #previews are binary data
print ("Execution is done. Fetching images.")
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
busy = False
return output_images
def save_images(images, prefix):
print ("Saving images.")
for i, image_data in enumerate(images["7"]):
with open(f"{prefix}_{i}.png", "wb") as f:
f.write(image_data)
def images_to_pil(images):
images_pil = []
for i, image_data in enumerate(images["7"]):
image = Image.open(io.BytesIO(image_data))
images_pil.append(image)
return images_pil
# Functions to edit the workflow
def set_checkpoint(checkpoint):
comfy_workflow["2"]["inputs"]["ckpt_name"] = checkpoint
def set_dimensions(dim, batch_size=1):
comfy_workflow["5"]["inputs"]["width"] = dim[0]
comfy_workflow["5"]["inputs"]["height"] = dim[1]
comfy_workflow["5"]["inputs"]["batch_size"] = batch_size
def set_prompt(pos, neg):
comfy_workflow["12"]["inputs"]["text"] = pos
comfy_workflow["18"]["inputs"]["text"] = neg
def set_sampler(seed, steps, cfg, sampler_name, scheduler, denoise=1):
comfy_workflow["3"]["inputs"]["seed"] = seed
comfy_workflow["3"]["inputs"]["steps"] = steps
comfy_workflow["3"]["inputs"]["cfg"] = cfg
comfy_workflow["3"]["inputs"]["sampler_name"] = sampler_name
comfy_workflow["3"]["inputs"]["scheduler"] = scheduler
comfy_workflow["3"]["inputs"]["denoise"] = denoise
def set_freeu(freeu):
comfy_workflow["22"]["inputs"]["b1"] = freeu[0]
comfy_workflow["22"]["inputs"]["b2"] = freeu[1]
comfy_workflow["22"]["inputs"]["s1"] = freeu[2]
comfy_workflow["22"]["inputs"]["s2"] = freeu[3]
def set_rescale(rescale):
comfy_workflow["21"]["inputs"]["rescale"] = rescale
def set_clip(clip):
comfy_workflow["33"]["inputs"]["stop_at_clip_layer"] = clip
def set_filename_prefix(prefix):
comfy_workflow["17"]["inputs"]["filename_prefix"] = prefix