Skip to content

Commit 8ad34e5

Browse files
authored
Create jina-embeddings-image-token-calculator.html
https://gist.github.com/simonw/7f3d2ff63423078f04d1fb2fc2b3e2a5
1 parent 8d67d6c commit 8ad34e5

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Image Token Calculator</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
max-width: 600px;
11+
margin: 0 auto;
12+
padding: 20px;
13+
line-height: 1.6;
14+
}
15+
#drop-zone {
16+
border: 2px dashed #ccc;
17+
border-radius: 20px;
18+
width: 100%;
19+
height: 200px;
20+
display: flex;
21+
justify-content: center;
22+
align-items: center;
23+
cursor: pointer;
24+
}
25+
#drop-zone.dragover {
26+
background-color: #f0f0f0;
27+
}
28+
#file-input {
29+
display: none;
30+
}
31+
#result {
32+
margin-top: 20px;
33+
}
34+
</style>
35+
</head>
36+
<body>
37+
<h1>Image Token Calculator</h1>
38+
<div id="drop-zone">
39+
<p>Drop an image here or click to select</p>
40+
</div>
41+
<input type="file" id="file-input" accept="image/*">
42+
<div id="result"></div>
43+
44+
<script>
45+
const dropZone = document.getElementById('drop-zone');
46+
const fileInput = document.getElementById('file-input');
47+
const result = document.getElementById('result');
48+
49+
dropZone.addEventListener('click', () => fileInput.click());
50+
dropZone.addEventListener('dragover', (e) => {
51+
e.preventDefault();
52+
dropZone.classList.add('dragover');
53+
});
54+
dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));
55+
dropZone.addEventListener('drop', handleDrop);
56+
fileInput.addEventListener('change', handleFileSelect);
57+
58+
function handleDrop(e) {
59+
e.preventDefault();
60+
dropZone.classList.remove('dragover');
61+
const file = e.dataTransfer.files[0];
62+
if (file && file.type.startsWith('image/')) {
63+
processImage(file);
64+
}
65+
}
66+
67+
function handleFileSelect(e) {
68+
const file = e.target.files[0];
69+
if (file) {
70+
processImage(file);
71+
}
72+
}
73+
74+
function processImage(file) {
75+
const img = new Image();
76+
img.onload = () => calculateTokens(img.width, img.height);
77+
img.src = URL.createObjectURL(file);
78+
}
79+
80+
function calculateTokens(width, height) {
81+
const tileSize = 224;
82+
const horizontalTiles = Math.ceil(width / tileSize);
83+
const verticalTiles = Math.ceil(height / tileSize);
84+
const totalTiles = horizontalTiles * verticalTiles;
85+
const tokenCost = totalTiles * 1000;
86+
87+
result.innerHTML = `
88+
<h2>Results:</h2>
89+
<p>Image dimensions: ${width}x${height} pixels</p>
90+
<p>Horizontal tiles: ${horizontalTiles}</p>
91+
<p>Vertical tiles: ${verticalTiles}</p>
92+
<p>Total tiles: ${totalTiles}</p>
93+
<p>Token cost: ${tokenCost.toLocaleString()} tokens</p>
94+
`;
95+
}
96+
</script>
97+
</body>
98+
</html>

0 commit comments

Comments
 (0)