-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcaptcha_solver.js
More file actions
216 lines (185 loc) · 7.37 KB
/
captcha_solver.js
File metadata and controls
216 lines (185 loc) · 7.37 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const OpenAI = require('openai');
// Validate environment variables
if (!process.env.OPENAI_API_KEY) {
console.error('Error: OPENAI_API_KEY is not set in .env file');
process.exit(1);
}
// Configure OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// Constants
const USD_TO_TL = parseFloat(process.env.USD_TO_TL || '34.99'); // USD/TL exchange rate
const RESULTS_DIR = 'results'; // Results directory
const IMAGE_DIR = 'image'; // Image directory
const DELAY_BETWEEN_REQUESTS = parseInt(process.env.DELAY_BETWEEN_REQUESTS || '1000'); // Delay between requests in ms
// Log configuration (without sensitive data)
console.log('Configuration:');
console.log(`- Exchange Rate: ${USD_TO_TL} TL/USD`);
console.log(`- Request Delay: ${DELAY_BETWEEN_REQUESTS}ms`);
console.log(`- Results Directory: ${RESULTS_DIR}`);
console.log(`- Image Directory: ${IMAGE_DIR}\n`);
// Generate timestamp filename
function getTimestampFilename() {
const now = new Date();
return `results-${now.getFullYear()}${(now.getMonth() + 1).toString().padStart(2, '0')}${now.getDate().toString().padStart(2, '0')}-${now.getHours().toString().padStart(2, '0')}${now.getMinutes().toString().padStart(2, '0')}${now.getSeconds().toString().padStart(2, '0')}.txt`;
}
// Calculate cost (in USD and TL)
function calculateCost(inputTokens, outputTokens) {
// GPT-4 Turbo pricing
const INPUT_PRICE_PER_1K = 0.01; // $0.01 per 1K input tokens
const OUTPUT_PRICE_PER_1K = 0.03; // $0.03 per 1K output tokens
const inputCost = (inputTokens / 1000) * INPUT_PRICE_PER_1K;
const outputCost = (outputTokens / 1000) * OUTPUT_PRICE_PER_1K;
const totalUSD = inputCost + outputCost;
const totalTL = totalUSD * USD_TO_TL;
return {
usd: totalUSD,
tl: totalTL
};
}
// Convert image to base64
function encodeImage(imagePath) {
try {
const imageBuffer = fs.readFileSync(imagePath);
return imageBuffer.toString('base64');
} catch (error) {
throw new Error(`Failed to read image file: ${error.message}`);
}
}
// Process image
async function processImage(imagePath) {
const startTime = Date.now();
const base64Image = encodeImage(imagePath);
try {
const response = await openai.chat.completions.create({
model: "gpt-4-turbo",
messages: [
{
role: "system",
content: "Extract text only. No explanations."
},
{
role: "user",
content: [
{
type: "text",
text: "Read text:"
},
{
type: "image_url",
image_url: {
url: `data:image/png;base64,${base64Image}`,
detail: "low"
}
}
]
}
],
max_tokens: 10,
temperature: 0
});
const endTime = Date.now();
const responseTime = (endTime - startTime) / 1000;
const inputTokens = response.usage.prompt_tokens;
const outputTokens = response.usage.completion_tokens;
const totalTokens = response.usage.total_tokens;
const cost = calculateCost(inputTokens, outputTokens);
return {
text: response.choices[0].message.content.trim(),
stats: {
responseTime,
inputTokens,
outputTokens,
totalTokens,
cost
}
};
} catch (error) {
throw new Error(`API request failed: ${error.message}`);
}
}
// Initialize directories
function initializeDirectories() {
// Check and create results directory
if (!fs.existsSync(RESULTS_DIR)) {
fs.mkdirSync(RESULTS_DIR);
}
// Check if image directory exists
if (!fs.existsSync(IMAGE_DIR)) {
console.error(`Error: ${IMAGE_DIR} directory not found`);
process.exit(1);
}
}
// Main function
async function main() {
console.log('Initializing...');
initializeDirectories();
const results = [];
let totalCostUSD = 0;
let totalCostTL = 0;
let totalTime = 0;
let totalTokens = 0;
try {
const files = fs.readdirSync(IMAGE_DIR)
.filter(file => file.toLowerCase().endsWith('.png'))
.sort();
if (files.length === 0) {
console.error('No PNG files found in the image directory');
process.exit(1);
}
console.log(`Found ${files.length} PNG files to process\n`);
for (const file of files) {
const imagePath = path.join(IMAGE_DIR, file);
console.log(`Processing: ${file}`);
try {
const result = await processImage(imagePath);
totalCostUSD += result.stats.cost.usd;
totalCostTL += result.stats.cost.tl;
totalTime += result.stats.responseTime;
totalTokens += result.stats.totalTokens;
results.push(
`${file}: ${result.text}\n` +
` Response Time: ${result.stats.responseTime.toFixed(2)} seconds\n` +
` Token Usage: ${result.stats.totalTokens} (${result.stats.inputTokens} input + ${result.stats.outputTokens} output)\n` +
` Cost: $${result.stats.cost.usd.toFixed(4)} (${result.stats.cost.tl.toFixed(2)} TL)`
);
console.log(` Response Time: ${result.stats.responseTime.toFixed(2)} seconds`);
console.log(` Token Usage: ${result.stats.totalTokens}`);
console.log(` Cost: $${result.stats.cost.usd.toFixed(4)} (${result.stats.cost.tl.toFixed(2)} TL)\n`);
// Rate limiting
await new Promise(resolve => setTimeout(resolve, DELAY_BETWEEN_REQUESTS));
} catch (error) {
console.error(`Error processing ${file}:`, error.message);
}
}
// Add total statistics
results.push(
`\nTotal Statistics:\n` +
` Total Time: ${totalTime.toFixed(2)} seconds\n` +
` Total Tokens: ${totalTokens}\n` +
` Average Tokens: ${(totalTokens / files.length).toFixed(0)}\n` +
` Total Cost: $${totalCostUSD.toFixed(4)} (${totalCostTL.toFixed(2)} TL)`
);
// Save results with timestamp
const resultFile = path.join(RESULTS_DIR, getTimestampFilename());
fs.writeFileSync(resultFile, results.join('\n\n'), 'utf8');
console.log('\nProcess completed! Results saved:');
console.log(`File: ${resultFile}`);
console.log(`Total Cost: $${totalCostUSD.toFixed(4)} (${totalCostTL.toFixed(2)} TL)`);
console.log(`Total Time: ${totalTime.toFixed(2)} seconds`);
console.log(`Total Tokens: ${totalTokens}`);
console.log(`Average Tokens: ${(totalTokens / files.length).toFixed(0)}`);
} catch (error) {
console.error('General error:', error.message);
process.exit(1);
}
}
// Start the application
main().catch(error => {
console.error('Application error:', error.message);
process.exit(1);
});