Spaces:
Runtime error
Runtime error
| import { PrismaClient } from '@prisma/client' | |
| const prisma = new PrismaClient() | |
| export async function POST( | |
| request: Request, | |
| ) { | |
| const global_headers = { | |
| Authorization: `Bearer ${process.env.NEXT_PUBLIC_APP_HF_TOKEN}`, | |
| 'Content-Type': 'application/json', | |
| ['x-use-cache']: "0" | |
| } | |
| const { inputs, negative_prompt } = await request.json() | |
| const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/models/stabilityai/stable-diffusion-xl-base-1.0`, { | |
| method: 'POST', | |
| body: JSON.stringify({ | |
| inputs, | |
| negative_prompt | |
| }), | |
| headers: global_headers, | |
| }) | |
| const res = await response.clone().json().catch(() => ({})); | |
| if (res?.error) return Response.json({ status: response.status, ok: false, message: res.error }); | |
| const blob = await response.blob() | |
| const headers = new Headers(); | |
| headers.set("Content-Type", "image/*"); | |
| const checkIfIsNSFW = (blob: Blob) => { | |
| return new Promise(async (resolve, reject) => { | |
| const response_nsfw = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/models/DamarJati/NSFW-Filterization-DecentScan`, { | |
| method: 'POST', | |
| headers: { | |
| ...global_headers, | |
| ...headers, | |
| }, | |
| body: blob, | |
| }) | |
| const isNSFW = await response_nsfw.clone().json().catch(() => ({})); | |
| if (isNSFW?.error && isNSFW?.estimated_time) { | |
| setTimeout(() => { | |
| checkIfIsNSFW(blob) | |
| }, isNSFW?.estimated_time * 100); | |
| } else resolve(isNSFW) | |
| }) | |
| } | |
| const isNSFW: any = await checkIfIsNSFW(blob) | |
| if (isNSFW?.error) return Response.json({ status: 500, ok: false, message: isNSFW?.error }); | |
| if (isNSFW?.length) { | |
| const scoreNotSafe = isNSFW?.find((n: { label: string }) => n.label === "no_safe"); | |
| if (scoreNotSafe?.score > 0.75) { | |
| return Response.json({ status: 401, ok: false, message: "Image is not safe for work." }); | |
| } | |
| } | |
| const arrayBuffer = await blob.arrayBuffer() | |
| const bytes = Buffer.from(arrayBuffer) | |
| const new_blob = await prisma.image.create({ | |
| data: { | |
| prompt: inputs, | |
| blob: bytes, | |
| }, | |
| }) | |
| return Response.json({ blob: new_blob, status: 200, ok: true, headers }); | |
| } |