-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomfy_client.py
176 lines (154 loc) · 8.16 KB
/
comfy_client.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import json
from PIL import Image
import io
from logger import logger
import os
import websocket # NOTE: websocket-client (https://github.com/websocket-client/websocket-client)
import uuid
from api.websocket_api import queue_prompt, get_history, get_image, upload_data, clear_comfy_cache
class ComfyClient:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.ws, self.server_addr, self.client_id = open_websocket_connection(self.ip, self.port)
def run_workflow(self, workflow, user_inputs, output_path='./output', save_previews=False):
prompt, data_to_upload = prepare_inputs(workflow, user_inputs)
self.post_to_comfy_server(prompt, data_to_upload, output_path=output_path, save_previews=save_previews)
pass
def post_to_comfy_server(self, prompt, data_to_upload, output_path='./output', save_previews=False):
if len(data_to_upload) > 0:
upload_data(data_to_upload, self.server_addr)
prompt_id = queue_prompt(prompt, self.client_id, self.server_addr)['prompt_id']
output_prompt_id = track_progress(prompt, self.ws, prompt_id)
output_images, output_videos = get_outputs(output_prompt_id, self.server_addr, save_previews)
if len(output_images):
save_image(output_images, output_path, save_previews)
if len(output_videos):
save_video(output_videos, output_path, save_previews)
def open_websocket_connection(ip='127.0.0.1', port=8288):
server_address = f'{ip}:{port}'
client_id = str(uuid.uuid4())
ws = websocket.WebSocket()
ws.connect("ws://{}/ws?clientId={}".format(server_address, client_id))
return ws, server_address, client_id
def prepare_inputs(workflow, user_inputs):
prompt = json.loads(workflow)
id_to_class_title = {id: details['_meta']['title'] for id, details in prompt.items()}
input_nodes = [key for key, value in id_to_class_title.items() if value.startswith('[Input]')]
data_to_upload = []
for input_node in input_nodes:
input_key = id_to_class_title.get(input_node).split('-')[1]
if input_key in user_inputs:
if prompt.get(input_node)['class_type'] == 'LoadImage':
prompt.get(input_node)['inputs']['image'] = user_inputs.get(input_key).split('/')[-1]
data_to_upload.append({'filepath': user_inputs.get(input_key), 'type': 'image'})
elif prompt.get(input_node)['class_type'] == 'VHS_LoadVideo':
prompt.get(input_node)['inputs']['video'] = user_inputs.get(input_key).split('/')[-1]
data_to_upload.append({'filepath': user_inputs.get(input_key), 'type': 'video'})
else:
if 'string' in prompt.get(input_node)['inputs']:
prompt.get(input_node)['inputs']['string'] = user_inputs.get(input_key)
elif 'value' in prompt.get(input_node)['inputs']:
prompt.get(input_node)['inputs']['value'] = user_inputs.get(input_key)
else:
class_type = input_node['class_type']
raise TypeError(f'Input type {class_type} is not a valid input for this workflow')
else:
raise ValueError(f'Input key {input_key} not found in user inputs')
return prompt, data_to_upload
def save_image(images, output_path, save_previews):
for itm in images:
directory = os.path.join(output_path, 'temp/') if itm['type'] == 'temp' and save_previews else output_path
os.makedirs(directory, exist_ok=True)
try:
image = Image.open(io.BytesIO(itm['image_data']))
image.save(os.path.join(directory, itm['file_name']))
except Exception as e:
logger.error(f"Failed to save image {itm['file_name']}: {e}")
def save_video(videos, output_path, save_previews):
for itm in videos:
directory = os.path.join(output_path, 'temp/') if itm['type'] == 'temp' and save_previews else output_path
os.makedirs(directory, exist_ok=True)
try:
with open(os.path.join(directory, itm['file_name']), 'wb') as f:
f.write(itm['video_data'])
except Exception as e:
logger.error(f"Failed to save image {itm['file_name']}: {e}")
def track_progress(prompt, ws, prompt_id):
node_ids = list(prompt.keys())
total_num_nodes = len(node_ids)
finished_nodes = []
finished = True
has_unfinished_batch = False
final_prompt_id = prompt_id
while True:
out = ws.recv()
if isinstance(out, str):
message = json.loads(out)
# print(message)
data = message['data']
if 'output' in data:
if 'unfinished_batch' in message['data']['output'] and \
message['data']['output']['unfinished_batch'][0] is True:
logger.info('Has unfinished batch, continue...')
finished = False
has_unfinished_batch = True
else:
if has_unfinished_batch:
final_prompt_id = data['prompt_id']
logger.info(f'Finished, final prompt id {final_prompt_id}, original prompt id {prompt_id}.')
finished = True
if message['type'] == 'progress':
current_step = data['value']
max_step = data['max']
logger.info(f'In K-Sampler -> Step: {current_step} of: {max_step}')
if message['type'] == 'execution_cached':
for itm in data['nodes']:
if itm not in finished_nodes:
finished_nodes.append(itm)
logger.info(f'Progress: {len(finished_nodes)}/{total_num_nodes} Tasks done')
if message['type'] == 'executing':
if data['node'] not in finished_nodes:
finished_nodes.append(data['node'])
logger.info(f'Progress: {len(finished_nodes)}/{total_num_nodes} Tasks done')
# if data['node'] is None and data['prompt_id'] == prompt_id and not unfinished:
if has_unfinished_batch:
if data['node'] is None and finished:
break # Execution is done
else:
if data['node'] is None and data['prompt_id'] == prompt_id and finished:
break
else:
continue # previews are binary data
return final_prompt_id
def get_outputs(prompt_id, server_address, allow_preview=False):
output_images = []
output_videos = []
history = get_history(prompt_id, server_address)[prompt_id]
for node_id in history['outputs']:
node_output = history['outputs'][node_id]
if 'images' in node_output:
output_data = {}
for image in node_output['images']:
if allow_preview and image['type'] == 'temp':
preview_data = get_image(image['filename'], image['subfolder'], image['type'], server_address)
output_data['image_data'] = preview_data
if image['type'] == 'output':
image_data = get_image(image['filename'], image['subfolder'], image['type'], server_address)
output_data['image_data'] = image_data
output_data['file_name'] = image['filename']
output_data['type'] = image['type']
output_images.append(output_data)
if 'gifs' in node_output:
output_data = {}
for video in node_output['gifs']:
if allow_preview and video['type'] == 'temp':
preview_data = get_image(video['filename'], video['subfolder'], video['type'], server_address)
output_data['video_data'] = preview_data
if video['type'] == 'output':
video_data = get_image(video['filename'], video['subfolder'], video['type'], server_address)
output_data['video_data'] = video_data
output_data['file_name'] = video['filename']
output_data['type'] = video['type']
output_videos.append(output_data)
return output_images, output_videos