| | import { client } from "@gradio/client" |
| |
|
| | import { generateSeed } from "../utils/generateSeed.mts" |
| | import { getValidNumber } from "./getValidNumber.mts" |
| |
|
| | |
| | const instances: string[] = [ |
| | `${process.env.VC_SDXL_SPACE_API_URL_1 || ""}`, |
| | `${process.env.VC_SDXL_SPACE_API_URL_2 || ""}`, |
| | ].filter(instance => instance?.length > 0) |
| |
|
| | export async function generateImageSDXLAsBase64(options: { |
| | positivePrompt: string; |
| | negativePrompt?: string; |
| | seed?: number; |
| | width?: number; |
| | height?: number; |
| | nbSteps?: number; |
| | }) { |
| |
|
| | const positivePrompt = options?.positivePrompt || "" |
| | if (!positivePrompt) { |
| | throw new Error("missing prompt") |
| | } |
| | const negativePrompt = options?.negativePrompt || "" |
| | const seed = getValidNumber(options?.seed, 0, 2147483647, generateSeed()) |
| | const width = getValidNumber(options?.width, 256, 1024, 512) |
| | const height = getValidNumber(options?.height, 256, 1024, 512) |
| | const nbSteps = getValidNumber(options?.nbSteps, 5, 100, 20) |
| | console.log("SEED:", seed) |
| |
|
| | const instance = instances.shift() |
| | instances.push(instance) |
| |
|
| | const positive = [ |
| | "beautiful", |
| | "intricate details", |
| | positivePrompt, |
| | "award winning", |
| | "high resolution" |
| | ].filter(word => word) |
| | .join(", ") |
| |
|
| | const negative = [ |
| | negativePrompt, |
| | "watermark", |
| | "copyright", |
| | "blurry", |
| | |
| | |
| | "low quality", |
| | "ugly" |
| | ].filter(word => word) |
| | .join(", ") |
| |
|
| | const api = await client(instance, { |
| | hf_token: `${process.env.VC_HF_API_TOKEN}` as any |
| | }) |
| |
|
| | |
| | const rawResponse = (await api.predict("/run", [ |
| | positive, |
| | negative, |
| | positive, |
| | negative, |
| | true, |
| | false, |
| | false, |
| | seed, |
| | width, |
| | height, |
| | 8, |
| | 8, |
| | nbSteps, |
| | nbSteps, |
| | true, |
| | ])) as any |
| | |
| | return rawResponse?.data?.[0] as string |
| | } |
| |
|