Spaces:
Running
Running
import WebSocket from 'ws'; | |
function toAnime({imgBuffer}) { | |
return new Promise(async(resolve, reject) => { | |
//const imgBuffer = await axios.get(urls, { responseType: "arraybuffer" }); | |
const imgBase64 = imgBuffer.toString("base64"); | |
const sessHash = Math.random().toString(36).slice(2); | |
const ws = new WebSocket("wss://pixnova.ai/demo-photo2anime/queue/join", { | |
headers: { | |
"Origin": "https://pixnova.ai", | |
"User-Agent": "Postify/1.0.0", | |
"Connection": "Upgrade", | |
"Upgrade": "websocket", | |
"Sec-WebSocket-Version": "13", | |
} | |
}); | |
ws.on("message", async (res) => { | |
const msg = JSON.parse(res.toString()); | |
if(msg.msg === "send_hash") { | |
ws.send(JSON.stringify({ session_hash: sessHash })); | |
} else if(msg.msg === "send_data") { | |
ws.send(JSON.stringify({ | |
data: { | |
negative_prompt: "(worst quality, low quality:1.4), (greyscale, monochrome:1.1), cropped, lowres , username, blurry, trademark, watermark, title, multiple view, Reference sheet, curvy, plump, fat, strabismus, clothing cutout, side slit,worst hand, (ugly face:1.2), extra leg, extra arm, bad foot, text, name", | |
prompt: "(masterpiece), best quality, anime style, manga, art", | |
request_from: 2, | |
source_image: "data:image/jpeg;base64," + imgBase64, | |
strength: 0.9 | |
} | |
})) | |
} else if(msg.msg === "process_completed" && msg.success) { | |
const resultUrl = `https://oss-global.pixnova.ai/${msg.output.result[0]}`; | |
resolve(resultUrl); | |
ws.close(); | |
} | |
}); | |
ws.on("error", (error) => { | |
reject(error); | |
ws.close(); | |
}); | |
}) | |
} | |
export { toAnime }; |