jake2004 commited on
Commit
10b371e
·
verified ·
1 Parent(s): 8264410

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +406 -19
index.html CHANGED
@@ -1,19 +1,406 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>3D Neural Network Visualization</title>
7
+ <style>
8
+ body {
9
+ margin: 0;
10
+ overflow: hidden;
11
+ font-family: Arial, sans-serif;
12
+ color: white;
13
+ background-color: #111;
14
+ }
15
+
16
+ #canvas-container {
17
+ position: absolute;
18
+ width: 100%;
19
+ height: 100%;
20
+ }
21
+
22
+ #info-panel {
23
+ position: absolute;
24
+ top: 10px;
25
+ left: 10px;
26
+ background-color: rgba(0, 0, 0, 0.7);
27
+ padding: 15px;
28
+ border-radius: 5px;
29
+ max-width: 300px;
30
+ z-index: 100;
31
+ }
32
+
33
+ #controls {
34
+ position: absolute;
35
+ bottom: 10px;
36
+ left: 10px;
37
+ background-color: rgba(0, 0, 0, 0.7);
38
+ padding: 15px;
39
+ border-radius: 5px;
40
+ z-index: 100;
41
+ }
42
+
43
+ button, input {
44
+ margin: 5px;
45
+ padding: 8px;
46
+ background-color: #444;
47
+ color: white;
48
+ border: none;
49
+ border-radius: 3px;
50
+ cursor: pointer;
51
+ }
52
+
53
+ button:hover {
54
+ background-color: #666;
55
+ }
56
+
57
+ h1 {
58
+ margin-top: 0;
59
+ font-size: 1.2em;
60
+ }
61
+ </style>
62
+ </head>
63
+ <body>
64
+ <div id="canvas-container"></div>
65
+
66
+ <div id="info-panel">
67
+ <h1>3D Neural Network</h1>
68
+ <p>This visualization represents a simple feedforward neural network with multiple layers.</p>
69
+ <p>The connections between neurons light up when data flows through the network.</p>
70
+ <p><strong>Controls:</strong> Drag to rotate, scroll to zoom.</p>
71
+ </div>
72
+
73
+ <div id="controls">
74
+ <button id="toggle-animation">Pause Animation</button>
75
+ <button id="reset-view">Reset View</button>
76
+ <label for="layer-count">Layers: </label>
77
+ <input type="range" id="layer-count" min="2" max="7" value="4">
78
+ <span id="layer-count-value">4</span>
79
+ </div>
80
+
81
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
82
+ <script>
83
+ // Main configuration for the network
84
+ const config = {
85
+ neuronSize: 0.15,
86
+ layerDistance: 1.5,
87
+ neuronsPerLayer: [8, 12, 10, 6], // Default structure
88
+ neuronColors: {
89
+ input: 0x4286f4,
90
+ hidden: 0x42b0f4,
91
+ output: 0xf442a7
92
+ },
93
+ animationSpeed: 0.01,
94
+ connectionOpacity: 0.3,
95
+ activeConnectionOpacity: 0.8,
96
+ activeConnectionColor: 0xffffff,
97
+ pulseDuration: 100, // Number of frames for a complete pulse
98
+ };
99
+
100
+ // Global variables
101
+ let scene, camera, renderer, neurons = [], connections = [], animationActive = true;
102
+ let networkGroup, frame = 0;
103
+
104
+ // Initialize the scene
105
+ function init() {
106
+ // Create scene
107
+ scene = new THREE.Scene();
108
+ scene.background = new THREE.Color(0x111111);
109
+
110
+ // Create camera
111
+ camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
112
+ camera.position.z = 5;
113
+
114
+ // Create renderer
115
+ renderer = new THREE.WebGLRenderer({ antialias: true });
116
+ renderer.setSize(window.innerWidth, window.innerHeight);
117
+ document.getElementById('canvas-container').appendChild(renderer.domElement);
118
+
119
+ // Add lights
120
+ const ambientLight = new THREE.AmbientLight(0x404040);
121
+ scene.add(ambientLight);
122
+
123
+ const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
124
+ directionalLight.position.set(1, 1, 1);
125
+ scene.add(directionalLight);
126
+
127
+ // Create a group for the entire network
128
+ networkGroup = new THREE.Group();
129
+ scene.add(networkGroup);
130
+
131
+ // Create the neural network
132
+ createNeuralNetwork();
133
+
134
+ // Set up controls for orbiting
135
+ setupOrbitControls();
136
+
137
+ // Event listeners
138
+ window.addEventListener('resize', onWindowResize);
139
+ document.getElementById('toggle-animation').addEventListener('click', toggleAnimation);
140
+ document.getElementById('reset-view').addEventListener('click', resetView);
141
+
142
+ const layerSlider = document.getElementById('layer-count');
143
+ const layerCountDisplay = document.getElementById('layer-count-value');
144
+ layerSlider.addEventListener('input', function() {
145
+ layerCountDisplay.textContent = this.value;
146
+ updateNetworkStructure(parseInt(this.value));
147
+ });
148
+
149
+ // Start animation loop
150
+ animate();
151
+ }
152
+
153
+ // Create a neural network with specified layers
154
+ function createNeuralNetwork() {
155
+ neurons = [];
156
+ connections = [];
157
+
158
+ // Remove existing network if any
159
+ while(networkGroup.children.length > 0) {
160
+ networkGroup.remove(networkGroup.children[0]);
161
+ }
162
+
163
+ // Material for neurons
164
+ const inputMaterial = new THREE.MeshPhongMaterial({ color: config.neuronColors.input });
165
+ const hiddenMaterial = new THREE.MeshPhongMaterial({ color: config.neuronColors.hidden });
166
+ const outputMaterial = new THREE.MeshPhongMaterial({ color: config.neuronColors.output });
167
+
168
+ // Geometry for neurons
169
+ const neuronGeometry = new THREE.SphereGeometry(config.neuronSize, 16, 16);
170
+
171
+ // Create neurons for each layer
172
+ for (let layerIndex = 0; layerIndex < config.neuronsPerLayer.length; layerIndex++) {
173
+ const layerNeurons = [];
174
+ const neuronsInLayer = config.neuronsPerLayer[layerIndex];
175
+
176
+ let material;
177
+ if (layerIndex === 0) material = inputMaterial;
178
+ else if (layerIndex === config.neuronsPerLayer.length - 1) material = outputMaterial;
179
+ else material = hiddenMaterial;
180
+
181
+ // Calculate vertical spacing
182
+ const layerHeight = (neuronsInLayer - 1) * 0.6;
183
+
184
+ for (let neuronIndex = 0; neuronIndex < neuronsInLayer; neuronIndex++) {
185
+ const neuron = new THREE.Mesh(neuronGeometry, material);
186
+
187
+ // Position each neuron
188
+ neuron.position.x = layerIndex * config.layerDistance - (config.neuronsPerLayer.length - 1) * config.layerDistance / 2;
189
+ neuron.position.y = neuronIndex * 0.6 - layerHeight / 2;
190
+
191
+ neuron.layerIndex = layerIndex;
192
+ neuron.neuronIndex = neuronIndex;
193
+
194
+ networkGroup.add(neuron);
195
+ layerNeurons.push(neuron);
196
+ }
197
+
198
+ neurons.push(layerNeurons);
199
+ }
200
+
201
+ // Create connections between neurons
202
+ for (let layerIndex = 0; layerIndex < neurons.length - 1; layerIndex++) {
203
+ const currentLayer = neurons[layerIndex];
204
+ const nextLayer = neurons[layerIndex + 1];
205
+
206
+ for (let i = 0; i < currentLayer.length; i++) {
207
+ for (let j = 0; j < nextLayer.length; j++) {
208
+ const startNeuron = currentLayer[i];
209
+ const endNeuron = nextLayer[j];
210
+
211
+ const connectionGeometry = new THREE.BufferGeometry();
212
+ const lineMaterial = new THREE.LineBasicMaterial({
213
+ color: 0xaaaaaa,
214
+ transparent: true,
215
+ opacity: config.connectionOpacity
216
+ });
217
+
218
+ // Create a line between neurons
219
+ const points = [
220
+ startNeuron.position,
221
+ endNeuron.position
222
+ ];
223
+
224
+ connectionGeometry.setFromPoints(points);
225
+ const connection = new THREE.Line(connectionGeometry, lineMaterial);
226
+
227
+ // Store connections for animation
228
+ connection.startNeuron = startNeuron;
229
+ connection.endNeuron = endNeuron;
230
+ connection.originalColor = 0xaaaaaa;
231
+ connection.activationTime = -1;
232
+
233
+ networkGroup.add(connection);
234
+ connections.push(connection);
235
+ }
236
+ }
237
+ }
238
+ }
239
+
240
+ // Update the network when layer count changes
241
+ function updateNetworkStructure(layerCount) {
242
+ // Generate a new structure with the specified number of layers
243
+ config.neuronsPerLayer = [];
244
+
245
+ // Input layer has 8 neurons
246
+ config.neuronsPerLayer.push(8);
247
+
248
+ // Generate hidden layers with varying sizes
249
+ for (let i = 0; i < layerCount - 2; i++) {
250
+ const layerSize = Math.round(6 + Math.sin(i * Math.PI / (layerCount - 2)) * 6);
251
+ config.neuronsPerLayer.push(layerSize);
252
+ }
253
+
254
+ // Output layer has 6 neurons
255
+ config.neuronsPerLayer.push(6);
256
+
257
+ // Recreate the network
258
+ createNeuralNetwork();
259
+ }
260
+
261
+ // Animation loop
262
+ function animate() {
263
+ requestAnimationFrame(animate);
264
+
265
+ if (animationActive) {
266
+ // Increment frame counter
267
+ frame += 1;
268
+
269
+ // Trigger activations
270
+ if (frame % 30 === 0) {
271
+ triggerNetworkActivation();
272
+ }
273
+
274
+ // Update all connections
275
+ updateConnections();
276
+ }
277
+
278
+ renderer.render(scene, camera);
279
+ }
280
+
281
+ // Trigger a new activation wave through the network
282
+ function triggerNetworkActivation() {
283
+ // Activate a random neuron in the input layer
284
+ const inputLayer = neurons[0];
285
+ const randomNeuronIndex = Math.floor(Math.random() * inputLayer.length);
286
+
287
+ // Get all connections from this neuron
288
+ for (let i = 0; i < connections.length; i++) {
289
+ const connection = connections[i];
290
+ if (connection.startNeuron === inputLayer[randomNeuronIndex]) {
291
+ connection.activationTime = frame;
292
+ connection.material.opacity = config.activeConnectionOpacity;
293
+ connection.material.color.set(config.activeConnectionColor);
294
+ }
295
+ }
296
+ }
297
+
298
+ // Update connection animations
299
+ function updateConnections() {
300
+ const activationLayerDuration = config.pulseDuration / (config.neuronsPerLayer.length - 1);
301
+
302
+ for (let i = 0; i < connections.length; i++) {
303
+ const connection = connections[i];
304
+
305
+ // If connection is active
306
+ if (connection.activationTime > 0) {
307
+ const elapsedFrames = frame - connection.activationTime;
308
+
309
+ // If this connection has been active long enough, propagate to next layer
310
+ if (elapsedFrames >= activationLayerDuration &&
311
+ connection.endNeuron.layerIndex < config.neuronsPerLayer.length - 1) {
312
+
313
+ // Find connections from the end neuron
314
+ for (let j = 0; j < connections.length; j++) {
315
+ const nextConnection = connections[j];
316
+ if (nextConnection.startNeuron === connection.endNeuron &&
317
+ nextConnection.activationTime < connection.activationTime) {
318
+ nextConnection.activationTime = frame;
319
+ nextConnection.material.opacity = config.activeConnectionOpacity;
320
+ nextConnection.material.color.set(config.activeConnectionColor);
321
+ }
322
+ }
323
+ }
324
+
325
+ // Fade out connection over time
326
+ if (elapsedFrames >= config.pulseDuration) {
327
+ connection.material.opacity = config.connectionOpacity;
328
+ connection.material.color.set(connection.originalColor);
329
+ connection.activationTime = -1;
330
+ }
331
+ }
332
+ }
333
+ }
334
+
335
+ // Toggle animation state
336
+ function toggleAnimation() {
337
+ animationActive = !animationActive;
338
+ document.getElementById('toggle-animation').textContent =
339
+ animationActive ? 'Pause Animation' : 'Resume Animation';
340
+ }
341
+
342
+ // Reset the camera view
343
+ function resetView() {
344
+ camera.position.set(0, 0, 5);
345
+ camera.lookAt(0, 0, 0);
346
+ }
347
+
348
+ // Handle window resize
349
+ function onWindowResize() {
350
+ camera.aspect = window.innerWidth / window.innerHeight;
351
+ camera.updateProjectionMatrix();
352
+ renderer.setSize(window.innerWidth, window.innerHeight);
353
+ }
354
+
355
+ // Basic orbit controls implementation
356
+ function setupOrbitControls() {
357
+ let isDragging = false;
358
+ let previousMousePosition = { x: 0, y: 0 };
359
+ const rotationSpeed = 0.01;
360
+
361
+ renderer.domElement.addEventListener('mousedown', function(e) {
362
+ isDragging = true;
363
+ previousMousePosition = { x: e.clientX, y: e.clientY };
364
+ });
365
+
366
+ renderer.domElement.addEventListener('mousemove', function(e) {
367
+ if (isDragging) {
368
+ const deltaMove = {
369
+ x: e.clientX - previousMousePosition.x,
370
+ y: e.clientY - previousMousePosition.y
371
+ };
372
+
373
+ networkGroup.rotation.y += deltaMove.x * rotationSpeed;
374
+ networkGroup.rotation.x += deltaMove.y * rotationSpeed;
375
+
376
+ previousMousePosition = { x: e.clientX, y: e.clientY };
377
+ }
378
+ });
379
+
380
+ renderer.domElement.addEventListener('mouseup', function() {
381
+ isDragging = false;
382
+ });
383
+
384
+ renderer.domElement.addEventListener('mouseleave', function() {
385
+ isDragging = false;
386
+ });
387
+
388
+ // Zoom functionality
389
+ renderer.domElement.addEventListener('wheel', function(e) {
390
+ e.preventDefault();
391
+
392
+ if (e.deltaY < 0) {
393
+ // Zoom in
394
+ camera.position.z = Math.max(2, camera.position.z - 0.5);
395
+ } else {
396
+ // Zoom out
397
+ camera.position.z = Math.min(10, camera.position.z + 0.5);
398
+ }
399
+ });
400
+ }
401
+
402
+ // Initialize the visualization when page loads
403
+ window.onload = init;
404
+ </script>
405
+ </body>
406
+ </html>