UNPKG

lnwai-image-thai

Version:

CreartAI is an advanced AI image generation module that seamlessly translates Thai text to English and creates stunning images in various artistic styles. Perfect for developers, artists, and content creators looking to bring their ideas to life with AI-p

87 lines (76 loc) 3.13 kB
const axios = require('axios'); const qs = require('qs'); class CreartAI { constructor(apiUrl = 'https://api.creartai.com/api/v1/text2image') { this.apiUrl = apiUrl; this.allowedStyles = ['animev2', 'anime', 'kawaii', 'neon', 'neonpunk', 'cartoon']; } async translateText(text) { try { let data = JSON.stringify({ "source": { "dialect": "th", "text": text }, "target": { "dialect": "en-UK" } }); let config = { method: 'post', maxBodyLength: Infinity, url: 'https://ssl-api.itranslateapp.com/v3/texts/translate', headers: { 'API-Key': 'dd71d7f0a905ecc757dae71156c8d2de', 'GUDID': 'ab55e939-ddbf-4200-8bd5-bc0db1074260', 'X-Correlation-ID': 'ab55e939-ddbf-4200-8bd5-bc0db1074260', 'Premium': '1', 'Secure': '1', 'Input-Source': '0', 'Content-Type': 'application/json' }, data: data }; const response = await axios.request(config); return response.data.target.text; } catch (error) { console.error('Error translating text:', error); throw error; } } async generateImage(prompt, style, options = {}) { if (!this.allowedStyles.includes(style)) { throw new Error(`Invalid style. Allowed styles are: ${this.allowedStyles.join(', ')}`); } try { const translatedPrompt = await this.translateText(prompt); let data = qs.stringify({ 'prompt': `${translatedPrompt}, ${style}, cinematic`, 'input_image_type': 'text2image', 'input_image_base64': options.image_base64 || '', 'negative_prompt': options.negative_prompt || '', 'aspect_ratio': options.aspect_ratio || '16x9', 'num_inference_steps': options.num_inference_steps || '', 'controlnet_conditioning_scale': options.controlnet_conditioning_scale || '0.5', 'guidance_scale': options.guidance_scale || '9.5', 'scheduler': options.scheduler || '', 'seed': options.seed || '' }); let config = { method: 'post', maxBodyLength: Infinity, url: this.apiUrl, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: data }; const response = await axios.request(config); return response.data.image_base64; } catch (error) { console.error('Error generating image:', error); throw error; } } } module.exports = CreartAI;