Spaces:
Configuration error
Configuration error
File size: 17,318 Bytes
8bd2709 | 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 | """
tools.py β 7 LangChain tool functions for BERTopic thematic analysis pipeline.
Constraints: ZERO if/else, ZERO for/while, ZERO try/except.
"""
from __future__ import annotations
import json
import re
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from pathlib import Path
from langchain_core.tools import tool
from sentence_transformers import SentenceTransformer
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics.pairwise import cosine_similarity
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain_mistralai import ChatMistralAI
from dotenv import load_dotenv
load_dotenv() # add this right after the imports
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
BOILERPLATE_PATTERNS = [
r"Β©\s*\d{4}",
r"all rights reserved",
r"published by elsevier",
r"doi:\s*10\.\S+",
r"this article is protected",
r"www\.\S+\.com",
r"^\s*abstract\s*$",
r"please cite this article",
r"accepted manuscript",
]
RUN_CONFIGS = {
"abstract": ["Abstract"],
"title": ["Title"],
}
PAJAIS_CATEGORIES = [
"Artificial Intelligence", "Machine Learning", "Deep Learning",
"Natural Language Processing", "Computer Vision", "Robotics",
"Knowledge Representation", "Expert Systems", "Decision Support",
"Data Mining", "Information Retrieval", "Human-Computer Interaction",
"Ethics in AI", "Explainable AI", "Fairness and Bias",
"AI in Healthcare", "AI in Education", "AI in Finance",
"AI in Manufacturing", "AI in Agriculture", "AI Governance",
"Neural Networks", "Reinforcement Learning", "Federated Learning",
"AI Safety",
]
_MISTRAL = ChatMistralAI(model="mistral-large-latest", temperature=0)
# ---------------------------------------------------------------------------
# Helper β pure functions, no loops
# ---------------------------------------------------------------------------
def _clean_text(text: str) -> str:
combined = "|".join(BOILERPLATE_PATTERNS)
return re.sub(combined, "", text, flags=re.IGNORECASE).strip()
def _sentences_from_series(series: pd.Series) -> list[str]:
raw = series.dropna().str.cat(sep=" ")
return list(filter(None, map(str.strip, re.split(r"(?<=[.!?])\s+", raw))))
def _nearest_centroids(embeddings: np.ndarray, labels: np.ndarray, n: int = 5):
unique_labels = np.unique(labels)
centroids = np.array(list(map(
lambda lbl: embeddings[labels == lbl].mean(axis=0),
unique_labels,
)))
sim_matrix = cosine_similarity(centroids)
np.fill_diagonal(sim_matrix, -1)
nearest = list(map(
lambda i: unique_labels[np.argsort(sim_matrix[i])[::-1][:n]].tolist(),
range(len(unique_labels)),
))
return dict(zip(unique_labels.tolist(), nearest))
def _top_sentences(sentences: list[str], embeddings: np.ndarray,
centroid: np.ndarray, k: int = 5) -> list[str]:
sims = cosine_similarity([centroid], embeddings)[0]
top_idx = np.argsort(sims)[::-1][:k]
return list(map(lambda i: sentences[i], top_idx))
# ---------------------------------------------------------------------------
# Tool 1 β load_scopus_csv
# ---------------------------------------------------------------------------
@tool
def load_scopus_csv(csv_path: str, run_config: str = "abstract") -> str:
"""Load a Scopus CSV file, count papers/sentences, apply boilerplate regex
filter, and return a JSON summary. run_config must be 'abstract' or 'title'."""
df = pd.read_csv(csv_path)
columns = RUN_CONFIGS[run_config]
available_cols = list(filter(lambda c: c in df.columns, columns))
texts = df[available_cols].fillna("").apply(
lambda row: " ".join(row.values.astype(str)), axis=1
)
import re
# Step 1: basic cleaning
cleaned = list(map(_clean_text, texts))
# Step 2: π₯ remove boilerplate noise (ADD HERE)
cleaned = list(map(
lambda x: re.sub(
r"Β©.*|all rights reserved|copyright.*|palgrave.*",
"",
x,
flags=re.I
),
cleaned
))
sentences = _sentences_from_series(pd.Series(cleaned))
df["_cleaned_text"] = cleaned
df.to_parquet(csv_path.replace(".csv", "_cleaned.parquet"), index=False)
summary = {
"csv_path": csv_path,
"run_config": run_config,
"columns_used": available_cols,
"total_papers": int(len(df)),
"total_sentences": len(sentences),
"sample_titles": df["Title"].head(5).tolist() if "Title" in df.columns else [],
}
Path("summaries.json").write_text(json.dumps(summary, indent=2))
return json.dumps(summary)
# ---------------------------------------------------------------------------
# Tool 2 β run_bertopic_discovery
# ---------------------------------------------------------------------------
@tool
def run_bertopic_discovery(parquet_path: str, run_config: str = "abstract") -> str:
"""Embed sentences with all-MiniLM-L6-v2, cluster with AgglomerativeClustering
(cosine, threshold=0.7), find 5 nearest centroids per cluster, generate 4
Plotly charts. Saves summaries.json + emb.npy. Returns topic summaries JSON."""
df = pd.read_parquet(parquet_path)
columns = RUN_CONFIGS[run_config]
available_cols = list(filter(lambda c: c in df.columns, columns))
texts = df[available_cols].fillna("").apply(
lambda row: " ".join(row.values.astype(str)), axis=1
)
sentences = _sentences_from_series(texts)
model = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = model.encode(sentences, normalize_embeddings=True, show_progress_bar=False)
np.save("emb.npy", embeddings)
clustering = AgglomerativeClustering(
metric="cosine",
linkage="average",
distance_threshold=0.7,
n_clusters=None,
)
labels = clustering.fit_predict(embeddings)
unique_labels, counts = np.unique(labels, return_counts=True)
nearest = _nearest_centroids(embeddings, labels)
topic_summaries = list(map(
lambda pair: {
"topic_id": int(pair[0]),
"sentence_count": int(pair[1]),
"nearest_topics": nearest.get(int(pair[0]), []),
"top_sentences": _top_sentences(
sentences, embeddings,
embeddings[labels == pair[0]].mean(axis=0),
),
},
zip(unique_labels, counts),
))
# Sort by sentence count desc
topic_summaries.sort(key=lambda t: t["sentence_count"], reverse=True)
top100 = topic_summaries[:100]
# ---- Chart 1: Bar chart β top 20 topics by sentence count ----
top20 = top100[:20]
fig1 = px.bar(
x=[f"T{t['topic_id']}" for t in top20],
y=[t["sentence_count"] for t in top20],
labels={"x": "Topic", "y": "Sentences"},
title="Top 20 Topics by Sentence Count",
)
# ---- Chart 2: Treemap ----
fig2 = px.treemap(
names=[f"Topic {t['topic_id']}" for t in top100],
parents=["All"] * len(top100),
values=[t["sentence_count"] for t in top100],
title="Topic Distribution Treemap",
)
# ---- Chart 3: Scatter (PCA 2D projection) ----
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
coords = pca.fit_transform(embeddings)
fig3 = go.Figure(go.Scatter(
x=coords[:, 0], y=coords[:, 1],
mode="markers",
marker=dict(color=labels, colorscale="Viridis", size=4, opacity=0.6),
))
fig3.update_layout(title="Sentence Clusters (PCA 2D)")
# ---- Chart 4: Heatmap β top 10 topic cosine similarity ----
top10_ids = [t["topic_id"] for t in top100[:10]]
centroids10 = np.array(list(map(
lambda lbl: embeddings[labels == lbl].mean(axis=0),
top10_ids,
)))
sim10 = cosine_similarity(centroids10)
fig4 = px.imshow(
sim10,
x=[f"T{i}" for i in top10_ids],
y=[f"T{i}" for i in top10_ids],
color_continuous_scale="Blues",
title="Top-10 Topic Cosine Similarity Heatmap",
)
charts = {
"bar_top20": fig1.to_json(),
"treemap": fig2.to_json(),
"scatter_pca": fig3.to_json(),
"heatmap": fig4.to_json(),
}
result = {
"total_clusters": int(len(unique_labels)),
"top100_topics": top100,
"charts_html": charts,
}
existing = json.loads(Path("summaries.json").read_text())
existing.update({"bertopic": {"total_clusters": result["total_clusters"]}})
Path("summaries.json").write_text(json.dumps(existing, indent=2))
Path("charts.json").write_text(json.dumps(charts, indent=2))
Path("topics.json").write_text(json.dumps(top100, indent=2))
return json.dumps({
"total_clusters": result["total_clusters"],
"top100_count": len(top100),
"charts_saved": list(charts.keys()),
})
# ---------------------------------------------------------------------------
# Tool 3 β label_topics_with_llm
# ---------------------------------------------------------------------------
@tool
def label_topics_with_llm(topics_json_path: str = "topics.json") -> str:
"""Send top-100 topics to Mistral via PromptTemplate + JsonOutputParser to
generate human-readable labels. Returns labelled topics JSON."""
topics = json.loads(Path(topics_json_path).read_text())
batch = topics[:100]
prompt = PromptTemplate.from_template(
"You are a qualitative research expert. Below are topic clusters from a "
"systematic literature review. For EACH topic assign a concise label "
"(3-6 words) and one sentence of reasoning.\n\n"
"Topics:\n{topics_text}\n\n"
"Return ONLY valid JSON: a list of objects with keys: "
"topic_id, label, reasoning. No markdown fences."
)
parser = JsonOutputParser()
chain = prompt | _MISTRAL | parser
topics_text = "\n".join(list(map(
lambda t: f"Topic {t['topic_id']} ({t['sentence_count']} sentences): "
+ " | ".join(t["top_sentences"][:2]),
batch,
)))
labelled = chain.invoke({"topics_text": topics_text})
label_map = {item["topic_id"]: item for item in labelled}
enriched = list(map(
lambda t: {**t, **label_map.get(t["topic_id"], {"label": f"Topic {t['topic_id']}", "reasoning": ""})},
batch,
))
Path("labelled_topics.json").write_text(json.dumps(enriched, indent=2))
return json.dumps({"labelled_count": len(enriched), "path": "labelled_topics.json"})
# ---------------------------------------------------------------------------
# Tool 4 β consolidate_into_themes
# ---------------------------------------------------------------------------
@tool
def consolidate_into_themes(approved_groups_json: str) -> str:
"""Merge approved topic groups into themes, recompute centroids from emb.npy.
approved_groups_json: JSON list of {theme_name, topic_ids: [...]} objects."""
groups = json.loads(approved_groups_json)
embeddings = np.load("emb.npy")
topics = json.loads(Path("labelled_topics.json").read_text())
topic_id_to_sentences = {t["topic_id"]: t["top_sentences"] for t in topics}
themes = list(map(
lambda g: {
"theme_name": g["theme_name"],
"topic_ids": g["topic_ids"],
"top_sentences": sum(
list(map(lambda tid: topic_id_to_sentences.get(tid, []), g["topic_ids"])),
[],
)[:10],
"centroid": embeddings[
np.isin(np.arange(len(embeddings)), g["topic_ids"])
].mean(axis=0).tolist(),
},
groups,
))
Path("themes.json").write_text(json.dumps(themes, indent=2))
return json.dumps({"themes_count": len(themes), "theme_names": [t["theme_name"] for t in themes]})
# ---------------------------------------------------------------------------
# Tool 5 β compare_with_taxonomy
# ---------------------------------------------------------------------------
@tool
def compare_with_taxonomy(themes_json_path: str = "themes.json") -> str:
"""Map consolidated themes to PAJAIS 25 categories via Mistral.
Returns a mapping JSON."""
themes = json.loads(Path(themes_json_path).read_text())
prompt = PromptTemplate.from_template(
"You are an AI research taxonomist. Map each theme to the most relevant "
"PAJAIS category.\n\n"
"PAJAIS Categories:\n{categories}\n\n"
"Themes:\n{themes_text}\n\n"
"Return ONLY valid JSON: a list of objects with keys: "
"theme_name, pajais_category, confidence (0-1), rationale. No markdown."
)
parser = JsonOutputParser()
chain = prompt | _MISTRAL | parser
themes_text = "\n".join(list(map(
lambda t: f"- {t['theme_name']}: " + "; ".join(t["top_sentences"][:2]),
themes,
)))
mapping = chain.invoke({
"categories": "\n".join(list(map(lambda c: f" β’ {c}", PAJAIS_CATEGORIES))),
"themes_text": themes_text,
})
Path("taxonomy_mapping.json").write_text(json.dumps(mapping, indent=2))
return json.dumps({"mapped_count": len(mapping), "path": "taxonomy_mapping.json"})
# ---------------------------------------------------------------------------
# Tool 6 β generate_comparison_csv
# ---------------------------------------------------------------------------
@tool
def generate_comparison_csv(original_csv_path: str) -> str:
"""Generate a side-by-side comparison CSV of abstract vs title clustering
results for each paper. Returns path to output CSV."""
df = pd.read_csv(original_csv_path)
abstract_col = "Abstract" if "Abstract" in df.columns else None
title_col = "Title" if "Title" in df.columns else None
comparison = df[[c for c in [title_col, abstract_col] if c is not None]].copy()
comparison.columns = list(map(
lambda c: c + "_text",
[c for c in [title_col, abstract_col] if c is not None],
))
comparison.insert(0, "Paper_ID", range(1, len(df) + 1))
taxonomy_path = Path("taxonomy_mapping.json")
theme_label = list(map(
lambda _: "See themes.json for full mapping",
range(len(comparison)),
))
comparison["Theme_Assignment"] = theme_label
out_path = "comparison_abstract_vs_title.csv"
comparison.to_csv(out_path, index=False)
return json.dumps({"output_csv": out_path, "rows": len(comparison), "columns": comparison.columns.tolist()})
# ---------------------------------------------------------------------------
# Tool 7 β export_narrative
# ---------------------------------------------------------------------------
@tool
def export_narrative(context_json: str = "{}") -> str:
"""Generate a ~500-word Section 7 narrative via Mistral, synthesising all
prior analysis. context_json may contain extra instructions. Returns the
narrative text and saves it to narrative.md."""
context = json.loads(context_json)
themes = json.loads(Path("themes.json").read_text()) if Path("themes.json").exists() else []
mapping = json.loads(Path("taxonomy_mapping.json").read_text()) if Path("taxonomy_mapping.json").exists() else []
summaries = json.loads(Path("summaries.json").read_text()) if Path("summaries.json").exists() else {}
themes_summary = "\n".join(list(map(
lambda t: f"- **{t['theme_name']}**: " + "; ".join(t["top_sentences"][:1]),
themes,
)))
mapping_summary = "\n".join(list(map(
lambda m: f"- {m.get('theme_name','?')} β {m.get('pajais_category','?')} "
f"(confidence: {m.get('confidence', '?')})",
mapping,
)))
prompt = PromptTemplate.from_template(
"You are a senior academic researcher writing a systematic literature review. "
"Write Section 7 (Discussion & Synthesis) of approximately 500 words. "
"Use an academic tone, Braun & Clarke (2006) thematic analysis framing, "
"and reference the themes and PAJAIS taxonomy mappings provided.\n\n"
"Dataset summary:\n{summaries}\n\n"
"Themes identified:\n{themes}\n\n"
"PAJAIS taxonomy mapping:\n{mapping}\n\n"
"Extra context: {extra}\n\n"
"Write the section now. Use markdown headings."
)
chain = prompt | _MISTRAL
result = chain.invoke({
"summaries": json.dumps(summaries, indent=2),
"themes": themes_summary,
"mapping": mapping_summary,
"extra": context.get("extra_instructions", "None"),
})
narrative = result.content
Path("narrative.md").write_text(narrative)
return json.dumps({"narrative_path": "narrative.md", "word_count": len(narrative.split())}) |