Update endpoints/imageProcessor
Browse files- endpoints/imageProcessor +60 -4
endpoints/imageProcessor
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
const fs = require('fs');
|
| 2 |
const https = require('https');
|
|
|
|
| 3 |
|
| 4 |
// Session IDs configuration
|
| 5 |
const SESSION_IDS = [
|
|
@@ -12,18 +13,72 @@ 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
|
|
@@ -97,5 +152,6 @@ if (require.main === module) {
|
|
| 97 |
|
| 98 |
module.exports = {
|
| 99 |
extractTextFromImage,
|
| 100 |
-
getRandomSessionId
|
|
|
|
| 101 |
};
|
|
|
|
| 1 |
const fs = require('fs');
|
| 2 |
const https = require('https');
|
| 3 |
+
const FormData = require('form-data');
|
| 4 |
|
| 5 |
// Session IDs configuration
|
| 6 |
const SESSION_IDS = [
|
|
|
|
| 13 |
return SESSION_IDS[Math.floor(Math.random() * SESSION_IDS.length)];
|
| 14 |
}
|
| 15 |
|
| 16 |
+
async function uploadImageToHosting(imageBuffer) {
|
| 17 |
+
return new Promise((resolve, reject) => {
|
| 18 |
+
try {
|
| 19 |
+
const formData = new FormData();
|
| 20 |
+
formData.append("file", imageBuffer, {
|
| 21 |
+
filename: `upload-${Date.now()}.jpg`,
|
| 22 |
+
contentType: "image/jpeg"
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
const options = {
|
| 26 |
+
hostname: 'fourmovie-uploader.hf.space',
|
| 27 |
+
path: '/api/upload',
|
| 28 |
+
method: 'POST',
|
| 29 |
+
headers: formData.getHeaders()
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
const req = https.request(options, (res) => {
|
| 33 |
+
let data = '';
|
| 34 |
+
|
| 35 |
+
res.on('data', (chunk) => {
|
| 36 |
+
data += chunk;
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
res.on('end', () => {
|
| 40 |
+
try {
|
| 41 |
+
const response = JSON.parse(data);
|
| 42 |
+
resolve(response.file?.url || null);
|
| 43 |
+
} catch (error) {
|
| 44 |
+
console.error('Error parsing upload response:', error);
|
| 45 |
+
resolve(null);
|
| 46 |
+
}
|
| 47 |
+
});
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
req.on('error', (error) => {
|
| 51 |
+
console.error('Upload error:', error);
|
| 52 |
+
resolve(null);
|
| 53 |
+
});
|
| 54 |
+
|
| 55 |
+
formData.pipe(req);
|
| 56 |
+
|
| 57 |
+
} catch (error) {
|
| 58 |
+
console.error("Gagal mengunggah gambar:", error);
|
| 59 |
+
resolve(null);
|
| 60 |
+
}
|
| 61 |
+
});
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
async function extractTextFromImage(imagePath) {
|
| 65 |
return new Promise(async (resolve, reject) => {
|
| 66 |
try {
|
| 67 |
+
// Baca file gambar
|
| 68 |
+
const imageBuffer = fs.readFileSync(imagePath);
|
| 69 |
+
|
| 70 |
+
// Upload gambar ke hosting terlebih dahulu
|
| 71 |
+
const imageUrl = await uploadImageToHosting(imageBuffer);
|
| 72 |
+
if (!imageUrl) {
|
| 73 |
+
return resolve({ status: false, response: 'Gagal mengunggah gambar ke hosting' });
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
// Dapatkan session ID secara random
|
| 77 |
const sessionId = getRandomSessionId();
|
| 78 |
|
| 79 |
// Encode parameter untuk URL
|
| 80 |
const textParam = encodeURIComponent("hi how are you");
|
| 81 |
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");
|
|
|
|
|
|
|
|
|
|
| 82 |
const imageUrlParam = encodeURIComponent(imageUrl);
|
| 83 |
|
| 84 |
// Buat URL API
|
|
|
|
| 152 |
|
| 153 |
module.exports = {
|
| 154 |
extractTextFromImage,
|
| 155 |
+
getRandomSessionId,
|
| 156 |
+
uploadImageToHosting
|
| 157 |
};
|