Ayogiakbar

Generator Image - CreArt

Aug 22nd, 2025 (edited)
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Generator Image - CreArt
  3.  * Author  : gienetic
  4.  * Base    : https://play.google.com/store/apps/details?id=ai.aiart.aiartgenerator.creart
  5.  */
  6.  
  7. const axios = require("axios");
  8. const FormData = require("form-data");
  9. const fs = require("fs");
  10.  
  11. /**
  12.  * NOTE:
  13.  * - CreartAI ini kagak mudeng bahasa Indonesia 😅
  14.  * - Jadi setiap prompt otomatis gue translate dulu ke English
  15.  * - Translatenya pake Google Translate gratisan (endpoint public)
  16.  */
  17.  
  18. /**
  19.  * Translate teks otomatis ke bahasa Inggris
  20.  * @param {string} text - input teks (indo / campur2)
  21.  * @returns {Promise<string>} - hasil translate ke english
  22.  */
  23. async function translateToEnglish(text) {
  24.   try {
  25.     const url = "https://translate.googleapis.com/translate_a/single";
  26.     const params = {
  27.       client: "gtx",
  28.       sl: "auto", // auto detek bahasa
  29.       tl: "en",   // target english
  30.       dt: "t",
  31.       q: text,
  32.     };
  33.  
  34.     const res = await axios.get(url, { params });
  35.     return res.data[0][0][0]; // ambil hasil translate
  36.   } catch (err) {
  37.     console.error("Translate Error:", err.message);
  38.     return text; // fallback: balikin teks asli aja
  39.   }
  40. }
  41.  
  42. /**
  43.  * Text2Image - bikin gambar dari teks
  44.  * @param {string} prompt - teks prompt (indo/eng)
  45.  * @returns {Promise<Buffer>} - buffer gambar hasil generate
  46.  */
  47. async function creartTxt2Img(prompt) {
  48.   try {
  49.     const translatedPrompt = await translateToEnglish(prompt);
  50.  
  51.     const form = new FormData();
  52.     form.append("prompt", translatedPrompt);
  53.     form.append("input_image_type", "text2image");
  54.     form.append("aspect_ratio", "4x5");
  55.     form.append("guidance_scale", "9.5");
  56.     form.append("controlnet_conditioning_scale", "0.5");
  57.  
  58.     const response = await axios.post(
  59.       "https://api.creartai.com/api/v2/text2image",
  60.       form,
  61.       {
  62.         headers: form.getHeaders(),
  63.         responseType: "arraybuffer", // hasilnya langsung gambar (binary)
  64.       }
  65.     );
  66.  
  67.     return Buffer.from(response.data);
  68.   } catch (err) {
  69.     throw new Error("❌ Gagal bikin gambar (txt2img): " + (err?.message || err));
  70.   }
  71. }
  72.  
  73. /**
  74.  * Image2Image - edit/generate gambar dari input image
  75.  * @param {string} prompt - teks prompt (indo/eng)
  76.  * @param {string} imagePath - path file gambar input
  77.  * @returns {Promise<Buffer>} - buffer gambar hasil generate
  78.  */
  79. async function creartImg2Img(prompt, imagePath) {
  80.   try {
  81.     const translatedPrompt = await translateToEnglish(prompt);
  82.  
  83.     const form = new FormData();
  84.     form.append("prompt", translatedPrompt);
  85.     form.append("input_image_type", "image2image");
  86.     form.append("aspect_ratio", "4x5");
  87.     form.append("guidance_scale", "9.5");
  88.     form.append("controlnet_conditioning_scale", "0.5");
  89.     form.append("image_file", fs.createReadStream(imagePath));
  90.  
  91.     const response = await axios.post(
  92.       "https://api.creartai.com/api/v2/image2image",
  93.       form,
  94.       {
  95.         headers: form.getHeaders(),
  96.         responseType: "arraybuffer", // hasilnya langsung gambar (binary)
  97.       }
  98.     );
  99.  
  100.     return Buffer.from(response.data);
  101.   } catch (err) {
  102.     throw new Error("❌ Gagal bikin gambar (img2img): " + (err?.message || err));
  103.   }
  104. }
  105.  
  106. module.exports = { creartTxt2Img, creartImg2Img };
  107.  
Add Comment
Please, Sign In to add comment