File size: 1,308 Bytes
6a2e106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// import { Credentials, downloadFile, uploadFile, whoAmI } from "@huggingface/hub"

import { makeSureSpaceIsRunning } from "./makeSureSpaceIsRunning.mts"
import { sleep } from "./sleep.mts"

const sec = 1000
const min = 60 *sec

export async function tryApiCalls<T>({
  func,
  huggingFaceSpace,
  debug = false,
  failureMessage = "failed to call the endpoint",
  delays = [
    5 *sec,
    15 *sec,
    40 *sec, // total 1 min wait time

    //at this stage, if it is so slow it means we are probably waking up a model
    // which is a slow operation (takes ~5 min)

    2 *min, //     ~ 3 min ~
    1 *min, //     ~ 4 min ~
    1 *min, //     ~ 5 min ~
  ]
}: {
  func: () => Promise<T>

  // optional: the name of the hugging face space
  // this will be used to "wake up" the space if necessary
  huggingFaceSpace?: string

  debug?: boolean
  failureMessage?: string
  delays?: number[]
}) {

  for (let i = 0; i < delays.length; i++) {
    try {
      await makeSureSpaceIsRunning({ space: huggingFaceSpace })
      const result = await func()
      return result
    } catch (err) {
      if (debug) { console.error(err) }
      process.stdout.write(".")

      if (i > 0) {
        await sleep(delays[i])
      }
    }
  }

  throw new Error(`${failureMessage} after ${delays.length} attempts`)
}