Fourstore commited on
Commit
c7a80ca
Β·
verified Β·
1 Parent(s): 8265a02

Update endpoints/imageProcessor

Browse files
Files changed (1) hide show
  1. endpoints/imageProcessor +49 -23
endpoints/imageProcessor CHANGED
@@ -2,26 +2,31 @@ const fs = require('fs');
2
  const https = require('https');
3
  const FormData = require('form-data');
4
 
5
- // Session IDs configuration
6
  const SESSION_IDS = [
7
- "neko",
8
- "session2",
9
- "session3"
10
  ];
11
 
12
  function getRandomSessionId() {
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',
@@ -32,57 +37,71 @@ async function uploadImageToHosting(imageBuffer) {
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
85
  const apiUrl = `https://api.nekolabs.web.id/ai/gemini/2.5-flash/v2?text=${textParam}&systemPrompt=${systemPrompt}&imageUrl=${imageUrlParam}&sessionId=${sessionId}`;
 
86
 
87
  const options = {
88
  hostname: 'api.nekolabs.web.id',
@@ -94,58 +113,65 @@ async function extractTextFromImage(imagePath) {
94
  }
95
  };
96
 
 
97
  const req = https.request(options, (res) => {
98
  let data = '';
99
 
 
 
100
  res.on('data', (chunk) => {
101
  data += chunk;
102
  });
103
 
104
  res.on('end', () => {
105
  try {
 
106
  const response = JSON.parse(data);
 
107
 
108
  if (response.success && response.result) {
 
109
  resolve({ status: true, response: response.result });
110
  } else {
111
- console.error('API Response error:', response);
112
  resolve({ status: false, response: response.result || 'Gagal mengekstrak teks' });
113
  }
114
  } catch (error) {
115
- console.error('Error parsing response:', error);
116
  reject(error);
117
  }
118
  });
119
  });
120
 
121
  req.on('error', (error) => {
122
- console.error('Request error:', error);
123
  reject(error);
124
  });
125
 
126
  req.end();
 
127
 
128
  } catch (error) {
 
129
  reject(error);
130
  }
131
  });
132
  }
133
 
134
- // Contoh penggunaan
135
  async function main() {
 
136
  try {
137
  const result = await extractTextFromImage('main_instruction.png');
138
  if (result.status) {
139
- console.log('Teks yang diekstrak:', result.response);
140
  } else {
141
- console.log('Gagal:', result.response);
142
  }
143
  } catch (error) {
144
- console.error('Error:', error);
145
  }
146
  }
147
 
148
- // Jalankan jika file dijalankan langsung
149
  if (require.main === module) {
150
  main();
151
  }
 
2
  const https = require('https');
3
  const FormData = require('form-data');
4
 
 
5
  const SESSION_IDS = [
6
+ "neko-" + Date.now(),
7
+ "session-" + Math.random().toString(36).substring(2, 15),
8
+ "bot-" + Math.random().toString(36).substring(2, 10)
9
  ];
10
 
11
  function getRandomSessionId() {
12
+ const sessionId = SESSION_IDS[Math.floor(Math.random() * SESSION_IDS.length)];
13
+ console.log(`🎲 DEBUG getRandomSessionId: Selected session ID: ${sessionId}`);
14
+ return sessionId;
15
  }
16
 
17
  async function uploadImageToHosting(imageBuffer) {
18
+ console.log('πŸ“€ DEBUG uploadImageToHosting: Starting image upload');
19
  return new Promise((resolve, reject) => {
20
  try {
21
  const formData = new FormData();
22
+ const filename = `upload-${Date.now()}.jpg`;
23
  formData.append("file", imageBuffer, {
24
+ filename: filename,
25
  contentType: "image/jpeg"
26
  });
27
 
28
+ console.log(`πŸ“ DEBUG: Uploading file: ${filename}, Size: ${imageBuffer.length} bytes`);
29
+
30
  const options = {
31
  hostname: 'fourmovie-uploader.hf.space',
32
  path: '/api/upload',
 
37
  const req = https.request(options, (res) => {
38
  let data = '';
39
 
40
+ console.log(`πŸ“‘ DEBUG: Upload response status: ${res.statusCode}`);
41
+
42
  res.on('data', (chunk) => {
43
  data += chunk;
44
  });
45
 
46
  res.on('end', () => {
47
  try {
48
+ console.log('πŸ“„ DEBUG: Upload response received');
49
  const response = JSON.parse(data);
50
+ console.log('πŸ”— DEBUG: Upload response data:', response);
51
+
52
+ if (response.file?.url) {
53
+ console.log(`βœ… DEBUG: Image uploaded successfully, URL: ${response.file.url}`);
54
+ resolve(response.file.url);
55
+ } else {
56
+ console.log('❌ DEBUG: No URL in upload response');
57
+ resolve(null);
58
+ }
59
  } catch (error) {
60
+ console.error('❌ DEBUG: Error parsing upload response:', error);
61
  resolve(null);
62
  }
63
  });
64
  });
65
 
66
  req.on('error', (error) => {
67
+ console.error('❌ DEBUG: Upload request error:', error);
68
  resolve(null);
69
  });
70
 
71
  formData.pipe(req);
72
+ console.log('πŸ”„ DEBUG: Form data piped to request');
73
 
74
  } catch (error) {
75
+ console.error('❌ DEBUG: Upload failed:', error);
76
  resolve(null);
77
  }
78
  });
79
  }
80
 
81
  async function extractTextFromImage(imagePath) {
82
+ console.log(`πŸ–ΌοΈ DEBUG extractTextFromImage: Processing image: ${imagePath}`);
83
  return new Promise(async (resolve, reject) => {
84
  try {
85
+ console.log('πŸ“– DEBUG: Reading image file...');
86
  const imageBuffer = fs.readFileSync(imagePath);
87
+ console.log(`βœ… DEBUG: Image read successfully, size: ${imageBuffer.length} bytes`);
88
 
89
+ console.log('☁️ DEBUG: Uploading image to hosting...');
90
  const imageUrl = await uploadImageToHosting(imageBuffer);
91
  if (!imageUrl) {
92
+ console.log('❌ DEBUG: Image upload failed');
93
  return resolve({ status: false, response: 'Gagal mengunggah gambar ke hosting' });
94
  }
95
 
96
+ console.log(`πŸ”— DEBUG: Image uploaded, URL: ${imageUrl}`);
97
  const sessionId = getRandomSessionId();
98
 
 
99
  const textParam = encodeURIComponent("hi how are you");
100
  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");
101
  const imageUrlParam = encodeURIComponent(imageUrl);
102
 
 
103
  const apiUrl = `https://api.nekolabs.web.id/ai/gemini/2.5-flash/v2?text=${textParam}&systemPrompt=${systemPrompt}&imageUrl=${imageUrlParam}&sessionId=${sessionId}`;
104
+ console.log(`🌐 DEBUG: API URL constructed (short): https://api.nekolabs.web.id/ai/gemini/2.5-flash/v2?...`);
105
 
106
  const options = {
107
  hostname: 'api.nekolabs.web.id',
 
113
  }
114
  };
115
 
116
+ console.log('πŸš€ DEBUG: Sending request to nekolabs API...');
117
  const req = https.request(options, (res) => {
118
  let data = '';
119
 
120
+ console.log(`πŸ“‘ DEBUG: API response status: ${res.statusCode}`);
121
+
122
  res.on('data', (chunk) => {
123
  data += chunk;
124
  });
125
 
126
  res.on('end', () => {
127
  try {
128
+ console.log('πŸ“„ DEBUG: API response received');
129
  const response = JSON.parse(data);
130
+ console.log('πŸ” DEBUG: Full API response:', response);
131
 
132
  if (response.success && response.result) {
133
+ console.log(`βœ… DEBUG: Text extraction successful: "${response.result}"`);
134
  resolve({ status: true, response: response.result });
135
  } else {
136
+ console.log('❌ DEBUG: API returned error:', response);
137
  resolve({ status: false, response: response.result || 'Gagal mengekstrak teks' });
138
  }
139
  } catch (error) {
140
+ console.error('❌ DEBUG: Error parsing API response:', error);
141
  reject(error);
142
  }
143
  });
144
  });
145
 
146
  req.on('error', (error) => {
147
+ console.error('❌ DEBUG: API request error:', error);
148
  reject(error);
149
  });
150
 
151
  req.end();
152
+ console.log('πŸ“€ DEBUG: Request sent to API');
153
 
154
  } catch (error) {
155
+ console.error('❌ DEBUG: extractTextFromImage error:', error);
156
  reject(error);
157
  }
158
  });
159
  }
160
 
 
161
  async function main() {
162
+ console.log('🏁 DEBUG: Starting main function');
163
  try {
164
  const result = await extractTextFromImage('main_instruction.png');
165
  if (result.status) {
166
+ console.log('πŸŽ‰ FINAL RESULT - Teks yang diekstrak:', result.response);
167
  } else {
168
+ console.log('πŸ’₯ FINAL RESULT - Gagal:', result.response);
169
  }
170
  } catch (error) {
171
+ console.error('πŸ’₯ FINAL ERROR:', error);
172
  }
173
  }
174
 
 
175
  if (require.main === module) {
176
  main();
177
  }