Spaces:
Running
Running
const fs = require("fs"); | |
const Form = require("form-data"); | |
global.data = { | |
messages: {}, | |
taskId: {}, | |
listimageedit: ["omnigen"], | |
}; | |
class Edit { | |
static async omnigen(buffer, prompt) { | |
try { | |
const formData = new FormData(); | |
const file = new File([buffer], "image.png", { type: "image/png" }); | |
formData.append("image", file); | |
formData.append("prompt", prompt); | |
const response = await fetch( | |
"https://omnigenai.org/api/ai/image/image-editor", | |
{ | |
method: "POST", | |
headers: { | |
accept: "*/*", | |
"accept-language": "id-ID,id;q=0.9,en-US;q=0.8", | |
Referer: "https://omnigenai.org/ai-image-editor", | |
}, | |
credentials: "include", | |
body: formData, | |
} | |
); | |
if (!response.ok) { | |
throw new Error(`Gagal: ${response.status} ${response.statusText}`); | |
} | |
const result = await response.json(); | |
return result; | |
} catch (error) { | |
console.error("Error:", error); | |
throw error; | |
} | |
} | |
} | |
let changeList = function (list) { | |
if (!(list in data)) return; | |
data[list].push(data[list].shift()); | |
}; | |
const edit = async (image, retries = 20, delay = 1000) => { | |
for (let attempt = 1; attempt <= retries; attempt++) { | |
try { | |
let ai = await Edit[data.listimageedit[0]](image); | |
console.log({ ai }); | |
console.log(`[${data.listimageedit[0]}]`, ai); | |
changeList("listimageedit"); | |
} catch (error) { | |
changeList("listimageedit"); | |
if (attempt < retries) { | |
console.log( | |
`IMAGE_EDIT[${ | |
data.listimageedit[data.listimageedit.length - 1] | |
}]: Retryng ${attempt} failed.`, | |
error | |
); | |
await new Promise((resolve) => setTimeout(resolve, delay)); | |
} else { | |
return new Error(`Failed after ${retries} attempts: ${error.message}`); | |
} | |
} | |
} | |
}; | |
let image = fetch.readFileSync("./a.jpg"); | |
edit(image).then((a) => console.log(a)); | |