forked from pengzhangzhi/Open-dLLM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual.html
More file actions
446 lines (379 loc) · 15.5 KB
/
visual.html
File metadata and controls
446 lines (379 loc) · 15.5 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Generation Visualizer</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;700&display=swap" rel="stylesheet">
<style>
/* General Styling */
body {
background-color: #1a1b26;
color: #c0c5f0;
font-family: 'Fira Code', 'Roboto Mono', monospace;
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
margin: 0;
padding: 2rem;
box-sizing: border-box;
}
/* Main Container */
.container {
width: 100%;
max-width: 900px;
display: flex;
flex-direction: column;
gap: 1.5rem;
}
/* Header and Controls */
.header {
text-align: center;
}
h1 {
font-size: 2.5rem;
margin-bottom: 0.5rem;
background: -webkit-linear-gradient(45deg, #ff79c6, #8be9fd);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
p {
font-size: 1rem;
color: #6272a4;
margin-top: 0;
}
/* Input Area */
.input-area textarea {
width: 100%;
height: 200px;
background-color: #282a36;
color: #f8f8f2;
border: 1px solid #44475a;
border-radius: 8px;
padding: 15px;
font-family: inherit;
font-size: 0.9rem;
resize: vertical;
box-sizing: border-box;
}
.input-area textarea::placeholder {
color: #6272a4;
}
.controls {
display: flex;
justify-content: center;
align-items: center;
gap: 1.5rem;
margin-top: 1rem;
flex-wrap: wrap;
}
.btn {
background-color: #44475a;
color: #f8f8f2;
border: none;
padding: 12px 24px;
border-radius: 8px;
cursor: pointer;
font-family: inherit;
font-size: 1rem;
transition: background-color 0.3s ease, transform 0.2s ease;
box-shadow: 0 4px 6px rgba(0,0,0,0.2);
}
/* Added style for record button */
.btn.record-btn {
background-color: #ff5f56;
}
.btn.record-btn.recording {
background-color: #e04a41;
animation: pulse 1.5s infinite;
}
@keyframes pulse {
0% { box-shadow: 0 0 0 0 rgba(255, 95, 86, 0.7); }
70% { box-shadow: 0 0 0 10px rgba(255, 95, 86, 0); }
100% { box-shadow: 0 0 0 0 rgba(255, 95, 86, 0); }
}
.btn:hover {
background-color: #6272a4;
}
.btn.record-btn:hover {
background-color: #e04a41;
}
.btn:disabled {
background-color: #3a3c4a;
cursor: not-allowed;
opacity: 0.6;
}
.slider-container {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.5rem;
color: #6272a4;
font-size: 0.9rem;
}
input[type="range"] {
-webkit-appearance: none;
appearance: none;
width: 150px;
height: 8px;
background: #44475a;
border-radius: 5px;
outline: none;
opacity: 0.8;
transition: opacity .2s;
}
input[type="range"]:hover {
opacity: 1;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
background: #8be9fd;
cursor: pointer;
border-radius: 50%;
}
input[type="range"]::-moz-range-thumb {
width: 20px;
height: 20px;
background: #8be9fd;
cursor: pointer;
border-radius: 50%;
}
/* Prompt Display */
.prompt-display {
background-color: #282a36;
border-radius: 8px;
padding: 20px;
border: 1px solid #44475a;
display: none; /* Hidden by default */
}
.prompt-display h3 {
margin: 0 0 10px 0;
color: #8be9fd;
font-size: 1.1rem;
}
.prompt-display p {
color: #f8f8f2;
margin: 0;
white-space: pre-wrap;
}
/* Editor Window Styling */
.editor-window {
background-color: #282a36;
border-radius: 8px;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
border: 1px solid #44475a;
overflow: hidden;
}
.title-bar {
background-color: #21222c;
padding: 12px;
display: flex;
align-items: center;
gap: 8px;
}
.title-bar::before {
content: ' ';
display: block;
width: 12px;
height: 12px;
border-radius: 50%;
background-color: #ff5f56;
box-shadow: 20px 0 0 #ffbd2e, 40px 0 0 #27c93f;
}
.code-body {
padding: 20px;
min-height: 450px;
font-size: 1rem;
line-height: 1.6;
overflow-x: auto;
position: relative; /* Needed for positioning the panes */
}
.code-pane {
position: absolute;
top: 20px; /* Match parent padding */
left: 20px; /* Match parent padding */
right: 20px;
bottom: 20px;
margin: 0;
opacity: 0;
transition: opacity 0.4s ease-in-out; /* Slower, smoother transition */
}
.code-pane.active {
opacity: 1;
}
.hljs { background: transparent !important; color: #f8f8f2 !important; }
</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/dracula.min.css">
</head>
<body>
<div class="container" id="capture-area">
<div class="header">
<h1>Code Generation Visualizer</h1>
<p>Paste your model's output below to see the generation process come to life.</p>
</div>
<div class="input-area">
<textarea id="text-input" placeholder="Paste your full generation output here, including the PROMPT, FINAL GENERATION, and GENERATION HISTORY sections..."></textarea>
</div>
<div class="controls">
<button class="btn" id="visualize-btn">Visualize</button>
<button class="btn record-btn" id="record-btn">Record & Export MP4</button>
<div class="slider-container">
<label for="speed-slider">Speed: <span id="speed-value">600</span>ms</label>
<input type="range" id="speed-slider" min="100" max="2000" value="600" step="50">
</div>
<button class="btn" id="download-btn">Download Files</button>
</div>
<div class="prompt-display" id="prompt-container">
<h3>Prompt:</h3>
<p id="prompt-content"></p>
</div>
<div class="editor-window">
<div class="title-bar"></div>
<div class="code-body">
<pre><code id="code-output-a" class="language-python code-pane"></code></pre>
<pre><code id="code-output-b" class="language-python code-pane"></code></pre>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://www.webrtc-experiment.com/screenshot.js"></script>
<script src="https://cdn.jsdelivr.net/npm/recordrtc@5.6.2/RecordRTC.min.js"></script>
<script>
// DOM Elements
const textInput = document.getElementById('text-input');
const visualizeBtn = document.getElementById('visualize-btn');
const downloadBtn = document.getElementById('download-btn');
const recordBtn = document.getElementById('record-btn');
const promptContainer = document.getElementById('prompt-container');
const promptContentEl = document.getElementById('prompt-content');
const codePaneA = document.getElementById('code-output-a');
const codePaneB = document.getElementById('code-output-b');
const speedSlider = document.getElementById('speed-slider');
const speedValueEl = document.getElementById('speed-value');
const captureArea = document.getElementById('capture-area');
let generationSteps = [];
let finalCode = "";
let promptText = "";
let animationTimeout;
let activePane = 'a';
let recorder = null;
// Event Listeners
visualizeBtn.addEventListener('click', () => handleVisualizeClick(false));
recordBtn.addEventListener('click', () => handleVisualizeClick(true));
downloadBtn.addEventListener('click', downloadFiles);
speedSlider.addEventListener('input', (e) => {
speedValueEl.textContent = e.target.value;
});
function handleVisualizeClick(isRecording) {
const content = textInput.value;
if (!content.trim()) {
alert("Please paste the generation output into the text area.");
return;
}
try {
parseContent(content);
runAnimation(isRecording);
} catch (error) { // FIXED: Added curly braces to the catch block
alert(`Error parsing content: ${error.message}`);
console.error(error);
}
}
function parseContent(content) {
generationSteps = [];
const promptMatch = content.match(/={25} PROMPT ={25}\n\n([\s\S]*?)\n\n={25}/);
promptText = promptMatch ? promptMatch[1].trim() : "Prompt not found.";
const finalGenMatch = content.match(/={25} FINAL GENERATION ={25}\n\n([\s\S]*?)\n\n={25}/);
finalCode = finalGenMatch ? finalGenMatch[1].trim() : "Final code not found.";
const historySectionMatch = content.match(/={25} GENERATION HISTORY ={25}\n\n([\s\S]*)/);
if (!historySectionMatch) {
throw new Error("Generation history section not found.");
}
const historySection = historySectionMatch[1];
const stepsRaw = historySection.trim().split(/--- Step \d+ ---\n/);
generationSteps = stepsRaw.slice(1).map(step => step.trim().replace(/\n\n$/, ''));
if (generationSteps.length === 0) {
throw new Error("No generation steps found in the history section.");
}
generationSteps.push(finalCode);
}
function runAnimation(isRecording) {
if (animationTimeout) {
clearTimeout(animationTimeout);
}
visualizeBtn.disabled = true;
recordBtn.disabled = true;
promptContentEl.textContent = promptText;
promptContainer.style.display = 'block';
codePaneA.innerHTML = '';
codePaneB.innerHTML = '';
codePaneA.classList.remove('active');
codePaneB.classList.remove('active');
if (isRecording) {
recorder = RecordRTC(captureArea, {
type: 'canvas',
recorderType: CanvasRecorder,
mimeType: 'video/webm',
bitsPerSecond: 128000
});
recorder.startRecording();
recordBtn.classList.add('recording');
recordBtn.textContent = "Recording...";
}
let stepIndex = 0;
function animateNextStep() {
if (stepIndex >= generationSteps.length) {
visualizeBtn.disabled = false;
recordBtn.disabled = false;
if (isRecording) {
stopAndDownloadRecording();
}
return;
}
const currentActivePane = (activePane === 'a') ? codePaneA : codePaneB;
const currentInactivePane = (activePane === 'a') ? codePaneB : codePaneA;
currentInactivePane.textContent = generationSteps[stepIndex];
hljs.highlightElement(currentInactivePane);
currentActivePane.classList.remove('active');
currentInactivePane.classList.add('active');
activePane = (activePane === 'a') ? 'b' : 'a';
stepIndex++;
animationTimeout = setTimeout(animateNextStep, parseInt(speedSlider.value));
}
animationTimeout = setTimeout(animateNextStep, 1500);
}
function stopAndDownloadRecording() {
if (!recorder) return;
recorder.stopRecording(function() {
const blob = recorder.getBlob();
invokeSaveAsDialog(blob, 'Code-Generation-Animation.mp4');
recorder = null;
recordBtn.classList.remove('recording');
recordBtn.textContent = "Record & Export MP4";
});
}
function downloadFiles() {
const htmlContent = document.documentElement.outerHTML;
downloadAsFile("visualizer.html", htmlContent, "text/html");
const readmeContent = `# Code Generation Visualizer\n\nTo use, open \`visualizer.html\` and paste your model output.`;
downloadAsFile("README.md", readmeContent, "text/markdown");
const exampleContent = `========================= PROMPT =========================\n\nWrite a quick sort algorithm in python.\n\n========================= FINAL GENERATION =========================\n\ndef quick_sort(arr):\n if len(arr) <= 1:\n return arr\n pivot = arr[0]\n left = [x for x in arr if x < pivot]\n middle = [x for x in arr if x == pivot]\n right = [x for x in arr if x > pivot]\n return quick_sort(left) + middle + quick_sort(right)\n\n========================= GENERATION HISTORY =========================\n\n--- Step 1 ---\n def\n\n--- Step 2 ---\n def quick_sort(arr):\n\n--- Step 3 ---\n def quick_sort(arr):\n if len(arr) <= 1:\n return arr\n\n--- Step 4 ---\n def quick_sort(arr):\n if len(arr) <= 1:\n return arr\n pivot = arr[0]\n left = [x for x in arr if x < pivot]\n middle = [x for x in arr if x == pivot]\n right = [x for x in arr if x > pivot]\n`;
downloadAsFile("example_output.txt", exampleContent, "text/plain");
}
function downloadAsFile(filename, content, mimeType) {
const blob = new Blob([content], { type: mimeType });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
</script>
</body>
</html>