File size: 1,444 Bytes
b162b24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ea2502
 
b162b24
 
4ea2502
8cf3257
b162b24
 
 
 
 
 
 
 
 
 
942fc29
 
 
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
45
46
47
48
49
50
51
52
53
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/HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.HF_TOKEN}`,
      'Content-Type': 'application/json',
      ['x-use-cache']: "0"
    },
    body: JSON.stringify({
      // inputs: `Generate and return only a music title based on the following prompt: ${prompt}`,
      inputs: `Generate a music title based on the following prompt: ${prompt}, return only the title between quotes.`,
      parameters: {
        num_return_sequences: 1,
        return_full_text: false,
        do_sample: false,
      }
    }),
  })
  .then((response: any) => {
    if (response.status !== 200) return Response.json({ status: 500, ok: false, message: response.statusText })

    return response.json()
  })
  .then((response) => {
    let title = response?.[0]?.generated_text;
    // regex to keep only string between quotes
    title = title?.match(/"(.*?)"/)?.[0];
    
    return {
      title: title,
    }
  })
  .catch((error) => {
    return {
      error: error.message,
    }
  })

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

  return Response.json({
    title: response?.title,
  })
  
}