Upload basic_api_example.py
Browse files
script_examples/basic_api_example.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from urllib import request, parse
|
3 |
+
import random
|
4 |
+
|
5 |
+
#this is the ComfyUI api prompt format. If you want it for a specific workflow you can copy it from the prompt section
|
6 |
+
#of the image metadata of images generated with ComfyUI
|
7 |
+
#keep in mind ComfyUI is pre alpha software so this format will change a bit.
|
8 |
+
|
9 |
+
#this is the one for the default workflow
|
10 |
+
prompt_text = """
|
11 |
+
{
|
12 |
+
"3": {
|
13 |
+
"class_type": "KSampler",
|
14 |
+
"inputs": {
|
15 |
+
"cfg": 8,
|
16 |
+
"denoise": 1,
|
17 |
+
"latent_image": [
|
18 |
+
"5",
|
19 |
+
0
|
20 |
+
],
|
21 |
+
"model": [
|
22 |
+
"4",
|
23 |
+
0
|
24 |
+
],
|
25 |
+
"negative": [
|
26 |
+
"7",
|
27 |
+
0
|
28 |
+
],
|
29 |
+
"positive": [
|
30 |
+
"6",
|
31 |
+
0
|
32 |
+
],
|
33 |
+
"sampler_name": "euler",
|
34 |
+
"scheduler": "normal",
|
35 |
+
"seed": 8566257,
|
36 |
+
"steps": 20
|
37 |
+
}
|
38 |
+
},
|
39 |
+
"4": {
|
40 |
+
"class_type": "CheckpointLoaderSimple",
|
41 |
+
"inputs": {
|
42 |
+
"ckpt_name": "v1-5-pruned-emaonly.ckpt"
|
43 |
+
}
|
44 |
+
},
|
45 |
+
"5": {
|
46 |
+
"class_type": "EmptyLatentImage",
|
47 |
+
"inputs": {
|
48 |
+
"batch_size": 1,
|
49 |
+
"height": 512,
|
50 |
+
"width": 512
|
51 |
+
}
|
52 |
+
},
|
53 |
+
"6": {
|
54 |
+
"class_type": "CLIPTextEncode",
|
55 |
+
"inputs": {
|
56 |
+
"clip": [
|
57 |
+
"4",
|
58 |
+
1
|
59 |
+
],
|
60 |
+
"text": "masterpiece best quality girl"
|
61 |
+
}
|
62 |
+
},
|
63 |
+
"7": {
|
64 |
+
"class_type": "CLIPTextEncode",
|
65 |
+
"inputs": {
|
66 |
+
"clip": [
|
67 |
+
"4",
|
68 |
+
1
|
69 |
+
],
|
70 |
+
"text": "bad hands"
|
71 |
+
}
|
72 |
+
},
|
73 |
+
"8": {
|
74 |
+
"class_type": "VAEDecode",
|
75 |
+
"inputs": {
|
76 |
+
"samples": [
|
77 |
+
"3",
|
78 |
+
0
|
79 |
+
],
|
80 |
+
"vae": [
|
81 |
+
"4",
|
82 |
+
2
|
83 |
+
]
|
84 |
+
}
|
85 |
+
},
|
86 |
+
"9": {
|
87 |
+
"class_type": "SaveImage",
|
88 |
+
"inputs": {
|
89 |
+
"filename_prefix": "ComfyUI",
|
90 |
+
"images": [
|
91 |
+
"8",
|
92 |
+
0
|
93 |
+
]
|
94 |
+
}
|
95 |
+
}
|
96 |
+
}
|
97 |
+
"""
|
98 |
+
|
99 |
+
def queue_prompt(prompt):
|
100 |
+
p = {"prompt": prompt}
|
101 |
+
data = json.dumps(p).encode('utf-8')
|
102 |
+
req = request.Request("http://127.0.0.1:8188/prompt", data=data)
|
103 |
+
request.urlopen(req)
|
104 |
+
|
105 |
+
|
106 |
+
prompt = json.loads(prompt_text)
|
107 |
+
#set the text prompt for our positive CLIPTextEncode
|
108 |
+
prompt["6"]["inputs"]["text"] = "masterpiece best quality man"
|
109 |
+
|
110 |
+
#set the seed for our KSampler node
|
111 |
+
prompt["3"]["inputs"]["seed"] = 5
|
112 |
+
|
113 |
+
|
114 |
+
queue_prompt(prompt)
|
115 |
+
|
116 |
+
|