-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
145 lines (123 loc) · 4.06 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const Rest = require("./rest.js");
const identify = require("./identify.js");
const download = require("./download.js");
const mkdirp = require("mkdirp");
const path = require("path");
let paint_rest = new Rest("paint.api.wombo.ai", 100);
module.exports = async function task(prompt, style, update_fn = () => {}, settings = {}) {
let {final = true, inter = false, identify_key, download_dir = "./generated/"} = settings;
if (final || inter) mkdirp(download_dir);
let id;
try {
id = await identify(identify_key);
} catch (err) {
console.error(err);
throw new Error(`Error while sending prompt:\n${err.toFriendly ? err.toFriendly() : err.toString()}`);
}
paint_rest.custom_headers = {
"Authorization": "bearer " + id,
"Origin": "https://app.wombo.art",
"Referer": "https://app.wombo.art/",
};
update_fn({
state: "authenticated",
id,
});
let task;
try {
task = await paint_rest.options("/api/tasks/", "POST")
.then(() => paint_rest.post("/api/tasks/", {premium: false}));
} catch (err) {
console.error(err);
throw new Error(`Error while allocating a new task:\n${err.toFriendly ? err.toFriendly() : err.toString()}`);
}
let task_path = "/api/tasks/" + task.id;
update_fn({
state: "allocated",
id,
task,
});
try {
task = await paint_rest.options(task_path, "PUT")
.then(() => paint_rest.put(task_path, {
input_spec: {
display_freq: 10,
prompt,
style: +style,
}
}));
} catch (err) {
console.error(err);
throw new Error(`Error while sending prompt:\n${err.toFriendly ? err.toFriendly() : err.toString()}`);
}
update_fn({
state: "submitted",
id,
task,
});
let inter_downloads = [];
let inter_paths = [];
let inter_finished = [];
while (!task.result) {
try {
task = await paint_rest.get(task_path, "GET");
} catch (err) {
console.error(err);
throw new Error(`Error while fetching update:\n${err.toFriendly ? err.toFriendly() : err.toString()}`);
}
if (task.state === "pending") console.warn("Warning: task is pending");
if (inter) {
for (let n = 0; n < task.photo_url_list.length; n++) {
if (inter_downloads[n] || /\/final\.je?pg/i.exec(task.photo_url_list[n])) continue;
inter_paths[n] = path.join(download_dir, `${task.id}-${n}.jpg`);
inter_downloads[n] = download(task.photo_url_list[n], inter_paths[n]).then(() => {
return inter_finished[n] = inter_paths[n];
});
}
}
update_fn({
state: "progress",
id,
task,
inter: inter_finished
});
await (new Promise((res) => setTimeout(res, 1000)));
}
update_fn({
state: "generated",
id,
task,
url: task.result.final,
inter: inter_finished,
});
let download_path = path.join(download_dir, `${task.id}-final.jpg`);
try {
if (final) await download(task.result.final, download_path);
if (inter) await Promise.all(inter_downloads);
} catch (err) {
console.error(err);
throw new Error(`Error while downloading results:\n${err.toFriendly ? err.toFriendly() : err.toString()}`);
}
update_fn({
state: "downloaded",
id,
task,
url: task.result.final,
path: final ? download_path : null,
inter: inter_finished,
});
return {
state: "downloaded",
id,
task,
url: task.result.final,
path: final ? download_path : null,
inter: inter_finished,
};
}
module.exports.styles = require("./styles.js");
module.exports.download = require("./download.js");
// Make `node .` a shorthand for `node cli.js`
if (require.main === module) {
require("./cli.js");
}