@mvkproject/nexus
Version:
Free AI SDK with API key (500 free daily requests). Access 25+ LLM models (GPT-4, Gemini, Llama, DeepSeek), generate images with 14+ models (Flux, Stable Diffusion), and integrate Akinator game - all completely free.
73 lines (58 loc) • 1.67 kB
JavaScript
import fs from 'fs';
import path from 'path';
import axios from 'axios';
export class ImageAPI {
constructor(client) {
this.client = client;
}
async generate(options) {
const {
prompt,
model = 'flux',
width = 512,
height = 512,
download = false,
downloadPath = './downloads'
} = options;
if (!prompt) {
throw new Error('Prompt is required for image generation');
}
const data = {
prompt,
model,
width,
height
};
const response = await this.client.post('/v1/generate-image', data);
if (response.imageUrl) {
response.imageUrl = `${this.client.baseURL}${response.imageUrl}`;
}
if (download && response.imageUrl) {
const downloadedPath = await this._downloadImage(response.imageUrl, downloadPath);
response.downloadedPath = downloadedPath;
}
return response;
}
async _downloadImage(imageUrl, downloadPath) {
try {
if (!fs.existsSync(downloadPath)) {
fs.mkdirSync(downloadPath, { recursive: true });
}
const filename = path.basename(new URL(imageUrl).pathname);
const filepath = path.join(downloadPath, filename);
const response = await axios({
method: 'GET',
url: imageUrl,
responseType: 'stream'
});
const writer = fs.createWriteStream(filepath);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => resolve(filepath));
writer.on('error', reject);
});
} catch (error) {
throw new Error(`Failed to download image: ${error.message}`);
}
}
}