Update endpoints/imageProcessor
Browse files- endpoints/imageProcessor +28 -108
endpoints/imageProcessor
CHANGED
|
@@ -1,59 +1,41 @@
|
|
| 1 |
const fs = require('fs');
|
| 2 |
const https = require('https');
|
| 3 |
|
| 4 |
-
//
|
| 5 |
-
const
|
| 6 |
-
"
|
| 7 |
-
"
|
|
|
|
| 8 |
];
|
| 9 |
|
| 10 |
-
function
|
| 11 |
-
return
|
| 12 |
}
|
| 13 |
|
| 14 |
async function extractTextFromImage(imagePath) {
|
| 15 |
return new Promise(async (resolve, reject) => {
|
| 16 |
try {
|
| 17 |
-
//
|
| 18 |
-
const
|
| 19 |
-
const base64Image = imageBuffer.toString('base64');
|
| 20 |
|
| 21 |
-
//
|
| 22 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
//
|
| 25 |
-
const
|
| 26 |
-
model: "accounts/fireworks/models/llama4-maverick-instruct-basic",
|
| 27 |
-
max_tokens: 1024,
|
| 28 |
-
temperature: 0.1, // Temperature rendah untuk hasil yang lebih konsisten
|
| 29 |
-
messages: [
|
| 30 |
-
{
|
| 31 |
-
role: "user",
|
| 32 |
-
content: [
|
| 33 |
-
{
|
| 34 |
-
type: "text",
|
| 35 |
-
text: "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"
|
| 36 |
-
},
|
| 37 |
-
{
|
| 38 |
-
type: "image_url",
|
| 39 |
-
image_url: {
|
| 40 |
-
url: `data:image/png;base64,${base64Image}`
|
| 41 |
-
}
|
| 42 |
-
}
|
| 43 |
-
]
|
| 44 |
-
}
|
| 45 |
-
]
|
| 46 |
-
});
|
| 47 |
|
| 48 |
const options = {
|
| 49 |
-
hostname: 'api.
|
| 50 |
-
path:
|
| 51 |
-
method: '
|
| 52 |
headers: {
|
| 53 |
'Accept': 'application/json',
|
| 54 |
-
'Content-Type': 'application/json'
|
| 55 |
-
'Authorization': `Bearer ${apiKey}`,
|
| 56 |
-
'Content-Length': Buffer.byteLength(requestData)
|
| 57 |
}
|
| 58 |
};
|
| 59 |
|
|
@@ -68,12 +50,11 @@ async function extractTextFromImage(imagePath) {
|
|
| 68 |
try {
|
| 69 |
const response = JSON.parse(data);
|
| 70 |
|
| 71 |
-
if (response.
|
| 72 |
-
|
| 73 |
-
resolve({ status: true, response: text });
|
| 74 |
} else {
|
| 75 |
-
console.error('API Response
|
| 76 |
-
resolve({ status: false, response:
|
| 77 |
}
|
| 78 |
} catch (error) {
|
| 79 |
console.error('Error parsing response:', error);
|
|
@@ -87,7 +68,6 @@ async function extractTextFromImage(imagePath) {
|
|
| 87 |
reject(error);
|
| 88 |
});
|
| 89 |
|
| 90 |
-
req.write(requestData);
|
| 91 |
req.end();
|
| 92 |
|
| 93 |
} catch (error) {
|
|
@@ -96,64 +76,6 @@ async function extractTextFromImage(imagePath) {
|
|
| 96 |
});
|
| 97 |
}
|
| 98 |
|
| 99 |
-
// Fungsi untuk upload gambar ke hosting (opsional)
|
| 100 |
-
async function uploadImageToHosting(buffer) {
|
| 101 |
-
try {
|
| 102 |
-
// Karena kita menggunakan base64 langsung, fungsi ini mungkin tidak diperlukan
|
| 103 |
-
// Tapi tetap disediakan untuk kompatibilitas
|
| 104 |
-
return null;
|
| 105 |
-
} catch (error) {
|
| 106 |
-
console.error("Gagal mengunggah gambar:", error);
|
| 107 |
-
return null;
|
| 108 |
-
}
|
| 109 |
-
}
|
| 110 |
-
|
| 111 |
-
// Fungsi untuk analisis gambar yang lebih detail
|
| 112 |
-
async function analyzeImage(imagePath) {
|
| 113 |
-
try {
|
| 114 |
-
const imageBuffer = fs.readFileSync(imagePath);
|
| 115 |
-
const base64Image = imageBuffer.toString('base64');
|
| 116 |
-
const apiKey = getRandomApiKey();
|
| 117 |
-
|
| 118 |
-
const payload = {
|
| 119 |
-
model: "accounts/fireworks/models/llama4-maverick-instruct-basic",
|
| 120 |
-
max_tokens: 1024,
|
| 121 |
-
temperature: 0.6,
|
| 122 |
-
messages: [
|
| 123 |
-
{
|
| 124 |
-
role: "user",
|
| 125 |
-
content: [
|
| 126 |
-
{ type: "text", text: "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" },
|
| 127 |
-
{ type: "image_url", image_url: { url: `data:image/jpeg;base64,${base64Image}` } }
|
| 128 |
-
]
|
| 129 |
-
}
|
| 130 |
-
]
|
| 131 |
-
};
|
| 132 |
-
|
| 133 |
-
const response = await fetch("https://api.fireworks.ai/inference/v1/chat/completions", {
|
| 134 |
-
method: "POST",
|
| 135 |
-
headers: {
|
| 136 |
-
"Accept": "application/json",
|
| 137 |
-
"Content-Type": "application/json",
|
| 138 |
-
"Authorization": `Bearer ${apiKey}`
|
| 139 |
-
},
|
| 140 |
-
body: JSON.stringify(payload)
|
| 141 |
-
});
|
| 142 |
-
|
| 143 |
-
if (!response.ok) {
|
| 144 |
-
const errorText = await response.text();
|
| 145 |
-
console.error("API Error response:", errorText);
|
| 146 |
-
throw new Error(`HTTP error! status: ${response.status}`);
|
| 147 |
-
}
|
| 148 |
-
|
| 149 |
-
const data = await response.json();
|
| 150 |
-
return data.choices?.[0]?.message?.content || "Tidak bisa menganalisis gambar ini";
|
| 151 |
-
} catch (error) {
|
| 152 |
-
console.error("Error analyzing image:", error);
|
| 153 |
-
return "Error saat menganalisis gambar";
|
| 154 |
-
}
|
| 155 |
-
}
|
| 156 |
-
|
| 157 |
// Contoh penggunaan
|
| 158 |
async function main() {
|
| 159 |
try {
|
|
@@ -174,8 +96,6 @@ if (require.main === module) {
|
|
| 174 |
}
|
| 175 |
|
| 176 |
module.exports = {
|
| 177 |
-
extractTextFromImage,
|
| 178 |
-
|
| 179 |
-
analyzeImage,
|
| 180 |
-
getRandomApiKey
|
| 181 |
};
|
|
|
|
| 1 |
const fs = require('fs');
|
| 2 |
const https = require('https');
|
| 3 |
|
| 4 |
+
// Session IDs configuration
|
| 5 |
+
const SESSION_IDS = [
|
| 6 |
+
"neko",
|
| 7 |
+
"session2",
|
| 8 |
+
"session3"
|
| 9 |
];
|
| 10 |
|
| 11 |
+
function getRandomSessionId() {
|
| 12 |
+
return SESSION_IDS[Math.floor(Math.random() * SESSION_IDS.length)];
|
| 13 |
}
|
| 14 |
|
| 15 |
async function extractTextFromImage(imagePath) {
|
| 16 |
return new Promise(async (resolve, reject) => {
|
| 17 |
try {
|
| 18 |
+
// Dapatkan session ID secara random
|
| 19 |
+
const sessionId = getRandomSessionId();
|
|
|
|
| 20 |
|
| 21 |
+
// Encode parameter untuk URL
|
| 22 |
+
const textParam = encodeURIComponent("hi how are you");
|
| 23 |
+
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");
|
| 24 |
+
|
| 25 |
+
// Untuk testing, gunakan URL gambar yang sudah ada
|
| 26 |
+
const imageUrl = "https://cdn.yupra.my.id/yp/gikn29ti.jpg";
|
| 27 |
+
const imageUrlParam = encodeURIComponent(imageUrl);
|
| 28 |
|
| 29 |
+
// Buat URL API
|
| 30 |
+
const apiUrl = `https://api.nekolabs.web.id/ai/gemini/2.5-flash/v2?text=${textParam}&systemPrompt=${systemPrompt}&imageUrl=${imageUrlParam}&sessionId=${sessionId}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
const options = {
|
| 33 |
+
hostname: 'api.nekolabs.web.id',
|
| 34 |
+
path: `/ai/gemini/2.5-flash/v2?text=${textParam}&systemPrompt=${systemPrompt}&imageUrl=${imageUrlParam}&sessionId=${sessionId}`,
|
| 35 |
+
method: 'GET',
|
| 36 |
headers: {
|
| 37 |
'Accept': 'application/json',
|
| 38 |
+
'Content-Type': 'application/json'
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
};
|
| 41 |
|
|
|
|
| 50 |
try {
|
| 51 |
const response = JSON.parse(data);
|
| 52 |
|
| 53 |
+
if (response.success && response.result) {
|
| 54 |
+
resolve({ status: true, response: response.result });
|
|
|
|
| 55 |
} else {
|
| 56 |
+
console.error('API Response error:', response);
|
| 57 |
+
resolve({ status: false, response: response.result || 'Gagal mengekstrak teks' });
|
| 58 |
}
|
| 59 |
} catch (error) {
|
| 60 |
console.error('Error parsing response:', error);
|
|
|
|
| 68 |
reject(error);
|
| 69 |
});
|
| 70 |
|
|
|
|
| 71 |
req.end();
|
| 72 |
|
| 73 |
} catch (error) {
|
|
|
|
| 76 |
});
|
| 77 |
}
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
// Contoh penggunaan
|
| 80 |
async function main() {
|
| 81 |
try {
|
|
|
|
| 96 |
}
|
| 97 |
|
| 98 |
module.exports = {
|
| 99 |
+
extractTextFromImage,
|
| 100 |
+
getRandomSessionId
|
|
|
|
|
|
|
| 101 |
};
|