File size: 3,579 Bytes
253bafd fe2d125 253bafd 7aa8f87 253bafd fe2d125 16b9dca fe2d125 253bafd fe2d125 253bafd fe2d125 253bafd fe2d125 16b9dca fe2d125 16b9dca fe2d125 253bafd fe2d125 253bafd fe2d125 253bafd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
const axios = require('axios');
const fs = require('fs');
const path = require('path');
async function fluxImage(prompt, width = 1024, height = 1024, server = "Azure Lite Supercomputer Server") {
try {
const { data: init } = await axios.post(
"https://nihalgazi-flux-unlimited.hf.space/gradio_api/call/generate_image",
{ data: [prompt, width, height, 3, true, server] },
{
headers: {
"Content-Type": "application/json",
Origin: "https://chrunos.com",
Referer: "https://chrunos.com/",
},
}
);
const eventId = init.event_id;
if (!eventId) throw new Error("Failed to obtain event_id.");
const streamUrl = `https://nihalgazi-flux-unlimited.hf.space/gradio_api/call/generate_image/${eventId}`;
let imageUrl = null;
for (let i = 0; i < 15; i++) {
const { data } = await axios.get(streamUrl, {
headers: { Accept: "text/event-stream" },
});
const match = data.match(/"url":\s*"([^"]+)"/);
if (match) {
imageUrl = match[1];
break;
}
await new Promise(r => setTimeout(r, 2000));
}
if (!imageUrl) throw new Error("Failed to retrieve image URL from stream.");
return imageUrl;
} catch (err) {
throw new Error(`fluxImage error: ${err.message}`);
}
}
async function downloadAndSaveImage(imageUrl, fileId) {
const tmpDir = path.join(process.cwd(), 'tmp');
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, { recursive: true });
}
const filePath = path.join(tmpDir, `flux-${fileId}.webp`);
const response = await axios.get(imageUrl, {
responseType: 'arraybuffer',
timeout: 30000
});
fs.writeFileSync(filePath, Buffer.from(response.data));
setTimeout(() => {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
}, 60000);
return `flux-${fileId}.webp`;
}
const handler = async (req, res) => {
try {
const { prompt, key, width, height, server } = req.query;
if (!prompt) {
return res.status(400).json({
author: "Herza",
success: false,
error: 'Missing required parameter: prompt'
});
}
if (!key) {
return res.status(400).json({
author: "Herza",
success: false,
error: 'Missing required parameter: key'
});
}
const imgWidth = width ? parseInt(width) : 1024;
const imgHeight = height ? parseInt(height) : 1024;
const selectedServer = server || "NSFW-Core: Uncensored Server 2";
const imageUrl = await fluxImage(prompt, imgWidth, imgHeight, selectedServer);
if (!imageUrl) {
return res.status(500).json({
author: "Herza",
success: false,
error: 'Failed to generate image'
});
}
const fileId = Date.now() + '_' + Math.random().toString(36).substring(7);
const filename = await downloadAndSaveImage(imageUrl, fileId);
const baseUrl = `${req.protocol}://${req.get('host')}`;
const localUrl = `${baseUrl}/tmp/${filename}`;
res.json({
author: "Herza",
success: true,
data: {
result: localUrl
}
});
} catch (error) {
res.status(500).json({
author: "Herza",
success: false,
error: error.message
});
}
};
module.exports = {
name: 'Flux Image Generator',
description: 'Generate images using Flux AI model',
type: 'GET',
routes: ['api/AI/Flux'],
tags: ['ai', 'image', 'flux', 'generator'],
main: ['AI'],
parameters: ['prompt', 'key', 'width', 'height', 'server'],
enabled: true,
handler
}; |