File size: 1,768 Bytes
a65e95e
 
5dfc565
bda5f6b
cd0e411
 
 
 
ceb44b0
9658ad9
07d10ce
ceb44b0
f7ac73c
7139c79
bda5f6b
9658ad9
bda5f6b
 
 
 
 
cd0e411
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bda5f6b
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
import { client } from "@gradio/client"

import { generateSeed } from "../utils/generateSeed.mts"

export const state = {
  load: 0,
}

// we don't use replicas yet, because it ain't easy to get their hostname
const instances: string[] = [
  `${process.env.VC_ZEROSCOPE_SPACE_API_URL_1 || ""}`,
  `${process.env.VC_ZEROSCOPE_SPACE_API_URL_2 || ""}`,
  // `${process.env.VC_ZEROSCOPE_SPACE_API_URL_3 || ""}`,
].filter(instance => instance?.length > 0)

export const generateVideo = async (prompt: string, options?: {
  seed: number;
  nbFrames: number;
  nbSteps: number;
}) => {

  if (state.load === instances.length) {
    throw new Error(`all video generation servers are busy, try again later..`)
  }

  state.load += 1

  try {
    const seed = options?.seed || generateSeed()
    const nbFrames = options?.nbFrames || 24 // we can go up to 48 frames, but then upscaling quill require too much memory!
    const nbSteps = options?.nbSteps || 35

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

    const api = await client(instance, {
      hf_token: `${process.env.VC_HF_API_TOKEN}` as any
    })

    const rawResponse = await api.predict('/run', [		
      prompt, // string  in 'Prompt' Textbox component		
      seed, // number (numeric value between 0 and 2147483647) in 'Seed' Slider component		
      nbFrames, // 24 // it is the nb of frames per seconds I think?
      nbSteps, // 10, (numeric value between 10 and 50) in 'Number of inference steps' Slider component
    ]) as any
    
    // console.log("rawResponse:", rawResponse)

    const { name } = rawResponse?.data?.[0]?.[0] as { name: string, orig_name: string }

    return `${instance}/file=${name}`
  } catch (err) {
    throw err
  } finally {
    state.load -= 1
  }
}