fix: diff compares words only (ignore punctuation/casing), strike true deletions only — removes false-positive strikethrough noise
Browse files131 unmodified lines132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185131 unmodified linesreturn _TOKEN_RE.findall(text or "")def diff_html(vanilla_text, tuned_text):"""Render the vanilla transcript with the parts the cleaned model REMOVEDshown as red strikethrough with tooltips on hover. Token-level via difflib.SequenceMatcher.Comparison is case-insensitive (the tuned model lowercases) so casingdifferences don't masquerade as edits; the original-cased vanilla token isalways the text shown to the user."""a = _tokenize(vanilla_text)b = _tokenize(tuned_text)a_lower = [t.lower() for t in a]b_lower = [t.lower() for t in b]sm = difflib.SequenceMatcher(a=a_lower, b=b_lower, autojunk=False)out = []def _kept(token):escaped = html.escape(token)if token.strip() and any(c.isalnum() for c in token):return f'<span class="fw-token fw-kept" data-tooltip="Kept: present as is">{escaped}</span>'return escapeddef _removed(token):escaped = html.escape(token)if token.strip() and any(c.isalnum() for c in token):return f'<span class="fw-token fw-removed" data-tooltip="Removed by clean LoRa">{escaped}</span>'return f'<span class="fw-removed">{escaped}</span>'for tag, i1, i2, j1, j2 in sm.get_opcodes():if tag == "equal":out.extend(_kept(t) for t in a[i1:i2])elif tag == "delete":out.extend(_removed(t) for t in a[i1:i2])elif tag == "replace":# Show the original (vanilla) tokens struck through. The# replacement appears on the cleaned side already; here we only# surface what vanilla had and the cleaned model changed/dropped.out.extend(_removed(t) for t in a[i1:i2])elif tag == "insert":# Tokens only in the cleaned output (rare: re-casing / re-spelling).# Nothing to strike through on the vanilla side; skip.pass# Re-join with spaces, then tighten spaces before punctuation for readability.joined = " ".join(out)joined = re.sub(r"\s+([,.;:!?])", r"\1", joined)joined = re.sub(r"\s+(</span>)", r"\1", joined)return f'<div class="fw-diff-text">{joined}</div>'# ---------------------------------------------------------------------------131 unmodified lines132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183131 unmodified linesreturn _TOKEN_RE.findall(text or "")_WORD_RE = re.compile(r"\w+(?:'\w+)*", re.UNICODE) # words, keeping contractions wholedef diff_html(vanilla_text, tuned_text):"""Render the vanilla transcript with the words the cleaned model REMOVEDshown as red strikethrough, and everything else left intact.The cleaned model lowercases its output and strips most punctuation, so araw text diff would flag every comma, capital letter, and full stop as an"edit" and bury the actual disfluency deletions in noise (this is exactlywhat made the diff look broken). To avoid that:* Comparison runs on lowercased *word* tokens only; punctuation and casingare ignored, so they never count as edits.* The display preserves the original vanilla text verbatim (its casing,punctuation, and spacing). Only whole words are ever struck through.* Only true deletions are struck. Word substitutions (an ASR variant like"mum" vs "mom", or an acronym respelled "CNN" vs "c n n") are left alone,because those are not disfluency removals and would only confuse a viewer."""vanilla_text = vanilla_text or ""a_matches = list(_WORD_RE.finditer(vanilla_text))a_words = [m.group(0).lower() for m in a_matches]b_words = [m.group(0).lower() for m in _WORD_RE.finditer(tuned_text or "")]sm = difflib.SequenceMatcher(a=a_words, b=b_words, autojunk=False)removed = set()for tag, i1, i2, j1, j2 in sm.get_opcodes():if tag == "delete": # only words dropped outright count as removedremoved.update(range(i1, i2))out = []cursor = 0for i, m in enumerate(a_matches):# Inter-word text (spaces + punctuation) is passed through untouched.out.append(html.escape(vanilla_text[cursor:m.start()]))word = html.escape(m.group(0))if i in removed:out.append(f'<span class="fw-token fw-removed" data-tooltip="Removed by the cleaning LoRA">{word}</span>')else:out.append(f'<span class="fw-token fw-kept" data-tooltip="Kept">{word}</span>')cursor = m.end()out.append(html.escape(vanilla_text[cursor:]))return f'<div class="fw-diff-text">{"".join(out)}</div>'# ---------------------------------------------------------------------------
app.py+42-44