enzostvs's picture
enzostvs HF staff
refacto models + community
eb29a95
raw
history blame
1.4 kB
/** @type {import('./$types').RequestHandler} */
import { UploaderDataset } from '$lib/utils/uploader';
import { json, type RequestEvent } from '@sveltejs/kit';
import prisma from '$lib/prisma';
export async function POST({ request } : RequestEvent) {
const { generation, image } = 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 blob = await fetch(image)
.then((res) => res.blob())
.then((blob) => blob)
.catch((error) => {
return json({
error: error.message,
}, { status: 400 })
})
const success: {
ok: boolean,
path?: string | undefined
} = await UploaderDataset(blob as Blob, generation.inputs)
if (!success.ok) {
return json({
error: {
token: "Error uploading image"
}
}, { status: 400 })
}
const gallery = prisma.gallery.create({
data: {
image: success.path as string,
prompt: generation.inputs,
model: {
connect: {
id: generation.model.id
}
},
}
})
.catch((error) => {
console.log(error)
})
return json({
message: "Successfully generated image",
gallery
})
}