-
Notifications
You must be signed in to change notification settings - Fork 1
/
stability.js
153 lines (147 loc) · 2.96 KB
/
stability.js
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
async function generate(prompt) {
const res = await fetch("http://localhost:8188/prompt", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ prompt }),
});
const data = await res.json();
return data;
}
function createPromptString(prompt, seed = 156680208700286) {
return JSON.parse(`{
"3": {
"inputs": {
"seed": ${seed},
"steps": 20,
"cfg": 8,
"sampler_name": "euler",
"scheduler": "normal",
"denoise": 1,
"model": [
"4",
0
],
"positive": [
"6",
0
],
"negative": [
"7",
0
],
"latent_image": [
"5",
0
]
},
"class_type": "KSampler",
"_meta": {
"title": "KSampler"
}
},
"4": {
"inputs": {
"ckpt_name": "epicrealism_naturalSinRC1VAE.safetensors"
},
"class_type": "CheckpointLoaderSimple",
"_meta": {
"title": "Load Checkpoint"
}
},
"5": {
"inputs": {
"width": 512,
"height": 512,
"batch_size": 1
},
"class_type": "EmptyLatentImage",
"_meta": {
"title": "Empty Latent Image"
}
},
"6": {
"inputs": {
"text": "${prompt}",
"clip": [
"4",
1
]
},
"class_type": "CLIPTextEncode",
"_meta": {
"title": "CLIP Text Encode (Prompt)"
}
},
"7": {
"inputs": {
"text": "text, watermark",
"clip": [
"4",
1
]
},
"class_type": "CLIPTextEncode",
"_meta": {
"title": "CLIP Text Encode (Prompt)"
}
},
"8": {
"inputs": {
"samples": [
"3",
0
],
"vae": [
"4",
2
]
},
"class_type": "VAEDecode",
"_meta": {
"title": "VAE Decode"
}
},
"9": {
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"8",
0
]
},
"class_type": "SaveImage",
"_meta": {
"title": "Save Image"
}
}
}`);
}
async function get_image(prompt_id) {
const res = await fetch(`http://localhost:8188/history`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const data = await res.json();
return data[prompt_id];
}
const prompt = createPromptString(
"Blue cafe with a cozy atmosphere, a place to relax and enjoy a cup of coffee.",
23253,
);
const data = await generate(prompt);
let image = await get_image(data.prompt_id);
while (image?.status.completed !== true) {
image = await get_image(data.prompt_id);
console.log("Image not ready, waiting...");
await new Promise((resolve) => setTimeout(resolve, 3000));
}
const lastImage = image.outputs["9"].images[0];
const filepath = `~/Documents/StabilityMatrix/Packages/ComfyUI/${lastImage.type}/${lastImage.filename}`;
console.log(filepath);
console.log(
`http://localhost:8188/view?filename=${lastImage.filename}&type=${lastImage.type}&subfolder=${lastImage.subfolder}`,
);