SuperRealCo commited on
Commit
cc9c76f
·
verified ·
1 Parent(s): e21db33

Delete script_examples

Browse files
script_examples/basic_api_example.py DELETED
@@ -1,127 +0,0 @@
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 "enable dev mode options"
7
- #in the settings of the UI (gear beside the "Queue Size: ") this will enable
8
- #a button on the UI to save workflows in api format.
9
-
10
- #keep in mind ComfyUI is pre alpha software so this format will change a bit.
11
-
12
- #this is the one for the default workflow
13
- prompt_text = """
14
- {
15
- "3": {
16
- "class_type": "KSampler",
17
- "inputs": {
18
- "cfg": 8,
19
- "denoise": 1,
20
- "latent_image": [
21
- "5",
22
- 0
23
- ],
24
- "model": [
25
- "4",
26
- 0
27
- ],
28
- "negative": [
29
- "7",
30
- 0
31
- ],
32
- "positive": [
33
- "6",
34
- 0
35
- ],
36
- "sampler_name": "euler",
37
- "scheduler": "normal",
38
- "seed": 8566257,
39
- "steps": 20
40
- }
41
- },
42
- "4": {
43
- "class_type": "CheckpointLoaderSimple",
44
- "inputs": {
45
- "ckpt_name": "v1-5-pruned-emaonly.safetensors"
46
- }
47
- },
48
- "5": {
49
- "class_type": "EmptyLatentImage",
50
- "inputs": {
51
- "batch_size": 1,
52
- "height": 512,
53
- "width": 512
54
- }
55
- },
56
- "6": {
57
- "class_type": "CLIPTextEncode",
58
- "inputs": {
59
- "clip": [
60
- "4",
61
- 1
62
- ],
63
- "text": "masterpiece best quality girl"
64
- }
65
- },
66
- "7": {
67
- "class_type": "CLIPTextEncode",
68
- "inputs": {
69
- "clip": [
70
- "4",
71
- 1
72
- ],
73
- "text": "bad hands"
74
- }
75
- },
76
- "8": {
77
- "class_type": "VAEDecode",
78
- "inputs": {
79
- "samples": [
80
- "3",
81
- 0
82
- ],
83
- "vae": [
84
- "4",
85
- 2
86
- ]
87
- }
88
- },
89
- "9": {
90
- "class_type": "SaveImage",
91
- "inputs": {
92
- "filename_prefix": "ComfyUI",
93
- "images": [
94
- "8",
95
- 0
96
- ]
97
- }
98
- }
99
- }
100
- """
101
-
102
- def queue_prompt(prompt):
103
- p = {"prompt": prompt}
104
-
105
- # If the workflow contains API nodes, you can add a Comfy API key to the `extra_data`` field of the payload.
106
- # p["extra_data"] = {
107
- # "api_key_comfy_org": "comfyui-87d01e28d*******************************************************" # replace with real key
108
- # }
109
- # See: https://docs.comfy.org/tutorials/api-nodes/overview
110
- # Generate a key here: https://platform.comfy.org/login
111
-
112
- data = json.dumps(p).encode('utf-8')
113
- req = request.Request("http://127.0.0.1:8188/prompt", data=data)
114
- request.urlopen(req)
115
-
116
-
117
- prompt = json.loads(prompt_text)
118
- #set the text prompt for our positive CLIPTextEncode
119
- prompt["6"]["inputs"]["text"] = "masterpiece best quality man"
120
-
121
- #set the seed for our KSampler node
122
- prompt["3"]["inputs"]["seed"] = 5
123
-
124
-
125
- queue_prompt(prompt)
126
-
127
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
script_examples/websockets_api_example.py DELETED
@@ -1,167 +0,0 @@
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 DELETED
@@ -1,159 +0,0 @@
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
-