Spaces:
Running
Running
File size: 11,902 Bytes
22d8a93 | 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 | """
core/bench_processor.py
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Document processor for the Peer Institution Benchmarking module.
Responsibilities
ββββββββββββββββ
β’ Accept Streamlit UploadedFile objects and return text chunks suitable for
LLM-based sustainability analysis.
β’ Support all common sustainability report formats:
PDF β text via pypdf
DOCX β text via python-docx (paragraphs + tables)
TXT β decoded directly (no external library needed)
CSV β tabular text via pandas
XLSX β multi-sheet tabular text via pandas
β’ Apply benchmarking-appropriate chunking (sentence-boundary split, 600-char
chunks with 80-char overlap β larger than the RAG default to preserve more
context per LLM call).
Public API
ββββββββββ
parse_peer_report(uploaded_file) β list[str]
Streamlit UploadedFile β chunked text list.
Returns [] on parse failure; surfaces errors via st.error().
extract_report_text(filepath) β str
Filepath string/Path β raw plain text (un-chunked).
Useful for ad-hoc extraction outside the Streamlit context.
chunk_report(text, chunk_size, overlap) β list[str]
Split raw text into overlapping sentence-boundary chunks.
Design notes
ββββββββββββ
This module intentionally does NOT import from core.processor to avoid
coupling β it only needs the low-level loaders, which it re-implements
as thin wrappers. core.processor remains the authoritative source for
SPJIMR's own operational data ingestion (extract_spjimr_metrics_raw,
extract_waste_series, etc.).
"""
from __future__ import annotations
import logging
import os
import re
import tempfile
from pathlib import Path
from typing import Union
logger = logging.getLogger(__name__)
# ββ Chunking defaults for benchmarking (larger than RAG default) ββββββββββββββ
BENCH_CHUNK_SIZE = 600 # chars per chunk
BENCH_CHUNK_OVERLAP = 80 # overlap between adjacent chunks
BENCH_MAX_CHARS = 120_000 # hard cap per document to prevent MemoryError
# ββ Accepted file extensions ββββββββββββββββββββββββββββββββββββββββββββββββββ
SUPPORTED_FORMATS = {".pdf", ".docx", ".txt", ".csv", ".xlsx", ".xls"}
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Text extraction β one function per format
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _extract_pdf(filepath: Union[str, Path]) -> str:
"""Extract text from a PDF using pypdf (page-by-page)."""
from pypdf import PdfReader
reader = PdfReader(str(filepath))
pages: list[str] = []
for i, page in enumerate(reader.pages):
try:
txt = page.extract_text()
if txt and txt.strip():
pages.append(txt.strip())
except Exception as exc:
logger.warning("PDF page %d extraction failed: %s", i, exc)
return "\n\n".join(pages)
def _extract_docx(filepath: Union[str, Path]) -> str:
"""Extract text from a DOCX file β paragraphs + table cells."""
from docx import Document
doc = Document(str(filepath))
parts: list[str] = []
# Paragraphs
for para in doc.paragraphs:
t = para.text.strip()
if t:
parts.append(t)
# Tables (each row joined with pipe separator)
for table in doc.tables:
for row in table.rows:
row_text = " | ".join(
cell.text.strip() for cell in row.cells if cell.text.strip()
)
if row_text:
parts.append(row_text)
return "\n".join(parts)
def _extract_txt(filepath: Union[str, Path]) -> str:
"""Read a plain-text file, trying UTF-8 then latin-1 fallback."""
path = Path(filepath)
try:
return path.read_text(encoding="utf-8")
except UnicodeDecodeError:
return path.read_text(encoding="latin-1", errors="replace")
def _extract_csv(filepath: Union[str, Path]) -> str:
"""Convert a CSV to readable plain text (first 500 rows)."""
import pandas as pd
try:
df = pd.read_csv(filepath, encoding="utf-8", on_bad_lines="skip")
except UnicodeDecodeError:
df = pd.read_csv(filepath, encoding="latin-1", on_bad_lines="skip")
df.dropna(how="all", inplace=True)
df = df.head(500)
return f"=== {Path(filepath).stem} ===\n{df.to_string(index=False, na_rep='N/A')}"
def _extract_xlsx(filepath: Union[str, Path]) -> str:
"""Convert all sheets of an XLSX to readable plain text (first 500 rows each)."""
import pandas as pd
xl = pd.ExcelFile(str(filepath), engine="openpyxl")
parts: list[str] = []
for sheet in xl.sheet_names:
df = xl.parse(sheet).dropna(how="all").head(500)
if df.empty:
continue
df.columns = [str(c).strip() for c in df.columns]
parts.append(
f"=== {Path(filepath).stem} β {sheet} ===\n"
+ df.to_string(index=False, na_rep="N/A")
)
return "\n\n".join(parts)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Chunking
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def chunk_report(
text: str,
chunk_size: int = BENCH_CHUNK_SIZE,
overlap: int = BENCH_CHUNK_OVERLAP,
) -> list[str]:
"""
Split text into overlapping chunks on sentence boundaries.
Algorithm:
1. Split on sentence-ending punctuation (. ! ?) followed by whitespace.
2. Accumulate sentences until the chunk would exceed `chunk_size`.
3. Slide forward by one sentence at a time to create overlap.
"""
if not text or not text.strip():
return []
# Sentence split β keep the delimiter attached to the preceding sentence
sentences = re.split(r"(?<=[.!?])\s+", text.strip())
sentences = [s.strip() for s in sentences if s.strip()]
chunks: list[str] = []
start_idx: int = 0
while start_idx < len(sentences):
chunk_sents: list[str] = []
char_count = 0
for i in range(start_idx, len(sentences)):
s = sentences[i]
if char_count + len(s) > chunk_size and chunk_sents:
break
chunk_sents.append(s)
char_count += len(s) + 1 # +1 for space
if not chunk_sents:
# Single sentence exceeds chunk_size β hard-split it
long = sentences[start_idx]
for j in range(0, len(long), chunk_size):
chunks.append(long[j : j + chunk_size])
start_idx += 1
continue
chunks.append(" ".join(chunk_sents))
# Find next start with overlap
overlap_chars = 0
next_start = len(chunk_sents) # default: no overlap
for back in range(len(chunk_sents) - 1, -1, -1):
overlap_chars += len(chunk_sents[back])
if overlap_chars >= overlap:
next_start = back
break
start_idx += max(1, next_start)
return chunks
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Public API
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def extract_report_text(filepath: Union[str, Path]) -> str:
"""
Extract plain text from a sustainability report file.
Supports: PDF, DOCX, TXT, CSV, XLSX/XLS.
Applies BENCH_MAX_CHARS hard cap.
Raises ValueError for unsupported extensions.
Raises exceptions from underlying libraries on parse failure.
"""
filepath = Path(filepath)
ext = filepath.suffix.lower()
if ext not in SUPPORTED_FORMATS:
raise ValueError(
f"Unsupported format '{ext}'. "
f"Accepted: {', '.join(sorted(SUPPORTED_FORMATS))}"
)
if ext == ".pdf": text = _extract_pdf(filepath)
elif ext == ".docx": text = _extract_docx(filepath)
elif ext == ".txt": text = _extract_txt(filepath)
elif ext == ".csv": text = _extract_csv(filepath)
elif ext in (".xlsx", ".xls"):text = _extract_xlsx(filepath)
else:
text = "" # unreachable, but satisfies type checker
# Hard cap
if len(text) > BENCH_MAX_CHARS:
logger.warning(
"Document %s truncated from %d β %d chars.",
filepath.name, len(text), BENCH_MAX_CHARS,
)
text = text[:BENCH_MAX_CHARS] + "\n\n[... document truncated ...]"
return text
def parse_peer_report(uploaded_file, institution_name: str = "") -> list[str]:
"""
Parse a Streamlit UploadedFile containing a peer institution's sustainability
report into a list of text chunks ready for LLM analysis.
Parameters
----------
uploaded_file : Streamlit UploadedFile
institution_name: str β used only in log messages
Returns
-------
list[str] β chunks (may be empty if extraction yields no text)
Side-effects
------------
Calls st.error() when the file cannot be parsed so the UI shows a
friendly message. Does NOT raise β always returns a list.
"""
import streamlit as st
label = institution_name or uploaded_file.name
suffix = Path(uploaded_file.name).suffix.lower()
if suffix not in SUPPORTED_FORMATS:
st.error(
f"β **{label}** β unsupported format '{suffix}'. "
f"Please upload one of: {', '.join(sorted(SUPPORTED_FORMATS))}"
)
return []
# Write to a temp file so all extractors can use filepath-based APIs
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
tmp.write(uploaded_file.read())
tmp_path = tmp.name
except Exception as exc:
st.error(f"β **{label}** β could not write temp file: {exc}")
return []
try:
text = extract_report_text(tmp_path)
except Exception as exc:
logger.error("parse_peer_report failed for %s: %s", label, exc)
st.error(f"β **{label}** β failed to extract text: {exc}")
return []
finally:
try:
os.unlink(tmp_path)
except OSError:
pass
if not text.strip():
st.warning(
f"β οΈ **{label}** β no text could be extracted. "
"The file may be scanned/image-only or empty."
)
return []
chunks = chunk_report(text)
logger.info(
"parse_peer_report: '%s' β %d chars β %d chunks", label, len(text), len(chunks)
)
return chunks
|