@enslo/sd-parsers-web 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/LICENSE.txt +21 -0
- package/README.md +221 -0
- package/dist/data/generators.d.ts +12 -0
- package/dist/data/generators.d.ts.map +1 -0
- package/dist/data/index.d.ts +10 -0
- package/dist/data/index.d.ts.map +1 -0
- package/dist/data/model.d.ts +25 -0
- package/dist/data/model.d.ts.map +1 -0
- package/dist/data/prompt.d.ts +20 -0
- package/dist/data/prompt.d.ts.map +1 -0
- package/dist/data/promptInfo.d.ts +56 -0
- package/dist/data/promptInfo.d.ts.map +1 -0
- package/dist/data/sampler.d.ts +24 -0
- package/dist/data/sampler.d.ts.map +1 -0
- package/dist/exceptions.d.ts +13 -0
- package/dist/exceptions.d.ts.map +1 -0
- package/dist/extractors/eagerness.d.ts +9 -0
- package/dist/extractors/eagerness.d.ts.map +1 -0
- package/dist/extractors/extractors.d.ts +30 -0
- package/dist/extractors/extractors.d.ts.map +1 -0
- package/dist/extractors/index.d.ts +14 -0
- package/dist/extractors/index.d.ts.map +1 -0
- package/dist/imageUtils.d.ts +16 -0
- package/dist/imageUtils.d.ts.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/parserManager.d.ts +26 -0
- package/dist/parserManager.d.ts.map +1 -0
- package/dist/parsers/automatic1111.d.ts +10 -0
- package/dist/parsers/automatic1111.d.ts.map +1 -0
- package/dist/parsers/comfyui.d.ts +10 -0
- package/dist/parsers/comfyui.d.ts.map +1 -0
- package/dist/parsers/dummy.d.ts +10 -0
- package/dist/parsers/dummy.d.ts.map +1 -0
- package/dist/parsers/fooocus.d.ts +10 -0
- package/dist/parsers/fooocus.d.ts.map +1 -0
- package/dist/parsers/index.d.ts +17 -0
- package/dist/parsers/index.d.ts.map +1 -0
- package/dist/parsers/invokeai.d.ts +13 -0
- package/dist/parsers/invokeai.d.ts.map +1 -0
- package/dist/parsers/novelai.d.ts +10 -0
- package/dist/parsers/novelai.d.ts.map +1 -0
- package/dist/parsers/parser.d.ts +38 -0
- package/dist/parsers/parser.d.ts.map +1 -0
- package/dist/sd-parsers-web.es.js +751 -0
- package/dist/sd-parsers-web.umd.js +4 -0
- package/package.json +62 -0
|
@@ -0,0 +1,751 @@
|
|
|
1
|
+
import L from "png-chunks-extract";
|
|
2
|
+
import { parse as j } from "exifr";
|
|
3
|
+
class h extends Error {
|
|
4
|
+
constructor(t) {
|
|
5
|
+
super(t), this.name = "ParserError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
class v extends h {
|
|
9
|
+
constructor(t) {
|
|
10
|
+
super(t), this.name = "MetadataError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
var g = /* @__PURE__ */ ((n) => (n[n.FAST = 1] = "FAST", n[n.DEFAULT = 2] = "DEFAULT", n[n.EAGER = 3] = "EAGER", n))(g || {}), y = /* @__PURE__ */ ((n) => (n.AUTOMATIC1111 = "AUTOMATIC1111", n.COMFYUI = "ComfyUI", n.FOOOCUS = "Fooocus", n.INVOKEAI = "InvokeAI", n.NOVELAI = "NovelAI", n.UNKNOWN = "unknown", n))(y || {});
|
|
14
|
+
function _(n) {
|
|
15
|
+
const { name: t, hash: e, modelId: r, metadata: s = {} } = n;
|
|
16
|
+
if (!t && !e)
|
|
17
|
+
throw new Error("Either name or hash need to be given.");
|
|
18
|
+
return {
|
|
19
|
+
name: t,
|
|
20
|
+
hash: e,
|
|
21
|
+
modelId: r,
|
|
22
|
+
metadata: s
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function he(n) {
|
|
26
|
+
return n.name && n.hash ? `${n.name} (${n.hash})` : n.name || n.hash || "";
|
|
27
|
+
}
|
|
28
|
+
function d(n, t = {}) {
|
|
29
|
+
const { promptId: e, metadata: r = {} } = t;
|
|
30
|
+
return {
|
|
31
|
+
value: n,
|
|
32
|
+
promptId: e,
|
|
33
|
+
metadata: r
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function M(n) {
|
|
37
|
+
return n.value;
|
|
38
|
+
}
|
|
39
|
+
function w(n, t = {}, e = {}) {
|
|
40
|
+
const { samplerId: r, model: s, prompts: a = [], negativePrompts: o = [] } = e;
|
|
41
|
+
return {
|
|
42
|
+
name: n,
|
|
43
|
+
parameters: t,
|
|
44
|
+
samplerId: r,
|
|
45
|
+
model: s,
|
|
46
|
+
prompts: a,
|
|
47
|
+
negativePrompts: o
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function A(n, t, e = {}, r = {}) {
|
|
51
|
+
return {
|
|
52
|
+
generator: n,
|
|
53
|
+
samplers: t,
|
|
54
|
+
metadata: e,
|
|
55
|
+
rawParameters: r
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function R(n) {
|
|
59
|
+
return n.metadata.full_prompt ? n.metadata.full_prompt : G(n).map(M).join(", ");
|
|
60
|
+
}
|
|
61
|
+
function F(n) {
|
|
62
|
+
return n.metadata.full_negative_prompt ? n.metadata.full_negative_prompt : z(n).map(M).join(", ");
|
|
63
|
+
}
|
|
64
|
+
function G(n) {
|
|
65
|
+
const t = /* @__PURE__ */ new Set(), e = [];
|
|
66
|
+
for (const r of n.samplers)
|
|
67
|
+
for (const s of r.prompts) {
|
|
68
|
+
const a = `${s.promptId || ""}:${s.value}`;
|
|
69
|
+
t.has(a) || (t.add(a), e.push(s));
|
|
70
|
+
}
|
|
71
|
+
return e;
|
|
72
|
+
}
|
|
73
|
+
function z(n) {
|
|
74
|
+
const t = /* @__PURE__ */ new Set(), e = [];
|
|
75
|
+
for (const r of n.samplers)
|
|
76
|
+
for (const s of r.negativePrompts) {
|
|
77
|
+
const a = `${s.promptId || ""}:${s.value}`;
|
|
78
|
+
t.has(a) || (t.add(a), e.push(s));
|
|
79
|
+
}
|
|
80
|
+
return e;
|
|
81
|
+
}
|
|
82
|
+
function ge(n) {
|
|
83
|
+
const t = /* @__PURE__ */ new Set(), e = [];
|
|
84
|
+
for (const r of n.samplers)
|
|
85
|
+
if (r.model) {
|
|
86
|
+
const s = `${r.model.modelId || ""}:${r.model.name || ""}:${r.model.hash || ""}`;
|
|
87
|
+
t.has(s) || (t.add(s), e.push(r.model));
|
|
88
|
+
}
|
|
89
|
+
return e;
|
|
90
|
+
}
|
|
91
|
+
function J(n) {
|
|
92
|
+
return {
|
|
93
|
+
full_prompt: R(n),
|
|
94
|
+
full_negative_prompt: F(n),
|
|
95
|
+
generator: n.generator,
|
|
96
|
+
samplers: n.samplers,
|
|
97
|
+
metadata: n.metadata,
|
|
98
|
+
rawParameters: n.rawParameters
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function ye(n) {
|
|
102
|
+
return JSON.stringify(J(n));
|
|
103
|
+
}
|
|
104
|
+
async function K(n, t) {
|
|
105
|
+
try {
|
|
106
|
+
const e = {};
|
|
107
|
+
return Object.keys(e).length > 0 ? e : null;
|
|
108
|
+
} catch (e) {
|
|
109
|
+
throw new v(`Error reading PNG metadata: ${e}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async function V(n, t) {
|
|
113
|
+
try {
|
|
114
|
+
const r = L(n).filter(
|
|
115
|
+
(a) => a.name === "tEXt" || a.name === "zTXt" || a.name === "iTXt"
|
|
116
|
+
);
|
|
117
|
+
if (r.length === 0)
|
|
118
|
+
return null;
|
|
119
|
+
const s = {};
|
|
120
|
+
for (const a of r)
|
|
121
|
+
try {
|
|
122
|
+
let o, i;
|
|
123
|
+
if (a.name === "tEXt") {
|
|
124
|
+
const p = a.data, c = p.indexOf(0);
|
|
125
|
+
if (c === -1) continue;
|
|
126
|
+
i = new TextDecoder("latin1").decode(p.subarray(0, c)), o = new TextDecoder("latin1").decode(p.subarray(c + 1));
|
|
127
|
+
} else {
|
|
128
|
+
if (a.name === "zTXt")
|
|
129
|
+
continue;
|
|
130
|
+
if (a.name === "iTXt")
|
|
131
|
+
continue;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
i && o && (s[i] = o, i.toLowerCase() === "parameters" && (s.parameters = o));
|
|
135
|
+
} catch {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
return Object.keys(s).length > 0 ? s : null;
|
|
139
|
+
} catch (e) {
|
|
140
|
+
throw new v(`Error reading PNG text chunks: ${e}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function B(n, t) {
|
|
144
|
+
try {
|
|
145
|
+
return null;
|
|
146
|
+
} catch (e) {
|
|
147
|
+
throw new v(`Error reading stenographic alpha: ${e}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async function I(n, t) {
|
|
151
|
+
try {
|
|
152
|
+
const e = await j(n, {
|
|
153
|
+
userComment: !0,
|
|
154
|
+
pick: ["userComment"]
|
|
155
|
+
}), r = (e == null ? void 0 : e.userComment) || (e == null ? void 0 : e.UserComment) || (e == null ? void 0 : e["0x9286"]);
|
|
156
|
+
if (r) {
|
|
157
|
+
if (r instanceof Uint8Array) {
|
|
158
|
+
const s = r.slice(0, 8), a = new TextDecoder("ascii").decode(s).replace(/\0/g, ""), o = r.slice(8);
|
|
159
|
+
let i;
|
|
160
|
+
return a === "UNICODE" ? i = new TextDecoder("utf-16be").decode(o) : a === "ASCII" ? i = new TextDecoder("ascii").decode(o) : i = new TextDecoder("utf-8").decode(o), t === y.AUTOMATIC1111 || t === y.FOOOCUS ? { parameters: i } : { userComment: i };
|
|
161
|
+
}
|
|
162
|
+
return typeof r == "string" ? { parameters: r } : null;
|
|
163
|
+
}
|
|
164
|
+
return null;
|
|
165
|
+
} catch (e) {
|
|
166
|
+
if (t === y.AUTOMATIC1111 || t === y.FOOOCUS)
|
|
167
|
+
return null;
|
|
168
|
+
throw new v(`Error reading JPEG UserComment: ${e}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const W = {
|
|
172
|
+
PNG: {
|
|
173
|
+
[g.FAST]: [K],
|
|
174
|
+
[g.DEFAULT]: [V],
|
|
175
|
+
[g.EAGER]: [B]
|
|
176
|
+
},
|
|
177
|
+
JPEG: {
|
|
178
|
+
[g.FAST]: [I],
|
|
179
|
+
[g.DEFAULT]: [],
|
|
180
|
+
[g.EAGER]: []
|
|
181
|
+
},
|
|
182
|
+
WEBP: {
|
|
183
|
+
[g.FAST]: [I],
|
|
184
|
+
[g.DEFAULT]: [],
|
|
185
|
+
[g.EAGER]: []
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
class P {
|
|
189
|
+
constructor(t = !0, e = !1) {
|
|
190
|
+
this.generator = y.UNKNOWN, this.doNormalizationPass = t, this.debug = e;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Apply replacement rules and basic formatting to the keys of select image metadata entries.
|
|
194
|
+
* Returns a dictionary with normalized parameter values.
|
|
195
|
+
*/
|
|
196
|
+
normalizeParameters(t, e, r = !0, s = !0) {
|
|
197
|
+
if (!this.doNormalizationPass)
|
|
198
|
+
return Array.isArray(t) ? Object.fromEntries(t) : t;
|
|
199
|
+
const a = Array.isArray(t) ? Object.fromEntries(t) : { ...t }, o = {};
|
|
200
|
+
if (e)
|
|
201
|
+
for (const i of e) {
|
|
202
|
+
const [p, c] = i;
|
|
203
|
+
if (typeof c == "string")
|
|
204
|
+
p in a && (o[c] = a[p], delete a[p]);
|
|
205
|
+
else {
|
|
206
|
+
const [m, l] = c;
|
|
207
|
+
if (p in a) {
|
|
208
|
+
const f = {};
|
|
209
|
+
for (const E of m)
|
|
210
|
+
E in a && (f[E] = a[E]);
|
|
211
|
+
let u = l;
|
|
212
|
+
for (const [E, S] of Object.entries(f))
|
|
213
|
+
u = u.replace(new RegExp(`\\{${E}\\}`, "g"), String(S));
|
|
214
|
+
o[p] = u;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
for (let [i, p] of Object.entries(a))
|
|
219
|
+
r && (i = i.toLowerCase()), s && (i = i.replace(/\s+/g, "_")), o[i] = p;
|
|
220
|
+
return o;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function k(n, t) {
|
|
224
|
+
const e = [];
|
|
225
|
+
for (const r of n)
|
|
226
|
+
r in t && (e.push([r, t[r]]), delete t[r]);
|
|
227
|
+
return e;
|
|
228
|
+
}
|
|
229
|
+
const C = ["Sampler", "CFG scale", "Seed", "Steps", "ENSD", "Schedule type", "Denoising strength", "Clip skip"], X = [
|
|
230
|
+
["Schedule type", "scheduler"],
|
|
231
|
+
["CFG scale", "cfg_scale"],
|
|
232
|
+
["Seed", "seed"],
|
|
233
|
+
["Steps", "steps"],
|
|
234
|
+
["Denoising strength", "denoising_strength"],
|
|
235
|
+
["Clip skip", "clip_skip"]
|
|
236
|
+
];
|
|
237
|
+
class Y extends P {
|
|
238
|
+
constructor() {
|
|
239
|
+
super(...arguments), this.generator = y.AUTOMATIC1111;
|
|
240
|
+
}
|
|
241
|
+
async parse(t) {
|
|
242
|
+
var r, s, a, o;
|
|
243
|
+
let e;
|
|
244
|
+
try {
|
|
245
|
+
if (!t.parameters || typeof t.parameters != "string")
|
|
246
|
+
throw new Error("parameters field is missing or not a string");
|
|
247
|
+
e = t.parameters.split(`
|
|
248
|
+
`);
|
|
249
|
+
} catch (i) {
|
|
250
|
+
throw new h(`Error reading parameter string: ${i}`);
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
const { infoIndex: i, samplerInfo: p, metadata: c } = H(e), m = e.slice(0, i).join(`
|
|
254
|
+
`).split("Negative prompt:"), l = ((r = m[0]) == null ? void 0 : r.trim()) || "", f = ((s = m[1]) == null ? void 0 : s.trim()) || "", u = Object.fromEntries(k(C, p)), E = u.Sampler || "unknown";
|
|
255
|
+
delete u.Sampler;
|
|
256
|
+
const S = this.normalizeParameters(u, X), T = c.Model, N = c["Model hash"];
|
|
257
|
+
delete c.Model, delete c["Model hash"];
|
|
258
|
+
const x = T || N ? _({ name: T, hash: N }) : void 0, U = l ? [d(l)] : [], $ = f ? [d(f)] : [], D = w(E, S, {
|
|
259
|
+
model: x,
|
|
260
|
+
prompts: U,
|
|
261
|
+
negativePrompts: $
|
|
262
|
+
});
|
|
263
|
+
return A(this.generator, [D], c, t);
|
|
264
|
+
} catch {
|
|
265
|
+
const c = e.join(`
|
|
266
|
+
`).split("Negative prompt:"), m = ((a = c[0]) == null ? void 0 : a.trim()) || "", l = ((o = c[1]) == null ? void 0 : o.trim()) || "", f = m ? [d(m)] : [], u = l ? [d(l)] : [], E = w("unknown", {}, {
|
|
267
|
+
prompts: f,
|
|
268
|
+
negativePrompts: u
|
|
269
|
+
});
|
|
270
|
+
return A(this.generator, [E], {}, t);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function H(n) {
|
|
275
|
+
for (let t = n.length - 1; t >= 0; t--) {
|
|
276
|
+
const e = n[t], r = q(e), s = Object.fromEntries(k(C, { ...r }));
|
|
277
|
+
if (Object.keys(s).length >= 3)
|
|
278
|
+
return { infoIndex: t, samplerInfo: s, metadata: r };
|
|
279
|
+
}
|
|
280
|
+
throw new h("No sampler information found");
|
|
281
|
+
}
|
|
282
|
+
function q(n) {
|
|
283
|
+
const t = {}, e = n.match(/(?:,\s*)?Hashes:\s*(\{[^}]*\})\s*/);
|
|
284
|
+
if (e)
|
|
285
|
+
try {
|
|
286
|
+
t.Hashes = JSON.parse(e[1]), n = n.substring(0, e.index) + n.substring(e.index + e[0].length);
|
|
287
|
+
} catch {
|
|
288
|
+
}
|
|
289
|
+
for (const r of n.split(","))
|
|
290
|
+
try {
|
|
291
|
+
const s = r.indexOf(":");
|
|
292
|
+
if (s > 0) {
|
|
293
|
+
const a = r.substring(0, s).trim(), o = r.substring(s + 1).trim();
|
|
294
|
+
t[a] = o;
|
|
295
|
+
}
|
|
296
|
+
} catch {
|
|
297
|
+
}
|
|
298
|
+
return t;
|
|
299
|
+
}
|
|
300
|
+
const Z = ["guidance_scale", "cfg_scale", "scheduler", "seed", "sharpness", "steps"], Q = [["guidance_scale", "cfg_scale"]];
|
|
301
|
+
class ee extends P {
|
|
302
|
+
constructor() {
|
|
303
|
+
super(...arguments), this.generator = y.FOOOCUS;
|
|
304
|
+
}
|
|
305
|
+
async parse(t) {
|
|
306
|
+
let e;
|
|
307
|
+
try {
|
|
308
|
+
if (!t.hasOwnProperty("parameters"))
|
|
309
|
+
throw new Error("parameters field is missing");
|
|
310
|
+
if (typeof t.parameters == "string")
|
|
311
|
+
e = JSON.parse(t.parameters);
|
|
312
|
+
else throw typeof t.parameters == "object" && t.parameters !== null ? new Error("parameters field is missing") : new Error("parameters field is missing");
|
|
313
|
+
} catch (r) {
|
|
314
|
+
throw new h(`Error decoding parameter data: ${r}`);
|
|
315
|
+
}
|
|
316
|
+
try {
|
|
317
|
+
const r = e.base_model || e.base_model_hash ? _({
|
|
318
|
+
name: e.base_model,
|
|
319
|
+
hash: e.base_model_hash
|
|
320
|
+
}) : void 0;
|
|
321
|
+
delete e.base_model, delete e.base_model_hash;
|
|
322
|
+
const s = Object.fromEntries(k(Z, e)), a = this.normalizeParameters(s, Q), o = e.sampler || "unknown";
|
|
323
|
+
delete e.sampler;
|
|
324
|
+
const i = e.prompt;
|
|
325
|
+
delete e.prompt;
|
|
326
|
+
const p = e.negative_prompt;
|
|
327
|
+
delete e.negative_prompt;
|
|
328
|
+
const c = i ? [d(i)] : [], m = p ? [d(p)] : [], l = w(o, a, {
|
|
329
|
+
model: r,
|
|
330
|
+
prompts: c,
|
|
331
|
+
negativePrompts: m
|
|
332
|
+
});
|
|
333
|
+
return A(this.generator, [l], e, t);
|
|
334
|
+
} catch (r) {
|
|
335
|
+
throw new h(`Error reading parameter value: ${r}`);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
const te = ["WanVideoSampler"], re = /* @__PURE__ */ new Set(["sampler_name", "steps", "cfg"]), se = [], ne = ["text", "positive"], oe = ["text", "negative"], b = ["ConditioningCombine"];
|
|
340
|
+
class ae extends P {
|
|
341
|
+
constructor() {
|
|
342
|
+
super(...arguments), this.generator = y.COMFYUI;
|
|
343
|
+
}
|
|
344
|
+
async parse(t) {
|
|
345
|
+
let e, r;
|
|
346
|
+
try {
|
|
347
|
+
e = t.prompt, typeof e == "string" && (e = JSON.parse(e)), r = t.workflow, typeof r == "string" && (r = JSON.parse(r));
|
|
348
|
+
} catch (o) {
|
|
349
|
+
throw new h(`Error reading parameters: ${o}`);
|
|
350
|
+
}
|
|
351
|
+
const { samplers: s, metadata: a } = O.extract(this, e, r);
|
|
352
|
+
return A(this.generator, s, a, t);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
class O {
|
|
356
|
+
constructor(t, e, r = {}) {
|
|
357
|
+
this.parser = t, this.processedNodes = /* @__PURE__ */ new Set();
|
|
358
|
+
try {
|
|
359
|
+
this.prompt = {};
|
|
360
|
+
for (const [s, a] of Object.entries(e))
|
|
361
|
+
this.prompt[String(s)] = a;
|
|
362
|
+
} catch (s) {
|
|
363
|
+
throw new h(`Prompt has unexpected format: ${s}`);
|
|
364
|
+
}
|
|
365
|
+
this.links = {};
|
|
366
|
+
try {
|
|
367
|
+
if (r && r.links && Array.isArray(r.links)) {
|
|
368
|
+
for (const s of r.links)
|
|
369
|
+
if (Array.isArray(s) && s.length >= 6) {
|
|
370
|
+
const [, a, , o, , i] = s, p = String(o), c = String(a);
|
|
371
|
+
this.links[p] || (this.links[p] = {}), this.links[p][c] || (this.links[p][c] = /* @__PURE__ */ new Set()), this.links[p][c].add(i);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
} catch (s) {
|
|
375
|
+
throw new h(`Workflow has unexpected format: ${s}`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
static extract(t, e, r = {}) {
|
|
379
|
+
const s = new O(t, e, r), a = [], o = {};
|
|
380
|
+
for (const [i, p] of Object.entries(s.prompt)) {
|
|
381
|
+
const c = s.tryGetSampler(i, p);
|
|
382
|
+
c && a.push(c);
|
|
383
|
+
}
|
|
384
|
+
for (const [i, p] of Object.entries(s.prompt))
|
|
385
|
+
if (!s.processedNodes.has(i))
|
|
386
|
+
try {
|
|
387
|
+
if (p.inputs) {
|
|
388
|
+
const c = s.getInputValues(p.inputs, i);
|
|
389
|
+
if (c && Object.keys(c).length > 0) {
|
|
390
|
+
const m = p.class_type;
|
|
391
|
+
o[m] || (o[m] = []), o[m].push(c);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
} catch {
|
|
395
|
+
}
|
|
396
|
+
return { samplers: a, metadata: o };
|
|
397
|
+
}
|
|
398
|
+
tryStepInto(t, e) {
|
|
399
|
+
var r;
|
|
400
|
+
for (const s of e)
|
|
401
|
+
try {
|
|
402
|
+
const a = (r = t[s]) == null ? void 0 : r[0];
|
|
403
|
+
if (a && this.prompt[a]) {
|
|
404
|
+
const o = this.prompt[a];
|
|
405
|
+
if (o.inputs)
|
|
406
|
+
return { ...o.inputs };
|
|
407
|
+
}
|
|
408
|
+
} catch {
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
return t;
|
|
412
|
+
}
|
|
413
|
+
tryGetSampler(t, e) {
|
|
414
|
+
try {
|
|
415
|
+
const c = { ...e.inputs }, m = e.class_type;
|
|
416
|
+
if (!te.includes(m) && !this.hasSamplerParams(c))
|
|
417
|
+
return null;
|
|
418
|
+
} catch {
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
this.parser.debug && console.log(`Found sampler #${t}`), this.processedNodes.add(t);
|
|
422
|
+
const r = { ...e.inputs }, s = r.sampler_name || r.scheduler || "unknown";
|
|
423
|
+
delete r.sampler_name, delete r.scheduler;
|
|
424
|
+
const a = this.parser.normalizeParameters(
|
|
425
|
+
this.getInputValues(r),
|
|
426
|
+
se
|
|
427
|
+
), o = this.getModel(t), { prompts: i, negativePrompts: p } = this.getPrompts(t);
|
|
428
|
+
return w(s, a, {
|
|
429
|
+
samplerId: t,
|
|
430
|
+
model: o,
|
|
431
|
+
prompts: i,
|
|
432
|
+
negativePrompts: p
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
hasSamplerParams(t) {
|
|
436
|
+
const e = new Set(Object.keys(t));
|
|
437
|
+
for (const r of re)
|
|
438
|
+
if (e.has(r))
|
|
439
|
+
return !0;
|
|
440
|
+
return !1;
|
|
441
|
+
}
|
|
442
|
+
getModel(t) {
|
|
443
|
+
try {
|
|
444
|
+
const e = this.getTrace([t], ["MODEL"]), r = this.getTraceMetadata(e);
|
|
445
|
+
for (const [s, a] of Object.entries(r))
|
|
446
|
+
if ((s.includes("Checkpoint") || s.includes("Model")) && Array.isArray(a) && a.length > 0) {
|
|
447
|
+
const o = a[0];
|
|
448
|
+
if (typeof o == "object" && o.ckpt_name)
|
|
449
|
+
return _({ name: o.ckpt_name });
|
|
450
|
+
}
|
|
451
|
+
} catch {
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
getPrompts(t) {
|
|
455
|
+
const e = [], r = [];
|
|
456
|
+
try {
|
|
457
|
+
const s = this.getTrace([t], ["CONDITIONING"]), a = this.getTraceMetadata(s);
|
|
458
|
+
for (const [o, i] of Object.entries(a))
|
|
459
|
+
if (!b.includes(o) && Array.isArray(i)) {
|
|
460
|
+
for (const p of i)
|
|
461
|
+
if (typeof p == "object") {
|
|
462
|
+
for (const c of ne)
|
|
463
|
+
p[c] && typeof p[c] == "string" && e.push(d(p[c]));
|
|
464
|
+
for (const c of oe)
|
|
465
|
+
p[c] && typeof p[c] == "string" && r.push(d(p[c]));
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
} catch {
|
|
469
|
+
}
|
|
470
|
+
return { prompts: e, negativePrompts: r };
|
|
471
|
+
}
|
|
472
|
+
getTrace(t, e) {
|
|
473
|
+
const r = /* @__PURE__ */ new Set(), s = [], a = (o) => {
|
|
474
|
+
if (r.has(o))
|
|
475
|
+
return;
|
|
476
|
+
r.add(o), s.push(o);
|
|
477
|
+
const i = this.links[o];
|
|
478
|
+
if (i)
|
|
479
|
+
for (const [p, c] of Object.entries(i))
|
|
480
|
+
for (const m of e)
|
|
481
|
+
c.has(m) && a(p);
|
|
482
|
+
};
|
|
483
|
+
for (const o of t)
|
|
484
|
+
a(o);
|
|
485
|
+
return s;
|
|
486
|
+
}
|
|
487
|
+
getTraceMetadata(t) {
|
|
488
|
+
const e = {};
|
|
489
|
+
for (const r of t)
|
|
490
|
+
try {
|
|
491
|
+
const s = this.prompt[r];
|
|
492
|
+
if (!s) continue;
|
|
493
|
+
const a = s.class_type;
|
|
494
|
+
if (b.includes(a))
|
|
495
|
+
continue;
|
|
496
|
+
const o = this.getInputValues(s.inputs, r);
|
|
497
|
+
if (!o || Object.keys(o).length === 0)
|
|
498
|
+
continue;
|
|
499
|
+
e[a] ? Array.isArray(e[a]) ? e[a].push(o) : e[a] = [e[a], o] : e[a] = o;
|
|
500
|
+
} catch {
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
return e;
|
|
504
|
+
}
|
|
505
|
+
getInputValues(t, e) {
|
|
506
|
+
try {
|
|
507
|
+
const r = {};
|
|
508
|
+
for (const [s, a] of Object.entries(t))
|
|
509
|
+
Array.isArray(a) || (r[s] = a);
|
|
510
|
+
if (Object.keys(r).length > 0)
|
|
511
|
+
return e ? { id: e, ...r } : r;
|
|
512
|
+
} catch {
|
|
513
|
+
}
|
|
514
|
+
return {};
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
class ie extends P {
|
|
518
|
+
constructor() {
|
|
519
|
+
super(...arguments), this.generator = y.INVOKEAI;
|
|
520
|
+
}
|
|
521
|
+
async parse(t) {
|
|
522
|
+
if (t["sd-metadata"])
|
|
523
|
+
return this.parseSdMetadata(t["sd-metadata"]);
|
|
524
|
+
if (t.Dream)
|
|
525
|
+
return this.parseDream(t.Dream);
|
|
526
|
+
if (t.invokeai_metadata || t.invokeai_graph)
|
|
527
|
+
return this.parseInvokeAiMeta(t);
|
|
528
|
+
throw new h("No supported InvokeAI metadata format found");
|
|
529
|
+
}
|
|
530
|
+
parseSdMetadata(t) {
|
|
531
|
+
var e;
|
|
532
|
+
try {
|
|
533
|
+
const r = typeof t == "string" ? JSON.parse(t) : t, s = r.sampler_name || ((e = r.image) == null ? void 0 : e.sampler) || r.sampler || "unknown", a = {}, o = r.image || r;
|
|
534
|
+
o.cfg_scale !== void 0 && (a.cfg_scale = o.cfg_scale), o.steps !== void 0 && (a.steps = o.steps), o.seed !== void 0 && (a.seed = o.seed), o.width !== void 0 && (a.width = o.width), o.height !== void 0 && (a.height = o.height);
|
|
535
|
+
const i = r.prompt || o.prompt ? [d(r.prompt || o.prompt)] : [], p = r.negative_prompt || o.negative_prompt ? [d(r.negative_prompt || o.negative_prompt)] : [], c = w(s, a, {
|
|
536
|
+
prompts: i,
|
|
537
|
+
negativePrompts: p
|
|
538
|
+
});
|
|
539
|
+
return A(this.generator, [c], r, { "sd-metadata": t });
|
|
540
|
+
} catch (r) {
|
|
541
|
+
throw new h(`Error parsing sd-metadata: ${r}`);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
parseDream(t) {
|
|
545
|
+
try {
|
|
546
|
+
let e = t.match(/^"(.*?)"(.*)$/), r, s;
|
|
547
|
+
if (e)
|
|
548
|
+
[, r, s] = e;
|
|
549
|
+
else {
|
|
550
|
+
const m = t.indexOf(" -");
|
|
551
|
+
m !== -1 ? (r = t.substring(0, m), s = t.substring(m)) : (r = t, s = "");
|
|
552
|
+
}
|
|
553
|
+
const a = {}, o = {};
|
|
554
|
+
if (s) {
|
|
555
|
+
const m = s.matchAll(/-(\w+)\s+([^\s-]+)/g);
|
|
556
|
+
for (const l of m) {
|
|
557
|
+
const [, f, u] = l;
|
|
558
|
+
switch (f) {
|
|
559
|
+
case "A":
|
|
560
|
+
o.sampler = u;
|
|
561
|
+
break;
|
|
562
|
+
case "C":
|
|
563
|
+
o.cfg_scale = u;
|
|
564
|
+
break;
|
|
565
|
+
case "H":
|
|
566
|
+
o.height = u;
|
|
567
|
+
break;
|
|
568
|
+
case "s":
|
|
569
|
+
o.steps = u;
|
|
570
|
+
break;
|
|
571
|
+
case "S":
|
|
572
|
+
o.seed = u;
|
|
573
|
+
break;
|
|
574
|
+
case "W":
|
|
575
|
+
o.width = u;
|
|
576
|
+
break;
|
|
577
|
+
default:
|
|
578
|
+
a[f] = u;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
const i = o.sampler || "unknown";
|
|
583
|
+
delete o.sampler;
|
|
584
|
+
const p = r ? [d(r)] : [], c = w(i, o, {
|
|
585
|
+
prompts: p
|
|
586
|
+
});
|
|
587
|
+
return A(this.generator, [c], a, { Dream: t });
|
|
588
|
+
} catch (e) {
|
|
589
|
+
throw new h(`Error parsing Dream format: ${e}`);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
parseInvokeAiMeta(t) {
|
|
593
|
+
try {
|
|
594
|
+
let e = {};
|
|
595
|
+
t.invokeai_metadata && (e = typeof t.invokeai_metadata == "string" ? JSON.parse(t.invokeai_metadata) : t.invokeai_metadata);
|
|
596
|
+
const r = e.sampler_name || e.scheduler || "unknown", s = {};
|
|
597
|
+
e.cfg_scale !== void 0 && (s.cfg_scale = e.cfg_scale), e.steps !== void 0 && (s.steps = e.steps), e.seed !== void 0 && (s.seed = e.seed), e.width !== void 0 && (s.width = e.width), e.height !== void 0 && (s.height = e.height);
|
|
598
|
+
const a = e.positive_prompt || e.prompt ? [d(e.positive_prompt || e.prompt)] : [], o = e.negative_prompt ? [d(e.negative_prompt)] : [], i = w(r, s, {
|
|
599
|
+
prompts: a,
|
|
600
|
+
negativePrompts: o
|
|
601
|
+
});
|
|
602
|
+
return A(this.generator, [i], e, t);
|
|
603
|
+
} catch (e) {
|
|
604
|
+
throw new h(`Error parsing InvokeAI metadata: ${e}`);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
const ce = ["seed", "strength", "noise", "scale", "steps"], pe = [];
|
|
609
|
+
class me extends P {
|
|
610
|
+
constructor() {
|
|
611
|
+
super(...arguments), this.generator = y.NOVELAI;
|
|
612
|
+
}
|
|
613
|
+
async parse(t) {
|
|
614
|
+
let e, r, s;
|
|
615
|
+
try {
|
|
616
|
+
t.parameters ? (typeof t.parameters == "string" ? e = JSON.parse(t.parameters) : e = t.parameters, r = e.prompt || "", s = e.source || "") : (e = JSON.parse(t.Comment || "{}"), r = t.Description || "", s = t.Source || "");
|
|
617
|
+
} catch (o) {
|
|
618
|
+
throw new h(`Error reading parameter values: ${o}`);
|
|
619
|
+
}
|
|
620
|
+
let a;
|
|
621
|
+
try {
|
|
622
|
+
if (a = e.sampler, !a)
|
|
623
|
+
throw new Error("Sampler name not found");
|
|
624
|
+
delete e.sampler;
|
|
625
|
+
const o = Object.fromEntries(k(ce, e)), i = this.normalizeParameters(o, pe), p = r != null && r.trim() ? [d(r.trim())] : [], c = e.uc;
|
|
626
|
+
delete e.uc;
|
|
627
|
+
const m = c ? [d(c)] : [];
|
|
628
|
+
let l;
|
|
629
|
+
const f = s == null ? void 0 : s.match(/^(.*?)\s+([A-Z0-9]+)$/);
|
|
630
|
+
if (f) {
|
|
631
|
+
const [, E, S] = f;
|
|
632
|
+
l = _({ name: E, hash: S });
|
|
633
|
+
}
|
|
634
|
+
const u = w(a, i, {
|
|
635
|
+
model: l,
|
|
636
|
+
prompts: p,
|
|
637
|
+
negativePrompts: m
|
|
638
|
+
});
|
|
639
|
+
return A(this.generator, [u], e, t);
|
|
640
|
+
} catch (o) {
|
|
641
|
+
throw new h(`No sampler found: ${o}`);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
class Ee extends P {
|
|
646
|
+
constructor() {
|
|
647
|
+
super(...arguments), this.generator = y.UNKNOWN;
|
|
648
|
+
}
|
|
649
|
+
async parse(t) {
|
|
650
|
+
const e = w("dummy_sampler", {});
|
|
651
|
+
return A(this.generator, [e], { "some other": "metadata" }, t);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
const le = [
|
|
655
|
+
ee,
|
|
656
|
+
Y,
|
|
657
|
+
ae,
|
|
658
|
+
ie,
|
|
659
|
+
me
|
|
660
|
+
];
|
|
661
|
+
function ue(n) {
|
|
662
|
+
return n[0] === 137 && n[1] === 80 && n[2] === 78 && n[3] === 71 ? "PNG" : n[0] === 255 && n[1] === 216 && n[2] === 255 ? "JPEG" : n[0] === 82 && n[1] === 73 && n[2] === 70 && n[3] === 70 && n[8] === 87 && n[9] === 69 && n[10] === 66 && n[11] === 80 ? "WEBP" : null;
|
|
663
|
+
}
|
|
664
|
+
class we {
|
|
665
|
+
constructor(t = {}) {
|
|
666
|
+
const {
|
|
667
|
+
debug: e = !1,
|
|
668
|
+
eagerness: r = g.DEFAULT,
|
|
669
|
+
managedParsers: s,
|
|
670
|
+
normalizeParameters: a = !0
|
|
671
|
+
} = t;
|
|
672
|
+
this.eagerness = r, this.debug = e;
|
|
673
|
+
const o = s || le;
|
|
674
|
+
this.managedParsers = o.map(
|
|
675
|
+
(i) => new i(a, e)
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Try to extract image generation parameters from the given image.
|
|
680
|
+
*/
|
|
681
|
+
async parse(t, e) {
|
|
682
|
+
const r = e || this.eagerness;
|
|
683
|
+
let s;
|
|
684
|
+
if (t instanceof Blob) {
|
|
685
|
+
const i = await t.arrayBuffer();
|
|
686
|
+
s = new Uint8Array(i);
|
|
687
|
+
} else t instanceof ArrayBuffer ? s = new Uint8Array(t) : s = t;
|
|
688
|
+
const a = ue(s);
|
|
689
|
+
if (!a)
|
|
690
|
+
return this.debug && console.log("Unknown image format"), null;
|
|
691
|
+
const o = W[a];
|
|
692
|
+
if (!o)
|
|
693
|
+
return this.debug && console.log(`Unsupported image format: ${a}`), null;
|
|
694
|
+
for (const i of [g.FAST, g.DEFAULT, g.EAGER]) {
|
|
695
|
+
if (i > r)
|
|
696
|
+
break;
|
|
697
|
+
const p = o[i];
|
|
698
|
+
if (p)
|
|
699
|
+
for (const c of p)
|
|
700
|
+
for (const m of this.managedParsers)
|
|
701
|
+
try {
|
|
702
|
+
const l = await c(s, m.generator);
|
|
703
|
+
if (!l)
|
|
704
|
+
continue;
|
|
705
|
+
try {
|
|
706
|
+
return await m.parse(l);
|
|
707
|
+
} catch (f) {
|
|
708
|
+
if (f instanceof h)
|
|
709
|
+
this.debug && console.error(`Error in parser[${m.constructor.name}]: ${f.message}`);
|
|
710
|
+
else
|
|
711
|
+
throw f;
|
|
712
|
+
}
|
|
713
|
+
} catch (l) {
|
|
714
|
+
if (l instanceof v) {
|
|
715
|
+
this.debug && console.error("Error reading metadata:", l.message);
|
|
716
|
+
break;
|
|
717
|
+
} else
|
|
718
|
+
throw l;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return null;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
export {
|
|
725
|
+
Y as AUTOMATIC1111Parser,
|
|
726
|
+
ae as ComfyUIParser,
|
|
727
|
+
Ee as DummyParser,
|
|
728
|
+
g as Eagerness,
|
|
729
|
+
ee as FooocusParser,
|
|
730
|
+
y as Generators,
|
|
731
|
+
ie as InvokeAIParser,
|
|
732
|
+
le as MANAGED_PARSERS,
|
|
733
|
+
v as MetadataError,
|
|
734
|
+
me as NovelAIParser,
|
|
735
|
+
P as Parser,
|
|
736
|
+
h as ParserError,
|
|
737
|
+
we as ParserManager,
|
|
738
|
+
_ as createModel,
|
|
739
|
+
d as createPrompt,
|
|
740
|
+
A as createPromptInfo,
|
|
741
|
+
w as createSampler,
|
|
742
|
+
F as getFullNegativePrompt,
|
|
743
|
+
R as getFullPrompt,
|
|
744
|
+
ge as getModels,
|
|
745
|
+
z as getNegativePrompts,
|
|
746
|
+
G as getPrompts,
|
|
747
|
+
he as modelToString,
|
|
748
|
+
J as promptInfoToDict,
|
|
749
|
+
ye as promptInfoToJSON,
|
|
750
|
+
M as promptToString
|
|
751
|
+
};
|