-
Notifications
You must be signed in to change notification settings - Fork 1
/
replace_object.js
53 lines (49 loc) · 1.53 KB
/
replace_object.js
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
/** @format */
const { NovitaSDK, TaskStatus } = require("novita-sdk");
const path = require("path");
const { convertImageToBase64 } = require("./utils.js");
const novitaClient = new NovitaSDK(process.env.NOVITA_API_KEY);
async function replaceObj(onFinish) {
const baseImg = await convertImageToBase64(path.join(__dirname, "test.png"));
const params = {
image_file: baseImg,
object_prompt: "the head of the girl",
prompt: "a dog head",
negative_prompt: "",
};
novitaClient
.replaceObject(params)
.then((res) => {
if (res && res.task_id) {
const timer = setInterval(() => {
novitaClient
.progressV3({
task_id: res.task_id,
})
.then((progressRes) => {
if (progressRes.task.status === TaskStatus.SUCCEED) {
console.log("finished!", progressRes.images);
clearInterval(timer);
onFinish(progressRes.images);
}
if (progressRes.task.status === TaskStatus.FAILED) {
console.warn("failed!", progressRes.task.reason);
clearInterval(timer);
}
if (progressRes.task.status === TaskStatus.QUEUED) {
console.log("queueing");
}
})
.catch((err) => {
console.error("progress error:", err);
});
}, 1000);
}
})
.catch((err) => {
console.error("error:", err);
});
}
replaceObj((imgs) => {
console.log(imgs);
});