Fourstore commited on
Commit
7af47a9
·
verified ·
1 Parent(s): 32bdc2d

Update endpoints/antibot.js

Browse files
Files changed (1) hide show
  1. endpoints/antibot.js +48 -168
endpoints/antibot.js CHANGED
@@ -1,184 +1,64 @@
1
- const fs = require("fs");
2
- const https = require("https");
3
- const path = require("path");
4
- const sharp = require("sharp");
5
 
6
- const buf = b => Buffer.from(b.replace(/^data:image\/\w+;base64,/, ""), "base64");
7
-
8
- // ==========================
9
- // AI GEMINI OCR (Original tanpa perubahan)
10
- // ==========================
11
- async function aiOcr(imageBuffer) {
12
- return new Promise((resolve, reject) => {
13
- const base64Image = imageBuffer.toString('base64');
14
-
15
- const requestData = JSON.stringify({
16
- "contents": [
17
- {
18
- "parts": [
19
- {
20
- "inline_data": {
21
- "mime_type": "image/png",
22
- "data": base64Image
23
- }
24
- },
25
- {
26
- "text": "Berikan text yang ada di gambar ini saja, tidak ada informasi lain cukup yang ada di gambar saja"
27
- }
28
- ]
29
- }
30
- ]
31
- });
32
-
33
- const options = {
34
- hostname: 'generativelanguage.googleapis.com',
35
- path: '/v1beta/models/gemini-2.5-flash-lite:generateContent',
36
- method: 'POST',
37
- headers: {
38
- 'x-goog-api-key': 'AIzaSyBvQUzfkfgmdxMHmOVFEXdQq3DFi4siRVE',
39
- 'Content-Type': 'application/json',
40
- 'Content-Length': Buffer.byteLength(requestData)
41
- }
42
- };
43
-
44
- const req = https.request(options, (res) => {
45
- let data = '';
46
-
47
- res.on('data', (chunk) => {
48
- data += chunk;
49
- });
50
-
51
- res.on('end', () => {
52
- try {
53
- const response = JSON.parse(data);
54
- if (response.candidates && response.candidates[0] && response.candidates[0].content) {
55
- const text = response.candidates[0].content.parts[0].text;
56
- resolve(text.trim());
57
- } else {
58
- resolve("");
59
- }
60
- } catch (error) {
61
- reject(error);
62
- }
63
- });
64
- });
65
-
66
- req.on('error', (error) => {
67
- reject(error);
68
- });
69
-
70
- req.write(requestData);
71
- req.end();
72
- });
73
- }
74
-
75
- // ==========================
76
- // OCR PREP (Preprocessing aja)
77
- // ==========================
78
- async function preprocessSoal(b) {
79
- return sharp(b)
80
- .resize({ width: 1000 })
81
- .grayscale()
82
- .normalize()
83
- .sharpen({ sigma: 3 })
84
- .median(3)
85
- .threshold(120)
86
- .toBuffer();
87
- }
88
-
89
- async function preprocessBot(b) {
90
- return sharp(b)
91
- .resize({ width: 800 })
92
- .grayscale()
93
- .normalize()
94
- .sharpen({ sigma: 1.5 })
95
- .median(1)
96
- .toBuffer();
97
- }
98
-
99
- // ==========================
100
- // SIMPLE MATCHING (Berdasarkan text asli Gemini)
101
- // ==========================
102
- function simpleMatch(soalText, botText) {
103
- // Cek apakah ada substring yang sama
104
- return soalText.includes(botText) || botText.includes(soalText);
105
- }
106
-
107
- // ==========================
108
- // MAIN FUNCTION - TEXT ASLI GEMINI
109
- // ==========================
110
- module.exports = async data => {
111
  try {
112
- // 1. PROSES SOAL & EKSTRAKSI
113
- const soalImg = buf(data.main);
114
- const soalProcessed = await preprocessSoal(soalImg);
115
- const soalText = await aiOcr(soalProcessed);
116
-
117
- console.log("SOAL TEXT FROM GEMINI:", soalText);
118
-
119
- let soalRaw = [];
120
 
121
- // Pisahkan berdasarkan koma atau ambil 3 kata pertama
122
- if (soalText.includes(',')) {
123
- soalRaw = soalText.split(',').map(s => s.trim()).filter(s => s.length > 0);
124
- } else {
125
- soalRaw = soalText.split(' ').filter(s => s.length > 0);
126
- }
127
 
128
- // Ambil maksimal 3 soal
129
- soalRaw = soalRaw.slice(0, 3);
130
 
131
- // 2. PROSES BOT
132
  const botResults = [];
133
- for (const b of data.bots) {
134
- const d = buf(b.img);
135
- const p = await preprocessBot(d);
136
- const botText = await aiOcr(p);
137
-
138
- console.log(`BOT ${b.id} TEXT:`, botText);
139
-
140
- botResults.push({
141
- id: b.id,
142
- text: botText, // Text asli dari Gemini
143
- original: botText // Simpan original juga
144
- });
145
- }
146
-
147
- // 3. LOGIKA PENCERCOKAN SEDERHANA
148
- const result = [];
149
- const usedBots = new Set();
150
-
151
- // Cari kecocokan untuk setiap soal
152
- for (const soal of soalRaw) {
153
- let foundBot = null;
154
-
155
- for (const bot of botResults) {
156
- if (usedBots.has(bot.id)) continue;
157
 
158
- // Cek kecocokan sederhana
159
- if (simpleMatch(soal.toLowerCase(), bot.text.toLowerCase())) {
160
- foundBot = bot.id;
161
- usedBots.add(bot.id);
162
- break;
163
- }
 
 
 
 
 
 
 
164
  }
165
-
166
- result.push(foundBot);
167
  }
168
 
169
- // 4. KEMBALIKAN HASIL
170
  return {
171
- soal: soalRaw,
172
- botResults: botResults,
173
- result: result
 
 
 
 
174
  };
175
-
176
  } catch (error) {
177
- console.error("Main function error:", error);
178
  return {
179
- soal: [],
180
- botResults: [],
181
- result: []
 
 
 
 
182
  };
183
  }
184
- };
 
 
 
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { extractTextFromImage } = require('./imageProcessor');
 
4
 
5
+ async function antibot(data) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  try {
7
+ const { main, bots } = data;
 
 
 
 
 
 
 
8
 
9
+ // Process main image (soal)
10
+ const mainBuffer = Buffer.from(main, 'base64');
11
+ const mainText = await extractTextFromImage(mainBuffer);
 
 
 
12
 
13
+ const soalRaw = [mainText.response];
14
+ const soalLeet = [mainText.response]; // bisa ditambahkan konversi leetcode jika perlu
15
 
16
+ // Process bot images
17
  const botResults = [];
18
+
19
+ for (const bot of bots) {
20
+ try {
21
+ const botBuffer = Buffer.from(bot.img, 'base64');
22
+ const botText = await extractTextFromImage(botBuffer);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ botResults.push({
25
+ id: bot.id,
26
+ text: botText.response,
27
+ value: botText.response // atau konversi ke leetcode jika perlu
28
+ });
29
+ } catch (error) {
30
+ console.error(`Error processing bot ${bot.id}:`, error);
31
+ botResults.push({
32
+ id: bot.id,
33
+ text: '',
34
+ value: '',
35
+ error: error.message
36
+ });
37
  }
 
 
38
  }
39
 
 
40
  return {
41
+ success: true,
42
+ data: {
43
+ soal: soalRaw,
44
+ soalLeet: soalLeet,
45
+ botResults: botResults,
46
+ result: botResults // atau hasil processing lainnya
47
+ }
48
  };
49
+
50
  } catch (error) {
51
+ console.error("Antibot function error:", error);
52
  return {
53
+ success: false,
54
+ error: error.message,
55
+ data: {
56
+ soal: [],
57
+ botResults: [],
58
+ result: []
59
+ }
60
  };
61
  }
62
+ }
63
+
64
+ module.exports = antibot;