hipinis commited on
Commit
f3dd88c
·
verified ·
1 Parent(s): 8a98423

Upload script_examples

Browse files
script_examples/basic_api_example.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from urllib import request
3
+
4
+ #This is the ComfyUI api prompt format.
5
+
6
+ #If you want it for a specific workflow you can "File -> Export (API)" in the interface.
7
+
8
+ #this is the one for the default workflow
9
+ prompt_text = """
10
+ {
11
+ "3": {
12
+ "class_type": "KSampler",
13
+ "inputs": {
14
+ "cfg": 8,
15
+ "denoise": 1,
16
+ "latent_image": [
17
+ "5",
18
+ 0
19
+ ],
20
+ "model": [
21
+ "4",
22
+ 0
23
+ ],
24
+ "negative": [
25
+ "7",
26
+ 0
27
+ ],
28
+ "positive": [
29
+ "6",
30
+ 0
31
+ ],
32
+ "sampler_name": "euler",
33
+ "scheduler": "normal",
34
+ "seed": 8566257,
35
+ "steps": 20
36
+ }
37
+ },
38
+ "4": {
39
+ "class_type": "CheckpointLoaderSimple",
40
+ "inputs": {
41
+ "ckpt_name": "v1-5-pruned-emaonly.safetensors"
42
+ }
43
+ },
44
+ "5": {
45
+ "class_type": "EmptyLatentImage",
46
+ "inputs": {
47
+ "batch_size": 1,
48
+ "height": 512,
49
+ "width": 512
50
+ }
51
+ },
52
+ "6": {
53
+ "class_type": "CLIPTextEncode",
54
+ "inputs": {
55
+ "clip": [
56
+ "4",
57
+ 1
58
+ ],
59
+ "text": "masterpiece best quality girl"
60
+ }
61
+ },
62
+ "7": {
63
+ "class_type": "CLIPTextEncode",
64
+ "inputs": {
65
+ "clip": [
66
+ "4",
67
+ 1
68
+ ],
69
+ "text": "bad hands"
70
+ }
71
+ },
72
+ "8": {
73
+ "class_type": "VAEDecode",
74
+ "inputs": {
75
+ "samples": [
76
+ "3",
77
+ 0
78
+ ],
79
+ "vae": [
80
+ "4",
81
+ 2
82
+ ]
83
+ }
84
+ },
85
+ "9": {
86
+ "class_type": "SaveImage",
87
+ "inputs": {
88
+ "filename_prefix": "ComfyUI",
89
+ "images": [
90
+ "8",
91
+ 0
92
+ ]
93
+ }
94
+ }
95
+ }
96
+ """
97
+
98
+ def queue_prompt(prompt):
99
+ p = {"prompt": prompt}
100
+
101
+ # If the workflow contains API nodes, you can add a Comfy API key to the `extra_data`` field of the payload.
102
+ # p["extra_data"] = {
103
+ # "api_key_comfy_org": "comfyui-87d01e28d*******************************************************" # replace with real key
104
+ # }
105
+ # See: https://docs.comfy.org/tutorials/api-nodes/overview
106
+ # Generate a key here: https://platform.comfy.org/login
107
+
108
+ data = json.dumps(p).encode('utf-8')
109
+ req = request.Request("http://127.0.0.1:8188/prompt", data=data)
110
+ request.urlopen(req)
111
+
112
+
113
+ prompt = json.loads(prompt_text)
114
+ #set the text prompt for our positive CLIPTextEncode
115
+ prompt["6"]["inputs"]["text"] = "masterpiece best quality man"
116
+
117
+ #set the seed for our KSampler node
118
+ prompt["3"]["inputs"]["seed"] = 5
119
+
120
+
121
+ queue_prompt(prompt)
122
+
123
+
script_examples/websockets_api_example.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #This is an example that uses the websockets api to know when a prompt execution is done
2
+ #Once the prompt execution is done it downloads the images using the /history endpoint
3
+
4
+ import websocket #NOTE: websocket-client (https://github.com/websocket-client/websocket-client)
5
+ import uuid
6
+ import json
7
+ import urllib.request
8
+ import urllib.parse
9
+
10
+ server_address = "127.0.0.1:8188"
11
+ client_id = str(uuid.uuid4())
12
+
13
+ def queue_prompt(prompt, prompt_id):
14
+ p = {"prompt": prompt, "client_id": client_id, "prompt_id": prompt_id}
15
+ data = json.dumps(p).encode('utf-8')
16
+ req = urllib.request.Request("http://{}/prompt".format(server_address), data=data)
17
+ urllib.request.urlopen(req).read()
18
+
19
+ def get_image(filename, subfolder, folder_type):
20
+ data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
21
+ url_values = urllib.parse.urlencode(data)
22
+ with urllib.request.urlopen("http://{}/view?{}".format(server_address, url_values)) as response:
23
+ return response.read()
24
+
25
+ def get_history(prompt_id):
26
+ with urllib.request.urlopen("http://{}/history/{}".format(server_address, prompt_id)) as response:
27
+ return json.loads(response.read())
28
+
29
+ def get_images(ws, prompt):
30
+ prompt_id = str(uuid.uuid4())
31
+ queue_prompt(prompt, prompt_id)
32
+ output_images = {}
33
+ while True:
34
+ out = ws.recv()
35
+ if isinstance(out, str):
36
+ message = json.loads(out)
37
+ if message['type'] == 'executing':
38
+ data = message['data']
39
+ if data['node'] is None and data['prompt_id'] == prompt_id:
40
+ break #Execution is done
41
+ else:
42
+ # If you want to be able to decode the binary stream for latent previews, here is how you can do it:
43
+ # bytesIO = BytesIO(out[8:])
44
+ # preview_image = Image.open(bytesIO) # This is your preview in PIL image format, store it in a global
45
+ continue #previews are binary data
46
+
47
+ history = get_history(prompt_id)[prompt_id]
48
+ for node_id in history['outputs']:
49
+ node_output = history['outputs'][node_id]
50
+ images_output = []
51
+ if 'images' in node_output:
52
+ for image in node_output['images']:
53
+ image_data = get_image(image['filename'], image['subfolder'], image['type'])
54
+ images_output.append(image_data)
55
+ output_images[node_id] = images_output
56
+
57
+ return output_images
58
+
59
+ prompt_text = """
60
+ {
61
+ "3": {
62
+ "class_type": "KSampler",
63
+ "inputs": {
64
+ "cfg": 8,
65
+ "denoise": 1,
66
+ "latent_image": [
67
+ "5",
68
+ 0
69
+ ],
70
+ "model": [
71
+ "4",
72
+ 0
73
+ ],
74
+ "negative": [
75
+ "7",
76
+ 0
77
+ ],
78
+ "positive": [
79
+ "6",
80
+ 0
81
+ ],
82
+ "sampler_name": "euler",
83
+ "scheduler": "normal",
84
+ "seed": 8566257,
85
+ "steps": 20
86
+ }
87
+ },
88
+ "4": {
89
+ "class_type": "CheckpointLoaderSimple",
90
+ "inputs": {
91
+ "ckpt_name": "v1-5-pruned-emaonly.safetensors"
92
+ }
93
+ },
94
+ "5": {
95
+ "class_type": "EmptyLatentImage",
96
+ "inputs": {
97
+ "batch_size": 1,
98
+ "height": 512,
99
+ "width": 512
100
+ }
101
+ },
102
+ "6": {
103
+ "class_type": "CLIPTextEncode",
104
+ "inputs": {
105
+ "clip": [
106
+ "4",
107
+ 1
108
+ ],
109
+ "text": "masterpiece best quality girl"
110
+ }
111
+ },
112
+ "7": {
113
+ "class_type": "CLIPTextEncode",
114
+ "inputs": {
115
+ "clip": [
116
+ "4",
117
+ 1
118
+ ],
119
+ "text": "bad hands"
120
+ }
121
+ },
122
+ "8": {
123
+ "class_type": "VAEDecode",
124
+ "inputs": {
125
+ "samples": [
126
+ "3",
127
+ 0
128
+ ],
129
+ "vae": [
130
+ "4",
131
+ 2
132
+ ]
133
+ }
134
+ },
135
+ "9": {
136
+ "class_type": "SaveImage",
137
+ "inputs": {
138
+ "filename_prefix": "ComfyUI",
139
+ "images": [
140
+ "8",
141
+ 0
142
+ ]
143
+ }
144
+ }
145
+ }
146
+ """
147
+
148
+ prompt = json.loads(prompt_text)
149
+ #set the text prompt for our positive CLIPTextEncode
150
+ prompt["6"]["inputs"]["text"] = "masterpiece best quality man"
151
+
152
+ #set the seed for our KSampler node
153
+ prompt["3"]["inputs"]["seed"] = 5
154
+
155
+ ws = websocket.WebSocket()
156
+ ws.connect("ws://{}/ws?clientId={}".format(server_address, client_id))
157
+ images = get_images(ws, prompt)
158
+ ws.close() # for in case this example is used in an environment where it will be repeatedly called, like in a Gradio app. otherwise, you'll randomly receive connection timeouts
159
+ #Commented out code to display the output images:
160
+
161
+ # for node_id in images:
162
+ # for image_data in images[node_id]:
163
+ # from PIL import Image
164
+ # import io
165
+ # image = Image.open(io.BytesIO(image_data))
166
+ # image.show()
167
+
script_examples/websockets_api_example_ws_images.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #This is an example that uses the websockets api and the SaveImageWebsocket node to get images directly without
2
+ #them being saved to disk
3
+
4
+ import websocket #NOTE: websocket-client (https://github.com/websocket-client/websocket-client)
5
+ import uuid
6
+ import json
7
+ import urllib.request
8
+ import urllib.parse
9
+
10
+ server_address = "127.0.0.1:8188"
11
+ client_id = str(uuid.uuid4())
12
+
13
+ def queue_prompt(prompt):
14
+ p = {"prompt": prompt, "client_id": client_id}
15
+ data = json.dumps(p).encode('utf-8')
16
+ req = urllib.request.Request("http://{}/prompt".format(server_address), data=data)
17
+ return json.loads(urllib.request.urlopen(req).read())
18
+
19
+ def get_image(filename, subfolder, folder_type):
20
+ data = {"filename": filename, "subfolder": subfolder, "type": folder_type}
21
+ url_values = urllib.parse.urlencode(data)
22
+ with urllib.request.urlopen("http://{}/view?{}".format(server_address, url_values)) as response:
23
+ return response.read()
24
+
25
+ def get_history(prompt_id):
26
+ with urllib.request.urlopen("http://{}/history/{}".format(server_address, prompt_id)) as response:
27
+ return json.loads(response.read())
28
+
29
+ def get_images(ws, prompt):
30
+ prompt_id = queue_prompt(prompt)['prompt_id']
31
+ output_images = {}
32
+ current_node = ""
33
+ while True:
34
+ out = ws.recv()
35
+ if isinstance(out, str):
36
+ message = json.loads(out)
37
+ if message['type'] == 'executing':
38
+ data = message['data']
39
+ if data['prompt_id'] == prompt_id:
40
+ if data['node'] is None:
41
+ break #Execution is done
42
+ else:
43
+ current_node = data['node']
44
+ else:
45
+ if current_node == 'save_image_websocket_node':
46
+ images_output = output_images.get(current_node, [])
47
+ images_output.append(out[8:])
48
+ output_images[current_node] = images_output
49
+
50
+ return output_images
51
+
52
+ prompt_text = """
53
+ {
54
+ "3": {
55
+ "class_type": "KSampler",
56
+ "inputs": {
57
+ "cfg": 8,
58
+ "denoise": 1,
59
+ "latent_image": [
60
+ "5",
61
+ 0
62
+ ],
63
+ "model": [
64
+ "4",
65
+ 0
66
+ ],
67
+ "negative": [
68
+ "7",
69
+ 0
70
+ ],
71
+ "positive": [
72
+ "6",
73
+ 0
74
+ ],
75
+ "sampler_name": "euler",
76
+ "scheduler": "normal",
77
+ "seed": 8566257,
78
+ "steps": 20
79
+ }
80
+ },
81
+ "4": {
82
+ "class_type": "CheckpointLoaderSimple",
83
+ "inputs": {
84
+ "ckpt_name": "v1-5-pruned-emaonly.safetensors"
85
+ }
86
+ },
87
+ "5": {
88
+ "class_type": "EmptyLatentImage",
89
+ "inputs": {
90
+ "batch_size": 1,
91
+ "height": 512,
92
+ "width": 512
93
+ }
94
+ },
95
+ "6": {
96
+ "class_type": "CLIPTextEncode",
97
+ "inputs": {
98
+ "clip": [
99
+ "4",
100
+ 1
101
+ ],
102
+ "text": "masterpiece best quality girl"
103
+ }
104
+ },
105
+ "7": {
106
+ "class_type": "CLIPTextEncode",
107
+ "inputs": {
108
+ "clip": [
109
+ "4",
110
+ 1
111
+ ],
112
+ "text": "bad hands"
113
+ }
114
+ },
115
+ "8": {
116
+ "class_type": "VAEDecode",
117
+ "inputs": {
118
+ "samples": [
119
+ "3",
120
+ 0
121
+ ],
122
+ "vae": [
123
+ "4",
124
+ 2
125
+ ]
126
+ }
127
+ },
128
+ "save_image_websocket_node": {
129
+ "class_type": "SaveImageWebsocket",
130
+ "inputs": {
131
+ "images": [
132
+ "8",
133
+ 0
134
+ ]
135
+ }
136
+ }
137
+ }
138
+ """
139
+
140
+ prompt = json.loads(prompt_text)
141
+ #set the text prompt for our positive CLIPTextEncode
142
+ prompt["6"]["inputs"]["text"] = "masterpiece best quality man"
143
+
144
+ #set the seed for our KSampler node
145
+ prompt["3"]["inputs"]["seed"] = 5
146
+
147
+ ws = websocket.WebSocket()
148
+ ws.connect("ws://{}/ws?clientId={}".format(server_address, client_id))
149
+ images = get_images(ws, prompt)
150
+ ws.close() # for in case this example is used in an environment where it will be repeatedly called, like in a Gradio app. otherwise, you'll randomly receive connection timeouts
151
+ #Commented out code to display the output images:
152
+
153
+ # for node_id in images:
154
+ # for image_data in images[node_id]:
155
+ # from PIL import Image
156
+ # import io
157
+ # image = Image.open(io.BytesIO(image_data))
158
+ # image.show()
159
+