|
"use server" |
|
|
|
|
|
|
|
|
|
import { generateSeed } from "@/lib/generateSeed"; |
|
import { getValidNumber } from "@/lib/getValidNumber"; |
|
|
|
|
|
|
|
|
|
const instance = `${process.env.FAST_IMAGE_SERVER_API_GRADIO_URL || ""}` |
|
const secretToken = `${process.env.FAST_IMAGE_SERVER_API_SECRET_TOKEN || ""}` |
|
|
|
|
|
|
|
export async function generateImage(options: { |
|
positivePrompt: string; |
|
negativePrompt?: string; |
|
seed?: number; |
|
width?: number; |
|
height?: number; |
|
nbSteps?: number; |
|
}): Promise<string> { |
|
|
|
|
|
const positivePrompt = options?.positivePrompt || "" |
|
if (!positivePrompt) { |
|
throw new Error("missing prompt") |
|
} |
|
|
|
|
|
|
|
|
|
const negativePrompt = options?.negativePrompt || "" |
|
|
|
|
|
const seed = (options?.seed ? options.seed : 0) || generateSeed() |
|
|
|
const width = getValidNumber(options?.width, 256, 1024, 512) |
|
const height = getValidNumber(options?.height, 256, 1024, 512) |
|
const nbSteps = getValidNumber(options?.nbSteps, 1, 8, 4) |
|
|
|
|
|
const positive = [ |
|
|
|
|
|
"beautiful", |
|
|
|
|
|
|
|
|
|
positivePrompt, |
|
|
|
"award winning", |
|
"high resolution" |
|
].filter(word => word) |
|
.join(", ") |
|
|
|
const negative = [ |
|
negativePrompt, |
|
"watermark", |
|
"copyright", |
|
"blurry", |
|
|
|
|
|
"low quality", |
|
"ugly" |
|
].filter(word => word) |
|
.join(", ") |
|
|
|
const res = await fetch(instance + (instance.endsWith("/") ? "" : "/") + "api/predict", { |
|
method: "POST", |
|
headers: { |
|
"Content-Type": "application/json", |
|
|
|
}, |
|
body: JSON.stringify({ |
|
fn_index: 0, |
|
data: [ |
|
positive, |
|
negative, |
|
seed, |
|
width, |
|
height, |
|
0.0, |
|
nbSteps, |
|
secretToken |
|
] |
|
}), |
|
cache: "no-store", |
|
}) |
|
|
|
const { data } = await res.json() |
|
|
|
if (res.status !== 200 || !Array.isArray(data)) { |
|
|
|
throw new Error(`Failed to fetch data (status: ${res.status})`) |
|
} |
|
|
|
if (!data[0]) { |
|
throw new Error(`the returned image was empty`) |
|
} |
|
|
|
return data[0] as string |
|
} |