|
"use server" |
|
|
|
import { generateSeed } from "@/lib/generateSeed" |
|
import { UpscalingParams } from "@/types" |
|
import { addBase64HeaderToPng } from "./addBase64HeaderToPng" |
|
|
|
const gradioApi = `https://jbilcke-hf-image-upscaling-api.hf.space` |
|
const microserviceApiKey = `${process.env.MICROSERVICE_API_SECRET_TOKEN || ""}` |
|
|
|
export async function upscale({ |
|
imageAsBase64, |
|
prompt, |
|
negativePrompt, |
|
scaleFactor, |
|
nbSteps, |
|
seed, |
|
}: UpscalingParams): Promise<string> { |
|
|
|
const addedPrompt = [ |
|
"clean", |
|
"high-resolution", |
|
"8k", |
|
"best quality", |
|
"masterpiece", |
|
"crisp", |
|
"sharp", |
|
"intricate details" |
|
].join(", ") |
|
|
|
const negPrompt = [ |
|
negativePrompt, |
|
"pixelated", |
|
"pixels", |
|
"noise", |
|
"blur", |
|
"motion blur", |
|
"lowres", |
|
"oversmooth", |
|
"longbody", |
|
"bad anatomy", |
|
"bad hands", |
|
"missing fingers", |
|
"extra digit", |
|
"fewer digits", |
|
"cropped", |
|
"worst quality", |
|
"low quality", |
|
"artificial", |
|
"unrealistic", |
|
"watermark", |
|
"trademark", |
|
"error", |
|
"mistake" |
|
].join(", ") |
|
|
|
const conditioningScale = 1.4 |
|
const classifierFreeGuidance = 9.5 |
|
|
|
|
|
const res = await fetch(gradioApi + (gradioApi.endsWith("/") ? "" : "/") + "api/predict", { |
|
method: "POST", |
|
headers: { |
|
"Content-Type": "application/json", |
|
|
|
}, |
|
body: JSON.stringify({ |
|
fn_index: 0, |
|
data: [ |
|
microserviceApiKey, |
|
imageAsBase64, |
|
prompt, |
|
addedPrompt, |
|
negPrompt, |
|
nbSteps, |
|
scaleFactor, |
|
conditioningScale, |
|
classifierFreeGuidance, |
|
seed || generateSeed(), |
|
], |
|
}), |
|
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})`) |
|
} |
|
|
|
|
|
const base64Content = (data?.[0] || "") as string |
|
|
|
if (!base64Content) { |
|
throw new Error(`invalid response (no content)`) |
|
} |
|
|
|
return addBase64HeaderToPng(base64Content) |
|
} |