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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Embeddings Visualizer</title>
    <style>
        body { 
            margin: 0; 
            overflow: hidden; 
            font-family: Arial, sans-serif;
        }
        canvas { 
            display: block; 
        }
        #info {
            position: absolute;
            top: 10px;
            left: 10px;
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 10px;
            border-radius: 5px;
            display: none;
            max-width: 300px;
            max-height: 200px;
            overflow: auto;
        }
        #loading {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 20px;
            border-radius: 5px;
            font-size: 18px;
        }
        #legend {
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 10px;
            border-radius: 5px;
            max-width: 200px;
        }
        .color-box {
            display: inline-block;
            width: 15px;
            height: 15px;
            margin-right: 8px;
            border-radius: 3px;
        }
        .legend-item {
            margin: 5px 0;
            display: flex;
            align-items: center;
        }
        #controls {
            position: absolute;
            bottom: 10px;
            left: 10px;
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 10px;
            border-radius: 5px;
        }
        select, button {
            margin: 5px 0;
            padding: 5px;
            border-radius: 3px;
            border: none;
        }
    </style>
</head>
<body>
    <div id="loading">Loading embeddings, please wait...</div>
    <div id="info"></div>
    <div id="legend"></div>
    <div id="controls">
        <div>
            <label for="category-filter">Filter by category:</label>
            <select id="category-filter">
                <option value="all">All Categories</option>
            </select>
        </div>
        <div>
            <label for="point-size">Point Size:</label>
            <input type="range" id="point-size" min="0.05" max="0.3" step="0.01" value="0.1">
        </div>
        <button id="reset-view">Reset View</button>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
    
    <script>
        // Set up the scene
        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0x111111);
        
        // Set up the camera
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.z = 5;
        
        // Set up the renderer
        const renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
        
        // Add controls for rotating, panning, and zooming
        const controls = new THREE.OrbitControls(camera, renderer.domElement);
        controls.enableDamping = true;
        controls.dampingFactor = 0.05;
        
        // Add ambient light
        const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
        scene.add(ambientLight);
        
        // Add directional light
        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
        directionalLight.position.set(1, 1, 1);
        scene.add(directionalLight);
        
        // Variables for raycasting (detecting when mouse hovers over points)
        const raycaster = new THREE.Raycaster();
        const mouse = new THREE.Vector2();
        let hoveredPoint = null;
        let points = [];
        let pointsData = [];
        let pointCloud = null;
        let pointSize = 0.1;
        let categoryColors = {};
        let categories = [];
        let originalPositions = null;
        let visibleCategories = new Set();
        
        // Create a color scheme for categories
        function generateColorMap(categories) {
            const colorMap = {};
            const hueStep = 1 / categories.length;
            
            categories.forEach((category, index) => {
                const color = new THREE.Color();
                color.setHSL(hueStep * index, 0.7, 0.5);
                colorMap[category] = color;
            });
            
            return colorMap;
        }
        
        // Create the legend
        function createLegend(colorMap) {
            const legendEl = document.getElementById('legend');
            legendEl.innerHTML = '<h3>Categories</h3>';
            
            Object.entries(colorMap).forEach(([category, color]) => {
                const item = document.createElement('div');
                item.className = 'legend-item';
                
                const colorBox = document.createElement('span');
                colorBox.className = 'color-box';
                colorBox.style.backgroundColor = `#${color.getHexString()}`;
                
                const label = document.createElement('span');
                label.textContent = category;
                
                item.appendChild(colorBox);
                item.appendChild(label);
                legendEl.appendChild(item);
            });
        }
        
        // Update category filter dropdown
        function updateCategoryFilter(categories) {
            const filterEl = document.getElementById('category-filter');
            
            // Clear existing options except the first one
            while (filterEl.options.length > 1) {
                filterEl.remove(1);
            }
            
            // Add categories
            categories.forEach(category => {
                const option = document.createElement('option');
                option.value = category;
                option.textContent = category;
                filterEl.appendChild(option);
            });
        }
        
        // Filter points by category
        function filterByCategory(category) {
            if (!pointCloud || !originalPositions) return;
            
            const positions = pointCloud.geometry.attributes.position.array;
            const colors = pointCloud.geometry.attributes.color.array;
            const visible = new Float32Array(positions.length);
            
            if (category === 'all') {
                // Show all points
                for (let i = 0; i < positions.length; i++) {
                    positions[i] = originalPositions[i];
                }
                visibleCategories = new Set(categories);
            } else {
                // Show only points of the selected category
                visibleCategories = new Set([category]);
                
                for (let i = 0; i < pointsData.length; i++) {
                    const idx = i * 3;
                    
                    if (pointsData[i].category === category) {
                        positions[idx] = originalPositions[idx];
                        positions[idx + 1] = originalPositions[idx + 1];
                        positions[idx + 2] = originalPositions[idx + 2];
                    } else {
                        // Move points of other categories far away (effectively hiding them)
                        positions[idx] = 10000;
                        positions[idx + 1] = 10000;
                        positions[idx + 2] = 10000;
                    }
                }
            }
            
            pointCloud.geometry.attributes.position.needsUpdate = true;
        }
        
        // Reset camera and controls to initial view
        function resetView() {
            if (!pointCloud) return;
            
            // Calculate center of visible points
            const visiblePoints = pointsData.filter(p => visibleCategories.has(p.category));
            if (visiblePoints.length === 0) return;
            
            const center = new THREE.Vector3(0, 0, 0);
            const boundingSphere = new THREE.Sphere(center, 5);
            const distance = boundingSphere.radius / Math.sin(camera.fov * Math.PI / 360);
            
            camera.position.set(0, 0, distance * 1.2);
            camera.lookAt(center);
            
            controls.target.copy(center);
            controls.update();
        }
        
        // Load the embeddings data
        Promise.all([
            fetch('/static/embeddings.json').then(res => res.json()),
            fetch('/static/categories.json').then(res => res.json()).catch(() => ({ categories: [] }))
        ]).then(([data, categoryData]) => {
            // Hide loading message
            document.getElementById('loading').style.display = 'none';
            
            // Store data for tooltip
            pointsData = data;
            
            // Extract categories from points if not provided separately
            if (categoryData.categories && categoryData.categories.length > 0) {
                categories = categoryData.categories;
            } else {
                const categorySet = new Set();
                data.forEach(point => {
                    if (point.category) categorySet.add(point.category);
                });
                categories = Array.from(categorySet);
            }
            
            // Generate colors for categories
            categoryColors = generateColorMap(categories);
            createLegend(categoryColors);
            updateCategoryFilter(categories);
            visibleCategories = new Set(categories);
            
            // Normalize the positions to keep them within reasonable bounds
            let xValues = data.map(p => p.x);
            let yValues = data.map(p => p.y);
            let zValues = data.map(p => p.z);
            
            let xMin = Math.min(...xValues), xMax = Math.max(...xValues);
            let yMin = Math.min(...yValues), yMax = Math.max(...yValues);
            let zMin = Math.min(...zValues), zMax = Math.max(...zValues);
            
            // Create a geometry for all points
            const geometry = new THREE.BufferGeometry();
            const positions = new Float32Array(data.length * 3);
            const colors = new Float32Array(data.length * 3);
            
            for (let i = 0; i < data.length; i++) {
                // Normalize to a range of approximately -5 to 5
                const x = ((data[i].x - xMin) / (xMax - xMin) * 10) - 5;
                const y = ((data[i].y - yMin) / (yMax - yMin) * 10) - 5;
                const z = ((data[i].z - zMin) / (zMax - zMin) * 10) - 5;
                
                positions[i * 3] = x;
                positions[i * 3 + 1] = y;
                positions[i * 3 + 2] = z;
                
                // Use category color
                let color;
                if (data[i].category && categoryColors[data[i].category]) {
                    color = categoryColors[data[i].category];
                } else {
                    // Fallback to a random color if no category
                    color = new THREE.Color();
                    color.setHSL(Math.random(), 0.7, 0.5);
                }
                
                colors[i * 3] = color.r;
                colors[i * 3 + 1] = color.g;
                colors[i * 3 + 2] = color.b;
            }
            
            // Store original positions for filtering
            originalPositions = positions.slice();
            
            geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
            geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
            
            // Create the point material
            const material = new THREE.PointsMaterial({
                size: pointSize,
                vertexColors: true,
                sizeAttenuation: true
            });
            
            // Create the point cloud
            pointCloud = new THREE.Points(geometry, material);
            scene.add(pointCloud);
            
            // Store individual points for raycasting
            for (let i = 0; i < data.length; i++) {
                points.push({
                    position: new THREE.Vector3(
                        positions[i * 3],
                        positions[i * 3 + 1],
                        positions[i * 3 + 2]
                    ),
                    index: i
                });
            }
            
            // Set camera position to view the entire scene
            resetView();
            
            // Setup event listeners
            document.getElementById('point-size').addEventListener('input', (e) => {
                pointSize = parseFloat(e.target.value);
                if (pointCloud) {
                    pointCloud.material.size = pointSize;
                }
            });
            
            document.getElementById('category-filter').addEventListener('change', (e) => {
                filterByCategory(e.target.value);
            });
            
            document.getElementById('reset-view').addEventListener('click', resetView);
        })
        .catch(error => {
            console.error('Error loading embeddings:', error);
            document.getElementById('loading').textContent = 'Error loading embeddings. Check console for details.';
        });
        
        // Handle mouse movement for hover effects
        function onMouseMove(event) {
            // Calculate mouse position in normalized device coordinates (-1 to +1)
            mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
            mouse.y = - (event.clientY / window.innerHeight) * 2 + 1;
        }
        
        // Handle window resize
        function onWindowResize() {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        }
        
        // Check if mouse is hovering over a point
        function checkIntersection() {
            if (!pointCloud) return;
            
            raycaster.setFromCamera(mouse, camera);
            
            let intersects = [];
            for (let i = 0; i < points.length; i++) {
                const point = points[i];
                const pointData = pointsData[i];
                
                // Skip points that aren't in visible categories
                if (!visibleCategories.has(pointData.category)) continue;
                
                const distance = raycaster.ray.distanceToPoint(point.position);
                
                // If mouse is close enough to a point (adjust the threshold as needed)
                if (distance < 0.1) {
                    intersects.push({ distance, index: point.index });
                }
            }
            
            // Sort by distance to get the closest point
            intersects.sort((a, b) => a.distance - b.distance);
            
            // Reset previous hover
            if (hoveredPoint !== null) {
                document.getElementById('info').style.display = 'none';
                hoveredPoint = null;
            }
            
            // Show info for the new hovered point
            if (intersects.length > 0) {
                const index = intersects[0].index;
                const data = pointsData[index];
                
                hoveredPoint = index;
                
                const infoElement = document.getElementById('info');
                infoElement.innerHTML = `
                    <h3>${data.title}</h3>
                    <p><strong>Category:</strong> ${data.category || 'Uncategorized'}</p>
                    <p>${data.text}</p>
                `;
                infoElement.style.display = 'block';
            }
        }
        
        // Animation loop
        function animate() {
            requestAnimationFrame(animate);
            
            controls.update();
            checkIntersection();
            
            renderer.render(scene, camera);
        }
        
        // Add event listeners
        window.addEventListener('mousemove', onMouseMove, false);
        window.addEventListener('resize', onWindowResize, false);
        
        // Start animation
        animate();
    </script>
</body>
</html>