Spaces:
Sleeping
Sleeping
Create img2anime.js
Browse files- plugins/img2anime.js +147 -0
plugins/img2anime.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const axios = require('axios');
|
| 2 |
+
const FormData = require('form-data');
|
| 3 |
+
const fs = require('fs');
|
| 4 |
+
const path = require('path');
|
| 5 |
+
|
| 6 |
+
async function downloadImage(url) {
|
| 7 |
+
const response = await axios.get(url, { responseType: 'arraybuffer' });
|
| 8 |
+
const tempPath = path.join('/tmp', `temp_${Date.now()}.jpg`);
|
| 9 |
+
fs.writeFileSync(tempPath, response.data);
|
| 10 |
+
return tempPath;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
async function uploadImage(imagePath) {
|
| 14 |
+
const form = new FormData();
|
| 15 |
+
form.append('file', fs.createReadStream(imagePath));
|
| 16 |
+
|
| 17 |
+
const response = await axios.post('https://api.anifun.ai/aitools/upload-img', form, {
|
| 18 |
+
headers: {
|
| 19 |
+
...form.getHeaders(),
|
| 20 |
+
'Accept': 'application/json, text/plain, */*',
|
| 21 |
+
'fp': '3bb76e9ae8e3349d2b18784710f9fcf2',
|
| 22 |
+
'fp1': 'VeGrjITw+hD28JEcHeZUQg+conr58/Ul8u0eyHje6+C7YMyhaVWOlzi9WpeREtBH',
|
| 23 |
+
'x-guide': 'qFk6oyLZ8ErEQcn0yYFwS3aw6uOKbwq5Z1reLc2AdRyMt03xqEzzuzwvu17Ccn98cPBIv4sFslmbLVsoMCjQYcvsBgkKFxhM7n2SsXAx4OYC0b6FZYcoknHFdwlg8TLgTzbI6eKO/UnlZ8OXy8oWXVZQpIwik++T9yB/GlHm/nk=',
|
| 24 |
+
'theme-version': '83EmcUoQTUv50LhNx0VrdcK8rcGexcP35FcZDcpgWsAXEyO4xqL5shCY6sFIWB2Q',
|
| 25 |
+
'X-code': Date.now().toString()
|
| 26 |
+
}
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
return response.data.data.path;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
async function createTask(imagePath) {
|
| 33 |
+
const response = await axios.post('https://api.anifun.ai/aitools/of/create', {
|
| 34 |
+
fn_name: 'demo-photo2anime',
|
| 35 |
+
call_type: 3,
|
| 36 |
+
input: {
|
| 37 |
+
source_image: imagePath,
|
| 38 |
+
model: 'meinamix_meinaV10.safetensors',
|
| 39 |
+
request_from: 16
|
| 40 |
+
},
|
| 41 |
+
data: '',
|
| 42 |
+
request_from: 16,
|
| 43 |
+
origin_from: '68d425c58e76bc6c'
|
| 44 |
+
}, {
|
| 45 |
+
headers: {
|
| 46 |
+
'Accept': 'application/json, text/plain, */*',
|
| 47 |
+
'Content-Type': 'application/json',
|
| 48 |
+
'fp': '3bb76e9ae8e3349d2b18784710f9fcf2',
|
| 49 |
+
'fp1': 'yT81wFb2X0ss6hNjIR+HCNiTcKLrMiAAYGUTnM0YgotgEFOxvn35zdXiy/KQQRr5',
|
| 50 |
+
'x-guide': 'czHwUA0mliSpfrqadJ+HVGC/VK1S4RCrfPTqd5QNjnaAkrBXnGFDIhi+8ACL2SI+hHNelqKtU9pjyD/OOaqCNv/WoPErVCv8DBya7MxfsHRv9mOyxnQNrUd8QjidARRYWauylMqb7AePM1DKzOKwv9lE8SnxiV1NhuKEFQwLI0M=',
|
| 51 |
+
'theme-version': '83EmcUoQTUv50LhNx0VrdcK8rcGexcP35FcZDcpgWsAXEyO4xqL5shCY6sFIWB2Q',
|
| 52 |
+
'X-code': Date.now().toString()
|
| 53 |
+
}
|
| 54 |
+
});
|
| 55 |
+
|
| 56 |
+
return response.data.data.task_id;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
async function checkStatus(taskId) {
|
| 60 |
+
const response = await axios.post('https://api.anifun.ai/aitools/of/check-status', {
|
| 61 |
+
task_id: taskId,
|
| 62 |
+
fn_name: 'demo-photo2anime',
|
| 63 |
+
call_type: 3,
|
| 64 |
+
request_from: 16,
|
| 65 |
+
origin_from: '68d425c58e76bc6c'
|
| 66 |
+
}, {
|
| 67 |
+
headers: {
|
| 68 |
+
'Accept': 'application/json, text/plain, */*',
|
| 69 |
+
'Content-Type': 'application/json',
|
| 70 |
+
'fp': '3bb76e9ae8e3349d2b18784710f9fcf2',
|
| 71 |
+
'fp1': '5uPGxFqp3u1UP/vmnHChK836vDakyyTGO9en534m2BxrMLnDZQ2zfLCS3zKlyoq/',
|
| 72 |
+
'x-guide': 'g+2k/haxEiQhjZv3zYRFsVTX20i3KimxaBTY7sYx4El/Bcndpwks/Te1vbl9nPItCyKnTuxTkoyGHR5KHtryDLYIT4EpoKiebYQntMIS11UBwlyxWs0Yj6i+zr3RFoWzw8jfitKCyNIJ4ZX/GvUAYHGKUXtAFiN/g05YXSTdgvE=',
|
| 73 |
+
'theme-version': '83EmcUoQTUv50LhNx0VrdcK8rcGexcP35FcZDcpgWsAXEyO4xqL5shCY6sFIWB2Q',
|
| 74 |
+
'X-code': Date.now().toString()
|
| 75 |
+
}
|
| 76 |
+
});
|
| 77 |
+
|
| 78 |
+
return response.data.data;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
async function sleep(ms) {
|
| 82 |
+
return new Promise(resolve => setTimeout(resolve, ms));
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
async function img2anime(imageUrl) {
|
| 86 |
+
const tempPath = await downloadImage(imageUrl);
|
| 87 |
+
|
| 88 |
+
try {
|
| 89 |
+
const uploadedPath = await uploadImage(tempPath);
|
| 90 |
+
const taskId = await createTask(uploadedPath);
|
| 91 |
+
|
| 92 |
+
while (true) {
|
| 93 |
+
const statusData = await checkStatus(taskId);
|
| 94 |
+
|
| 95 |
+
if (statusData.status === 2) {
|
| 96 |
+
return `https://temp.anifun.ai/${statusData.result_image}`;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
await sleep(2000);
|
| 100 |
+
}
|
| 101 |
+
} finally {
|
| 102 |
+
if (fs.existsSync(tempPath)) {
|
| 103 |
+
fs.unlinkSync(tempPath);
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
const handler = async (req, res) => {
|
| 109 |
+
try {
|
| 110 |
+
const { image, key } = req.query;
|
| 111 |
+
|
| 112 |
+
if (!image || !key) {
|
| 113 |
+
return res.status(400).json({
|
| 114 |
+
success: false,
|
| 115 |
+
error: 'Missing required parameters: image and key'
|
| 116 |
+
});
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
const resultUrl = await img2anime(image);
|
| 120 |
+
|
| 121 |
+
res.json({
|
| 122 |
+
author: "Herza",
|
| 123 |
+
success: true,
|
| 124 |
+
data: {
|
| 125 |
+
url: resultUrl
|
| 126 |
+
}
|
| 127 |
+
});
|
| 128 |
+
|
| 129 |
+
} catch (error) {
|
| 130 |
+
res.status(500).json({
|
| 131 |
+
success: false,
|
| 132 |
+
error: error.message
|
| 133 |
+
});
|
| 134 |
+
}
|
| 135 |
+
};
|
| 136 |
+
|
| 137 |
+
module.exports = {
|
| 138 |
+
name: 'Image to Anime Converter',
|
| 139 |
+
description: 'Convert real photos to anime style using AI',
|
| 140 |
+
type: 'GET',
|
| 141 |
+
routes: ['api/AI/img2anime'],
|
| 142 |
+
tags: ['ai', 'image', 'anime', 'converter'],
|
| 143 |
+
main: ['AI'],
|
| 144 |
+
parameters: ['image', 'key'],
|
| 145 |
+
enabled: true,
|
| 146 |
+
handler
|
| 147 |
+
};
|