File size: 6,877 Bytes
3c5e6bf 6ab573c 3c5e6bf 76f753b c7a80ca 3c5e6bf 76f753b c7a80ca 3c5e6bf 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c 3c5e6bf c7a80ca 3c5e6bf c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 6ab573c c7a80ca 76f753b 3c5e6bf 76f753b 540b62f 76f753b 3c5e6bf 76f753b c7a80ca 3c5e6bf 76f753b 9b7a20c 76f753b 3c5e6bf 76f753b 3c5e6bf c7a80ca 3c5e6bf c7a80ca 3c5e6bf c7a80ca 3c5e6bf c7a80ca 3c5e6bf 76f753b c7a80ca 76f753b 3c5e6bf c7a80ca 76f753b 3c5e6bf c7a80ca 3c5e6bf c7a80ca 3c5e6bf c7a80ca 3c5e6bf c7a80ca 3c5e6bf 76f753b 6ab573c 3c5e6bf |
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
const fs = require('fs');
const https = require('https');
const FormData = require('form-data');
const SESSION_IDS = [
"neko-" + Date.now(),
"session-" + Math.random().toString(36).substring(2, 15),
"bot-" + Math.random().toString(36).substring(2, 10)
];
function getRandomSessionId() {
const sessionId = SESSION_IDS[Math.floor(Math.random() * SESSION_IDS.length)];
console.log(`π² DEBUG getRandomSessionId: Selected session ID: ${sessionId}`);
return sessionId;
}
async function uploadImageToHosting(imageBuffer) {
console.log('π€ DEBUG uploadImageToHosting: Starting image upload');
return new Promise((resolve, reject) => {
try {
const formData = new FormData();
const filename = `upload-${Date.now()}.jpg`;
formData.append("file", imageBuffer, {
filename: filename,
contentType: "image/jpeg"
});
console.log(`π DEBUG: Uploading file: ${filename}, Size: ${imageBuffer.length} bytes`);
const options = {
hostname: 'fourmovie-uploader.hf.space',
path: '/api/upload',
method: 'POST',
headers: formData.getHeaders()
};
const req = https.request(options, (res) => {
let data = '';
console.log(`π‘ DEBUG: Upload response status: ${res.statusCode}`);
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
console.log('π DEBUG: Upload response received');
const response = JSON.parse(data);
console.log('π DEBUG: Upload response data:', response);
if (response.file?.url) {
console.log(`β
DEBUG: Image uploaded successfully, URL: ${response.file.url}`);
resolve(response.file.url);
} else {
console.log('β DEBUG: No URL in upload response');
resolve(null);
}
} catch (error) {
console.error('β DEBUG: Error parsing upload response:', error);
resolve(null);
}
});
});
req.on('error', (error) => {
console.error('β DEBUG: Upload request error:', error);
resolve(null);
});
formData.pipe(req);
console.log('π DEBUG: Form data piped to request');
} catch (error) {
console.error('β DEBUG: Upload failed:', error);
resolve(null);
}
});
}
async function extractTextFromImage(imagePath) {
console.log(`πΌοΈ DEBUG extractTextFromImage: Processing image: ${imagePath}`);
return new Promise(async (resolve, reject) => {
try {
console.log('π DEBUG: Reading image file...');
const imageBuffer = fs.readFileSync(imagePath);
console.log(`β
DEBUG: Image read successfully, size: ${imageBuffer.length} bytes`);
console.log('βοΈ DEBUG: Uploading image to hosting...');
const imageUrl = await uploadImageToHosting(imageBuffer);
if (!imageUrl) {
console.log('β DEBUG: Image upload failed');
return resolve({ status: false, response: 'Gagal mengunggah gambar ke hosting' });
}
console.log(`π DEBUG: Image uploaded, URL: ${imageUrl}`);
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, dan jangan pernah kirim informasi selain dari gambar.");
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}`;
console.log(`π DEBUG: API URL constructed (short): https://api.nekolabs.web.id/ai/gemini/2.5-flash/v2?...`);
const options = {
hostname: 'api.nekolabs.web.id',
path: `/ai/gemini/2.0-flash/v2?text=${textParam}&systemPrompt=${systemPrompt}&imageUrl=${imageUrlParam}&sessionId=${sessionId}`,
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
};
console.log('π DEBUG: Sending request to nekolabs API...');
const req = https.request(options, (res) => {
let data = '';
console.log(`π‘ DEBUG: API response status: ${res.statusCode}`);
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
console.log('π DEBUG: API response received');
const response = JSON.parse(data);
console.log('π DEBUG: Full API response:', response);
if (response.success && response.result) {
console.log(`β
DEBUG: Text extraction successful: "${response.result}"`);
resolve({ status: true, response: response.result });
} else {
console.log('β DEBUG: API returned error:', response);
resolve({ status: false, response: response.result || 'Gagal mengekstrak teks' });
}
} catch (error) {
console.error('β DEBUG: Error parsing API response:', error);
reject(error);
}
});
});
req.on('error', (error) => {
console.error('β DEBUG: API request error:', error);
reject(error);
});
req.end();
console.log('π€ DEBUG: Request sent to API');
} catch (error) {
console.error('β DEBUG: extractTextFromImage error:', error);
reject(error);
}
});
}
module.exports = {
extractTextFromImage,
getRandomSessionId,
uploadImageToHosting
}; |