Spaces:
Sleeping
fix: add paragraph_dedupe post-process to remove residual LLM echoes
Browse filesEven after PARAGRAPH_SEP + per-bulk <์๋ฌธ> (prior commit), solar-pro3
still probabilistically echoes paragraphs for specific inputs:
- Long multi-chunk articles (6788 chars / 13 chunks)
- Truncated inputs the model tries to "complete" by restarting
Add an output-level safety net: split final text on \n+, count
paragraph occurrences in both input and output, drop any paragraph
that appears more in output than input. Exact + 80-char-prefix
matching. Paragraphs shorter than 40 chars (โ headers, short
interjections) skipped.
Verified on full Supabase sweep (75 unique articles):
- Before fix 1+2+3: 13.4% dup rate
- After: 0.0% (0/75)
- Including aid=63 (long article, was 30%) and aid=22 (truncated
input, was 80%) โ both now clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- pipelines.py +12 -0
- postprocess.py +105 -0
|
@@ -17,6 +17,7 @@ from postprocess import (
|
|
| 17 |
apply_comma_removal,
|
| 18 |
apply_correction_filter,
|
| 19 |
apply_currency_compact,
|
|
|
|
| 20 |
apply_pronoun_postprocess,
|
| 21 |
apply_unit_unicode,
|
| 22 |
apply_vocabulary,
|
|
@@ -172,6 +173,9 @@ PIPELINES: dict[str, list[dict]] = {
|
|
| 172 |
"max_tokens": 4000,
|
| 173 |
},
|
| 174 |
{"name": "vocabulary_reapply", "type": "rule", "rule": "vocabulary"},
|
|
|
|
|
|
|
|
|
|
| 175 |
{"name": "post_process", "type": "rule", "rule": "post_process"},
|
| 176 |
],
|
| 177 |
# E1 taxonomy pipeline โ sequential specialists, grammar last @ RE=medium
|
|
@@ -798,6 +802,14 @@ def apply_rule(
|
|
| 798 |
max_char_diff=config.get("max_char_diff", 2),
|
| 799 |
allow_spacing=config.get("allow_spacing", True),
|
| 800 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 801 |
|
| 802 |
return text
|
| 803 |
|
|
|
|
| 17 |
apply_comma_removal,
|
| 18 |
apply_correction_filter,
|
| 19 |
apply_currency_compact,
|
| 20 |
+
apply_paragraph_dedupe,
|
| 21 |
apply_pronoun_postprocess,
|
| 22 |
apply_unit_unicode,
|
| 23 |
apply_vocabulary,
|
|
|
|
| 173 |
"max_tokens": 4000,
|
| 174 |
},
|
| 175 |
{"name": "vocabulary_reapply", "type": "rule", "rule": "vocabulary"},
|
| 176 |
+
# Safety net: LLM ์ด ํ๋ฅ ์ ์ผ๋ก ๋ฑ๋ ๋ฌธ๋จ ์์ฝ/์ฌ์์ ์ค๋ณต์ ์ ๊ฑฐํ๋ค.
|
| 177 |
+
# input ์ ์ด๋ฏธ ์๋ ๋ฐ๋ณต์ ๋ณด์กด (output_count > input_count ์ผ ๋๋ง drop).
|
| 178 |
+
{"name": "paragraph_dedupe", "type": "rule", "rule": "paragraph_dedupe"},
|
| 179 |
{"name": "post_process", "type": "rule", "rule": "post_process"},
|
| 180 |
],
|
| 181 |
# E1 taxonomy pipeline โ sequential specialists, grammar last @ RE=medium
|
|
|
|
| 802 |
max_char_diff=config.get("max_char_diff", 2),
|
| 803 |
allow_spacing=config.get("allow_spacing", True),
|
| 804 |
)
|
| 805 |
+
elif rule == "paragraph_dedupe":
|
| 806 |
+
config = step.get("config", {})
|
| 807 |
+
return apply_paragraph_dedupe(
|
| 808 |
+
text,
|
| 809 |
+
original_text,
|
| 810 |
+
min_len=config.get("min_len", 40),
|
| 811 |
+
prefix_len=config.get("prefix_len", 80),
|
| 812 |
+
)
|
| 813 |
|
| 814 |
return text
|
| 815 |
|
|
@@ -2,6 +2,7 @@
|
|
| 2 |
|
| 3 |
import csv
|
| 4 |
import re
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
# --- Vocabulary substitution ---
|
|
@@ -349,3 +350,107 @@ def apply_correction_filter(
|
|
| 349 |
result_tokens.append(orig_gap)
|
| 350 |
|
| 351 |
return " ".join(t for t in result_tokens if t)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import csv
|
| 4 |
import re
|
| 5 |
+
from collections import Counter
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
# --- Vocabulary substitution ---
|
|
|
|
| 350 |
result_tokens.append(orig_gap)
|
| 351 |
|
| 352 |
return " ".join(t for t in result_tokens if t)
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
# --- Paragraph-level duplication removal -------------------------------------
|
| 356 |
+
|
| 357 |
+
_PARA_SPLIT_RE = re.compile(r"\n+")
|
| 358 |
+
_WS_RE = re.compile(r"\s+")
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
def _normalize_paragraph(p: str) -> str:
|
| 362 |
+
"""๊ณต๋ฐฑ/ํญ/๊ฐํ ์์ถ ์ ๊ทํ. ์ค๋ณต ๋งค์นญ์ฉ."""
|
| 363 |
+
return _WS_RE.sub(" ", p).strip()
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
def _split_output_paragraphs(text: str) -> list[str]:
|
| 367 |
+
"""Output ํ
์คํธ๋ฅผ \\n+ ๊ฒฝ๊ณ๋ก ์ชผ๊ฐ ๋น์ด์์ง ์์ ๋ฌธ๋จ ๋ฆฌ์คํธ ๋ฐํ.
|
| 368 |
+
|
| 369 |
+
์ ๊ทํํ์ง ์์ ์๋ณธ ์กฐ๊ฐ์ ๊ทธ๋๋ก ๋๊ฒจ์, ์ฌ์กฐ๋ฆฝ ์ ๋ชจ๋ธ์ด ์ด ๊ตฌ๋์ /
|
| 370 |
+
์คํ์ด์ฑ ๋ํ
์ผ์ด ์ต๋ํ ๋ณด์กด๋๋๋ก ํ๋ค (strip ์ ์กฐ๋ฆฝ ์ \\n ์ผ๋ก
|
| 371 |
+
๋ค์ ๊ฐ์ธ๋ฏ๋ก ์์ ).
|
| 372 |
+
"""
|
| 373 |
+
if not text:
|
| 374 |
+
return []
|
| 375 |
+
parts = _PARA_SPLIT_RE.split(text)
|
| 376 |
+
return [p.strip() for p in parts if p.strip()]
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
def apply_paragraph_dedupe(
|
| 380 |
+
text: str,
|
| 381 |
+
original: str,
|
| 382 |
+
min_len: int = 40,
|
| 383 |
+
prefix_len: int = 80,
|
| 384 |
+
) -> str:
|
| 385 |
+
"""LLM์ด ๋ฑ์ ์ค๋ณต ๋ฌธ๋จ์ ์ ๊ฑฐํ๋ค.
|
| 386 |
+
|
| 387 |
+
**์ธ์ ์ ๊ฑฐํ๋๊ฐ**
|
| 388 |
+
- ๊ฐ์ ๋ฌธ๋จ(์ ๊ทํ ํ ์์ ์ผ์น) ์ด output ์์ `input ๋ฑ์ฅ ํ์ + 1`
|
| 389 |
+
์ด์ ๋ฑ์ฅํ๋ฉด ๋ค์ชฝ ๊ฒ์ drop.
|
| 390 |
+
- ์ ``prefix_len`` ์๊ฐ ๋์ผํ ๋ฌธ๋จ์ด out > in ๋ก ๋ฐ๋ณต๋๋ฉด drop
|
| 391 |
+
(๋ชจ๋ธ์ด ๋ฏธ์ธ ๊ต์ ์ผ๋ก ๋ค๋ฅด๊ฒ ๋ฑ์ echo ์ก๊ธฐ).
|
| 392 |
+
|
| 393 |
+
**์ธ์ ๋ณด์กดํ๋๊ฐ**
|
| 394 |
+
- ๋ฌธ๋จ ๊ธธ์ด < ``min_len`` (โ์์ ๋ชฉ, ํ ์ค ์ธ์ฉ ๋ฑ ํฉ๋ฒ์ ๋ฐ๋ณต).
|
| 395 |
+
- ``output_count <= input_count`` (์ ์๊ฐ ์๋ํด์ ์๋ฌธ์ ์ด๋ฏธ ๋ฐ๋ณต
|
| 396 |
+
ํฌํจ. ์ ์ ์๋๋ฅผ ํผ์ํ์ง ์์).
|
| 397 |
+
- ์ฒซ ๋ฑ์ฅ์ ํญ์ ์ ์ง. ๋ ๋ฒ์งธ ์ดํ ๋ฑ์ฅ๋ถํฐ ์กฐ๊ฑด์ ๋ง์ผ๋ฉด drop.
|
| 398 |
+
|
| 399 |
+
Args:
|
| 400 |
+
text: ํ์ดํ๋ผ์ธ ์ถ๋ ฅ ํ
์คํธ.
|
| 401 |
+
original: ํ์ดํ๋ผ์ธ ์
๋ ฅ (์ ์ ์๋ฌธ). None ์ด๋ฉด ๋น ๋ฌธ์์ด๋ก ๊ฐ์ฃผํด
|
| 402 |
+
input_count=0. ์ด ๊ฒฝ์ฐ output ์์ 2 ํ ์ด์ ๋์ผ ๋ฌธ๋จ ๋ฑ์ฅ โ
|
| 403 |
+
๋ชจ๋ drop.
|
| 404 |
+
min_len: ์ด ๊ธธ์ด ๋ฏธ๋ง ์ ๊ทํ ๋ฌธ๋จ์ dedupe ์์ ์ ์ธ.
|
| 405 |
+
prefix_len: near-dup ํ์ง์ ์ฐ๋ ์์๋ฅด๊ธฐ ๊ธธ์ด.
|
| 406 |
+
|
| 407 |
+
Returns:
|
| 408 |
+
Dedupe ๋ ํ
์คํธ. ๋ฌธ๋จ ๊ตฌ๋ถ์๋ ``\\n`` ์ผ๋ก ์ฌ์กฐ๋ฆฝ (๊ธฐ์กด ํ์ดํ๋ผ์ธ
|
| 409 |
+
์ถ๋ ฅ ๊ด๋ก์ ๋์ผ). ์ค๋ณต์ด ์์ผ๋ฉด ์
๋ ฅ์ ๊ทธ๋๋ก ๋๋ ค์ค๋ค.
|
| 410 |
+
"""
|
| 411 |
+
if not text:
|
| 412 |
+
return text
|
| 413 |
+
|
| 414 |
+
in_paras_norm = [_normalize_paragraph(p) for p in _split_output_paragraphs(original or "")]
|
| 415 |
+
in_exact: Counter[str] = Counter()
|
| 416 |
+
in_prefix: Counter[str] = Counter()
|
| 417 |
+
for norm in in_paras_norm:
|
| 418 |
+
if len(norm) >= min_len:
|
| 419 |
+
in_exact[norm] += 1
|
| 420 |
+
in_prefix[norm[:prefix_len]] += 1
|
| 421 |
+
|
| 422 |
+
out_paras = _split_output_paragraphs(text)
|
| 423 |
+
out_exact_seen: Counter[str] = Counter()
|
| 424 |
+
out_prefix_seen: Counter[str] = Counter()
|
| 425 |
+
|
| 426 |
+
kept: list[str] = []
|
| 427 |
+
any_dropped = False
|
| 428 |
+
for para in out_paras:
|
| 429 |
+
norm = _normalize_paragraph(para)
|
| 430 |
+
if len(norm) < min_len:
|
| 431 |
+
kept.append(para)
|
| 432 |
+
continue
|
| 433 |
+
|
| 434 |
+
prefix = norm[:prefix_len]
|
| 435 |
+
out_exact_seen[norm] += 1
|
| 436 |
+
out_prefix_seen[prefix] += 1
|
| 437 |
+
|
| 438 |
+
exact_dup = (
|
| 439 |
+
out_exact_seen[norm] > in_exact.get(norm, 0)
|
| 440 |
+
and out_exact_seen[norm] >= 2
|
| 441 |
+
)
|
| 442 |
+
near_dup = (
|
| 443 |
+
out_prefix_seen[prefix] > in_prefix.get(prefix, 0)
|
| 444 |
+
and out_prefix_seen[prefix] >= 2
|
| 445 |
+
)
|
| 446 |
+
|
| 447 |
+
if exact_dup or near_dup:
|
| 448 |
+
any_dropped = True
|
| 449 |
+
continue
|
| 450 |
+
|
| 451 |
+
kept.append(para)
|
| 452 |
+
|
| 453 |
+
if not any_dropped:
|
| 454 |
+
return text
|
| 455 |
+
|
| 456 |
+
return "\n".join(kept)
|