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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
"""Evidence localization & highlighting helpers.
|
| 4 |
+
|
| 5 |
+
This module provides a **single public API**:
|
| 6 |
+
|
| 7 |
+
highlight_html_with_evidence(html_text: str, evidence: str, *, n: int = 3,
|
| 8 |
+
overlap_threshold: float = 0.5) -> str
|
| 9 |
+
|
| 10 |
+
It will:
|
| 11 |
+
1. Extract quoted segments from *evidence* – if any exist they are treated as
|
| 12 |
+
exact strings that must appear verbatim in *html_text* (case–insensitive).
|
| 13 |
+
2. If the evidence string had **no quotes**, we first try an exact match of the
|
| 14 |
+
raw evidence text. When that fails, we fall back to an n-gram overlap
|
| 15 |
+
heuristic (default n = 3). The window in *html_text* with ≥ *overlap_threshold*
|
| 16 |
+
Jaccard overlap is considered a match.
|
| 17 |
+
3. All matched character spans are wrapped in `<mark>` tags.
|
| 18 |
+
|
| 19 |
+
The helper is HTML-agnostic – it simply operates on the raw string. That means
|
| 20 |
+
it may occasionally highlight inside an HTML attribute if the evidence happens
|
| 21 |
+
to occur there, but for our Gradio viewer the relevant text lives in normal
|
| 22 |
+
content nodes so this trade-off keeps the implementation lightweight.
|
| 23 |
+
|
| 24 |
+
No try/excepts are used in accordance with user guidelines; we prefer clear
|
| 25 |
+
errors.
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
from typing import List, Tuple
|
| 29 |
+
import re
|
| 30 |
+
import html
|
| 31 |
+
|
| 32 |
+
__all__ = [
|
| 33 |
+
"localize_evidence",
|
| 34 |
+
"highlight_html_with_evidence",
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# ---------------------------------------------------------------------------
|
| 39 |
+
# Internal helpers -----------------------------------------------------------
|
| 40 |
+
# ---------------------------------------------------------------------------
|
| 41 |
+
|
| 42 |
+
def _extract_targets(evidence: str) -> List[str]:
|
| 43 |
+
"""Return the substrings we need to look for in *evidence*.
|
| 44 |
+
|
| 45 |
+
1. If there are quoted regions – e.g. `"foo"` – each quoted region is
|
| 46 |
+
returned separately **without** the quotes.
|
| 47 |
+
2. Otherwise we return the full evidence string (stripped of whitespace).
|
| 48 |
+
"""
|
| 49 |
+
if not evidence or evidence.strip() == "":
|
| 50 |
+
return []
|
| 51 |
+
|
| 52 |
+
# Pull out "quoted" substrings
|
| 53 |
+
quoted = re.findall(r'"([^"\\]*(?:\\.[^"\\]*)*)"', evidence)
|
| 54 |
+
return quoted if quoted else [evidence.strip()]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _tokenize(text: str) -> List[str]:
|
| 58 |
+
"""A very small tokenizer – splits on word boundaries."""
|
| 59 |
+
return re.findall(r"\b\w+\b", text.lower())
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def _ngrams(tokens: List[str], n: int) -> List[str]:
|
| 63 |
+
return [" ".join(tokens[i : i + n]) for i in range(len(tokens) - n + 1)]
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# ---------------------------------------------------------------------------
|
| 67 |
+
# Public localisation logic --------------------------------------------------
|
| 68 |
+
# ---------------------------------------------------------------------------
|
| 69 |
+
|
| 70 |
+
def localize_evidence(
|
| 71 |
+
text: str,
|
| 72 |
+
evidence: str,
|
| 73 |
+
*,
|
| 74 |
+
n: int = 3,
|
| 75 |
+
overlap_threshold: float = 0.5,
|
| 76 |
+
) -> List[Tuple[int, int]]:
|
| 77 |
+
"""Return a list of (start, end) indices where *evidence* occurs in *text*.
|
| 78 |
+
|
| 79 |
+
If *evidence* contains quotes we treat the quoted substrings as exact
|
| 80 |
+
matches (case-insensitive).
|
| 81 |
+
|
| 82 |
+
When *evidence* has no quotes, we apply a simple n-gram Jaccard overlap to
|
| 83 |
+
approximate the location. The window with the highest overlap ≥
|
| 84 |
+
*overlap_threshold* is returned. Only a single window is selected in that
|
| 85 |
+
fuzzy path to keep things deterministic.
|
| 86 |
+
"""
|
| 87 |
+
if not evidence or evidence in {"N/A", "None"}:
|
| 88 |
+
return []
|
| 89 |
+
|
| 90 |
+
matches: List[Tuple[int, int]] = []
|
| 91 |
+
targets = _extract_targets(evidence)
|
| 92 |
+
|
| 93 |
+
# ------------------------------------------------------------------
|
| 94 |
+
# 1. Exact search for each target (quoted or the raw evidence string)
|
| 95 |
+
# ------------------------------------------------------------------
|
| 96 |
+
lowered_text = text.lower()
|
| 97 |
+
for target in targets:
|
| 98 |
+
lowered_target = target.lower()
|
| 99 |
+
for m in re.finditer(re.escape(lowered_target), lowered_text):
|
| 100 |
+
matches.append(m.span())
|
| 101 |
+
|
| 102 |
+
if matches:
|
| 103 |
+
return _merge_overlaps(matches)
|
| 104 |
+
|
| 105 |
+
# ---------------------------------------------------------------
|
| 106 |
+
# 2. Fuzzy n-gram overlap if *evidence* had NO quotes and no exact
|
| 107 |
+
# substring match was detected above.
|
| 108 |
+
# ---------------------------------------------------------------
|
| 109 |
+
raw_target = targets[0] # either the only quoted string, or the full evidence
|
| 110 |
+
evid_tokens = _tokenize(raw_target)
|
| 111 |
+
if not evid_tokens:
|
| 112 |
+
return []
|
| 113 |
+
|
| 114 |
+
n = min(n, len(evid_tokens))
|
| 115 |
+
target_ngrams = set(_ngrams(evid_tokens, n))
|
| 116 |
+
if not target_ngrams:
|
| 117 |
+
return []
|
| 118 |
+
|
| 119 |
+
# Tokenise *text* and keep char offsets for each token start
|
| 120 |
+
token_spans: List[Tuple[str, int]] = [
|
| 121 |
+
(m.group().lower(), m.start()) for m in re.finditer(r"\b\w+\b", text)
|
| 122 |
+
]
|
| 123 |
+
if not token_spans:
|
| 124 |
+
return []
|
| 125 |
+
|
| 126 |
+
tokens_only = [tok for tok, _ in token_spans]
|
| 127 |
+
window_size = len(evid_tokens)
|
| 128 |
+
|
| 129 |
+
best_overlap = 0.0
|
| 130 |
+
best_span: Tuple[int, int] | None = None
|
| 131 |
+
|
| 132 |
+
for i in range(len(tokens_only) - window_size + 1):
|
| 133 |
+
window_tokens = tokens_only[i : i + window_size]
|
| 134 |
+
window_ngrams = set(_ngrams(window_tokens, n))
|
| 135 |
+
if not window_ngrams:
|
| 136 |
+
continue
|
| 137 |
+
overlap = len(window_ngrams & target_ngrams) / float(len(target_ngrams))
|
| 138 |
+
if overlap >= overlap_threshold and overlap > best_overlap:
|
| 139 |
+
start_char = token_spans[i][1]
|
| 140 |
+
end_char = token_spans[i + window_size - 1][1] + len(token_spans[i + window_size - 1][0])
|
| 141 |
+
best_overlap = overlap
|
| 142 |
+
best_span = (start_char, end_char)
|
| 143 |
+
|
| 144 |
+
if best_span is not None:
|
| 145 |
+
matches.append(best_span)
|
| 146 |
+
|
| 147 |
+
return _merge_overlaps(matches)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
# ---------------------------------------------------------------------------
|
| 151 |
+
# Highlighting ---------------------------------------------------------------
|
| 152 |
+
# ---------------------------------------------------------------------------
|
| 153 |
+
|
| 154 |
+
def _merge_overlaps(spans: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
|
| 155 |
+
"""Merge overlapping/adjacent spans."""
|
| 156 |
+
if not spans:
|
| 157 |
+
return []
|
| 158 |
+
spans = sorted(spans, key=lambda x: x[0])
|
| 159 |
+
merged = [spans[0]]
|
| 160 |
+
for start, end in spans[1:]:
|
| 161 |
+
last_start, last_end = merged[-1]
|
| 162 |
+
if start <= last_end: # overlapping or touching
|
| 163 |
+
merged[-1] = (last_start, max(last_end, end))
|
| 164 |
+
else:
|
| 165 |
+
merged.append((start, end))
|
| 166 |
+
return merged
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def _insert_tags(text: str, spans: List[Tuple[int, int]], tag: str = "mark") -> str:
|
| 170 |
+
"""Return *text* with each span wrapped in `<tag>` .. `</tag>`.
|
| 171 |
+
|
| 172 |
+
Assumes *spans* are non-overlapping **and sorted ascending**.
|
| 173 |
+
"""
|
| 174 |
+
if not spans:
|
| 175 |
+
return text
|
| 176 |
+
|
| 177 |
+
parts: List[str] = []
|
| 178 |
+
last_idx = 0
|
| 179 |
+
for start, end in spans:
|
| 180 |
+
parts.append(text[last_idx:start])
|
| 181 |
+
parts.append(f"<{tag}>" + text[start:end] + f"</{tag}>")
|
| 182 |
+
last_idx = end
|
| 183 |
+
parts.append(text[last_idx:])
|
| 184 |
+
return "".join(parts)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def highlight_html_with_evidence(
|
| 188 |
+
html_text: str,
|
| 189 |
+
evidence: str,
|
| 190 |
+
*,
|
| 191 |
+
n: int = 3,
|
| 192 |
+
overlap_threshold: float = 0.5,
|
| 193 |
+
) -> str:
|
| 194 |
+
"""Return *html_text* with occurrences of *evidence* wrapped in `<mark>` tags."""
|
| 195 |
+
if not evidence or evidence in {"N/A", "None"}:
|
| 196 |
+
return html_text
|
| 197 |
+
|
| 198 |
+
# Working on the raw HTML string directly – obtain spans first
|
| 199 |
+
spans = localize_evidence(html_text, evidence, n=n, overlap_threshold=overlap_threshold)
|
| 200 |
+
if not spans:
|
| 201 |
+
return html_text # nothing to highlight
|
| 202 |
+
|
| 203 |
+
highlighted = _insert_tags(html_text, spans, tag="mark")
|
| 204 |
+
|
| 205 |
+
# Inject tiny CSS once – id attribute prevents duplicates
|
| 206 |
+
style_block = (
|
| 207 |
+
"<style id=\"evidence-highlight-style\">\n"
|
| 208 |
+
"mark { background: #fffd6b; font-weight: 600; padding: 0 2px; border-radius: 2px; }\n"
|
| 209 |
+
"</style>\n"
|
| 210 |
+
)
|
| 211 |
+
if "evidence-highlight-style" not in html_text:
|
| 212 |
+
highlighted = style_block + highlighted
|
| 213 |
+
|
| 214 |
+
return highlighted
|
lmmvibes/vis_gradio/conversation_display.py
CHANGED
|
@@ -121,7 +121,7 @@ def pretty_print_embedded_dicts(text: str) -> str:
|
|
| 121 |
new_parts.append(html.escape(text[last_idx:start], quote=False))
|
| 122 |
pretty = json.dumps(parsed, indent=2, ensure_ascii=False)
|
| 123 |
new_parts.append(
|
| 124 |
-
f"<
|
| 125 |
)
|
| 126 |
last_idx = end
|
| 127 |
new_parts.append(html.escape(text[last_idx:], quote=False))
|
|
|
|
| 121 |
new_parts.append(html.escape(text[last_idx:start], quote=False))
|
| 122 |
pretty = json.dumps(parsed, indent=2, ensure_ascii=False)
|
| 123 |
new_parts.append(
|
| 124 |
+
f"<span style='white-space: pre-wrap; font-family: monospace;'>{html.escape(pretty, quote=False)}</span>"
|
| 125 |
)
|
| 126 |
last_idx = end
|
| 127 |
new_parts.append(html.escape(text[last_idx:], quote=False))
|
lmmvibes/vis_gradio/utils.py
CHANGED
|
@@ -14,6 +14,9 @@ from typing import Dict, List, Any, Optional, Tuple
|
|
| 14 |
import html
|
| 15 |
import ast
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
# Conversation rendering helpers are now in a dedicated module for clarity
|
| 18 |
from . import conversation_display as _convdisp
|
| 19 |
from .conversation_display import (
|
|
@@ -1524,6 +1527,10 @@ def format_examples_display(examples: List[Dict[str, Any]],
|
|
| 1524 |
else:
|
| 1525 |
conversation_html = "<p style='color: #dc3545; font-style: italic;'>No response data available</p>"
|
| 1526 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1527 |
# Determine cluster info
|
| 1528 |
cluster_info = ""
|
| 1529 |
if example['fine_cluster_label'] != 'N/A':
|
|
|
|
| 14 |
import html
|
| 15 |
import ast
|
| 16 |
|
| 17 |
+
# Evidence highlighter
|
| 18 |
+
from lmmvibes.examples_helpers import highlight_html_with_evidence
|
| 19 |
+
|
| 20 |
# Conversation rendering helpers are now in a dedicated module for clarity
|
| 21 |
from . import conversation_display as _convdisp
|
| 22 |
from .conversation_display import (
|
|
|
|
| 1527 |
else:
|
| 1528 |
conversation_html = "<p style='color: #dc3545; font-style: italic;'>No response data available</p>"
|
| 1529 |
|
| 1530 |
+
# Highlight evidence (if any) inside the conversation HTML
|
| 1531 |
+
if example.get("evidence") not in ["N/A", None, "None"]:
|
| 1532 |
+
conversation_html = highlight_html_with_evidence(conversation_html, example["evidence"])
|
| 1533 |
+
|
| 1534 |
# Determine cluster info
|
| 1535 |
cluster_info = ""
|
| 1536 |
if example['fine_cluster_label'] != 'N/A':
|