Skip to content

Commit 9b85c08

Browse files
committed
webui-plugin: lora/hypernetworks/embeddings support
1 parent 02dda52 commit 9b85c08

File tree

2 files changed

+126
-3
lines changed

2 files changed

+126
-3
lines changed

packages/stablestudio-plugin-webui/src/Utilities.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { StableDiffusionInput } from "@stability/stablestudio-plugin";
22

3+
const LORA_PATTERN = "<lora:#LORA#:1>";
4+
const HYPERNETWORK_PATTERN = "<hypernet:#HYPERNETWORK#:1>";
5+
36
export function base64ToBlob(base64: string, contentType = ""): Promise<Blob> {
47
return fetch(`data:${contentType};base64,${base64}`).then((res) =>
58
res.blob()
@@ -28,6 +31,40 @@ export async function fetchOptions(baseUrl: string | undefined) {
2831
return await optionsResponse.json();
2932
}
3033

34+
export async function fetchEmbeddings(baseUrl: string | undefined) {
35+
const embeddingsResponse = await fetch(`${baseUrl}/sdapi/v1/embeddings`, {
36+
method: "GET",
37+
headers: {
38+
"Content-Type": "application/json",
39+
},
40+
});
41+
42+
return await embeddingsResponse.json();
43+
}
44+
45+
export async function fetchHypernetworks(baseUrl: string | undefined) {
46+
const hypernetworksResponse = await fetch(`${baseUrl}/sdapi/v1/hypernetworks`, {
47+
method: "GET",
48+
headers: {
49+
"Content-Type": "application/json",
50+
},
51+
});
52+
53+
return await hypernetworksResponse.json();
54+
}
55+
56+
export async function fetchExtraModels(baseUrl: string | undefined, modelType: string) {
57+
const extraModelsResponse = await fetch(`${baseUrl}/StableStudio/get-extra-models`, {
58+
method: "POST",
59+
headers: {
60+
"Content-Type": "application/json",
61+
},
62+
body: JSON.stringify({ type: modelType }),
63+
});
64+
65+
return await extraModelsResponse.json();
66+
}
67+
3168
export async function setOptions(baseUrl: string | undefined, options: any) {
3269
const optionsResponse = await fetch(`${baseUrl}/sdapi/v1/options`, {
3370
method: "POST",
@@ -114,9 +151,10 @@ export async function constructPayload(
114151
count?: number | undefined;
115152
},
116153
isUpscale = false,
117-
upscaler: string | undefined
154+
upscaler: string | undefined,
155+
styleType: string | undefined
118156
) {
119-
const { sampler, prompts, initialImage, maskImage, width, height, steps } =
157+
const { sampler, prompts, initialImage, maskImage, width, height, steps, style } =
120158
options?.input ?? {};
121159

122160
// Construct payload
@@ -151,6 +189,18 @@ export async function constructPayload(
151189
prompts?.find((p) => (p.text && (p.weight ?? 0) < 0) ?? 0 < 0)?.text ??
152190
"";
153191

192+
if ((styleType != "none") && style && (style != "none") && (style != "enhance")) {
193+
let extraStyle = style;
194+
195+
if (styleType == "lora") {
196+
extraStyle = LORA_PATTERN.replace("#LORA#", style);
197+
} else if (styleType == "hypernetworks") {
198+
extraStyle = HYPERNETWORK_PATTERN.replace("#HYPERNETWORK#", style);
199+
}
200+
201+
data.prompt = data.prompt + " " + extraStyle;
202+
}
203+
154204
data.steps = steps ?? 20;
155205
data.batch_size = options?.count;
156206
data.save_images = true;

packages/stablestudio-plugin-webui/src/index.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
getImageInfo,
99
setOptions,
1010
testForHistoryPlugin,
11+
fetchHypernetworks,
12+
fetchEmbeddings,
13+
fetchExtraModels
1114
} from "./Utilities";
1215

1316
const manifest = {
@@ -64,6 +67,25 @@ const webuiUpscalers = [
6467
},
6568
];
6669

70+
const webuiStyleTypes = [
71+
{
72+
label: "None",
73+
value: "none",
74+
},
75+
{
76+
label: "LoRA",
77+
value: "lora",
78+
},
79+
{
80+
label: "Hypernetworks",
81+
value: "hypernetworks",
82+
},
83+
{
84+
label: "Textual Inversions",
85+
value: "embeddings",
86+
},
87+
];
88+
6789
const getNumber = (strValue: string | null, defaultValue: number) => {
6890
let retValue = defaultValue;
6991

@@ -80,6 +102,7 @@ export const createPlugin = StableStudio.createPlugin<{
80102
baseUrl: StableStudio.PluginSettingString;
81103
upscaler: StableStudio.PluginSettingString;
82104
historyImagesCount: StableStudio.PluginSettingNumber;
105+
styleType: StableStudio.PluginSettingString;
83106
};
84107
}>(({ set, get }) => {
85108
const webuiLoad = (
@@ -93,6 +116,7 @@ export const createPlugin = StableStudio.createPlugin<{
93116
| "getStableDiffusionDefaultCount"
94117
| "getStableDiffusionDefaultInput"
95118
| "getStableDiffusionExistingImages"
119+
| "getStableDiffusionStyles"
96120
> => {
97121
webuiHostUrl = webuiHostUrl ?? "http://127.0.0.1:7861";
98122

@@ -138,7 +162,8 @@ export const createPlugin = StableStudio.createPlugin<{
138162
const data = await constructPayload(
139163
options,
140164
isUpscale,
141-
get().settings.upscaler.value
165+
get().settings.upscaler.value,
166+
get().settings.styleType.value
142167
);
143168

144169
// Send payload to webui
@@ -273,6 +298,45 @@ export const createPlugin = StableStudio.createPlugin<{
273298
}));
274299
},
275300

301+
getStableDiffusionStyles: async () => {
302+
const styleType = localStorage.getItem("styleType") ?? "None";
303+
304+
const styles: any = [];
305+
306+
if (styleType === "lora") {
307+
const loras = await fetchExtraModels(webuiHostUrl, "Lora");
308+
309+
loras.forEach((lora: any) => {
310+
styles.push({
311+
id: lora["name"],
312+
name: lora["name"],
313+
});
314+
})
315+
}
316+
if (styleType === "hypernetworks") {
317+
const hypernetworks = await fetchHypernetworks(webuiHostUrl);
318+
319+
hypernetworks.forEach((hypernetwork: any) => {
320+
styles.push({
321+
id: hypernetwork["name"],
322+
name: hypernetwork["name"],
323+
});
324+
})
325+
}
326+
if (styleType === "embeddings") {
327+
const embeddings = await fetchEmbeddings(webuiHostUrl);
328+
329+
for (const key in embeddings.loaded) {
330+
styles.push({
331+
id: key,
332+
name: key,
333+
});
334+
}
335+
}
336+
337+
return styles;
338+
},
339+
276340
getStableDiffusionExistingImages: async () => {
277341
const existingImagesResponse = await fetch(
278342
`${webuiHostUrl}/StableStudio/get-generated-images`,
@@ -379,6 +443,13 @@ export const createPlugin = StableStudio.createPlugin<{
379443
variant: "slider",
380444
value: getNumber(localStorage.getItem("historyImagesCount"), 20),
381445
},
446+
447+
styleType: {
448+
type: "string",
449+
title: "Style Type",
450+
options: webuiStyleTypes,
451+
value: localStorage.getItem("styleType") ?? "None"
452+
}
382453
},
383454

384455
setSetting: (key, value) => {
@@ -396,6 +467,8 @@ export const createPlugin = StableStudio.createPlugin<{
396467
localStorage.setItem("upscaler1", value);
397468
} else if (key === "historyImagesCount" && typeof value === "number") {
398469
localStorage.setItem("historyImagesCount", value.toString());
470+
} else if (key === "styleType" && typeof value === "string") {
471+
localStorage.setItem("styleType", value);
399472
}
400473
},
401474

0 commit comments

Comments
 (0)