File size: 5,203 Bytes
3c5e6bf
 
6ab573c
3c5e6bf
76f753b
 
 
 
 
3c5e6bf
 
76f753b
 
3c5e6bf
 
6ab573c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c5e6bf
 
 
6ab573c
 
 
 
 
 
 
 
 
76f753b
 
3c5e6bf
76f753b
 
 
 
3c5e6bf
76f753b
 
3c5e6bf
 
76f753b
 
 
3c5e6bf
 
76f753b
3c5e6bf
 
 
 
 
 
 
 
 
 
 
 
 
 
76f753b
 
3c5e6bf
76f753b
 
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
const fs = require('fs');
const https = require('https');
const FormData = require('form-data');

// Session IDs configuration
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 {
            // Baca file gambar
            const imageBuffer = fs.readFileSync(imagePath);
            
            // Upload gambar ke hosting terlebih dahulu
            const imageUrl = await uploadImageToHosting(imageBuffer);
            if (!imageUrl) {
                return resolve({ status: false, response: 'Gagal mengunggah gambar ke hosting' });
            }

            // Dapatkan session ID secara random
            const sessionId = getRandomSessionId();

            // Encode parameter untuk URL
            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);

            // Buat URL API
            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);
        }
    });
}

// Contoh penggunaan
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);
    }
}

// Jalankan jika file dijalankan langsung
if (require.main === module) {
    main();
}

module.exports = { 
    extractTextFromImage,
    getRandomSessionId,
    uploadImageToHosting
};