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