Spaces:
Sleeping
Sleeping
File size: 16,246 Bytes
b7948be | 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 | <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Aversion</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, "PingFang SC", sans-serif; background: #0a0a0a; color: #e0e0e0; }
.app { max-width: 900px; margin: 0 auto; padding: 20px; }
h1 { font-size: 22px; margin-bottom: 20px; }
h1 span { color: #555; font-weight: normal; font-size: 13px; }
/* 输入区 */
.input-section { margin-bottom: 16px; }
textarea {
width: 100%; height: 180px; background: #151515; color: #e0e0e0;
border: 1px solid #2a2a2a; border-radius: 10px; padding: 14px;
font-size: 14px; line-height: 1.7; resize: vertical; font-family: inherit;
}
textarea:focus { outline: none; border-color: #3b82f6; }
.controls { margin-top: 10px; display: flex; gap: 10px; align-items: center; }
button.primary {
background: #3b82f6; color: #fff; border: none; border-radius: 8px;
padding: 10px 28px; font-size: 14px; cursor: pointer; font-weight: 500;
}
button.primary:hover { background: #2563eb; }
button.primary:disabled { background: #333; cursor: not-allowed; }
select { background: #151515; color: #ccc; border: 1px solid #2a2a2a; border-radius: 6px; padding: 8px; font-size: 13px; }
.timer { color: #555; font-size: 13px; }
/* 分析区 */
.analysis-section {
background: #111; border: 1px solid #1e1e1e; border-radius: 10px;
padding: 14px 16px; margin-bottom: 16px; display: none;
}
.analysis-section h3 { font-size: 13px; color: #888; margin-bottom: 8px; }
.analysis-section .facts { font-size: 13px; color: #93c5fd; line-height: 1.8; }
.analysis-section .meta { font-size: 12px; color: #666; margin-top: 6px; }
.analysis-section .loading { color: #60a5fa; font-size: 13px; }
/* 改写区 */
.rewrite-section { display: none; }
.rewrite-header {
font-size: 13px; color: #666; margin-bottom: 8px;
display: flex; justify-content: space-between; align-items: center;
}
.rewrite-status { color: #60a5fa; }
.output-box {
background: #111; border: 1px solid #1e1e1e; border-radius: 10px;
padding: 18px 20px; min-height: 150px; max-height: 600px; overflow-y: auto;
font-size: 15px; line-height: 2; white-space: pre-wrap; position: relative;
outline: none;
}
.output-box .cursor { display: inline-block; width: 2px; height: 18px; background: #3b82f6; animation: blink 0.7s infinite; vertical-align: text-bottom; }
.output-box.done .cursor { display: none; }
@keyframes blink { 50% { opacity: 0; } }
/* 高亮 */
.output-box .highlight { background: rgba(59, 130, 246, 0.2); border-radius: 3px; transition: background 0.3s; }
/* 浮动工具栏 (选中文字时) */
.float-toolbar {
display: none; position: fixed; background: #1e293b; border: 1px solid #334155;
border-radius: 8px; padding: 6px; z-index: 100; box-shadow: 0 4px 12px rgba(0,0,0,0.5);
}
.float-toolbar button {
background: none; border: none; color: #93c5fd; padding: 6px 12px;
font-size: 13px; cursor: pointer; border-radius: 4px; white-space: nowrap;
}
.float-toolbar button:hover { background: #334155; }
/* 改写说明 */
.reasoning-section {
background: #0d1117; border: 1px solid #1e1e1e; border-radius: 10px;
padding: 14px 16px; margin-top: 12px; display: none;
}
.reasoning-section h3 { font-size: 13px; color: #888; margin-bottom: 10px; }
.change-item {
padding: 8px 0; border-bottom: 1px solid #161b22; font-size: 13px;
cursor: pointer; transition: background 0.2s;
}
.change-item:hover { background: #161b22; border-radius: 4px; padding-left: 8px; }
.change-item .what { color: #e0e0e0; }
.change-item .why { color: #8b949e; font-size: 12px; margin-top: 2px; }
.change-item .span-preview { color: #58a6ff; font-size: 12px; margin-top: 2px; font-style: italic; }
.stats-bar {
display: flex; gap: 16px; margin-top: 10px; padding-top: 10px;
border-top: 1px solid #161b22; font-size: 12px; color: #666;
}
.stats-bar b { color: #8b949e; }
</style>
</head>
<body>
<div class="app">
<h1>Aversion <span>内容改写工具</span></h1>
<!-- 输入 -->
<div class="input-section">
<textarea id="input" placeholder="粘贴一段 AI 生成的文案, 点击改写..."></textarea>
<div class="controls">
<button class="primary" id="btn" onclick="doRewrite()">改写</button>
<select id="mode">
<option value="prod" selected>标准模式</option>
<option value="dev">评估模式 (含评分)</option>
</select>
<span class="timer" id="timer"></span>
</div>
</div>
<!-- 分析 -->
<div class="analysis-section" id="analysis">
<h3>🔍 内容分析</h3>
<div id="analysis-content"></div>
</div>
<!-- 改写 -->
<div class="rewrite-section" id="rewriteSection">
<div class="rewrite-header">
<span>✍️ 改写结果</span>
<span class="rewrite-status" id="rewriteStatus"></span>
</div>
<div class="output-box" id="output" contenteditable="true" spellcheck="false">
<span class="cursor"></span>
</div>
</div>
<!-- 改写说明 -->
<div class="reasoning-section" id="reasoning">
<h3>📝 改写说明 <span style="font-weight:normal;color:#555">(点击可高亮对应文本)</span></h3>
<div id="changes-list"></div>
<div class="stats-bar" id="stats"></div>
</div>
</div>
<!-- 浮动工具栏 -->
<div class="float-toolbar" id="floatToolbar">
<button onclick="spanRewrite('更自然')">🔄 重新生成</button>
<button onclick="spanRewrite('更口语化,像朋友聊天')">💬 更口语</button>
<button onclick="spanRewrite('更正式严谨')">📋 更正式</button>
<button onclick="customSpanRewrite()">✏️ 自定义</button>
</div>
<script>
const $ = id => document.getElementById(id);
let currentOutput = '';
let selectionRange = null;
let rewriteContext = null; // 存初次改写的完整 context (facts/voice/industry)
// ============================================================
// 主改写流程
// ============================================================
async function doRewrite() {
const text = $('input').value.trim();
if (!text) return alert('请粘贴一段文案');
if (text.length < 50) return alert('至少 50 字');
const mode = $('mode').value;
const btn = $('btn');
btn.disabled = true;
btn.textContent = '改写中...';
$('analysis').style.display = 'block';
$('analysis-content').innerHTML = '<span class="loading">分析中, 正在提取关键信息...</span>';
$('rewriteSection').style.display = 'none';
$('reasoning').style.display = 'none';
$('output').innerHTML = '<span class="cursor"></span>';
$('output').classList.remove('done');
$('stats').innerHTML = '';
$('changes-list').innerHTML = '';
currentOutput = '';
const startTime = Date.now();
const timerInterval = setInterval(() => {
$('timer').textContent = ((Date.now() - startTime) / 1000).toFixed(1) + 's';
}, 100);
try {
const resp = await fetch('/api/v1/rewrite/stream', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({text, mode}),
});
const reader = resp.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const {done, value} = await reader.read();
if (done) break;
buffer += decoder.decode(value, {stream: true}).replace(/\r\n/g, '\n');
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith(':') || !trimmed.startsWith('data:')) continue;
let evt;
try { evt = JSON.parse(trimmed.substring(5).trim()); } catch { continue; }
handleEvent(evt);
}
}
} catch (e) {
$('analysis-content').innerHTML = `<span style="color:#f87171">连接失败: ${e.message}</span>`;
}
clearInterval(timerInterval);
$('output').classList.add('done');
btn.disabled = false;
btn.textContent = '改写';
}
// ============================================================
// 事件处理
// ============================================================
function handleEvent(evt) {
const t = evt.type;
const s = evt.step || '';
const d = evt.data || {};
// 分析完成 — 存 context 给后续 span rewrite 用
if (t === 'step_done' && s === 'analyze') {
const facts = d.facts || [];
const exps = d.experiences || [];
// 存完整 context
rewriteContext = {
facts: facts,
experiences: exps,
emotions: d.emotions || [],
voice_hint: d.voice_hint || '',
industry_hint: d.industry_hint || '',
target_platform: d.target_platform || 'xiaohongshu',
intent: d.intent || '',
};
let html = '';
if (facts.length) {
html += `<div class="facts">📋 发现 ${facts.length} 个关键事实: ${facts.slice(0, 6).join(' · ')}${facts.length > 6 ? ' ...' : ''}</div>`;
}
if (exps.length) {
html += `<div class="facts" style="margin-top:4px">📖 ${exps.length} 段个人经历</div>`;
}
html += `<div class="meta">🎯 ${d.target_platform || '小红书'} · ${d.industry_hint || '通用'} · ${d.voice_hint || ''}</div>`;
$('analysis-content').innerHTML = html;
}
// 检索完成 → 显示改写区
if (t === 'step_done' && s === 'retrieve') {
$('rewriteSection').style.display = 'block';
$('rewriteStatus').textContent = '改写中...';
}
// 流式 token
if (t === 'rewrite_token') {
currentOutput += d.token;
const cursor = $('output').querySelector('.cursor');
if (cursor) cursor.insertAdjacentText('beforebegin', d.token);
$('output').scrollTop = $('output').scrollHeight;
}
// 改写完成 (含 reasoning annotations)
if (t === 'step_done' && s === 'rewrite') {
$('rewriteStatus').textContent = `改写完成 (${d.output_length || 0}字)`;
// 用干净的 output 替换 (去掉可能 stream 出来的 marker 残留)
if (d.output) {
currentOutput = d.output;
$('output').textContent = d.output;
}
// techniques_applied 不在这里展示 (是 ref 的 craft_notes, 不是本文改动)
// 真正的改写说明从 reasoning_done 事件获取 (单独的分析模型生成)
}
// Reasoning annotations (单独模型分析原文 vs 改写的差异)
if (t === 'step_done' && s === 'reasoning') {
const annotations = d.annotations || [];
if (annotations.length) {
$('reasoning').style.display = 'block';
window._annotations = annotations;
let html = '';
annotations.forEach((a, i) => {
const hasSpan = a.span && a.span.trim();
html += `
<div class="change-item" onclick="highlightByIndex(${i})">
<div class="what">${i+1}. ${escapeHtml(a.what || '')}</div>
<div class="why">${escapeHtml(a.why || '')}</div>
${hasSpan ? `<div class="span-preview">"${escapeHtml(a.span.slice(0, 60))}"</div>` : ''}
</div>
`;
});
$('changes-list').innerHTML = html;
}
}
// SlopCleanup
if (t === 'step_done' && s === 'slop_cleanup') {
const n = d.replaced_total || 0;
if (n > 0) {
const words = (d.replaced_words || []).map(w => `${w.from}→${w.to}`).join(', ');
$('stats').innerHTML += `<div><b>🧹 AI词替换:</b> ${words}</div>`;
if (d.output) {
currentOutput = d.output;
$('output').textContent = d.output;
}
}
}
// 事实校验
if (t === 'step_done' && s === 'validate') {
$('stats').innerHTML += `<div><b>✓ 事实:</b> ${d.facts_preserved}/${d.facts_total}</div>`;
}
// Judge (dev mode)
if (t === 'step_done' && s === 'judge') {
$('stats').innerHTML += `<div><b>🏆 评分:</b> 人味=${d.human_score} 质量=${d.quality_score}</div>`;
}
// Pipeline done
if (t === 'pipeline_done') {
const elapsed = $('timer').textContent;
$('stats').innerHTML += `<div><b>⏱️</b> ${elapsed}</div>`;
if (d.degraded) {
$('stats').innerHTML += `<div style="color:#f59e0b"><b>⚠️ 降级模式</b></div>`;
}
}
}
// ============================================================
// 高亮功能: 点击改写说明 → 高亮输出文本中的对应片段
// ============================================================
function highlightByIndex(idx) {
const annotations = window._annotations || [];
if (idx >= annotations.length) return;
const ann = annotations[idx];
const outputEl = $('output');
const text = outputEl.textContent;
// 清除旧高亮
outputEl.innerHTML = escapeHtml(text);
// 用后端返回的精确 match_start/match_end (由 difflib 确定性匹配)
const start = ann.match_start;
const end = ann.match_end;
if (start < 0 || end <= start || end > text.length) return;
const before = text.substring(0, start);
const match = text.substring(start, end);
const after = text.substring(end);
outputEl.innerHTML = escapeHtml(before) +
'<span class="highlight">' + escapeHtml(match) + '</span>' +
escapeHtml(after);
const hl = outputEl.querySelector('.highlight');
if (hl) hl.scrollIntoView({behavior: 'smooth', block: 'center'});
}
// ============================================================
// 浮动工具栏: 选中文字 → 显示重新生成选项
// ============================================================
document.addEventListener('mouseup', (e) => {
const sel = window.getSelection();
const outputEl = $('output');
if (!sel.rangeCount || sel.isCollapsed || !outputEl.contains(sel.anchorNode)) {
$('floatToolbar').style.display = 'none';
return;
}
const range = sel.getRangeAt(0);
const rect = range.getBoundingClientRect();
// 记录选区
selectionRange = {
text: sel.toString(),
start: getTextOffset(outputEl, range.startContainer, range.startOffset),
end: getTextOffset(outputEl, range.endContainer, range.endOffset),
};
// 显示工具栏
const toolbar = $('floatToolbar');
toolbar.style.display = 'flex';
toolbar.style.left = rect.left + 'px';
toolbar.style.top = (rect.top - 45) + 'px';
});
function getTextOffset(root, node, offset) {
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
let total = 0;
while (walker.nextNode()) {
if (walker.currentNode === node) return total + offset;
total += walker.currentNode.length;
}
return total + offset;
}
// ============================================================
// Span Rewrite (选中重新生成)
// ============================================================
async function spanRewrite(feedback) {
if (!selectionRange || !currentOutput) return;
$('floatToolbar').style.display = 'none';
const {start, end, text: selectedText} = selectionRange;
$('rewriteStatus').textContent = '正在重新生成选中部分...';
// 用轻量 /api/v1/span-quick 端点 — 直接传 context, 不走全 pipeline
try {
const resp = await fetch('/api/v1/span-quick', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
full_text: currentOutput,
start: start,
end: end,
feedback: feedback,
}),
});
const data = await resp.json();
if (data.new_text) {
currentOutput = data.new_text;
$('output').textContent = data.new_text;
$('rewriteStatus').textContent = `局部重写完成: ${data.explanation || ''}`;
} else {
$('rewriteStatus').textContent = '重写失败: ' + (data.error || 'unknown');
}
} catch (e) {
$('rewriteStatus').textContent = '重写失败: ' + e.message;
}
}
function customSpanRewrite() {
const feedback = prompt('请输入你的修改要求:');
if (feedback) spanRewrite(feedback);
}
// ============================================================
// Utils
// ============================================================
function escapeHtml(str) {
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
// 点击输出区外部时隐藏工具栏
document.addEventListener('mousedown', (e) => {
if (!$('floatToolbar').contains(e.target) && !$('output').contains(e.target)) {
$('floatToolbar').style.display = 'none';
}
});
</script>
</body>
</html>
|