Spaces:
Running
Running
File size: 1,694 Bytes
1cea837 |
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 |
import { makeSureSpaceIsRunning } from "./makeSureSpaceIsRunning"
import { sleep } from "./sleep"
import { timeout } from "./timeout"
const sec = 1000
const min = 60 *sec
export async function tryApiCalls<T>({
func,
huggingFaceSpace,
debug = false,
failureMessage = "failed to call the endpoint",
autostart = true,
// wait up to 10 min
timeoutInSec = 10 * 60,
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
autostart?: boolean
timeoutInSec?: number
delays?: number[]
}) {
for (let i = 0; i < delays.length; i++) {
try {
if (autostart) {
await makeSureSpaceIsRunning({ space: huggingFaceSpace })
}
// due to an error with the Gradio client, sometimes calling the api.predict
// will never throw an error
const result = await timeout(
func(), // grab the promise
timeoutInSec * 1000,
new Error(`call to ${huggingFaceSpace || "the API"} failed after ${timeoutInSec} seconds`)
)
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`)
}
|