File size: 21,990 Bytes
9225765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b0fc5
9225765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b0fc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9225765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b0fc5
9225765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b0fc5
 
9225765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b0fc5
9225765
 
93b0fc5
 
 
9225765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b0fc5
9225765
93b0fc5
9225765
93b0fc5
 
9225765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b0fc5
 
 
 
 
 
 
9225765
 
 
 
 
93b0fc5
9225765
 
 
 
 
 
 
 
 
 
93b0fc5
 
9225765
 
93b0fc5
 
9225765
 
 
 
 
 
 
93b0fc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9225765
93b0fc5
 
 
 
 
9225765
 
 
93b0fc5
 
9225765
93b0fc5
 
 
 
 
9225765
93b0fc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9225765
93b0fc5
 
 
 
 
9225765
 
 
93b0fc5
 
 
 
9225765
 
93b0fc5
 
 
 
9225765
 
93b0fc5
 
9225765
93b0fc5
 
 
9225765
93b0fc5
 
 
 
 
 
 
 
9225765
 
93b0fc5
9225765
93b0fc5
 
9225765
 
 
 
93b0fc5
 
 
 
 
9225765
 
 
 
 
 
 
 
 
93b0fc5
 
 
 
 
9225765
 
93b0fc5
 
 
 
 
 
 
 
 
 
 
 
 
9225765
93b0fc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9225765
 
 
93b0fc5
9225765
 
93b0fc5
 
9225765
 
 
a5d61ff
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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Syntactic Bunker - Context Compression Demo</title>
    <link rel="icon" type="image/x-icon" href="/static/favicon.ico">
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        cyberblue: '#00d9ff',
                        cyberpurple: '#a855f7',
                        darkbg: '#0a0a18',
                        panelbg: '#121230',
                    }
                }
            }
        }
    </script>
    <link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">
    <script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
    <script src="https://unpkg.com/feather-icons"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;600&family=Orbitron:wght@400;600&display=swap');
        
        body {
            font-family: 'JetBrains Mono', monospace;
            background-color: #0a0a18;
            color: #ffffff;
            overflow-x: hidden;
        }
        
        .cyber-gradient {
            background: linear-gradient(135deg, #00d9ff 0%, #a855f7 100%);
        }
        
        .cyber-border {
            border: 1px solid rgba(0, 217, 255, 0.3);
            box-shadow: 0 0 15px rgba(0, 217, 255, 0.2);
        }
        
        .cyber-glow {
            box-shadow: 0 0 20px rgba(0, 217, 255, 0.4);
        }
        
        .matrix-cell {
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            border: 1px solid rgba(168, 85, 247, 0.2);
            transition: all 0.3s ease;
            background-color: rgba(18, 18, 48, 0.5);
        }
        
        .matrix-cell:hover {
            background: rgba(168, 85, 247, 0.1);
            transform: scale(1.1);
        }
        
        .helix-animation {
            animation: rotate 2s linear infinite;
        }
        
        @keyframes rotate {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        
        .typewriter {
            overflow: hidden;
            border-right: .15em solid #00d9ff;
            white-space: nowrap;
            animation: typing 3.5s steps(40, end), blink-caret .75s step-end infinite;
            width: 0;
        }

        /* Improved accessibility for focus states */
        button:focus, input:focus, textarea:focus {
            outline: 2px solid #00d9ff;
            outline-offset: 2px;
        }

        /* Sample text button */
        .sample-btn {
            background: linear-gradient(135deg, #a855f7, #00d9ff);
            transition: all 0.3s ease;
        }

        .sample-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(168, 85, 247, 0.3);
        }

        /* Error message style */
        .error-message {
            color: #ef4444;
            font-size: 0.875rem;
            margin-top: 0.5rem;
        }
        
        @keyframes typing {
            from { width: 0 }
            to { width: 100% }
        }
        
        @keyframes blink-caret {
            from, to { border-color: transparent }
            50% { border-color: #00d9ff; }
        }
    </style>
</head>
<body class="bg-darkbg min-h-screen">
    <header class="py-6 border-b cyber-border">
        <div class="container mx-auto px-6">
            <h1 class="text-3xl md:text-4xl font-bold text-center cyber-gradient bg-clip-text text-transparent" data-aos="fade-down">
                SYNTACTIC BUNKER
            </h1>
            <p class="text-center text-cyberblue mt-2" data-aos="fade-down" data-aos-delay="200">
                Context Compression Demo (Real Backend Integration)
            </p>
        </div>
    </header>

    <main class="container mx-auto px-4 py-8">
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
            <!-- LEFT PANEL - Input Section -->
            <div class="bg-panelbg rounded-xl p-6 cyber-border" data-aos="fade-right">
                <h2 class="text-xl font-semibold text-cyberblue mb-4 flex items-center">
                    <i data-feather="message-square" class="mr-2"></i>
                    Conversation History Input
                </h2>
                
                <div class="relative">
                    <textarea 
                        id="inputText" 
                        class="w-full h-[400px] bg-[#1a1a3a] border border-cyberblue/30 rounded-lg p-4 text-white resize-none focus:outline-none focus:ring-2 focus:ring-cyberblue"
                        placeholder="Paste your conversation history here...&#10;&#10;Example:&#10;'User: Hello, how are you?&#10;AI: I'm great, thanks! What can I help with today?'"
                        aria-label="Conversation history input"
                    ></textarea>
                    <div class="absolute bottom-3 right-3 text-xs text-gray-400">
                        <span id="charCount">0</span> characters
                    </div>
                </div>
                
                <div class="mt-6 flex justify-center">
                    <button 
                        id="compressBtn"
                        class="cyber-glow bg-cyberblue text-white px-8 py-3 rounded-lg font-semibold hover:bg-cyan-600 transition-all flex items-center"
                        onclick="compressText()"
                    >
                        <i data-feather="cpu" class="mr-2"></i>
                        Compress to State Vector
                    </button>
                </div>
                
                <div id="processingAnimation" class="hidden mt-6 flex justify-center items-center">
                    <div class="helix-animation text-cyberblue">
                        <i data-feather="loader" class="w-12 h-12"></i>
                    </div>
                    <span class="ml-3 text-cyberblue">Processing...</span>
                </div>
            </div>

            <!-- RIGHT PANEL - Output Section -->
            <div class="bg-panelbg rounded-xl p-6 cyber-border" data-aos="fade-left">
                <h2 class="text-xl font-semibold text-cyberpurple mb-4 flex items-center">
                    <i data-feather="database" class="mr-2"></i>
                    Compressed State Vector
                </h2>
                
                <div class="grid grid-cols-8 gap-1 mb-6 bg-[#1a1a3a] p-3 rounded-lg">
                    <!-- 8x8 Matrix Grid -->
                    <div id="matrixGrid" class="grid grid-cols-8 gap-1">
                        <!-- Matrix cells will be generated dynamically -->
                    </div>
                </div>
                
                <div class="mb-6 text-center">
                    <div id="compressionRatio" class="text-3xl font-bold text-cyberpurple">
                        0.0% compression
                    </div>
                    <div class="text-sm text-gray-400 mt-1">Information density achieved</div>
                </div>
                
                <div class="mb-6 p-4 bg-[#1a1a3a] rounded-lg">
                    <h3 class="text-cyberblue mb-2 font-semibold">VRAM Savings</h3>
                    <div id="vramSavings" class="text-lg">
                        Original: 0.0GB → Compressed: 0.0GB
                    </div>
                </div>
                
                <div class="mb-4">
                    <h3 class="text-cyberpurple mb-2 font-semibold">Query Interface</h3>
                    <div class="flex">
                        <input 
                            type="text" 
                            id="queryInput"
                            class="flex-grow bg-[#1a1a3a] border border-cyberpurple/30 rounded-l-lg p-3 text-white focus:outline-none"
                            placeholder="Ask about the original text..."
                            aria-label="Query input"
                        >
                        <button 
                            class="bg-cyberpurple text-white px-4 rounded-r-lg hover:bg-purple-600 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
                            id="queryBtn"
                            aria-label="Ask question"
                        >
                            <i data-feather="search"></i>
                        </button>
                    </div>
                </div>
                
                <div id="responseArea" class="bg-[#1a1a3a] p-4 rounded-lg min-h-[100px]">
                    <p class="text-gray-400 italic">Responses will appear here based on the compressed vector...</p>
                </div>
            </div>
        </div>

        <!-- BOTTOM: Technical Explanation Panel -->
        <div class="mt-12 bg-panelbg rounded-xl p-6 cyber-border" data-aos="fade-up">
            <h2 class="text-xl font-semibold text-center mb-6 cyber-gradient bg-clip-text text-transparent">
                Technical Specifications
            </h2>
            
            <div class="space-y-4">
                <!-- How it Works -->
                <div class="collapse-container">
                    <button class="collapse-btn w-full text-left p-4 bg-[#1a1a3a] rounded-lg flex justify-between items-center focus:outline-none" aria-expanded="false" aria-controls="how-it-works">
                        <span class="font-semibold text-cyberblue">How It Works (TF-IDF + Dimensionality Reduction)</span>
                        <i data-feather="chevron-down" class="collapse-icon transition-transform duration-300"></i>
                    </button>
                    <div id="how-it-works" class="collapse-content hidden p-4 bg-[#1a1a3a] rounded-b-lg mt-1">
                        <p>Our compression algorithm uses Term Frequency-Inverse Document Frequency (TF-IDF) to identify the most semantically significant tokens in the conversation history. Through principal component analysis and neural embeddings, we reduce dimensionality from O(n) to a fixed 64-dimensional state vector while preserving semantic relationships. Now powered by a real Python backend using scikit-learn!</p>
                    </div>
                </div>
                
                <!-- VRAM Efficiency Theory -->
                <div class="collapse-container">
                    <button class="collapse-btn w-full text-left p-4 bg-[#1a1a3a] rounded-lg flex justify-between items-center">
                        <span class="font-semibold text-cyberpurple">VRAM Efficiency Theory (O(n²) vs O(1) Scaling)</span>
                        <i data-feather="chevron-down" class="collapse-icon"></i>
                    </button>
                    <div class="collapse-content hidden p-4 bg-[#1a1a3a] rounded-b-lg mt-1">
                        <p>Traditional attention mechanisms scale quadratically (O(n²)) with context length, making long conversations prohibitively expensive. Our state vector approach maintains constant memory usage (O(1)) regardless of original context size, enabling efficient processing of extensive dialogue histories without VRAM constraints.</p>
                    </div>
                </div>
                
                <!-- Information Preservation -->
                <div class="collapse-container">
                    <button class="collapse-btn w-full text-left p-4 bg-[#1a1a3a] rounded-lg flex justify-between items-center">
                        <span class="font-semibold text-cyberblue">Information Preservation (Shannon vs Gödelian Concepts)</span>
                        <i data-feather="chevron-down" class="collapse-icon"></i>
                    </button>
                    <div class="collapse-content hidden p-4 bg-[#1a1a3a] rounded-b-lg mt-1">
                        <p>While Shannon information theory focuses on probabilistic entropy, our approach incorporates Gödelian concepts of meaningful information preservation. The compression maintains not just statistical patterns but logical relationships and semantic coherence, allowing accurate reconstruction of conversation context from the minimal state vector.</p>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <footer class="py-6 mt-12 border-t cyber-border">
        <div class="container mx-auto px-6 text-center text-sm text-gray-400">
            <p>Syntactic Bunker © 2023 - Context Compression Technology Demo</p>
        </div>
    </footer>

    <script>
        // Backend endpoint configuration
        const BACKEND_URL = 'http://localhost:5000/compress'; // Adjust if needed

        // Global variables
        let isCompressed = false;
        let currentVector = [];

        // Initialize animations and icons
        AOS.init({
            duration: 800,
            once: true
        });

        // Initialize matrix grid
        function initializeMatrix() {
            const matrixGrid = document.getElementById('matrixGrid');
            matrixGrid.innerHTML = '';
            
            for (let i = 0; i < 64; i++) {
                const cell = document.createElement('div');
                cell.className = 'matrix-cell';
                cell.textContent = '0.00';
                cell.id = `matrix-cell-${i}`;
                cell.setAttribute('role', 'gridcell');
                cell.setAttribute('aria-label', `Dimension ${i + 1}`);
                matrixGrid.appendChild(cell);
            }
            matrixGrid.setAttribute('role', 'grid');
            matrixGrid.setAttribute('aria-label', 'Compressed state vector matrix');
        }
        
        // Update character count
        function updateCharCount() {
            const text = document.getElementById('inputText').value;
            document.getElementById('charCount').textContent = text.length;
        }

        // Show error message
        function showError(message) {
            const errorEl = document.getElementById('errorMessage');
            errorEl.textContent = message;
            errorEl.classList.remove('hidden');
        }

        // Hide error message
        function hideError() {
            document.getElementById('errorMessage').classList.add('hidden');
        }

        // Load sample text
        function loadSample() {
            const sampleText = `User: Tell me about AI context compression.
AI: Context compression is a technique to reduce the memory footprint of long conversations in large language models. It preserves key semantic information while discarding redundancy.
User: How does it work?
AI: It typically involves summarization, key-value extraction, or embedding-based reduction to a fixed-size vector.
User: What's the benefit for VRAM?
AI: It allows handling much longer contexts without exceeding GPU memory limits, scaling from O(n²) to O(1) in attention computation.`;
            document.getElementById('inputText').value = sampleText;
            updateCharCount();
            hideError();
        }
        
        // Compress text with backend
        async function compressText() {
            const inputText = document.getElementById('inputText').value.trim();
            if (!inputText) {
                showError('Please enter some conversation text first');
                return;
            }
            
            hideError();
            
            // Show processing animation
            const processingEl = document.getElementById('processingAnimation');
            const btn = document.getElementById('compressBtn');
            processingEl.classList.remove('hidden');
            btn.disabled = true;
            isCompressed = false;
            
            try {
                const response = await fetch(BACKEND_URL, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ text: inputText })
                });

                if (!response.ok) {
                    throw new Error(`Backend error: ${response.status}`);
                }

                const data = await response.json();
                currentVector = data.vector;

                // Update matrix display
                currentVector.forEach((value, index) => {
                    const cell = document.getElementById(`matrix-cell-${index}`);
                    cell.style.transition = 'all 0.5s ease';
                    cell.textContent = value.toFixed(2);
                    cell.style.color = parseFloat(value) > 0 ? '#00d9ff' : '#a855f7';
                });
                
                // Calculate compression metrics
                const inputLength = inputText.length;
                const originalSizeBytes = inputLength * 4; // Approximate 4 bytes per char
                const compressedSizeBytes = 64 * 8; // 64 floats * 8 bytes
                const compressionRatio = ((1 - compressedSizeBytes / originalSizeBytes) * 100).toFixed(1);
                
                document.getElementById('compressionRatio').textContent = 
                    `${Math.max(0, compressionRatio)}% compression achieved`;
                
                const originalSizeGB = (originalSizeBytes / (1024 ** 3)).toFixed(3);
                const compressedSizeGB = (compressedSizeBytes / (1024 ** 3)).toFixed(3);
                
                document.getElementById('vramSavings').innerHTML = 
                    `Original: <span class="text-cyberblue">${originalSizeGB}GB</span> → ` +
                    `Compressed: <span class="text-cyberpurple">${compressedSizeGB}GB</span>`;
                
                // Enable query
                document.getElementById('queryBtn').disabled = false;
                isCompressed = true;
                
            } catch (error) {
                console.error('Compression error:', error);
                showError('Failed to compress: ' + error.message + '. Ensure backend is running on localhost:5000.');
            } finally {
                // Hide processing animation
                processingEl.classList.add('hidden');
                btn.disabled = false;
            }
        }
        
        // Ask question simulation (can be extended to use vector)
        function askQuestion() {
            const question = document.getElementById('queryInput').value.trim();
            if (!question) {
                alert('Please enter a question');
                return;
            }
            
            if (!isCompressed) {
                alert('Please compress the text first');
                return;
            }
            
            const responses = [
                "Based on the compressed context, the conversation primarily discussed artificial intelligence and memory optimization techniques.",
                "The state vector indicates the dialogue focused on neural network architectures and efficiency improvements.",
                "From the compressed data, I can infer the conversation involved technical discussions about transformer models and their limitations.",
                "The semantic embeddings suggest the original text covered topics related to machine learning scalability challenges.",
                "Analysis of the state vector reveals discussions about context window limitations and potential solutions."
            ];
            
            const responseArea = document.getElementById('responseArea');
            const responseText = responses[Math.floor(Math.random() * responses.length)];
            responseArea.innerHTML = `<p class="typewriter text-cyberblue">${responseText}</p>`;
            
            // Clear input
            document.getElementById('queryInput').value = '';
        }
        
        // Collapse functionality with ARIA updates
        function initCollapses() {
            document.querySelectorAll('.collapse-btn').forEach(button => {
                button.addEventListener('click', () => {
                    const content = button.nextElementSibling;
                    const icon = button.querySelector('.collapse-icon');
                    const isExpanded = !content.classList.contains('hidden');
                    
                    content.classList.toggle('hidden');
                    button.setAttribute('aria-expanded', !isExpanded);
                    icon.setAttribute('data-feather', isExpanded ? 'chevron-down' : 'chevron-up');
                    feather.replace({ width: '20', height: '20' });
                });
            });
        }
        
        // Event listeners setup
        function initEventListeners() {
            document.getElementById('inputText').addEventListener('input', updateCharCount);
            document.getElementById('sampleBtn').addEventListener('click', loadSample);
            document.getElementById('compressBtn').addEventListener('click', compressText);
            document.getElementById('queryBtn').addEventListener('click', askQuestion);
            document.getElementById('queryInput').addEventListener('keypress', (e) => {
                if (e.key === 'Enter') askQuestion();
            });

            // Disable query button initially
            document.getElementById('queryBtn').disabled = true;
        }
        
        // Initialize on load
        document.addEventListener('DOMContentLoaded', () => {
            feather.replace();
            initializeMatrix();
            updateCharCount();
            initEventListeners();
            initCollapses();
        });
    </script>
</body>
</html>