File size: 2,868 Bytes
ec194c9
 
 
 
 
 
 
 
 
74bfab8
 
 
 
 
 
 
ec194c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3d5145
 
 
ec194c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { client } from "@gradio/client"

import { VideoGenerationOptions } from "./types.mts"
import { getNegativePrompt, getPositivePrompt } from "./defaultPrompts.mts"
import { generateSeed } from "../../utils/misc/generateSeed.mts"

// we don't use replicas yet, because it ain't easy to get their hostname
const instances: string[] = [
  `${process.env.VC_HOTSHOT_XL_GRADIO_SPACE_API_URL_1 || ""}`,
  //`${process.env.VC_HOTSHOT_XL_GRADIO_SPACE_API_URL_2 || ""}`,
  //`${process.env.VC_HOTSHOT_XL_GRADIO_SPACE_API_URL_3 || ""}`,
  //`${process.env.VC_HOTSHOT_XL_GRADIO_SPACE_API_URL_4 || ""}`,
  //`${process.env.VC_HOTSHOT_XL_GRADIO_SPACE_API_URL_5 || ""}`,
  //`${process.env.VC_HOTSHOT_XL_GRADIO_SPACE_API_URL_6 || ""}`,
  //`${process.env.VC_HOTSHOT_XL_GRADIO_SPACE_API_URL_7 || ""}`,
  //`${process.env.VC_HOTSHOT_XL_GRADIO_SPACE_API_URL_8 || ""}`,
].filter(instance => instance?.length > 0)

const secretToken = `${process.env.VC_MICROSERVICE_SECRET_TOKEN || ""}`

export const generateVideo = async ({
  positivePrompt,
  negativePrompt = "",
  seed,
  nbFrames = 8, // for now the only values that make sense are 1 (for a jpg) or 8 (for a video)
  videoDuration = 1000, // for now Hotshot doesn't really supports anything else
  nbSteps = 30, // when rendering a final video, we want a value like 50 or 70 here
  size = "768x320",

  // for jbilcke-hf/sdxl-cinematic-2 it is "cinematic-2"
  triggerWord = "cinematic-2",

  huggingFaceLora = "jbilcke-hf/sdxl-cinematic-2",
}: VideoGenerationOptions) => {

  const instance = instances.shift()
  instances.push(instance)

  const api = await client(instance, {
    hf_token: `${process.env.VC_HF_API_TOKEN}` as any
  })
  
  // pimp the prompt
  positivePrompt = getPositivePrompt(positivePrompt, triggerWord)
  negativePrompt = getNegativePrompt(negativePrompt)

  try {

    const rawResponse = await api.predict(
      1, // <- important!
      [
      secretToken,
			positivePrompt, // string  in 'Prompt' Textbox component		
			negativePrompt || "", 
      huggingFaceLora?.length || undefined, // string  in 'Public LoRA ID' Textbox component		
			size || '512x512', // string (Option from: [('320x768', '320x768'), ('384x672', '384x672'), ('416x608', '416x608'), ('512x512', '512x512'), ('608x416', '608x416'), ('672x384', '672x384'), ('768x320', '768x320')]) in 'Size' Dropdown component		
      !isNaN(seed) && isFinite(seed) ? seed : generateSeed(), // number (numeric value between -1 and 423538377342) in 'Seed' Slider component, -1 to set to random
      nbSteps || 30, 
      nbFrames || 8,
      videoDuration || 1000,
    ]) as any

    // console.log("rawResponse:", rawResponse)

    console.log("data:", rawResponse?.data)
    const { name } = rawResponse?.data?.[0]?.[0] as { name: string, orig_name: string }

    return `${instance}/file=${name}`
  } catch (err) {
    throw err
  }
}