|
| 1 | +import { StableDiffusionInput } from "@stability/stablestudio-plugin"; |
| 2 | + |
| 3 | +export function base64ToBlob(base64: string, contentType = ""): Promise<Blob> { |
| 4 | + return fetch(`data:${contentType};base64,${base64}`).then((res) => |
| 5 | + res.blob() |
| 6 | + ); |
| 7 | +} |
| 8 | + |
| 9 | +export function blobToBase64(blob: Blob): Promise<string> { |
| 10 | + return new Promise((resolve, reject) => { |
| 11 | + const reader = new FileReader(); |
| 12 | + |
| 13 | + reader.onloadend = () => resolve(reader.result as string); |
| 14 | + reader.onerror = reject; |
| 15 | + |
| 16 | + reader.readAsDataURL(blob); |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +export async function fetchOptions(baseUrl: string | undefined) { |
| 21 | + const optionsResponse = await fetch(`${baseUrl}/sdapi/v1/options`, { |
| 22 | + method: "GET", |
| 23 | + headers: { |
| 24 | + "Content-Type": "application/json", |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + return await optionsResponse.json(); |
| 29 | +} |
| 30 | + |
| 31 | +export async function setOptions(baseUrl: string | undefined, options: any) { |
| 32 | + const optionsResponse = await fetch(`${baseUrl}/sdapi/v1/options`, { |
| 33 | + method: "POST", |
| 34 | + headers: { |
| 35 | + "Content-Type": "application/json", |
| 36 | + }, |
| 37 | + body: JSON.stringify(options), |
| 38 | + }); |
| 39 | + |
| 40 | + return await optionsResponse.json(); |
| 41 | +} |
| 42 | + |
| 43 | +export async function getImageInfo( |
| 44 | + baseUrl: string | undefined, |
| 45 | + base64image: any |
| 46 | +) { |
| 47 | + const imageInfoResponse = await fetch(`${baseUrl}/sdapi/v1/png-info`, { |
| 48 | + method: "POST", |
| 49 | + headers: { |
| 50 | + "Content-Type": "application/json", |
| 51 | + }, |
| 52 | + body: JSON.stringify({ image: base64image }), |
| 53 | + }); |
| 54 | + |
| 55 | + const imageInfoJson = await imageInfoResponse.json(); |
| 56 | + |
| 57 | + const info = imageInfoJson.info.split("\n"); |
| 58 | + |
| 59 | + const data: any = {}; |
| 60 | + |
| 61 | + if (info.length === 0) { |
| 62 | + return data; |
| 63 | + } |
| 64 | + |
| 65 | + data.prompt = info[0]; |
| 66 | + |
| 67 | + let detailIndex = 1; |
| 68 | + |
| 69 | + if (info.length === 3) { |
| 70 | + data.nagtivePrompt = info[1].split(":")[1].trim(); |
| 71 | + |
| 72 | + detailIndex = 2; |
| 73 | + } |
| 74 | + |
| 75 | + const details = info[detailIndex].split(","); |
| 76 | + |
| 77 | + details.map((detail: any) => { |
| 78 | + const detailInfo = detail.trim().split(":"); |
| 79 | + |
| 80 | + data[detailInfo[0]] = detailInfo[1].trim(); |
| 81 | + }); |
| 82 | + |
| 83 | + return data; |
| 84 | +} |
| 85 | + |
| 86 | +export async function testForHistoryPlugin(webuiHostUrl: string) { |
| 87 | + // timeout after 1 second |
| 88 | + const finished = Promise.race([ |
| 89 | + fetch(`${webuiHostUrl}/StableStudio/get-generated-images`, { |
| 90 | + method: "POST", |
| 91 | + headers: { |
| 92 | + "Content-Type": "application/json", |
| 93 | + }, |
| 94 | + body: JSON.stringify({ |
| 95 | + limit: 1, |
| 96 | + }), |
| 97 | + }), |
| 98 | + new Promise((_, reject) => |
| 99 | + setTimeout(() => reject(new Error("Request timed out")), 1000) |
| 100 | + ), |
| 101 | + ]); |
| 102 | + |
| 103 | + try { |
| 104 | + await finished; |
| 105 | + return (finished as any).ok; |
| 106 | + } catch (error) { |
| 107 | + return false; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export async function constructPayload( |
| 112 | + options: { |
| 113 | + input?: StableDiffusionInput | undefined; |
| 114 | + count?: number | undefined; |
| 115 | + }, |
| 116 | + isUpscale = false, |
| 117 | + upscaler: string | undefined |
| 118 | +) { |
| 119 | + const { sampler, prompts, initialImage, maskImage, width, height, steps } = |
| 120 | + options?.input ?? {}; |
| 121 | + |
| 122 | + // Construct payload |
| 123 | + const data: any = { |
| 124 | + seed: options?.input?.seed === 0 ? -1 : options?.input?.seed, |
| 125 | + cfgScale: options?.input?.cfgScale ?? 7, |
| 126 | + }; |
| 127 | + |
| 128 | + if (isUpscale) { |
| 129 | + /* |
| 130 | + Upscaling values |
| 131 | + */ |
| 132 | + |
| 133 | + data.upscaling_resize_w = width ?? 512; |
| 134 | + data.upscaling_resize_h = height ?? 512; |
| 135 | + data.upscaler_1 = upscaler; |
| 136 | + } else { |
| 137 | + /* |
| 138 | + regular image generation values |
| 139 | + */ |
| 140 | + |
| 141 | + data.width = width ?? 512; |
| 142 | + data.height = height ?? 512; |
| 143 | + |
| 144 | + data.sampler_name = sampler?.name ?? ""; |
| 145 | + data.sampler_index = sampler?.name ?? ""; |
| 146 | + |
| 147 | + data.prompt = |
| 148 | + prompts?.find((p) => (p.text && (p.weight ?? 0) > 0) ?? 0 > 0)?.text ?? |
| 149 | + ""; |
| 150 | + data.negative_prompt = |
| 151 | + prompts?.find((p) => (p.text && (p.weight ?? 0) < 0) ?? 0 < 0)?.text ?? |
| 152 | + ""; |
| 153 | + |
| 154 | + data.steps = steps ?? 20; |
| 155 | + data.batch_size = options?.count; |
| 156 | + data.save_images = true; |
| 157 | + } |
| 158 | + |
| 159 | + if (initialImage?.weight && !isUpscale) { |
| 160 | + data.denoising_strength = 1 - initialImage.weight; |
| 161 | + } |
| 162 | + |
| 163 | + if (initialImage?.blob) { |
| 164 | + const initImgB64 = await blobToBase64(initialImage?.blob); |
| 165 | + |
| 166 | + if (isUpscale) { |
| 167 | + data.image = initImgB64.split(",")[1]; |
| 168 | + } else { |
| 169 | + data.init_images = [initImgB64.split(",")[1]]; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if (maskImage?.blob) { |
| 174 | + const maskImgB64 = await blobToBase64(maskImage?.blob); |
| 175 | + |
| 176 | + data.mask = maskImgB64.split(",")[1]; |
| 177 | + |
| 178 | + data.inpainting_mask_invert = 1; // Mask mode |
| 179 | + data.inpainting_fill = 1; // Masked content |
| 180 | + data.inpaint_full_res = false; // Inpaint area |
| 181 | + } |
| 182 | + |
| 183 | + return data; |
| 184 | +} |
0 commit comments