File size: 1,061 Bytes
b162b24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
import { NextRequest } from "next/server";

export async function POST(
  request: NextRequest,
) {
  const { prompt } = await request.json();

  const response = await fetch("https://api-inference.huggingface.co/models/CiroN2022/cd-md-music", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.HF_TOKEN}`,
      'Content-Type': 'application/json',
      ['x-use-cache']: "0"
    },
    body: JSON.stringify({
      inputs: prompt,
    }),
  })
  .then((response: any) => {
    if (response.status !== 200) return Response.json({ status: 500, ok: false, message: response.statusText })

    return response.arrayBuffer()
  })
  .then((response) => {
    return Buffer.from(response)
  })
  .catch((error) => {
    return {
      error: error.message,
    }
  })

  if ("error" in response) {
    return Response.json({ status: 500, ok: false, message: response.error })
  }

  // buffer to blob
  const image = Buffer.from(response).toString('base64')

  return Response.json({
    image: "data:image/png;base64," + image,
  })
  
}