File size: 1,001 Bytes
6b92c38
4f2c36e
 
 
6b92c38
 
 
 
 
 
 
 
 
 
4f2c36e
6b92c38
 
3b6777d
6b92c38
5240c42
6b92c38
 
 
 
 
 
4f2c36e
 
 
 
 
 
 
 
 
 
6b92c38
4f2c36e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { NextResponse } from "next/server";
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

export async function POST(
  request: Request,
) {
  const { inputs } = await request.json()

  const response = await fetch('https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0', {
    method: 'POST',
    body: JSON.stringify({
      inputs,
      stream: true,
    }),
    headers: {
      Authorization: `Bearer ${process.env.NEXT_PUBLIC_APP_HF_TOKEN}`,
      'Content-Type': 'application/json',
      ['x-use-cache']: "0"
    },
  })

  const blob = await response.blob()
  const headers = new Headers();
  headers.set("Content-Type", "image/*");

  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, statusText: "OK", headers });
}