File size: 4,985 Bytes
2eb85f7 ea07d89 4ea0708 b31023a 02a4464 0b5131a 1536d2b 2eb85f7 4192ae2 5a03baf 4192ae2 2eb85f7 235ac5c 2eb85f7 235ac5c 2eb85f7 7d77877 2eb85f7 902ac08 fd2abf1 9b0206c fd2abf1 9b0206c fd2abf1 dd18e9b fd2abf1 1536d2b 972da13 57aeaa6 1536d2b 57aeaa6 2b10a71 57aeaa6 b37c4d7 57aeaa6 b37c4d7 2b10a71 d7a9731 b37c4d7 d7a9731 9b0206c 57aeaa6 b37c4d7 57aeaa6 b37c4d7 1536d2b bef24ee 1536d2b b41d8cd 1536d2b b41d8cd 1536d2b 57aeaa6 1536d2b 57aeaa6 1536d2b 57aeaa6 1536d2b 57aeaa6 1536d2b |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import fetch from "node-fetch";
import express from 'express';
import { Readable } from "stream";
import sharp from "sharp";
import * as config from '../config.js';
import { FluxSchnell } from '../lib/@randydev/flux-1-schnell.js';
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
const FluxRoutes = express.Router();
/**
* @param {string} args - The input string
*/
async function schellwithflux(args) {
const EncodeBaseUrl = "aHR0cHM6Ly9hcGktaW5mZXJlbmNlLmh1Z2dpbmdmYWNlLmNvL21vZGVscy9ibGFjay1mb3Jlc3QtbGFicy9GTFVYLjEtc2NobmVsbA==";
try {
const response = await fetch(atob(EncodeBaseUrl), {
method: "POST",
headers: {
"Authorization": `Bearer ${config.HUGGING_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ inputs: args }),
});
if (!response.ok) {
console.error(`API Error: ${response.status}`);
return null;
}
return await response.arrayBuffer();
} catch (error) {
console.error("Error fetching data:", error.message);
return null;
}
}
/**
* @swagger
* /api/v1/flux/black-forest-labs/flux-1-schnell:
* get:
* summary: black forest labs
* tags: [FLUX]
* parameters:
* - in: query
* name: query
* required: true
* description: User's input query
* schema:
* type: string
* example: "A beautiful sunset over the mountains"
* - in: header
* name: x-api-key
* required: true
* description: API key for authentication
* schema:
* type: string
* example: "lu api key di @aknuserbot telegram"
* responses:
* 200:
* description: Success
*/
FluxRoutes.get('/api/v1/flux/black-forest-labs/flux-1-schnell', authenticateApiKey, apiLimiter, async (req, res) => {
try {
const query = req.query.query;
const result = await FluxSchnell(query);
const imageBytes = Buffer.from(result, "base64");
const processedImage = await sharp(imageBytes).jpeg().toBuffer();
res.set("Content-Type", "image/jpeg");
Readable.from(processedImage).pipe(res);
} catch (error) {
res.status(401).json({ error: error.message });
}
});
/**
* @swagger
* /api/v1/flux/fluxai-ai:
* post:
* summary: Generate an image using Flux AI.
* tags: [FLUX]
* description: This endpoint interacts with Flux AI to generate an image based on the query.
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - query
* properties:
* query:
* type: string
* description: The input query for image generation.
* example: "A beautiful sunset over the mountains"
* parameters:
* - in: header
* name: x-api-key
* required: true
* description: API key for authentication.
* schema:
* type: string
* example: "lu api key di @aknuserbot telegram"
* responses:
* 200:
* description: A successfully processed image.
* content:
* image/jpeg:
* schema:
* type: string
* format: binary
* 400:
* description: Missing query parameter.
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* example: "Query parameter is missing"
* 500:
* description: Internal server error.
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* example: "Internal server error"
*/
FluxRoutes.post("/api/v1/flux/fluxai-ai", authenticateApiKey, apiLimiter, async (req, res) => {
try {
const { query } = req.body;
if (!query) {
return res.status(400).json({ error: "Query parameter is missing" });
}
const imageBytes = await schellwithflux(query);
if (!imageBytes) {
return res.status(500).json({ error: "Failed to fetch image bytes" });
}
const buffer = Buffer.isBuffer(imageBytes) ? imageBytes : Buffer.from(imageBytes);
const processedImage = await sharp(buffer).jpeg().toBuffer();
res.set("Content-Type", "image/jpeg");
Readable.from(processedImage).pipe(res);
} catch (error) {
console.error("Error processing image:", error.message);
res.status(500).json({ error: "Internal server error" });
}
});
export { FluxRoutes }; |