Spaces:
Sleeping
Sleeping
Update endpoints/imageProcessor
Browse files- endpoints/imageProcessor +37 -0
endpoints/imageProcessor
CHANGED
|
@@ -2,8 +2,12 @@ const fs = require('fs');
|
|
| 2 |
const https = require('https');
|
| 3 |
|
| 4 |
async function extractTextFromImage(imageBuffer) {
|
|
|
|
|
|
|
|
|
|
| 5 |
return new Promise((resolve, reject) => {
|
| 6 |
const base64Image = imageBuffer.toString('base64');
|
|
|
|
| 7 |
|
| 8 |
const requestData = JSON.stringify({
|
| 9 |
"contents": [
|
|
@@ -23,6 +27,8 @@ async function extractTextFromImage(imageBuffer) {
|
|
| 23 |
]
|
| 24 |
});
|
| 25 |
|
|
|
|
|
|
|
| 26 |
const options = {
|
| 27 |
hostname: 'generativelanguage.googleapis.com',
|
| 28 |
path: '/v1beta/models/gemini-2.5-flash-lite:generateContent',
|
|
@@ -34,7 +40,12 @@ async function extractTextFromImage(imageBuffer) {
|
|
| 34 |
}
|
| 35 |
};
|
| 36 |
|
|
|
|
|
|
|
| 37 |
const req = https.request(options, (res) => {
|
|
|
|
|
|
|
|
|
|
| 38 |
let data = '';
|
| 39 |
|
| 40 |
res.on('data', (chunk) => {
|
|
@@ -42,26 +53,52 @@ async function extractTextFromImage(imageBuffer) {
|
|
| 42 |
});
|
| 43 |
|
| 44 |
res.on('end', () => {
|
|
|
|
|
|
|
|
|
|
| 45 |
try {
|
| 46 |
const response = JSON.parse(data);
|
|
|
|
|
|
|
| 47 |
if (response.candidates && response.candidates[0] && response.candidates[0].content) {
|
| 48 |
const text = response.candidates[0].content.parts[0].text;
|
|
|
|
| 49 |
resolve({ status: true, response: text });
|
| 50 |
} else {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
resolve({ status: false, response: 'Tidak ada teks yang ditemukan' });
|
| 52 |
}
|
| 53 |
} catch (error) {
|
|
|
|
|
|
|
| 54 |
reject(error);
|
| 55 |
}
|
| 56 |
});
|
| 57 |
});
|
| 58 |
|
| 59 |
req.on('error', (error) => {
|
|
|
|
| 60 |
reject(error);
|
| 61 |
});
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
req.write(requestData);
|
| 64 |
req.end();
|
|
|
|
| 65 |
});
|
| 66 |
}
|
| 67 |
|
|
|
|
| 2 |
const https = require('https');
|
| 3 |
|
| 4 |
async function extractTextFromImage(imageBuffer) {
|
| 5 |
+
console.log('[DEBUG] extractTextFromImage: Starting image text extraction');
|
| 6 |
+
console.log('[DEBUG] Image buffer size:', imageBuffer.length, 'bytes');
|
| 7 |
+
|
| 8 |
return new Promise((resolve, reject) => {
|
| 9 |
const base64Image = imageBuffer.toString('base64');
|
| 10 |
+
console.log('[DEBUG] Base64 image length:', base64Image.length);
|
| 11 |
|
| 12 |
const requestData = JSON.stringify({
|
| 13 |
"contents": [
|
|
|
|
| 27 |
]
|
| 28 |
});
|
| 29 |
|
| 30 |
+
console.log('[DEBUG] Request data prepared, length:', requestData.length);
|
| 31 |
+
|
| 32 |
const options = {
|
| 33 |
hostname: 'generativelanguage.googleapis.com',
|
| 34 |
path: '/v1beta/models/gemini-2.5-flash-lite:generateContent',
|
|
|
|
| 40 |
}
|
| 41 |
};
|
| 42 |
|
| 43 |
+
console.log('[DEBUG] Making request to Gemini API...');
|
| 44 |
+
|
| 45 |
const req = https.request(options, (res) => {
|
| 46 |
+
console.log('[DEBUG] API Response status:', res.statusCode);
|
| 47 |
+
console.log('[DEBUG] API Response headers:', res.headers);
|
| 48 |
+
|
| 49 |
let data = '';
|
| 50 |
|
| 51 |
res.on('data', (chunk) => {
|
|
|
|
| 53 |
});
|
| 54 |
|
| 55 |
res.on('end', () => {
|
| 56 |
+
console.log('[DEBUG] Received all response data');
|
| 57 |
+
console.log('[DEBUG] Response data length:', data.length);
|
| 58 |
+
|
| 59 |
try {
|
| 60 |
const response = JSON.parse(data);
|
| 61 |
+
console.log('[DEBUG] Response parsed successfully');
|
| 62 |
+
|
| 63 |
if (response.candidates && response.candidates[0] && response.candidates[0].content) {
|
| 64 |
const text = response.candidates[0].content.parts[0].text;
|
| 65 |
+
console.log('[DEBUG] ✅ Text extracted:', text);
|
| 66 |
resolve({ status: true, response: text });
|
| 67 |
} else {
|
| 68 |
+
console.log('[DEBUG] ❌ No text found in response');
|
| 69 |
+
console.log('[DEBUG] Response structure:', JSON.stringify(response, null, 2));
|
| 70 |
+
|
| 71 |
+
if (response.error) {
|
| 72 |
+
console.log('[DEBUG] API Error:', response.error);
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
resolve({ status: false, response: 'Tidak ada teks yang ditemukan' });
|
| 76 |
}
|
| 77 |
} catch (error) {
|
| 78 |
+
console.error('[DEBUG] ❌ JSON parse error:', error);
|
| 79 |
+
console.log('[DEBUG] Raw response data:', data);
|
| 80 |
reject(error);
|
| 81 |
}
|
| 82 |
});
|
| 83 |
});
|
| 84 |
|
| 85 |
req.on('error', (error) => {
|
| 86 |
+
console.error('[DEBUG] ❌ Request error:', error);
|
| 87 |
reject(error);
|
| 88 |
});
|
| 89 |
|
| 90 |
+
req.on('timeout', () => {
|
| 91 |
+
console.error('[DEBUG] ❌ Request timeout');
|
| 92 |
+
req.destroy();
|
| 93 |
+
reject(new Error('Request timeout'));
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
req.setTimeout(30000); // 30 second timeout
|
| 97 |
+
|
| 98 |
+
console.log('[DEBUG] Sending request...');
|
| 99 |
req.write(requestData);
|
| 100 |
req.end();
|
| 101 |
+
console.log('[DEBUG] Request sent');
|
| 102 |
});
|
| 103 |
}
|
| 104 |
|