Spaces:
Build error
Build error
File size: 10,768 Bytes
c5aaf7c 846b06e c5aaf7c 846b06e c5aaf7c |
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 |
from __future__ import annotations # keep list[str] type hints on Py β€3.9
import os
import streamlit as st
import pandas as pd
import numpy as np
import torch
from typing import Optional
from sentence_transformers import SentenceTransformer
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from matplotlib.colors import LinearSegmentedColormap
# βββββββββββ MODEL CONSTANTS βββββββββββ
EMBED_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2" # "bowphs/PhilBerta"
# "julian-schelb/xlm-roberta-base-latin-intertextuality"
CLF_MODEL_NAME = "ParitKansal/BERT_Paraphrase_Detection_GLUE_MRPC"
POS_CLASS_IDX = 1 # positive ("intertextual") is *first* label
COLOR_MAP = LinearSegmentedColormap.from_list(
"light_blues", ["#ffffff", "#2676b8"])
# βββββββββββ CACHE LOADER βββββββββββ
@st.cache_resource(show_spinner="π Loading HF models β¦")
def load_models():
"""Load SentenceTransformer & classifier on one device (CPU in Streamlit Cloud, GPU if available)."""
device = "cuda" if torch.cuda.is_available() else "cpu"
embedder = SentenceTransformer(EMBED_MODEL_NAME, device=device)
tokenizer = AutoTokenizer.from_pretrained(
CLF_MODEL_NAME, use_fast=False, trust_remote_code=True)
clf_model = AutoModelForSequenceClassification.from_pretrained(
CLF_MODEL_NAME).to(device)
clf_model.eval()
return embedder, tokenizer, clf_model, device
# βββββββββββ HELPERS βββββββββββ
def cosine_similarity_batch(embedder, originals, paraphrases, batch_size: int = 32):
sims: list[float] = []
for i in range(0, len(originals), batch_size):
o_vec = embedder.encode(
originals[i: i + batch_size], convert_to_numpy=True, normalize_embeddings=True)
p_vec = embedder.encode(
paraphrases[i: i + batch_size], convert_to_numpy=True, normalize_embeddings=True)
sims.extend((o_vec * p_vec).sum(axis=1))
return sims
def probability_batch(tokenizer, model, originals, paraphrases, device: str, batch_size: int = 16):
probs: list[float] = []
for i in range(0, len(originals), batch_size):
enc = tokenizer(
paraphrases[i: i + batch_size],
originals[i: i + batch_size],
padding=True,
truncation=True,
return_tensors="pt",
).to(device)
with torch.no_grad():
logits = model(**enc).logits
probs.extend(torch.softmax(logits, dim=1)[
:, POS_CLASS_IDX].cpu().tolist())
return probs
# =================== ATTENTION WIEGHTS ===================
def get_avg_attention_per_token(
tokenizer, model, para: str, orig: str, device: str,
max_tokens: int = 512, filter_special_tokens: bool = True
) -> tuple[list, list]:
"""Return two lists of (token, average_attention_received) pairs for para and orig separately."""
enc = tokenizer(para.strip(), orig.strip(), return_tensors="pt",
truncation=True, max_length=max_tokens).to(device)
with torch.no_grad():
attn = model(**enc, output_attentions=True).attentions[-4]
attn = attn[0].mean(dim=0).cpu().numpy() # (seq_len, seq_len)
input_ids = enc["input_ids"][0][:max_tokens]
tokens = tokenizer.convert_ids_to_tokens(input_ids)
attn = attn[:len(tokens), :len(tokens)]
# Compute attention received
avg_received = attn.mean(axis=0)
# Find separator token index to split para and orig
sep_id = tokenizer.sep_token_id
sep_indices = (input_ids == sep_id).nonzero(as_tuple=True)[0].tolist()
if len(sep_indices) < 1:
raise ValueError(
"Could not find separator token to split para and orig.")
# para ends at first [SEP], orig starts after
split_index = sep_indices[0] + 1
# Split tokens, attention scores, and ids
para_parts = list(
zip(tokens[:split_index], avg_received[:split_index], input_ids[:split_index]))
orig_parts = list(
zip(tokens[split_index:], avg_received[split_index:], input_ids[split_index:]))
if filter_special_tokens:
special_ids = tokenizer.all_special_ids
para_parts = [(tok, score) for tok, score,
tok_id in para_parts if tok_id.item() not in special_ids]
orig_parts = [(tok, score) for tok, score,
tok_id in orig_parts if tok_id.item() not in special_ids]
return para_parts, orig_parts
def attention_tokens_to_html(token_attention: list, cmap: str = "Blues") -> str:
"""Render tokens as colored boxes with pill-style visual grouping for subwords."""
import matplotlib.cm as cm
import matplotlib.colors as mcolors
tokens, scores = zip(*token_attention)
# Normalize attention scores
norm = mcolors.Normalize(vmin=min(scores), vmax=max(scores))
colormap = COLOR_MAP # cm.get_cmap(cmap)
html = ""
for i, (token, score) in enumerate(token_attention):
rgba = colormap(norm(score))
hex_color = mcolors.to_hex(rgba)
clean_token = token.replace("Δ ", "").replace("β", "").replace("##", "")
is_start = (
i == 0
or token.startswith("Δ ")
or token.startswith("β")
or token.startswith("<")
or token.startswith("[")
)
is_end = (
i == len(tokens) - 1
or tokens[i + 1].startswith("Δ ")
or tokens[i + 1].startswith("β")
or tokens[i + 1].startswith("<")
or tokens[i + 1].startswith("[")
)
# Border radius logic
if is_start and is_end:
border_radius = "6px"
elif is_start:
border_radius = "6px 0 0 6px"
elif is_end:
border_radius = "0 6px 6px 0"
else:
border_radius = "0"
# Padding logic
if is_start or is_end:
padding = "2px 6px"
else:
padding = "2px 4px"
# Add space between word groups
if is_start and i != 0:
html += " "
html += f'<span style="background-color:{hex_color}; padding:{padding}; margin:1px 0px; border-radius:{border_radius}; display:inline-block;">{clean_token}</span>'
return html
# βββββββββββ UI CONFIG βββββββββββ
st.set_page_config(page_title="Inspector", layout="wide")
# st.title("π Intertextuality Quick-Check")
# βββββββββββ DATA LOADING βββββββββββ
st.subheader("Model & Data Configuration")
df: Optional[pd.DataFrame] = None
if os.path.exists("test_cases.csv"):
try:
df = pd.read_csv("test_cases.csv")
except Exception as e:
pass
clf_model_name = st.text_input(
"Name of the Classification Model:", CLF_MODEL_NAME
)
embed_model_name = st.text_input(
"Name of the Sentence Transformer Model:", EMBED_MODEL_NAME
)
uploaded = st.file_uploader("File with Sentence Pairs:", type="csv")
if uploaded is not None:
df = pd.read_csv(uploaded)
if df is None or df.empty:
st.info("Upload a CSV or place **test_cases.csv** next to the script.")
st.stop()
missing = {"original", "paraphrased"} - set(df.columns)
if missing:
st.error("CSV missing required column(s): " + ", ".join(missing))
st.stop()
# Enable the Process button only when both model names are specified and a CSV file is uploaded.
can_process = bool(
clf_model_name and embed_model_name and uploaded is not None)
if not st.button("Process", disabled=not can_process):
st.stop()
if can_process:
# βββββββββββ MODEL INFERENCE βββββββββββ
embedder, tokenizer, clf_model, device = load_models()
with st.spinner("π Scoring pairs β¦"):
df["cosine_similarity"] = np.round(
cosine_similarity_batch(
embedder, df["original"].tolist(), df["paraphrased"].tolist()), 3
)
df["P_positive"] = np.round(
probability_batch(tokenizer, clf_model, df["original"].tolist(
), df["paraphrased"].tolist(), device), 3
)
# βββββββββββ LAYOUT βββββββββββ
st.subheader("Results")
# st.markdown("Number of sentence pairs: {}".format(len(df)))
for idx, row in df.iterrows():
with st.container(border=True, key=f"row_{idx}"):
# Divide the layout into two columns: left for text details, right for metrics.
left_col, right_col = st.columns([2, 1])
with left_col:
st.markdown(
f"**Case ID:** {row.get('case_id', 'N/A')} \n"
f"**Original Text:** {row['original']} \n"
f"**Paraphrased Text:** {row['paraphrased']} \n"
f"**Comment:** {row['operation_description']}"
)
with right_col:
# Display cosine similarity
sim_value = row["cosine_similarity"]
sim_pct = max(-1, min(1, sim_value)) # Clamp the value
st.progress(int(sim_pct * 100),
text=f"**Cosine Similarity:** `{sim_value:.3f}`")
# Display probability
prob_value = row["P_positive"]
# Clamp the value to [0, 1]
prob_pct = max(0, min(1, prob_value))
st.progress(int(prob_pct * 100),
text=f"**Probability:** `{prob_value:.3f}`")
# Add a popover button for Attention Weights using an expander.
# with st.expander("Attention Weights"):
with st.spinner("Computing attention β¦"):
st.markdown("**Attention Weights:**")
weights_para, weight_orig = get_avg_attention_per_token(
tokenizer, clf_model, row["paraphrased"], row["original"], device)
# st.markdown(weights_para)
st.markdown(weight_orig)
# Display attention weights for original texts
html = attention_tokens_to_html(weight_orig)
st.markdown(html, unsafe_allow_html=True)
# Display attention weights for paraphrased texts
html = attention_tokens_to_html(weights_para)
st.markdown(html, unsafe_allow_html=True)
# βββββββββββ DOWNLOAD BUTTON βββββββββββ
st.download_button(
"πΎ Download Scored CSV",
data=df.to_csv(index=False).encode(),
file_name="results_with_scores.csv",
mime="text/csv",
)
|