const axios = require('axios'); const FormData = require('form-data'); const Jimp = require('jimp'); const ProxyAgent = require('@rynn-k/proxy-agent'); const proxy = new ProxyAgent('./proxies.txt', { random: true }); // Adjust proxy path class ToGhibli { constructor() { this.api_url = 'https://jamesliu1217-easycontrol-ghibli.hf.space/gradio_api'; this.file_url = 'https://jamesliu1217-easycontrol-ghibli.hf.space/gradio_api/file='; this.maxDimension = 896; this.agent = proxy.config(); } generateSession = function () { return Math.random().toString(36).substring(2); } getOptimalDimensions = async function (buffer) { try { const image = await Jimp.read(buffer); let width = image.bitmap.width; let height = image.bitmap.height; if (width <= this.maxDimension && height <= this.maxDimension) { return { width, height }; } const aspectRatio = width / height; if (width > height) { width = this.maxDimension; height = Math.round(width / aspectRatio); } else { height = this.maxDimension; width = Math.round(height * aspectRatio); } if (width > this.maxDimension) { width = this.maxDimension; height = Math.round(width / aspectRatio); } if (height > this.maxDimension) { height = this.maxDimension; width = Math.round(height * aspectRatio); } width = Math.max(64, width); height = Math.max(64, height); return { width, height }; } catch (error) { console.warn('Failed to read image with Jimp:', error.message); return { width: 768, height: 768 }; } } upload = async function (buffer) { try { const upload_id = this.generateSession(); const orig_name = `rynn_${Date.now()}.jpg`; const form = new FormData(); form.append('files', buffer, orig_name); const { data } = await axios.post(`${this.api_url}/upload?upload_id=${upload_id}`, form, { headers: { ...form.getHeaders() }, ...this.agent }); return { orig_name, path: data[0], url: `${this.file_url}${data[0]}` }; } catch (error) { throw new Error(error.message); } } process = async function (buffer) { try { if (!Buffer.isBuffer(buffer)) throw new Error('Image buffer is required'); const dimensions = await this.getOptimalDimensions(buffer); const image_url = await this.upload(buffer); const session_hash = this.generateSession(); const d = await axios.post(`${this.api_url}/queue/join?`, { data: [ 'Ghibli Studio style, Charming hand-drawn anime-style illustration', // Can be adjust { path: image_url.path, url: image_url.url, orig_name: image_url.orig_name, size: buffer.length, mime_type: 'image/jpeg', meta: { _type: 'gradio.FileData' } }, dimensions.height, dimensions.width, 42, 'Ghibli', false, 1 ], event_data: null, fn_index: 1, trigger_id: 19, session_hash: session_hash }, this.agent); const { data } = await axios.get(`${this.api_url}/queue/data?session_hash=${session_hash}`, this.agent); let result; const lines = data.split('\n\n'); for (const line of lines) { if (line.startsWith('data:')) { const d = JSON.parse(line.substring(6)); if (d.msg === 'process_completed') result = d.output.data[0].url; } } return result; } catch (error) { throw new Error(error.message); } } } // Usage: const fs = require('fs'); const g = new ToGhibli(); const resp = await g.process(fs.readFileSync('./image2.jpg')); console.log(resp);