Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
/** @type {import('./$types').RequestHandler} */ | |
import { json, type RequestEvent } from '@sveltejs/kit'; | |
import { env } from '$env/dynamic/private' | |
export async function POST({ request } : RequestEvent) { | |
const generation = await request.json() | |
if (!generation?.model?.id) { | |
return json({ | |
error: { | |
token: "A model id is required" | |
} | |
}, { status: 400 }) | |
} | |
if (!generation?.inputs) { | |
return json({ | |
error: { | |
token: "An inputs is required" | |
} | |
}, { status: 400 }) | |
} | |
const response = await fetch(env.SECRET_INFERENCE_API_URL + "/models/" + generation?.model?.id, { | |
method: "POST", | |
headers: { | |
Authorization: `Bearer ${env.SECRET_HF_TOKEN}`, | |
'Content-Type': 'application/json', | |
['x-use-cache']: "0" | |
}, | |
body: JSON.stringify(generation), | |
}) | |
.then((res) => res.blob()) | |
.then((blob) => blob) | |
.catch((error) => { | |
return { | |
error: error.message, | |
} | |
}) | |
if ("error" in response) { | |
return json({ | |
error: { | |
token: response.error | |
} | |
}, { status: 400 }) | |
} | |
return new Response(response, { | |
headers: { | |
'Content-Type': 'image/png', | |
}, | |
}) | |
} | |