-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.html
More file actions
691 lines (623 loc) · 21.2 KB
/
chat.html
File metadata and controls
691 lines (623 loc) · 21.2 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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JPGPT — 故事续写 & 训练监控</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg: #0f1117;
--surface: #1a1d27;
--border: #2a2d3e;
--accent: #6c63ff;
--accent2: #00d4aa;
--text: #e8eaf6;
--text-muted: #7b7f9e;
--loss-color: #ff6b6b;
--tok-color: #6c63ff;
}
body {
background: var(--bg);
color: var(--text);
font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
min-height: 100vh;
}
/* ── 顶部标题栏 ─────────────────────────────────── */
header {
padding: 20px 32px;
border-bottom: 1px solid var(--border);
display: flex;
align-items: center;
gap: 16px;
}
header .logo {
font-size: 22px;
font-weight: 700;
background: linear-gradient(135deg, var(--accent), var(--accent2));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
letter-spacing: -0.5px;
}
header .subtitle { color: var(--text-muted); font-size: 13px; }
.status-badge {
margin-left: auto;
display: flex;
align-items: center;
gap: 8px;
font-size: 12px;
color: var(--text-muted);
}
.dot {
width: 8px; height: 8px; border-radius: 50%;
background: #555;
animation: none;
}
.dot.ready { background: var(--accent2); animation: pulse 2s infinite; }
.dot.training { background: #ffc107; animation: pulse 1s infinite; }
@keyframes pulse {
0%, 100% { opacity: 1; } 50% { opacity: 0.4; }
}
/* ── 两栏布局 ───────────────────────────────────── */
.layout {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0;
height: calc(100vh - 73px);
}
/* ── 左栏:故事续写 ─────────────────────────────── */
.panel {
padding: 24px 28px;
border-right: 1px solid var(--border);
display: flex;
flex-direction: column;
gap: 16px;
overflow-y: auto;
}
.panel-title {
font-size: 13px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 1px;
color: var(--text-muted);
}
/* 示例开头 */
.examples-label {
font-size: 11px;
color: var(--text-muted);
text-transform: uppercase;
letter-spacing: 0.5px;
margin-bottom: 6px;
}
.examples-grid {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 4px;
}
.example-chip {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 20px;
padding: 5px 12px;
font-size: 12px;
color: var(--text-muted);
cursor: pointer;
transition: border-color 0.2s, color 0.2s;
white-space: nowrap;
}
.example-chip:hover {
border-color: var(--accent);
color: var(--accent);
}
/* 提示词输入 */
textarea {
width: 100%;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 10px;
padding: 14px 16px;
color: var(--text);
font-size: 15px;
line-height: 1.6;
resize: vertical;
min-height: 100px;
outline: none;
transition: border-color 0.2s;
}
textarea:focus { border-color: var(--accent); }
textarea::placeholder { color: var(--text-muted); }
/* 参数调节 */
.params-row {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 12px;
}
.param-group label {
display: block;
font-size: 11px;
color: var(--text-muted);
margin-bottom: 6px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.param-group .param-val {
font-size: 13px;
color: var(--accent);
font-weight: 600;
float: right;
}
input[type="range"] {
width: 100%;
accent-color: var(--accent);
cursor: pointer;
}
input[type="number"] {
width: 100%;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 6px;
padding: 6px 10px;
color: var(--text);
font-size: 13px;
outline: none;
}
/* 生成按钮 */
.btn-generate {
width: 100%;
padding: 13px;
background: linear-gradient(135deg, var(--accent), #5a52d5);
border: none;
border-radius: 10px;
color: #fff;
font-size: 15px;
font-weight: 600;
cursor: pointer;
transition: opacity 0.2s, transform 0.1s;
letter-spacing: 0.3px;
}
.btn-generate:hover { opacity: 0.9; }
.btn-generate:active { transform: scale(0.98); }
.btn-generate:disabled { opacity: 0.5; cursor: not-allowed; }
/* 故事输出框 */
.story-box {
flex: 1;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 10px;
padding: 16px;
min-height: 200px;
font-size: 15px;
line-height: 1.8;
overflow-y: auto;
}
.story-box .prompt-part { color: var(--text-muted); }
.story-box .gen-part { color: var(--text); }
.story-box .placeholder { color: var(--text-muted); font-style: italic; }
.typing-cursor {
display: inline-block;
width: 2px; height: 1em;
background: var(--accent);
margin-left: 2px;
vertical-align: text-bottom;
animation: blink 1s infinite;
}
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }
/* 元信息 */
.meta-row {
display: flex;
gap: 16px;
font-size: 12px;
color: var(--text-muted);
}
.meta-row span { display: flex; align-items: center; gap: 4px; }
/* ── 右栏:训练监控 ─────────────────────────────── */
.monitor {
padding: 24px 28px;
display: flex;
flex-direction: column;
gap: 20px;
overflow-y: auto;
}
/* 指标卡片 */
.metrics-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.metric-card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 10px;
padding: 14px 16px;
}
.metric-card .label-en {
font-size: 11px;
font-weight: 700;
letter-spacing: 1px;
color: var(--text-muted);
text-transform: uppercase;
}
.metric-card .label-zh {
font-size: 11px;
color: var(--text-muted);
opacity: 0.6;
margin-top: 1px;
}
.metric-card .value {
font-size: 28px;
font-weight: 700;
margin-top: 6px;
background: linear-gradient(135deg, var(--accent), var(--accent2));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.metric-card .value.warn { background: none; -webkit-text-fill-color: #ffc107; }
/* 进度条 */
.progress-section .progress-label {
display: flex;
justify-content: space-between;
font-size: 12px;
color: var(--text-muted);
margin-bottom: 8px;
}
.progress-bar-track {
height: 6px;
background: var(--border);
border-radius: 3px;
overflow: hidden;
}
.progress-bar-fill {
height: 100%;
background: linear-gradient(90deg, var(--accent), var(--accent2));
border-radius: 3px;
transition: width 0.5s ease;
width: 0%;
}
/* Loss 曲线 */
.chart-section {}
.chart-section canvas {
width: 100%;
height: 200px;
display: block;
}
/* 训练日志最新条目 */
.log-section {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 10px;
padding: 14px;
font-family: 'SF Mono', 'Fira Code', monospace;
font-size: 12px;
color: var(--accent2);
min-height: 60px;
}
.log-line { margin-bottom: 3px; opacity: 0.7; }
.log-line:last-child { opacity: 1; color: var(--text); }
/* 刷新按钮 */
.btn-refresh {
align-self: flex-start;
padding: 6px 14px;
background: transparent;
border: 1px solid var(--border);
border-radius: 6px;
color: var(--text-muted);
font-size: 12px;
cursor: pointer;
transition: border-color 0.2s, color 0.2s;
}
.btn-refresh:hover { border-color: var(--accent); color: var(--accent); }
/* 未训练提示 */
.not-ready {
background: var(--surface);
border: 1px dashed var(--border);
border-radius: 10px;
padding: 32px;
text-align: center;
color: var(--text-muted);
font-size: 14px;
line-height: 1.8;
}
.not-ready strong { color: var(--text); display: block; margin-bottom: 8px; font-size: 16px; }
</style>
</head>
<body>
<header>
<div>
<div class="logo">JPGPT</div>
<div class="subtitle">0.1B GPT · TinyStories · 从零训练</div>
</div>
<div class="status-badge">
<div class="dot" id="statusDot"></div>
<span id="statusText">检查状态中...</span>
</div>
</header>
<div class="layout">
<!-- ── 左栏:故事续写 ── -->
<div class="panel">
<div class="panel-title">故事续写 / Story Completion</div>
<div>
<div class="examples-label">示例开头(点击填入)</div>
<div class="examples-grid">
<div class="example-chip" onclick="fillPrompt(this)">Once upon a time, there was a little cat named Mochi who loved to explore the forest.</div>
<div class="example-chip" onclick="fillPrompt(this)">There was a brave little rabbit named Benny who wanted to find the biggest carrot in the world.</div>
<div class="example-chip" onclick="fillPrompt(this)">Lily was a small girl who had a magic crayon. Whatever she drew came to life.</div>
<div class="example-chip" onclick="fillPrompt(this)">The dragon was sad because he could not breathe fire like the other dragons.</div>
<div class="example-chip" onclick="fillPrompt(this)">Tom the turtle walked very slowly, but he had a secret — he could talk to the stars.</div>
<div class="example-chip" onclick="fillPrompt(this)">One rainy day, a little puppy found a mysterious door at the end of the garden.</div>
</div>
</div>
<textarea id="prompt" placeholder="Once upon a time, there was a little cat named Mochi... (输入故事开头,模型帮你续写)" rows="4"></textarea>
<div class="params-row">
<div class="param-group">
<label>续写长度 <span class="param-val" id="lenVal">200</span></label>
<input type="range" id="maxTokens" min="50" max="500" value="200" step="10"
oninput="document.getElementById('lenVal').textContent=this.value">
</div>
<div class="param-group">
<label>Temperature <span class="param-val" id="tempVal">0.8</span></label>
<input type="range" id="temperature" min="0.1" max="2.0" value="0.8" step="0.05"
oninput="document.getElementById('tempVal').textContent=(+this.value).toFixed(2)">
</div>
<div class="param-group">
<label>Top-K <span class="param-val" id="topkVal">50</span></label>
<input type="range" id="topK" min="1" max="200" value="50"
oninput="document.getElementById('topkVal').textContent=this.value">
</div>
</div>
<button class="btn-generate" id="genBtn" onclick="generate()">生成故事</button>
<div class="story-box" id="storyBox">
<span class="placeholder">生成的故事将显示在这里...</span>
</div>
<div class="meta-row" id="metaRow" style="display:none">
<span>⏱ <span id="elapsed">-</span> ms</span>
<span>📝 新生成 <span id="newToks">-</span> tokens</span>
</div>
</div>
<!-- ── 右栏:训练监控 ── -->
<div class="monitor">
<div style="display:flex;align-items:center;justify-content:space-between">
<div class="panel-title">训练监控 / Training Monitor</div>
<button class="btn-refresh" onclick="refreshMonitor()">↻ 刷新</button>
</div>
<div id="monitorContent">
<div class="not-ready">
<strong>模型尚未就绪</strong>
训练开始后,每 500 步保存一次 checkpoint<br>
届时点击「刷新」即可看到训练进度
</div>
</div>
</div>
</div>
<script>
// ─── 状态检查 ──────────────────────────────────────────────
async function checkStatus() {
try {
const r = await fetch('/status');
const d = await r.json();
const dot = document.getElementById('statusDot');
const text = document.getElementById('statusText');
if (d.model_loaded) {
dot.className = 'dot ready';
text.textContent = `模型已就绪 · step ${d.last_step}`;
} else if (d.checkpoint_exists) {
dot.className = 'dot training';
text.textContent = `训练中 · step ${d.last_step} · loss ${d.last_loss?.toFixed(4) ?? '-'}`;
} else {
dot.className = 'dot';
text.textContent = '等待训练开始...';
}
return d;
} catch (e) {
document.getElementById('statusText').textContent = '服务器未连接';
return null;
}
}
// ─── 故事生成 ──────────────────────────────────────────────
async function generate() {
const prompt = document.getElementById('prompt').value.trim();
const maxToks = parseInt(document.getElementById('maxTokens').value);
const temp = parseFloat(document.getElementById('temperature').value);
const topK = parseInt(document.getElementById('topK').value);
const btn = document.getElementById('genBtn');
const box = document.getElementById('storyBox');
if (!prompt) { alert('请先输入故事开头'); return; }
btn.disabled = true;
btn.textContent = '生成中...';
box.innerHTML = `<span class="prompt-part">${escapeHtml(prompt)}</span><span class="typing-cursor"></span>`;
try {
const r = await fetch('/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt, max_new_tokens: maxToks, temperature: temp, top_k: topK })
});
const d = await r.json();
if (d.error) {
box.innerHTML = `<span style="color:#ff6b6b">⚠ ${escapeHtml(d.error)}</span>`;
if (d.tip) box.innerHTML += `<br><span style="color:var(--text-muted);font-size:13px">${escapeHtml(d.tip)}</span>`;
} else {
box.innerHTML =
`<span class="prompt-part">${escapeHtml(d.prompt)}</span>` +
`<span class="gen-part">${escapeHtml(d.generated)}</span>`;
document.getElementById('metaRow').style.display = 'flex';
document.getElementById('elapsed').textContent = d.elapsed_ms;
document.getElementById('newToks').textContent = d.new_tokens;
}
} catch (e) {
box.innerHTML = `<span style="color:#ff6b6b">⚠ 连接服务器失败:${e.message}</span>`;
} finally {
btn.disabled = false;
btn.textContent = '生成故事';
}
}
function escapeHtml(s) {
return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/\n/g,'<br>');
}
// ─── 训练监控 ──────────────────────────────────────────────
let lossChart = null;
async function refreshMonitor() {
const status = await checkStatus();
if (!status) return;
// 获取训练日志
let logs = [];
try {
const r = await fetch('/train_log');
logs = await r.json();
} catch (e) {}
const container = document.getElementById('monitorContent');
if (!status.checkpoint_exists && logs.length === 0) {
container.innerHTML = `
<div class="not-ready">
<strong>模型尚未就绪</strong>
训练开始后,每 500 步保存一次 checkpoint<br>
届时点击「刷新」即可看到训练进度
</div>`;
return;
}
const last = logs.length > 0 ? logs[logs.length - 1] : {};
const TARGET = 2_000_000_000;
const pct = last.tokens_seen ? (last.tokens_seen / TARGET * 100).toFixed(1) : 0;
const loss = last.loss ? last.loss.toFixed(4) : '--';
const tokps = last.tok_per_sec ? (last.tok_per_sec / 1000).toFixed(1) + 'K' : '--';
const tokSeen = last.tokens_seen ? (last.tokens_seen / 1e6).toFixed(0) + 'M' : '0';
const remaining = last.tokens_seen ? TARGET - last.tokens_seen : TARGET;
const etaH = last.tok_per_sec && last.tok_per_sec > 0
? (remaining / last.tok_per_sec / 3600).toFixed(1)
: '--';
container.innerHTML = `
<div class="metrics-grid">
<div class="metric-card">
<div class="label-en">Loss</div><div class="label-zh">训练损失</div>
<div class="value">${loss}</div>
</div>
<div class="metric-card">
<div class="label-en">tok/s</div><div class="label-zh">训练速度</div>
<div class="value">${tokps}</div>
</div>
<div class="metric-card">
<div class="label-en">Tokens Seen</div><div class="label-zh">已看数据量</div>
<div class="value">${tokSeen}</div>
</div>
<div class="metric-card">
<div class="label-en">ETA</div><div class="label-zh">预计剩余</div>
<div class="value ${etaH === '--' ? 'warn' : ''}">${etaH === '--' ? '--' : etaH + 'h'}</div>
</div>
</div>
<div class="progress-section">
<div class="progress-label">
<span>训练进度</span>
<span>${pct}% · 目标 2B tokens</span>
</div>
<div class="progress-bar-track">
<div class="progress-bar-fill" id="progFill"></div>
</div>
</div>
<div class="chart-section">
<div class="panel-title" style="margin-bottom:12px">Loss 曲线</div>
<canvas id="lossCanvas"></canvas>
</div>
<div class="log-section" id="logLines">最新日志...</div>
`;
// 设置进度条
setTimeout(() => {
const fill = document.getElementById('progFill');
if (fill) fill.style.width = pct + '%';
}, 100);
// 绘制 loss 曲线
if (logs.length > 1) {
drawLossChart(logs);
}
// 最近几条日志
const logEl = document.getElementById('logLines');
if (logEl && logs.length > 0) {
const recent = logs.slice(-5);
logEl.innerHTML = recent.map(l =>
`<div class="log-line">step ${l.step} | loss ${l.loss?.toFixed(4)} | ${(l.tok_per_sec/1000).toFixed(1)}K tok/s</div>`
).join('');
}
}
function drawLossChart(logs) {
const canvas = document.getElementById('lossCanvas');
if (!canvas) return;
// 每隔几个点采样,避免太密
const step = Math.max(1, Math.floor(logs.length / 200));
const sampled = logs.filter((_, i) => i % step === 0);
const steps = sampled.map(l => l.step);
const losses = sampled.map(l => l.loss);
const minL = Math.min(...losses);
const maxL = Math.max(...losses);
const range = maxL - minL || 1;
const W = canvas.parentElement.offsetWidth - 56;
const H = 200;
canvas.width = W;
canvas.height = H;
const ctx = canvas.getContext('2d');
const pad = { top: 20, right: 20, bottom: 30, left: 50 };
const iW = W - pad.left - pad.right;
const iH = H - pad.top - pad.bottom;
ctx.clearRect(0, 0, W, H);
// 背景网格
ctx.strokeStyle = 'rgba(255,255,255,0.05)';
ctx.lineWidth = 1;
for (let i = 0; i <= 4; i++) {
const y = pad.top + (iH * i / 4);
ctx.beginPath(); ctx.moveTo(pad.left, y); ctx.lineTo(pad.left + iW, y); ctx.stroke();
const val = (maxL - range * i / 4).toFixed(3);
ctx.fillStyle = 'rgba(123,127,158,0.8)';
ctx.font = '10px monospace';
ctx.textAlign = 'right';
ctx.fillText(val, pad.left - 6, y + 4);
}
// Loss 曲线(渐变填充)
const grad = ctx.createLinearGradient(0, pad.top, 0, pad.top + iH);
grad.addColorStop(0, 'rgba(108,99,255,0.4)');
grad.addColorStop(1, 'rgba(108,99,255,0.0)');
ctx.beginPath();
sampled.forEach((l, i) => {
const x = pad.left + (i / (sampled.length - 1)) * iW;
const y = pad.top + ((maxL - l.loss) / range) * iH;
i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
});
// 填充区域
ctx.lineTo(pad.left + iW, pad.top + iH);
ctx.lineTo(pad.left, pad.top + iH);
ctx.closePath();
ctx.fillStyle = grad;
ctx.fill();
// 曲线本体
ctx.beginPath();
sampled.forEach((l, i) => {
const x = pad.left + (i / (sampled.length - 1)) * iW;
const y = pad.top + ((maxL - l.loss) / range) * iH;
i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
});
ctx.strokeStyle = '#6c63ff';
ctx.lineWidth = 2;
ctx.stroke();
// X 轴标签
ctx.fillStyle = 'rgba(123,127,158,0.8)';
ctx.font = '10px monospace';
ctx.textAlign = 'center';
[0, 0.25, 0.5, 0.75, 1].forEach(t => {
const i = Math.floor(t * (sampled.length - 1));
const x = pad.left + t * iW;
ctx.fillText(`s${steps[i]}`, x, H - 6);
});
}
// ─── 示例填入 ───────────────────────────────────────────────
function fillPrompt(el) {
document.getElementById('prompt').value = el.textContent.trim();
document.getElementById('prompt').focus();
}
// ─── 键盘快捷键 ────────────────────────────────────────────
document.addEventListener('keydown', e => {
if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') generate();
});
// ─── 初始化 ────────────────────────────────────────────────
checkStatus();
refreshMonitor();
setInterval(checkStatus, 10000); // 每 10 秒更新状态
setInterval(refreshMonitor, 30000); // 每 30 秒刷新监控
</script>
</body>
</html>