Fourstore commited on
Commit
76f753b
·
verified ·
1 Parent(s): 3c5e6bf

Update endpoints/imageProcessor

Browse files
Files changed (1) hide show
  1. endpoints/imageProcessor +28 -108
endpoints/imageProcessor CHANGED
@@ -1,59 +1,41 @@
1
  const fs = require('fs');
2
  const https = require('https');
3
 
4
- // API Keys configuration
5
- const API_KEYS = [
6
- "fw_3ZNhzPGSUhHBxTL5PuB5Cpkv",
7
- "fw_3ZchAJSfZBupby3hnESzSkbk"
 
8
  ];
9
 
10
- function getRandomApiKey() {
11
- return API_KEYS[Math.floor(Math.random() * API_KEYS.length)];
12
  }
13
 
14
  async function extractTextFromImage(imagePath) {
15
  return new Promise(async (resolve, reject) => {
16
  try {
17
- // Baca file gambar dan encode ke base64
18
- const imageBuffer = fs.readFileSync(imagePath);
19
- const base64Image = imageBuffer.toString('base64');
20
 
21
- // Dapatkan API key secara random
22
- const apiKey = getRandomApiKey();
 
 
 
 
 
23
 
24
- // Request payload untuk Fireworks.ai
25
- const requestData = JSON.stringify({
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.fireworks.ai',
50
- path: '/inference/v1/chat/completions',
51
- method: 'POST',
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.choices && response.choices[0] && response.choices[0].message) {
72
- const text = response.choices[0].message.content;
73
- resolve({ status: true, response: text });
74
  } else {
75
- console.error('API Response structure unexpected:', response);
76
- resolve({ status: false, response: 'Struktur respons tidak sesuai' });
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
- uploadImageToHosting,
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
  };