Download plugins/huggingface2.js from CieLZ/apis: direct link, hf CLI and curl.
- Browser
- Download file 3.54 kB
-
https://huggingface.co/spaces/CieLZ/apis/resolve/main/plugins/huggingface2.js
- Command line
-
hf download hf://spaces/CieLZ/apis/plugins/huggingface2.js
-
curl -L -o huggingface2.js https://huggingface.co/spaces/CieLZ/apis/resolve/main/plugins/huggingface2.js
3.54 kB
| 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 }; | |
| } | |
| }; |