Update app.js
Browse files
app.js
CHANGED
|
@@ -15,6 +15,7 @@ const require = createRequire(import.meta.url);
|
|
| 15 |
|
| 16 |
import EventEmitter from 'events';
|
| 17 |
import WebSocket from 'ws';
|
|
|
|
| 18 |
|
| 19 |
import pkg from 'axios-cookiejar-support';
|
| 20 |
const { wrapper } = pkg;
|
|
@@ -2261,6 +2262,153 @@ app.post('/compile', async (req, res) => {
|
|
| 2261 |
}
|
| 2262 |
});
|
| 2263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2264 |
const PORT = process.env.PORT || 7860;
|
| 2265 |
|
| 2266 |
app.listen(PORT, async () => {
|
|
|
|
| 15 |
|
| 16 |
import EventEmitter from 'events';
|
| 17 |
import WebSocket from 'ws';
|
| 18 |
+
import crypto from 'crypto';
|
| 19 |
|
| 20 |
import pkg from 'axios-cookiejar-support';
|
| 21 |
const { wrapper } = pkg;
|
|
|
|
| 2262 |
}
|
| 2263 |
});
|
| 2264 |
|
| 2265 |
+
class ColorifyAI {
|
| 2266 |
+
constructor() {
|
| 2267 |
+
this.ws = null;
|
| 2268 |
+
this.sessionHash = this.generateHash();
|
| 2269 |
+
}
|
| 2270 |
+
|
| 2271 |
+
generateHash() {
|
| 2272 |
+
return crypto.randomBytes(8).toString('hex');
|
| 2273 |
+
}
|
| 2274 |
+
|
| 2275 |
+
async imageToBase64(imageUrl = "https://i.pinimg.com/236x/21/81/c4/2181c4e2d51db79bb2ac000dcac2df90.jpg") {
|
| 2276 |
+
try {
|
| 2277 |
+
const res = await axios.get(imageUrl, { responseType: 'arraybuffer' });
|
| 2278 |
+
return `data:image/webp;base64,${Buffer.from(res.data).toString('base64')}`;
|
| 2279 |
+
} catch (error) {
|
| 2280 |
+
console.error('Error converting image to base64:', error);
|
| 2281 |
+
throw error;
|
| 2282 |
+
}
|
| 2283 |
+
}
|
| 2284 |
+
|
| 2285 |
+
async start(options) {
|
| 2286 |
+
return new Promise((resolve, reject) => {
|
| 2287 |
+
let wsUrl = "";
|
| 2288 |
+
if (options.type === "img2color") {
|
| 2289 |
+
wsUrl = "wss://colorifyai.art/demo-auto-coloring/queue/join";
|
| 2290 |
+
} else if (options.type === "txt2img") {
|
| 2291 |
+
wsUrl = "wss://colorifyai.art/demo-colorify-text2img/queue/join";
|
| 2292 |
+
} else if (options.type === "img2img") {
|
| 2293 |
+
wsUrl = "wss://colorifyai.art/demo-colorify-img2img/queue/join";
|
| 2294 |
+
} else {
|
| 2295 |
+
return reject(new Error("Invalid type. Use 'img2color', 'txt2img', or 'img2img'."));
|
| 2296 |
+
}
|
| 2297 |
+
|
| 2298 |
+
this.ws = new WebSocket(wsUrl, {
|
| 2299 |
+
headers: {
|
| 2300 |
+
"Upgrade": "websocket",
|
| 2301 |
+
"Origin": "https://colorifyai.art",
|
| 2302 |
+
"Cache-Control": "no-cache",
|
| 2303 |
+
"Accept-Language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7",
|
| 2304 |
+
"Pragma": "no-cache",
|
| 2305 |
+
"Connection": "Upgrade",
|
| 2306 |
+
"Sec-WebSocket-Key": crypto.randomBytes(16).toString("base64"),
|
| 2307 |
+
"User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36",
|
| 2308 |
+
"Sec-WebSocket-Version": "13",
|
| 2309 |
+
"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits"
|
| 2310 |
+
}
|
| 2311 |
+
});
|
| 2312 |
+
|
| 2313 |
+
this.ws.on("open", () => {
|
| 2314 |
+
console.log("Connected to WebSocket, waiting for send_hash...");
|
| 2315 |
+
});
|
| 2316 |
+
|
| 2317 |
+
this.ws.on("message", async (data) => {
|
| 2318 |
+
const response = JSON.parse(data.toString());
|
| 2319 |
+
|
| 2320 |
+
if (response.msg === "send_hash") {
|
| 2321 |
+
console.log("Sending session_hash...");
|
| 2322 |
+
this.ws.send(JSON.stringify({ session_hash: this.sessionHash }));
|
| 2323 |
+
}
|
| 2324 |
+
|
| 2325 |
+
if (response.msg === "send_data") {
|
| 2326 |
+
console.log("Sending data...");
|
| 2327 |
+
try {
|
| 2328 |
+
let requestData = {};
|
| 2329 |
+
if (options.type === "img2color" || options.type === "img2img") {
|
| 2330 |
+
const base64Image = await this.imageToBase64(options.imageUrl);
|
| 2331 |
+
requestData = {
|
| 2332 |
+
data: {
|
| 2333 |
+
source_image: base64Image,
|
| 2334 |
+
prompt: options.prompt || "(masterpiece), best quality",
|
| 2335 |
+
request_from: 10
|
| 2336 |
+
}
|
| 2337 |
+
};
|
| 2338 |
+
} else {
|
| 2339 |
+
requestData = {
|
| 2340 |
+
data: {
|
| 2341 |
+
prompt: options.prompt,
|
| 2342 |
+
style: options.style || "default",
|
| 2343 |
+
aspect_ratio: options.aspectRatio || "9:16",
|
| 2344 |
+
request_from: 10
|
| 2345 |
+
}
|
| 2346 |
+
};
|
| 2347 |
+
}
|
| 2348 |
+
this.ws.send(JSON.stringify(requestData));
|
| 2349 |
+
} catch (err) {
|
| 2350 |
+
reject(err);
|
| 2351 |
+
}
|
| 2352 |
+
}
|
| 2353 |
+
|
| 2354 |
+
if (response.msg === "process_completed") {
|
| 2355 |
+
console.log("Process completed:", response);
|
| 2356 |
+
resolve({
|
| 2357 |
+
baseUrl: 'https://temp.colorifyai.art',
|
| 2358 |
+
...(typeof response.output === 'object' && response.output !== null ? response.output : {})
|
| 2359 |
+
});
|
| 2360 |
+
this.ws.close();
|
| 2361 |
+
}
|
| 2362 |
+
});
|
| 2363 |
+
|
| 2364 |
+
this.ws.on("error", (error) => {
|
| 2365 |
+
console.error("WebSocket Error:", error);
|
| 2366 |
+
reject(error);
|
| 2367 |
+
});
|
| 2368 |
+
|
| 2369 |
+
this.ws.on("close", () => {
|
| 2370 |
+
console.log("WebSocket closed");
|
| 2371 |
+
});
|
| 2372 |
+
});
|
| 2373 |
+
}
|
| 2374 |
+
}
|
| 2375 |
+
|
| 2376 |
+
// Handle POST request to /colorify
|
| 2377 |
+
app.post('/colorifyai', async (req, res) => {
|
| 2378 |
+
const params = req.body;
|
| 2379 |
+
const validTypes = ["img2color", "txt2img", "img2img"];
|
| 2380 |
+
|
| 2381 |
+
if (!params.type || !validTypes.includes(params.type.toLowerCase())) {
|
| 2382 |
+
return res.status(400).json({ error: "Invalid type. Allowed types: img2color, txt2img, img2img" });
|
| 2383 |
+
}
|
| 2384 |
+
|
| 2385 |
+
const ai = new ColorifyAI();
|
| 2386 |
+
try {
|
| 2387 |
+
const data = await ai.start(params);
|
| 2388 |
+
return res.status(200).json(data);
|
| 2389 |
+
} catch (error) {
|
| 2390 |
+
return res.status(500).json({ error: "Error during WebSocket request", details: error.message });
|
| 2391 |
+
}
|
| 2392 |
+
});
|
| 2393 |
+
|
| 2394 |
+
// Handle GET request to /colorify
|
| 2395 |
+
app.get('/colorifyai', async (req, res) => {
|
| 2396 |
+
const params = req.query;
|
| 2397 |
+
const validTypes = ["img2color", "txt2img", "img2img"];
|
| 2398 |
+
|
| 2399 |
+
if (!params.type || !validTypes.includes(params.type.toLowerCase())) {
|
| 2400 |
+
return res.status(400).json({ error: "Invalid type. Allowed types: img2color, txt2img, img2img" });
|
| 2401 |
+
}
|
| 2402 |
+
|
| 2403 |
+
const ai = new ColorifyAI();
|
| 2404 |
+
try {
|
| 2405 |
+
const data = await ai.start(params);
|
| 2406 |
+
return res.status(200).json(data);
|
| 2407 |
+
} catch (error) {
|
| 2408 |
+
return res.status(500).json({ error: "Error during WebSocket request", details: error.message });
|
| 2409 |
+
}
|
| 2410 |
+
});
|
| 2411 |
+
|
| 2412 |
const PORT = process.env.PORT || 7860;
|
| 2413 |
|
| 2414 |
app.listen(PORT, async () => {
|