UNPKG

askai-img

Version:

AI Art Generation Tool

83 lines (82 loc) 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.askai_img = void 0; const styles_1 = require("./services/styles"); const headers_1 = require("./utils/headers"); class askai_img { static async styles() { return await this.stylesService.getAllStyles(); } static async generate(text, style) { const headers = await (0, headers_1.getHeader)(); const customStyles = await this.stylesService.getCustomStyles(); let textQ = text; let styleQ = style; const customIds = Object.entries(customStyles).reduce((acc, [key, value]) => { acc[value.id] = key; return acc; }, {}); if (customIds[style]) { const customStyle = customStyles[customIds[style]]; textQ = customStyle.prompt.replace('{PROMPT}', text); styleQ = customStyle.style; } return await this.generateArtwork(headers, textQ, styleQ); } static async generateArtwork(headers, text, style) { try { const genResponse = await fetch('https://paint.api.wombo.ai/api/v2/tasks', { method: 'POST', headers: { ...headers, 'Content-Type': 'application/json' }, body: JSON.stringify({ is_premium: false, input_spec: { prompt: text, style: style, display_freq: 10 } }) }); const data = await genResponse.json(); if (!data.id) { throw new Error('Failed to get task ID'); } let result = null; for (let i = 0; i < 30; i++) { result = await this.pollResult(headers, data.id); if (result) break; await new Promise(resolve => setTimeout(resolve, 3000)); } return result; } catch (error) { console.error('Failed to generate artwork:', error); return null; } } static async pollResult(headers, taskId) { try { const response = await fetch(`https://paint.api.wombo.ai/api/v2/tasks/${taskId}`, { headers }); const result = await response.json(); if (result.state === "failed") { return null; } if (result.result?.final) { return result.result.final; } return null; } catch (error) { console.error('Error polling result:', error); return null; } } } exports.askai_img = askai_img; askai_img.stylesService = new styles_1.StylesService();