Curious1008 commited on
Commit
b7948be
·
verified ·
1 Parent(s): 867e90f

Upload static/index.html with huggingface_hub

Browse files
Files changed (1) hide show
  1. static/index.html +463 -0
static/index.html ADDED
@@ -0,0 +1,463 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Aversion</title>
6
+ <style>
7
+ * { box-sizing: border-box; margin: 0; padding: 0; }
8
+ body { font-family: -apple-system, "PingFang SC", sans-serif; background: #0a0a0a; color: #e0e0e0; }
9
+
10
+ .app { max-width: 900px; margin: 0 auto; padding: 20px; }
11
+ h1 { font-size: 22px; margin-bottom: 20px; }
12
+ h1 span { color: #555; font-weight: normal; font-size: 13px; }
13
+
14
+ /* 输入区 */
15
+ .input-section { margin-bottom: 16px; }
16
+ textarea {
17
+ width: 100%; height: 180px; background: #151515; color: #e0e0e0;
18
+ border: 1px solid #2a2a2a; border-radius: 10px; padding: 14px;
19
+ font-size: 14px; line-height: 1.7; resize: vertical; font-family: inherit;
20
+ }
21
+ textarea:focus { outline: none; border-color: #3b82f6; }
22
+ .controls { margin-top: 10px; display: flex; gap: 10px; align-items: center; }
23
+ button.primary {
24
+ background: #3b82f6; color: #fff; border: none; border-radius: 8px;
25
+ padding: 10px 28px; font-size: 14px; cursor: pointer; font-weight: 500;
26
+ }
27
+ button.primary:hover { background: #2563eb; }
28
+ button.primary:disabled { background: #333; cursor: not-allowed; }
29
+ select { background: #151515; color: #ccc; border: 1px solid #2a2a2a; border-radius: 6px; padding: 8px; font-size: 13px; }
30
+ .timer { color: #555; font-size: 13px; }
31
+
32
+ /* 分析区 */
33
+ .analysis-section {
34
+ background: #111; border: 1px solid #1e1e1e; border-radius: 10px;
35
+ padding: 14px 16px; margin-bottom: 16px; display: none;
36
+ }
37
+ .analysis-section h3 { font-size: 13px; color: #888; margin-bottom: 8px; }
38
+ .analysis-section .facts { font-size: 13px; color: #93c5fd; line-height: 1.8; }
39
+ .analysis-section .meta { font-size: 12px; color: #666; margin-top: 6px; }
40
+ .analysis-section .loading { color: #60a5fa; font-size: 13px; }
41
+
42
+ /* 改写区 */
43
+ .rewrite-section { display: none; }
44
+ .rewrite-header {
45
+ font-size: 13px; color: #666; margin-bottom: 8px;
46
+ display: flex; justify-content: space-between; align-items: center;
47
+ }
48
+ .rewrite-status { color: #60a5fa; }
49
+ .output-box {
50
+ background: #111; border: 1px solid #1e1e1e; border-radius: 10px;
51
+ padding: 18px 20px; min-height: 150px; max-height: 600px; overflow-y: auto;
52
+ font-size: 15px; line-height: 2; white-space: pre-wrap; position: relative;
53
+ outline: none;
54
+ }
55
+ .output-box .cursor { display: inline-block; width: 2px; height: 18px; background: #3b82f6; animation: blink 0.7s infinite; vertical-align: text-bottom; }
56
+ .output-box.done .cursor { display: none; }
57
+ @keyframes blink { 50% { opacity: 0; } }
58
+
59
+ /* 高亮 */
60
+ .output-box .highlight { background: rgba(59, 130, 246, 0.2); border-radius: 3px; transition: background 0.3s; }
61
+
62
+ /* 浮动工具栏 (选中文字时) */
63
+ .float-toolbar {
64
+ display: none; position: fixed; background: #1e293b; border: 1px solid #334155;
65
+ border-radius: 8px; padding: 6px; z-index: 100; box-shadow: 0 4px 12px rgba(0,0,0,0.5);
66
+ }
67
+ .float-toolbar button {
68
+ background: none; border: none; color: #93c5fd; padding: 6px 12px;
69
+ font-size: 13px; cursor: pointer; border-radius: 4px; white-space: nowrap;
70
+ }
71
+ .float-toolbar button:hover { background: #334155; }
72
+
73
+ /* 改写说明 */
74
+ .reasoning-section {
75
+ background: #0d1117; border: 1px solid #1e1e1e; border-radius: 10px;
76
+ padding: 14px 16px; margin-top: 12px; display: none;
77
+ }
78
+ .reasoning-section h3 { font-size: 13px; color: #888; margin-bottom: 10px; }
79
+ .change-item {
80
+ padding: 8px 0; border-bottom: 1px solid #161b22; font-size: 13px;
81
+ cursor: pointer; transition: background 0.2s;
82
+ }
83
+ .change-item:hover { background: #161b22; border-radius: 4px; padding-left: 8px; }
84
+ .change-item .what { color: #e0e0e0; }
85
+ .change-item .why { color: #8b949e; font-size: 12px; margin-top: 2px; }
86
+ .change-item .span-preview { color: #58a6ff; font-size: 12px; margin-top: 2px; font-style: italic; }
87
+
88
+ .stats-bar {
89
+ display: flex; gap: 16px; margin-top: 10px; padding-top: 10px;
90
+ border-top: 1px solid #161b22; font-size: 12px; color: #666;
91
+ }
92
+ .stats-bar b { color: #8b949e; }
93
+ </style>
94
+ </head>
95
+ <body>
96
+
97
+ <div class="app">
98
+ <h1>Aversion <span>内容改写工具</span></h1>
99
+
100
+ <!-- 输入 -->
101
+ <div class="input-section">
102
+ <textarea id="input" placeholder="粘贴一段 AI 生成的文案, 点击改写..."></textarea>
103
+ <div class="controls">
104
+ <button class="primary" id="btn" onclick="doRewrite()">改写</button>
105
+ <select id="mode">
106
+ <option value="prod" selected>标准模式</option>
107
+ <option value="dev">评估模式 (含评分)</option>
108
+ </select>
109
+ <span class="timer" id="timer"></span>
110
+ </div>
111
+ </div>
112
+
113
+ <!-- 分析 -->
114
+ <div class="analysis-section" id="analysis">
115
+ <h3>🔍 内容分析</h3>
116
+ <div id="analysis-content"></div>
117
+ </div>
118
+
119
+ <!-- 改写 -->
120
+ <div class="rewrite-section" id="rewriteSection">
121
+ <div class="rewrite-header">
122
+ <span>✍️ 改写结果</span>
123
+ <span class="rewrite-status" id="rewriteStatus"></span>
124
+ </div>
125
+ <div class="output-box" id="output" contenteditable="true" spellcheck="false">
126
+ <span class="cursor"></span>
127
+ </div>
128
+ </div>
129
+
130
+ <!-- 改写说明 -->
131
+ <div class="reasoning-section" id="reasoning">
132
+ <h3>📝 改写说明 <span style="font-weight:normal;color:#555">(点击可高亮对应文本)</span></h3>
133
+ <div id="changes-list"></div>
134
+ <div class="stats-bar" id="stats"></div>
135
+ </div>
136
+ </div>
137
+
138
+ <!-- 浮动工具栏 -->
139
+ <div class="float-toolbar" id="floatToolbar">
140
+ <button onclick="spanRewrite('更自然')">🔄 重新生成</button>
141
+ <button onclick="spanRewrite('更口语化,像朋友聊天')">💬 更口语</button>
142
+ <button onclick="spanRewrite('更正式严谨')">📋 更正式</button>
143
+ <button onclick="customSpanRewrite()">✏️ 自定义</button>
144
+ </div>
145
+
146
+ <script>
147
+ const $ = id => document.getElementById(id);
148
+
149
+ let currentOutput = '';
150
+ let selectionRange = null;
151
+ let rewriteContext = null; // 存初次改写的完整 context (facts/voice/industry)
152
+
153
+ // ============================================================
154
+ // 主改写流程
155
+ // ============================================================
156
+ async function doRewrite() {
157
+ const text = $('input').value.trim();
158
+ if (!text) return alert('请粘贴一段文案');
159
+ if (text.length < 50) return alert('至少 50 字');
160
+
161
+ const mode = $('mode').value;
162
+ const btn = $('btn');
163
+
164
+ btn.disabled = true;
165
+ btn.textContent = '改写中...';
166
+ $('analysis').style.display = 'block';
167
+ $('analysis-content').innerHTML = '<span class="loading">分析中, 正在提取关键信息...</span>';
168
+ $('rewriteSection').style.display = 'none';
169
+ $('reasoning').style.display = 'none';
170
+ $('output').innerHTML = '<span class="cursor"></span>';
171
+ $('output').classList.remove('done');
172
+ $('stats').innerHTML = '';
173
+ $('changes-list').innerHTML = '';
174
+ currentOutput = '';
175
+
176
+ const startTime = Date.now();
177
+ const timerInterval = setInterval(() => {
178
+ $('timer').textContent = ((Date.now() - startTime) / 1000).toFixed(1) + 's';
179
+ }, 100);
180
+
181
+ try {
182
+ const resp = await fetch('/api/v1/rewrite/stream', {
183
+ method: 'POST',
184
+ headers: {'Content-Type': 'application/json'},
185
+ body: JSON.stringify({text, mode}),
186
+ });
187
+
188
+ const reader = resp.body.getReader();
189
+ const decoder = new TextDecoder();
190
+ let buffer = '';
191
+
192
+ while (true) {
193
+ const {done, value} = await reader.read();
194
+ if (done) break;
195
+
196
+ buffer += decoder.decode(value, {stream: true}).replace(/\r\n/g, '\n');
197
+ const lines = buffer.split('\n');
198
+ buffer = lines.pop();
199
+
200
+ for (const line of lines) {
201
+ const trimmed = line.trim();
202
+ if (!trimmed || trimmed.startsWith(':') || !trimmed.startsWith('data:')) continue;
203
+ let evt;
204
+ try { evt = JSON.parse(trimmed.substring(5).trim()); } catch { continue; }
205
+ handleEvent(evt);
206
+ }
207
+ }
208
+ } catch (e) {
209
+ $('analysis-content').innerHTML = `<span style="color:#f87171">连接失败: ${e.message}</span>`;
210
+ }
211
+
212
+ clearInterval(timerInterval);
213
+ $('output').classList.add('done');
214
+ btn.disabled = false;
215
+ btn.textContent = '改写';
216
+ }
217
+
218
+ // ============================================================
219
+ // 事件处理
220
+ // ============================================================
221
+ function handleEvent(evt) {
222
+ const t = evt.type;
223
+ const s = evt.step || '';
224
+ const d = evt.data || {};
225
+
226
+ // 分析完成 — 存 context 给后续 span rewrite 用
227
+ if (t === 'step_done' && s === 'analyze') {
228
+ const facts = d.facts || [];
229
+ const exps = d.experiences || [];
230
+
231
+ // 存完整 context
232
+ rewriteContext = {
233
+ facts: facts,
234
+ experiences: exps,
235
+ emotions: d.emotions || [],
236
+ voice_hint: d.voice_hint || '',
237
+ industry_hint: d.industry_hint || '',
238
+ target_platform: d.target_platform || 'xiaohongshu',
239
+ intent: d.intent || '',
240
+ };
241
+
242
+ let html = '';
243
+ if (facts.length) {
244
+ html += `<div class="facts">📋 发现 ${facts.length} 个关键事实: ${facts.slice(0, 6).join(' · ')}${facts.length > 6 ? ' ...' : ''}</div>`;
245
+ }
246
+ if (exps.length) {
247
+ html += `<div class="facts" style="margin-top:4px">📖 ${exps.length} 段个人经历</div>`;
248
+ }
249
+ html += `<div class="meta">🎯 ${d.target_platform || '小红书'} · ${d.industry_hint || '通用'} · ${d.voice_hint || ''}</div>`;
250
+ $('analysis-content').innerHTML = html;
251
+ }
252
+
253
+ // 检索完成 → 显示改写区
254
+ if (t === 'step_done' && s === 'retrieve') {
255
+ $('rewriteSection').style.display = 'block';
256
+ $('rewriteStatus').textContent = '改写中...';
257
+ }
258
+
259
+ // 流式 token
260
+ if (t === 'rewrite_token') {
261
+ currentOutput += d.token;
262
+ const cursor = $('output').querySelector('.cursor');
263
+ if (cursor) cursor.insertAdjacentText('beforebegin', d.token);
264
+ $('output').scrollTop = $('output').scrollHeight;
265
+ }
266
+
267
+ // 改写完成 (含 reasoning annotations)
268
+ if (t === 'step_done' && s === 'rewrite') {
269
+ $('rewriteStatus').textContent = `改写完成 (${d.output_length || 0}字)`;
270
+
271
+ // 用干净的 output 替换 (去掉可能 stream 出来的 marker 残留)
272
+ if (d.output) {
273
+ currentOutput = d.output;
274
+ $('output').textContent = d.output;
275
+ }
276
+
277
+ // techniques_applied 不在这里展示 (是 ref 的 craft_notes, 不是本文改动)
278
+ // 真正的改写说明从 reasoning_done 事件获取 (单独的分析模型生成)
279
+ }
280
+
281
+ // Reasoning annotations (单独模型分析原文 vs 改写的差异)
282
+ if (t === 'step_done' && s === 'reasoning') {
283
+ const annotations = d.annotations || [];
284
+ if (annotations.length) {
285
+ $('reasoning').style.display = 'block';
286
+ window._annotations = annotations;
287
+ let html = '';
288
+ annotations.forEach((a, i) => {
289
+ const hasSpan = a.span && a.span.trim();
290
+ html += `
291
+ <div class="change-item" onclick="highlightByIndex(${i})">
292
+ <div class="what">${i+1}. ${escapeHtml(a.what || '')}</div>
293
+ <div class="why">${escapeHtml(a.why || '')}</div>
294
+ ${hasSpan ? `<div class="span-preview">"${escapeHtml(a.span.slice(0, 60))}"</div>` : ''}
295
+ </div>
296
+ `;
297
+ });
298
+ $('changes-list').innerHTML = html;
299
+ }
300
+ }
301
+
302
+ // SlopCleanup
303
+ if (t === 'step_done' && s === 'slop_cleanup') {
304
+ const n = d.replaced_total || 0;
305
+ if (n > 0) {
306
+ const words = (d.replaced_words || []).map(w => `${w.from}→${w.to}`).join(', ');
307
+ $('stats').innerHTML += `<div><b>🧹 AI词替换:</b> ${words}</div>`;
308
+ if (d.output) {
309
+ currentOutput = d.output;
310
+ $('output').textContent = d.output;
311
+ }
312
+ }
313
+ }
314
+
315
+ // 事实校验
316
+ if (t === 'step_done' && s === 'validate') {
317
+ $('stats').innerHTML += `<div><b>✓ 事实:</b> ${d.facts_preserved}/${d.facts_total}</div>`;
318
+ }
319
+
320
+ // Judge (dev mode)
321
+ if (t === 'step_done' && s === 'judge') {
322
+ $('stats').innerHTML += `<div><b>🏆 评分:</b> 人味=${d.human_score} 质量=${d.quality_score}</div>`;
323
+ }
324
+
325
+ // Pipeline done
326
+ if (t === 'pipeline_done') {
327
+ const elapsed = $('timer').textContent;
328
+ $('stats').innerHTML += `<div><b>⏱️</b> ${elapsed}</div>`;
329
+ if (d.degraded) {
330
+ $('stats').innerHTML += `<div style="color:#f59e0b"><b>⚠️ 降级模式</b></div>`;
331
+ }
332
+ }
333
+ }
334
+
335
+ // ============================================================
336
+ // 高亮功能: 点击改写说明 → 高亮输出文本中的对应片段
337
+ // ============================================================
338
+ function highlightByIndex(idx) {
339
+ const annotations = window._annotations || [];
340
+ if (idx >= annotations.length) return;
341
+ const ann = annotations[idx];
342
+
343
+ const outputEl = $('output');
344
+ const text = outputEl.textContent;
345
+
346
+ // 清除旧高亮
347
+ outputEl.innerHTML = escapeHtml(text);
348
+
349
+ // 用后端返回的精确 match_start/match_end (由 difflib 确定性匹配)
350
+ const start = ann.match_start;
351
+ const end = ann.match_end;
352
+
353
+ if (start < 0 || end <= start || end > text.length) return;
354
+
355
+ const before = text.substring(0, start);
356
+ const match = text.substring(start, end);
357
+ const after = text.substring(end);
358
+
359
+ outputEl.innerHTML = escapeHtml(before) +
360
+ '<span class="highlight">' + escapeHtml(match) + '</span>' +
361
+ escapeHtml(after);
362
+
363
+ const hl = outputEl.querySelector('.highlight');
364
+ if (hl) hl.scrollIntoView({behavior: 'smooth', block: 'center'});
365
+ }
366
+
367
+ // ============================================================
368
+ // 浮动工具栏: 选中文字 → 显示重新生成选项
369
+ // ============================================================
370
+ document.addEventListener('mouseup', (e) => {
371
+ const sel = window.getSelection();
372
+ const outputEl = $('output');
373
+
374
+ if (!sel.rangeCount || sel.isCollapsed || !outputEl.contains(sel.anchorNode)) {
375
+ $('floatToolbar').style.display = 'none';
376
+ return;
377
+ }
378
+
379
+ const range = sel.getRangeAt(0);
380
+ const rect = range.getBoundingClientRect();
381
+
382
+ // 记录选区
383
+ selectionRange = {
384
+ text: sel.toString(),
385
+ start: getTextOffset(outputEl, range.startContainer, range.startOffset),
386
+ end: getTextOffset(outputEl, range.endContainer, range.endOffset),
387
+ };
388
+
389
+ // 显示工具栏
390
+ const toolbar = $('floatToolbar');
391
+ toolbar.style.display = 'flex';
392
+ toolbar.style.left = rect.left + 'px';
393
+ toolbar.style.top = (rect.top - 45) + 'px';
394
+ });
395
+
396
+ function getTextOffset(root, node, offset) {
397
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
398
+ let total = 0;
399
+ while (walker.nextNode()) {
400
+ if (walker.currentNode === node) return total + offset;
401
+ total += walker.currentNode.length;
402
+ }
403
+ return total + offset;
404
+ }
405
+
406
+ // ============================================================
407
+ // Span Rewrite (选中重新生成)
408
+ // ============================================================
409
+ async function spanRewrite(feedback) {
410
+ if (!selectionRange || !currentOutput) return;
411
+ $('floatToolbar').style.display = 'none';
412
+
413
+ const {start, end, text: selectedText} = selectionRange;
414
+ $('rewriteStatus').textContent = '正在重新生成选中部分...';
415
+
416
+ // 用轻量 /api/v1/span-quick 端点 — 直接传 context, 不走全 pipeline
417
+ try {
418
+ const resp = await fetch('/api/v1/span-quick', {
419
+ method: 'POST',
420
+ headers: {'Content-Type': 'application/json'},
421
+ body: JSON.stringify({
422
+ full_text: currentOutput,
423
+ start: start,
424
+ end: end,
425
+ feedback: feedback,
426
+ }),
427
+ });
428
+
429
+ const data = await resp.json();
430
+ if (data.new_text) {
431
+ currentOutput = data.new_text;
432
+ $('output').textContent = data.new_text;
433
+ $('rewriteStatus').textContent = `局部重写完成: ${data.explanation || ''}`;
434
+ } else {
435
+ $('rewriteStatus').textContent = '重写失败: ' + (data.error || 'unknown');
436
+ }
437
+ } catch (e) {
438
+ $('rewriteStatus').textContent = '重写失败: ' + e.message;
439
+ }
440
+ }
441
+
442
+ function customSpanRewrite() {
443
+ const feedback = prompt('请输入你的修改要求:');
444
+ if (feedback) spanRewrite(feedback);
445
+ }
446
+
447
+ // ============================================================
448
+ // Utils
449
+ // ============================================================
450
+ function escapeHtml(str) {
451
+ return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
452
+ }
453
+
454
+ // 点击输出区外部时隐藏工具栏
455
+ document.addEventListener('mousedown', (e) => {
456
+ if (!$('floatToolbar').contains(e.target) && !$('output').contains(e.target)) {
457
+ $('floatToolbar').style.display = 'none';
458
+ }
459
+ });
460
+ </script>
461
+
462
+ </body>
463
+ </html>