File size: 3,535 Bytes
568802f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | const puppeteer = require('puppeteer');
const fetch = require('node-fetch');
// Logika utama tetap sama, dibungkus dalam fungsi
async function generateImage(prompt, negativePrompt = "low quality") {
const browser = await puppeteer.launch({
headless: true,
executablePath: '/usr/bin/google-chrome-stable', // Path default di linux
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
'--single-process', // Penting untuk menghemat RAM di Space gratisan
'--disable-gpu'
]
});
const page = await browser.newPage();
return new Promise(async (resolve, reject) => {
try {
await page.goto('https://www.croxyproxy.com/', { waitUntil: 'networkidle2' });
await page.type('#url', 'https://nech-c-wainsfwillustrious-v140.hf.space/');
await Promise.all([
page.click('#requestSubmit'),
page.waitForNavigation({ waitUntil: 'networkidle0' }),
]);
await new Promise(r => setTimeout(r, 5000));
const currentUrl = page.url();
const urlObj = new URL(currentUrl);
const proxyBase = `${urlObj.protocol}//${urlObj.host}`;
const cpoToken = urlObj.searchParams.get('__cpo');
const cookies = await page.cookies();
const cookieString = cookies.map(c => `${c.name}=${c.value}`).join('; ');
const session_hash = Math.random().toString(36).substring(2);
const headers = {
"accept": "*/*",
"content-type": "application/json",
"cookie": cookieString,
"user-agent": await page.evaluate(() => navigator.userAgent),
"referer": currentUrl
};
const joinRes = await fetch(`${proxyBase}/gradio_api/queue/join?__cpo=${cpoToken}`, {
method: "POST",
headers,
body: JSON.stringify({
"data": ["v160", prompt, "masterpiece, best quality, " + prompt, negativePrompt, Math.floor(Math.random() * 2000000000), true, 1024, 1024, 6, 30, 1, null, true, null],
"fn_index": 12,
"session_hash": session_hash
})
});
const joinData = await joinRes.json();
if (!joinData.event_id) throw new Error("Gagal Queue");
const response = await fetch(`${proxyBase}/gradio_api/queue/data?session_hash=${session_hash}&__cpo=${cpoToken}`, { headers });
response.body.on('data', (chunk) => {
const dataStr = chunk.toString();
if (dataStr.includes("process_completed")) {
const json = JSON.parse(dataStr.split("data: ")[1].trim());
const gallery = json.output.data.find(item => Array.isArray(item));
const results = gallery?.map(item => {
const pathOnly = (item.image?.url || item.url).split('gradio_api')[1].split('&')[0];
return `https://nech-c-wainsfwillustrious-v140.hf.space/gradio_api${pathOnly}`;
}) || [];
browser.close();
resolve(results);
}
});
} catch (err) {
browser.close();
reject(err);
}
});
}
// Export dengan nama route dan metodenya
module.exports = {
path: '/huggingface2',
method: 'GET',
handler: async (request, reply) => {
const { prompt } = request.query;
if (!prompt) return { error: "Isi prompt di query?prompt=..." };
const images = await generateImage(prompt);
return { status: "success", images };
}
}; |