File size: 2,755 Bytes
0176e5b
 
1cef24b
0176e5b
 
 
 
 
 
 
 
 
 
 
 
 
 
1cef24b
 
 
 
 
0176e5b
1cef24b
 
0176e5b
 
 
 
 
 
1cef24b
0176e5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cef24b
0176e5b
 
 
 
 
 
 
 
 
1cef24b
0176e5b
 
 
 
1cef24b
 
0176e5b
1cef24b
 
 
 
0176e5b
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { addBase64Header } from "@/lib/data/addBase64Header"
import { MusicGenerationParams } from "./types"
import { getClusterMachine } from "./cluster"

const microserviceApiKey = `${process.env.MICROSERVICE_API_SECRET_TOKEN || ""}`

/**
 * Note: this generates a base64 mp3 file
 */
export async function generateMusicWithMusicgen({
  prompt,
  durationInSec,
  hd,
  debug = false,
  neverThrow = false,
}: MusicGenerationParams): Promise<string> {

  if (!prompt?.length) {
    throw new Error(`prompt is too short!`)
  }

  const machine = await getClusterMachine()

  try {
    const res = await fetch(machine.url + (machine.url.endsWith("/") ? "" : "/") + "api/predict", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        // Authorization: `Bearer ${token}`,
      },
      body: JSON.stringify({
        fn_index: 1, // <- important!
        data: [
          microserviceApiKey, // string  in 'Secret Token' Textbox component		
          "facebook/musicgen-stereo-large", // string  in 'Model' Radio component		
          "", // string  in 'Model Path (custom models)' Textbox component	
          
          // can be one of Default or MultiBand_Diffusion
          // since speed isn't an issue for AI Tube,
          // we can afford to use the MultiBand Decoder
          hd ? "MultiBand_Diffusion" : "Default",

          prompt, // string  in 'Input Text' Textbox component
          null, 	// blob in 'File' Audio component		
          durationInSec, // number (numeric value between 1 and 300) in 'Duration' Slider component		
          250, // number  in 'Top-k' Number component		
          0, // number  in 'Top-p' Number component		
          1, // number  in 'Temperature' Number component		
          3, // number  in 'Classifier Free Guidance' Number component
        ],
      }),
      cache: "no-store",
      // we can also use this (see https://vercel.com/blog/vercel-cache-api-nextjs-cache)
      // next: { revalidate: 1 }
    })

    if (res.status !== 200) {
      throw new Error('Failed to fetch data')
    }
  
    const { data } = await res.json()

    // console.log("data:", data)
    // Recommendation: handle errors
    if (res.status !== 200 || !Array.isArray(data)) {
      // This will activate the closest `error.js` Error Boundary
      throw new Error(`Failed to fetch data (status: ${res.status})`)
    }
    // console.log("data:", data.slice(0, 50))

    if (!data[0]) {
      throw new Error(`the returned music was empty`)
    }

    // console.log("data:", data[0].slice(0, 60))
    return addBase64Header(data[0] as string, "mp3")
  } catch (err) {
    throw err
  } finally {
    // important: we need to free up the machine!
    machine.busy = false
  }
}