Spaces:
Running
Running
| import { NextRequest } from "next/server"; | |
| import { PrismaClient } from '@prisma/client' | |
| import { generateImages } from "@/utils/generation/images"; | |
| const prisma = new PrismaClient() | |
| export async function POST(request: NextRequest,) { | |
| const { headers } = request | |
| if (headers.get("x-hf-token") !== process.env.HF_TOKEN) { | |
| return Response.json({ | |
| message: "Wrong castle fam :^)" | |
| }, { status: 401 }); | |
| } | |
| const req_headers = { | |
| Authorization: `Bearer ${process.env.HF_TOKEN}`, | |
| 'Content-Type': 'application/json', | |
| ['x-use-cache']: "0" | |
| } | |
| const start_time = Date.now() | |
| const { prompts } = await request.json() | |
| const old_count = await prisma.promptImage.count() | |
| for ( | |
| const array_of_prompt of prompts | |
| ) { | |
| const prompt_collection = await prisma.prompt.create({ | |
| data: {} | |
| }) | |
| const generated_prompts = await generateImages(req_headers, array_of_prompt); | |
| for ( | |
| const prompt of generated_prompts | |
| ) { | |
| await prisma.promptImage.create({ | |
| data: { | |
| file_name: prompt.file_name, | |
| text: prompt.prompt, | |
| Prompt: { | |
| connect: { | |
| id: prompt_collection.id | |
| } | |
| } | |
| } | |
| }) | |
| } | |
| } | |
| const new_count = await prisma.promptImage.count() | |
| const end_time = Date.now() | |
| return Response.json({ message: `${(new_count - old_count)} prompts has been created in a total of ${(end_time - start_time) / 1000} seconds`, ok: true, status: 200 }); | |
| } |