| const fs = require('fs'); |
| const https = require('https'); |
| const FormData = require('form-data'); |
|
|
| |
| const SESSION_IDS = [ |
| "neko", |
| "session2", |
| "session3" |
| ]; |
|
|
| function getRandomSessionId() { |
| return SESSION_IDS[Math.floor(Math.random() * SESSION_IDS.length)]; |
| } |
|
|
| async function uploadImageToHosting(imageBuffer) { |
| return new Promise((resolve, reject) => { |
| try { |
| const formData = new FormData(); |
| formData.append("file", imageBuffer, { |
| filename: `upload-${Date.now()}.jpg`, |
| contentType: "image/jpeg" |
| }); |
|
|
| const options = { |
| hostname: 'fourmovie-uploader.hf.space', |
| path: '/api/upload', |
| method: 'POST', |
| headers: formData.getHeaders() |
| }; |
|
|
| const req = https.request(options, (res) => { |
| let data = ''; |
|
|
| res.on('data', (chunk) => { |
| data += chunk; |
| }); |
|
|
| res.on('end', () => { |
| try { |
| const response = JSON.parse(data); |
| resolve(response.file?.url || null); |
| } catch (error) { |
| console.error('Error parsing upload response:', error); |
| resolve(null); |
| } |
| }); |
| }); |
|
|
| req.on('error', (error) => { |
| console.error('Upload error:', error); |
| resolve(null); |
| }); |
|
|
| formData.pipe(req); |
|
|
| } catch (error) { |
| console.error("Gagal mengunggah gambar:", error); |
| resolve(null); |
| } |
| }); |
| } |
|
|
| async function extractTextFromImage(imagePath) { |
| return new Promise(async (resolve, reject) => { |
| try { |
| |
| const imageBuffer = fs.readFileSync(imagePath); |
| |
| |
| const imageUrl = await uploadImageToHosting(imageBuffer); |
| if (!imageUrl) { |
| return resolve({ status: false, response: 'Gagal mengunggah gambar ke hosting' }); |
| } |
|
|
| |
| const sessionId = getRandomSessionId(); |
|
|
| |
| const textParam = encodeURIComponent("hi how are you"); |
| const systemPrompt = encodeURIComponent("Berikan text yang ada di gambar ini saja, tidak ada informasi lain cukup yang ada di gambar saja, jangan ada text lain kalo bukan dari gambar nya. dan ini adalah pembatas iya _ , : ; dan tolong tiap text berbeda beda kalo ada pembatas itu kirim satu kalimat saja"); |
| const imageUrlParam = encodeURIComponent(imageUrl); |
|
|
| |
| const apiUrl = `https://api.nekolabs.web.id/ai/gemini/2.5-flash/v2?text=${textParam}&systemPrompt=${systemPrompt}&imageUrl=${imageUrlParam}&sessionId=${sessionId}`; |
|
|
| const options = { |
| hostname: 'api.nekolabs.web.id', |
| path: `/ai/gemini/2.5-flash/v2?text=${textParam}&systemPrompt=${systemPrompt}&imageUrl=${imageUrlParam}&sessionId=${sessionId}`, |
| method: 'GET', |
| headers: { |
| 'Accept': 'application/json', |
| 'Content-Type': 'application/json' |
| } |
| }; |
|
|
| const req = https.request(options, (res) => { |
| let data = ''; |
|
|
| res.on('data', (chunk) => { |
| data += chunk; |
| }); |
|
|
| res.on('end', () => { |
| try { |
| const response = JSON.parse(data); |
| |
| if (response.success && response.result) { |
| resolve({ status: true, response: response.result }); |
| } else { |
| console.error('API Response error:', response); |
| resolve({ status: false, response: response.result || 'Gagal mengekstrak teks' }); |
| } |
| } catch (error) { |
| console.error('Error parsing response:', error); |
| reject(error); |
| } |
| }); |
| }); |
|
|
| req.on('error', (error) => { |
| console.error('Request error:', error); |
| reject(error); |
| }); |
|
|
| req.end(); |
|
|
| } catch (error) { |
| reject(error); |
| } |
| }); |
| } |
|
|
| |
| async function main() { |
| try { |
| const result = await extractTextFromImage('main_instruction.png'); |
| if (result.status) { |
| console.log('Teks yang diekstrak:', result.response); |
| } else { |
| console.log('Gagal:', result.response); |
| } |
| } catch (error) { |
| console.error('Error:', error); |
| } |
| } |
|
|
| |
| if (require.main === module) { |
| main(); |
| } |
|
|
| module.exports = { |
| extractTextFromImage, |
| getRandomSessionId, |
| uploadImageToHosting |
| }; |