Skip to content

Commit 58868e6

Browse files
committed
adjust basic api
1 parent 4581c67 commit 58868e6

File tree

1 file changed

+56
-53
lines changed

1 file changed

+56
-53
lines changed

basic_api.py

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,6 @@ def open_websocket_connection():
3333
# ---------------------------------------------------------------------------------------------------------------------
3434
# Basic API calls
3535

36-
def upload_image(input_path, name, server_address, image_type="input", overwrite=False):
37-
"""
38-
Uploads an image to ComfyUI using multipart/form-data encoding.
39-
40-
This function opens an image file from the specified path and uploads it to running ComfyUI. The server's address,
41-
the name to save the image as, and optional parameters for image type and overwrite behavior are provided as arguments.
42-
The image is uploaded as 'image/png'.
43-
44-
Args:
45-
input_path (str): The file system path to the image file to be uploaded.
46-
name (str): The name under which the image will be saved on ComfyUI.
47-
server_address (str): The address of running ComfyUI where the image will be uploaded, excluding the protocol prefix.
48-
image_type (str, optional): The type/category of the image being uploaded. Defaults to "input". Other options are "output" and "temp".
49-
overwrite (bool, optional): Flag indicating whether an existing file with the same name should be overwritten.
50-
Defaults to False.
51-
52-
Returns:
53-
The ComfyUI response to the upload request.
54-
"""
55-
with open(input_path, 'rb') as file:
56-
multipart_data = MultipartEncoder(
57-
fields= {
58-
'image': (name, file, 'image/png'),
59-
'type': image_type,
60-
'overwrite': str(overwrite).lower()
61-
}
62-
)
63-
64-
data = multipart_data
65-
headers = { 'Content-Type': multipart_data.content_type }
66-
request = urllib.request.Request("http://{}/upload/image".format(server_address), data=data, headers=headers)
67-
with urllib.request.urlopen(request) as response:
68-
return response.read()
69-
7036
def queue_prompt(prompt, client_id, server_address):
7137
"""
7238
Sends a prompt to a ComfyUI to place it into the workflow queue
@@ -89,6 +55,26 @@ def queue_prompt(prompt, client_id, server_address):
8955
req = urllib.request.Request("http://{}/prompt".format(server_address), data=data, headers=headers)
9056
return json.loads(urllib.request.urlopen(req).read())
9157

58+
59+
60+
def get_history(prompt_id, server_address):
61+
"""
62+
Fetches the history to a given prompt ID from ComfyUI
63+
64+
This function makes an HTTP GET request to a specified server address, requesting the history associated with
65+
a given prompt ID. The server is expected to return a JSON response that contains the history data, that
66+
include e.g the paths to the generated Images
67+
68+
Args:
69+
prompt_id (str): The unique identifier for the prompt whose history is being requested.
70+
server_address (str): The address of ComfyUI from which to retrieve the history, excluding the protocol prefix.
71+
72+
Returns:
73+
A dictionary parsed from the JSON response containing the history associated with the specified prompt ID.
74+
"""
75+
with urllib.request.urlopen("http://{}/history/{}".format(server_address, prompt_id)) as response:
76+
return json.loads(response.read())
77+
9278
def get_image(filename, subfolder, folder_type, server_address):
9379
"""
9480
Retrieves an image from ComfyUI based on specified parameters and returns the image data.
@@ -111,23 +97,39 @@ def get_image(filename, subfolder, folder_type, server_address):
11197
with urllib.request.urlopen("http://{}/view?{}".format(server_address, url_values)) as response:
11298
return response.read()
11399

114-
def get_history(prompt_id, server_address):
100+
def upload_image(input_path, name, server_address, image_type="input", overwrite=False):
115101
"""
116-
Fetches the history to a given prompt ID from ComfyUI
102+
Uploads an image to ComfyUI using multipart/form-data encoding.
117103
118-
This function makes an HTTP GET request to a specified server address, requesting the history associated with
119-
a given prompt ID. The server is expected to return a JSON response that contains the history data, that
120-
include e.g the paths to the generated Images
104+
This function opens an image file from the specified path and uploads it to running ComfyUI. The server's address,
105+
the name to save the image as, and optional parameters for image type and overwrite behavior are provided as arguments.
106+
The image is uploaded as 'image/png'.
121107
122108
Args:
123-
prompt_id (str): The unique identifier for the prompt whose history is being requested.
124-
server_address (str): The address of ComfyUI from which to retrieve the history, excluding the protocol prefix.
109+
input_path (str): The file system path to the image file to be uploaded.
110+
name (str): The name under which the image will be saved on ComfyUI.
111+
server_address (str): The address of running ComfyUI where the image will be uploaded, excluding the protocol prefix.
112+
image_type (str, optional): The type/category of the image being uploaded. Defaults to "input". Other options are "output" and "temp".
113+
overwrite (bool, optional): Flag indicating whether an existing file with the same name should be overwritten.
114+
Defaults to False.
125115
126116
Returns:
127-
A dictionary parsed from the JSON response containing the history associated with the specified prompt ID.
117+
The ComfyUI response to the upload request.
128118
"""
129-
with urllib.request.urlopen("http://{}/history/{}".format(server_address, prompt_id)) as response:
130-
return json.loads(response.read())
119+
with open(input_path, 'rb') as file:
120+
multipart_data = MultipartEncoder(
121+
fields= {
122+
'image': (name, file, 'image/png'),
123+
'type': image_type,
124+
'overwrite': str(overwrite).lower()
125+
}
126+
)
127+
128+
data = multipart_data
129+
headers = { 'Content-Type': multipart_data.content_type }
130+
request = urllib.request.Request("http://{}/upload/image".format(server_address), data=data, headers=headers)
131+
with urllib.request.urlopen(request) as response:
132+
return response.read()
131133

132134
# -------------------------------------------------------------------------------------------------------
133135
# API helper
@@ -184,7 +186,7 @@ def generate_image_by_prompt_and_image(prompt, output_path, input_path, filename
184186
"""
185187
try:
186188
ws, server_address, client_id = open_websocket_connection()
187-
upload_image(input_path, filename, server_address, client_id)
189+
upload_image(input_path, filename, server_address)
188190
prompt_id = queue_prompt(prompt, client_id, server_address)['prompt_id']
189191
track_progress(prompt, ws, prompt_id)
190192
images = get_images(prompt_id, server_address, save_previews)
@@ -371,9 +373,9 @@ def prompt_to_image(workflow, positve_prompt, negative_prompt='', save_previews=
371373

372374
if negative_prompt != '':
373375
negative_input_id = prompt.get(k_sampler)['inputs']['negative'][0]
374-
prompt.get(negative_input_id)['inputs']['text'] = positve_prompt
376+
prompt.get(negative_input_id)['inputs']['text'] = negative_prompt
375377

376-
generate_image_by_prompt(prompt, './output/', save_previews)
378+
generate_image_by_prompt(prompt, './output/blog/cyborg', save_previews)
377379

378380
def prompt_image_to_image(workflow, input_path, positve_prompt, negative_prompt='', save_previews=False):
379381
"""
@@ -403,20 +405,21 @@ def prompt_image_to_image(workflow, input_path, positve_prompt, negative_prompt=
403405
k_sampler = [key for key, value in id_to_class_type.items() if value == 'KSampler'][0]
404406
prompt.get(k_sampler)['inputs']['seed'] = random.randint(10**14, 10**15 - 1)
405407
postive_input_id = prompt.get(k_sampler)['inputs']['positive'][0]
406-
prompt.get(postive_input_id)['inputs']['text_g'] = positve_prompt
407-
prompt.get(postive_input_id)['inputs']['text_l'] = positve_prompt
408+
prompt.get(postive_input_id)['inputs']['text'] = positve_prompt
408409

409410
if negative_prompt != '':
410411
negative_input_id = prompt.get(k_sampler)['inputs']['negative'][0]
411-
id_to_class_type.get(negative_input_id)['inputs']['text_g'] = negative_prompt
412-
id_to_class_type.get(negative_input_id)['inputs']['text_l'] = negative_prompt
412+
prompt.get(negative_input_id)['inputs']['text'] = negative_prompt
413413

414414
image_loader = [key for key, value in id_to_class_type.items() if value == 'LoadImage'][0]
415415
filename = input_path.split('/')[-1]
416416
prompt.get(image_loader)['inputs']['image'] = filename
417417

418-
generate_image_by_prompt_and_image(prompt, './output/', input_path, filename, save_previews)
418+
generate_image_by_prompt_and_image(prompt, './output/blog/img2img', input_path, filename, save_previews)
419419

420420

421421
workflow = load_workflow('./workflows/base_workflow.json')
422-
prompt_to_image(workflow, 'Woman standing in a red dress in the middle of a crowded place with high skyscrapers', 'lowres, branding, watermark', save_previews=True)
422+
prompt_to_image(workflow, 'Cyborg in the cyberspace connection to different interfaces and screens with wires, cinematic, colorful, black and neon turquioise', 'ugly, lowres, text, branding', save_previews=True)
423+
# input_path = './input/ComfyUI_00299_.png'
424+
# for iter in range(1, 6):
425+
# prompt_image_to_image(workflow, input_path, 'Woman in a red dress standing in middle of a crowded place, skyscrapers in the background, cinematic, dark colors, distopian', save_previews=True)

0 commit comments

Comments
 (0)