Create flux.js
Browse files- plugins/flux.js +105 -0
plugins/flux.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const axios = require('axios');
|
| 2 |
+
|
| 3 |
+
async function fluxImage(prompt, width = 1024, height = 1024, server = "NSFW-Core: Uncensored Server 2") {
|
| 4 |
+
try {
|
| 5 |
+
const { data: init } = await axios.post(
|
| 6 |
+
"https://nihalgazi-flux-unlimited.hf.space/gradio_api/call/generate_image",
|
| 7 |
+
{ data: [prompt, width, height, 3, true, server] },
|
| 8 |
+
{
|
| 9 |
+
headers: {
|
| 10 |
+
"Content-Type": "application/json",
|
| 11 |
+
Origin: "https://chrunos.com",
|
| 12 |
+
Referer: "https://chrunos.com/",
|
| 13 |
+
},
|
| 14 |
+
}
|
| 15 |
+
);
|
| 16 |
+
|
| 17 |
+
const eventId = init.event_id;
|
| 18 |
+
if (!eventId) throw new Error("Failed to obtain event_id.");
|
| 19 |
+
|
| 20 |
+
const streamUrl = `https://nihalgazi-flux-unlimited.hf.space/gradio_api/call/generate_image/${eventId}`;
|
| 21 |
+
let imageUrl = null;
|
| 22 |
+
|
| 23 |
+
for (let i = 0; i < 15; i++) {
|
| 24 |
+
const { data } = await axios.get(streamUrl, {
|
| 25 |
+
headers: { Accept: "text/event-stream" },
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
const match = data.match(/"url":\s*"([^"]+)"/);
|
| 29 |
+
if (match) {
|
| 30 |
+
imageUrl = match[1];
|
| 31 |
+
break;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
await new Promise(r => setTimeout(r, 2000));
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
if (!imageUrl) throw new Error("Failed to retrieve image URL from stream.");
|
| 38 |
+
return imageUrl;
|
| 39 |
+
} catch (err) {
|
| 40 |
+
throw new Error(`fluxImage error: ${err.message}`);
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
const handler = async (req, res) => {
|
| 45 |
+
try {
|
| 46 |
+
const { prompt, key, width, height, server } = req.query;
|
| 47 |
+
|
| 48 |
+
if (!prompt) {
|
| 49 |
+
return res.status(400).json({
|
| 50 |
+
author: "Herza",
|
| 51 |
+
success: false,
|
| 52 |
+
error: 'Missing required parameter: prompt'
|
| 53 |
+
});
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
if (!key) {
|
| 57 |
+
return res.status(400).json({
|
| 58 |
+
author: "Herza",
|
| 59 |
+
success: false,
|
| 60 |
+
error: 'Missing required parameter: key'
|
| 61 |
+
});
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
const imgWidth = width ? parseInt(width) : 1024;
|
| 65 |
+
const imgHeight = height ? parseInt(height) : 1024;
|
| 66 |
+
const selectedServer = server || "NSFW-Core: Uncensored Server 2";
|
| 67 |
+
|
| 68 |
+
const result = await fluxImage(prompt, imgWidth, imgHeight, selectedServer);
|
| 69 |
+
|
| 70 |
+
if (!result) {
|
| 71 |
+
return res.status(500).json({
|
| 72 |
+
author: "Herza",
|
| 73 |
+
success: false,
|
| 74 |
+
error: 'Failed to generate image'
|
| 75 |
+
});
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
res.json({
|
| 79 |
+
author: "Herza",
|
| 80 |
+
success: true,
|
| 81 |
+
data: {
|
| 82 |
+
result: result
|
| 83 |
+
}
|
| 84 |
+
});
|
| 85 |
+
|
| 86 |
+
} catch (error) {
|
| 87 |
+
res.status(500).json({
|
| 88 |
+
author: "Herza",
|
| 89 |
+
success: false,
|
| 90 |
+
error: error.message
|
| 91 |
+
});
|
| 92 |
+
}
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
module.exports = {
|
| 96 |
+
name: 'Flux Image Generator',
|
| 97 |
+
description: 'Generate images using Flux AI model',
|
| 98 |
+
type: 'GET',
|
| 99 |
+
routes: ['api/AI/Flux'],
|
| 100 |
+
tags: ['ai', 'image', 'flux', 'generator'],
|
| 101 |
+
main: ['AI'],
|
| 102 |
+
parameters: ['prompt', 'width', 'height', 'server', 'key'],
|
| 103 |
+
enabled: true,
|
| 104 |
+
handler
|
| 105 |
+
};
|