-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
658 lines (572 loc) Β· 24 KB
/
app.py
File metadata and controls
658 lines (572 loc) Β· 24 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
"""
app.py β Streamlit UI for the Excel Workbook Risk Diagnostic Tool.
This is the application entry point. Run with:
streamlit run app.py
The UI layer is intentionally thin β all analysis logic lives in
workbook_analyser.py and the checker modules. This file handles:
- File upload and session state management
- Settings sidebar with configurable thresholds
- Progress display during analysis
- Interactive results summary
- PDF report download
"""
from __future__ import annotations
import tempfile
from pathlib import Path
import streamlit as st
import settings
from models import WorkbookAnalysisResult
from workbook_analyser import analyse_workbook
from reporter import generate_pdf_bytes
# ---------------------------------------------------------------------------
# Page configuration (must be first Streamlit call)
# ---------------------------------------------------------------------------
st.set_page_config(
page_title="Excel Risk Diagnostic",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded",
)
# ---------------------------------------------------------------------------
# Custom CSS for professional look
# ---------------------------------------------------------------------------
st.markdown("""
<style>
.main-header {
font-size: 1.8rem;
font-weight: 700;
color: #2C3E50;
margin-bottom: 0.25rem;
}
.sub-header {
font-size: 0.95rem;
color: #7F8C8D;
margin-bottom: 1.5rem;
}
.rag-red {
background-color: #C0392B;
color: white;
padding: 0.4rem 1.2rem;
border-radius: 6px;
font-weight: bold;
font-size: 1.1rem;
display: inline-block;
}
.rag-amber {
background-color: #E67E22;
color: white;
padding: 0.4rem 1.2rem;
border-radius: 6px;
font-weight: bold;
font-size: 1.1rem;
display: inline-block;
}
.rag-green {
background-color: #27AE60;
color: white;
padding: 0.4rem 1.2rem;
border-radius: 6px;
font-weight: bold;
font-size: 1.1rem;
display: inline-block;
}
.severity-high { color: #C0392B; font-weight: bold; }
.severity-medium { color: #E67E22; font-weight: bold; }
.severity-low { color: #27AE60; font-weight: bold; }
.finding-card {
background-color: #F8F9FA;
border-left: 4px solid #BDC3C7;
padding: 0.5rem 0.8rem;
margin-bottom: 0.5rem;
border-radius: 0 4px 4px 0;
}
.stDownloadButton > button {
width: 100%;
}
</style>
""", unsafe_allow_html=True)
# ---------------------------------------------------------------------------
# Session state initialisation
# ---------------------------------------------------------------------------
def _init_session_state() -> None:
"""Initialise session state keys if not already present."""
defaults = {
"results": [], # List of WorkbookAnalysisResult
"settings_applied": False,
# Settings overrides (None = use defaults from settings.py)
"ov_file_size_warn": None,
"ov_file_size_critical": None,
"ov_row_count_warn": None,
"ov_keywords": None,
"ov_literal_min_occ": None,
"visible_severities": ["High", "Medium", "Low"],
# AI Commentary settings (session-only β never written to disk)
"ai_enabled": False,
"ai_api_key": "",
"ai_feature_narrative": True,
"ai_feature_purpose": True,
"ai_feature_formulas": True,
"ai_feature_assumptions": True,
}
for key, default_val in defaults.items():
if key not in st.session_state:
st.session_state[key] = default_val
_init_session_state()
# ---------------------------------------------------------------------------
# Sidebar β Settings panel
# ---------------------------------------------------------------------------
def _render_sidebar() -> None:
"""Render the settings sidebar and apply any user overrides."""
with st.sidebar:
st.markdown("## βοΈ Settings")
st.markdown("Adjust thresholds without editing code.")
st.divider()
st.markdown("**File Size Thresholds (MB)**")
ov_warn = st.slider(
"Warning threshold",
min_value=1,
max_value=100,
value=int(st.session_state.get("ov_file_size_warn") or settings.FILE_SIZE_WARN_MB),
step=1,
help="Files above this size receive a Medium severity finding.",
)
ov_critical = st.slider(
"Critical threshold",
min_value=ov_warn,
max_value=200,
value=int(st.session_state.get("ov_file_size_critical") or settings.FILE_SIZE_CRITICAL_MB),
step=5,
help="Files above this size receive a High severity finding.",
)
st.markdown("**Cell Population Threshold**")
ov_rows = st.number_input(
"Max populated cells per sheet",
min_value=1000,
max_value=500000,
value=int(st.session_state.get("ov_row_count_warn") or settings.ROW_COUNT_WARN),
step=5000,
help="Sheets with more populated cells than this trigger a finding.",
)
st.markdown("**Actuarial Keywords**")
current_keywords = st.session_state.get("ov_keywords") or ", ".join(settings.ACTUARIAL_KEYWORDS)
ov_keywords_raw = st.text_area(
"Comma-separated keywords",
value=current_keywords,
height=120,
help=(
"Sheet names containing these words (case-insensitive) are "
"treated as high-sensitivity actuarial sheets."
),
)
st.markdown("**Hardcoded Literal Sensitivity**")
ov_literal_min_occ = st.number_input(
"Min. formula cells to flag a literal",
min_value=1,
max_value=10,
value=int(
st.session_state.get("ov_literal_min_occ")
or settings.LITERAL_MIN_OCCURRENCES
),
step=1,
help=(
"A numeric literal (e.g. 0.045 in =A1*0.045) is only flagged "
"if the same number appears in this many formula cells across "
"the workbook. Set to 1 to flag every occurrence; increase to "
"reduce false positives from legitimate one-off constants."
),
)
st.markdown("**Show Findings of Severity**")
visible_severities = st.multiselect(
"Severities to display in results",
options=["High", "Medium", "Low"],
default=st.session_state.get("visible_severities", ["High", "Medium", "Low"]),
help=(
"Filter which severity levels appear in the on-screen results. "
"The downloaded PDF always contains all findings."
),
)
st.divider()
if st.button("Apply Settings", use_container_width=True, type="primary"):
st.session_state["ov_file_size_warn"] = float(ov_warn)
st.session_state["ov_file_size_critical"] = float(ov_critical)
st.session_state["ov_row_count_warn"] = int(ov_rows)
st.session_state["ov_keywords"] = ov_keywords_raw
st.session_state["ov_literal_min_occ"] = int(ov_literal_min_occ)
st.session_state["visible_severities"] = visible_severities if visible_severities else ["High", "Medium", "Low"]
st.session_state["settings_applied"] = True
_apply_settings_overrides()
st.success("Settings applied.")
st.divider()
st.markdown("**Current RAG Score Thresholds**")
st.caption(
f"Green: 0β{settings.RAG_GREEN_MAX} | "
f"Amber: {settings.RAG_GREEN_MAX+1}β{settings.RAG_AMBER_MAX} | "
f"Red: {settings.RAG_AMBER_MAX+1}+"
)
st.markdown("**Severity Weights**")
st.caption(
f"High: {settings.SEVERITY_WEIGHTS['High']} pts | "
f"Medium: {settings.SEVERITY_WEIGHTS['Medium']} pts | "
f"Low: {settings.SEVERITY_WEIGHTS['Low']} pt"
)
st.divider()
with st.expander("π€ AI Commentary (Optional)"):
ai_enabled = st.checkbox(
"Enable AI commentary",
value=st.session_state.get("ai_enabled", False),
key="ai_enabled_widget",
)
st.session_state["ai_enabled"] = ai_enabled
if ai_enabled:
if st.session_state.get("ai_api_key", ""):
st.success("API key set for this session.")
if st.button("Remove API key", key="ai_api_key_remove"):
st.session_state["ai_api_key"] = ""
st.rerun()
else:
with st.form("ai_api_key_form", clear_on_submit=True):
entered_key = st.text_input(
"Anthropic API Key",
type="password",
help="Stored in this browser session only. Never written to disk.",
)
submitted = st.form_submit_button("Set API Key")
if submitted and entered_key:
st.session_state["ai_api_key"] = entered_key
st.rerun()
st.markdown("**Features to include:**")
st.session_state["ai_feature_narrative"] = st.checkbox(
"Findings narrative",
value=st.session_state.get("ai_feature_narrative", True),
key="ai_feat_narrative",
)
st.session_state["ai_feature_purpose"] = st.checkbox(
"Workbook purpose & sheet summaries",
value=st.session_state.get("ai_feature_purpose", True),
key="ai_feat_purpose",
)
st.session_state["ai_feature_formulas"] = st.checkbox(
"Key formula explanations",
value=st.session_state.get("ai_feature_formulas", True),
key="ai_feat_formulas",
)
st.session_state["ai_feature_assumptions"] = st.checkbox(
"Assumption commentary",
value=st.session_state.get("ai_feature_assumptions", True),
key="ai_feat_assumptions",
)
st.warning("β AI analysis adds ~30β60 seconds per workbook.")
def _apply_settings_overrides() -> None:
"""
Apply session state overrides to the settings module in-place.
This patches the module-level constants so that workbook_analyser
and all checkers pick up the user's values without code changes.
"""
if st.session_state.get("ov_file_size_warn") is not None:
settings.FILE_SIZE_WARN_MB = st.session_state["ov_file_size_warn"]
if st.session_state.get("ov_file_size_critical") is not None:
settings.FILE_SIZE_CRITICAL_MB = st.session_state["ov_file_size_critical"]
if st.session_state.get("ov_row_count_warn") is not None:
settings.ROW_COUNT_WARN = st.session_state["ov_row_count_warn"]
if st.session_state.get("ov_keywords") is not None:
raw = st.session_state["ov_keywords"]
keywords = [k.strip().lower() for k in raw.split(",") if k.strip()]
if keywords:
settings.ACTUARIAL_KEYWORDS = keywords
if st.session_state.get("ov_literal_min_occ") is not None:
settings.LITERAL_MIN_OCCURRENCES = int(st.session_state["ov_literal_min_occ"])
# Apply any previously saved overrides on re-run
if st.session_state.get("settings_applied"):
_apply_settings_overrides()
# ---------------------------------------------------------------------------
# RAG badge HTML helper
# ---------------------------------------------------------------------------
def _rag_badge_html(rag: str) -> str:
"""Return an HTML badge string for the given RAG rating."""
css_class = {"Red": "rag-red", "Amber": "rag-amber", "Green": "rag-green"}.get(
rag, "rag-green"
)
return f'<span class="{css_class}">{rag.upper()}</span>'
def _severity_html(severity: str) -> str:
"""Return coloured HTML for a severity label."""
css_class = {
"High": "severity-high",
"Medium": "severity-medium",
"Low": "severity-low",
}.get(severity, "")
return f'<span class="{css_class}">{severity}</span>'
# ---------------------------------------------------------------------------
# Results display
# ---------------------------------------------------------------------------
def _display_result(result: WorkbookAnalysisResult, index: int) -> None:
"""
Render an interactive summary of a single workbook analysis result.
Shows:
- RAG badge and risk score
- Count by severity (columns)
- Top 5 findings
- PDF download button
Args:
result: The completed WorkbookAnalysisResult.
index: Position index (for unique widget keys).
"""
with st.expander(
f"{'β
' if result.rag_rating == 'Green' else 'β οΈ' if result.rag_rating == 'Amber' else 'π΄'} "
f"{result.filename} β {result.rag_rating} (Score: {result.total_score})",
expanded=True,
):
if result.error_message:
st.error(f"**Analysis failed:** {result.error_message}")
return
# RAG + score
col_rag, col_score, col_ts = st.columns([2, 1, 2])
with col_rag:
st.markdown(
f"**Overall Rating:** {_rag_badge_html(result.rag_rating)}",
unsafe_allow_html=True,
)
with col_score:
st.metric("Risk Score", result.total_score)
with col_ts:
ts = result.analysis_timestamp.strftime("%d %b %Y %H:%M UTC")
st.caption(f"Analysed at: {ts}")
st.divider()
# Severity counts (always show full counts β unaffected by filter)
counts = result.count_by_severity()
c1, c2, c3, c4 = st.columns(4)
with c1:
st.metric("Total Findings", len(result.findings))
with c2:
st.metric("π΄ High", counts["High"])
with c3:
st.metric("π Medium", counts["Medium"])
with c4:
st.metric("π’ Low", counts["Low"])
# High-sensitivity sheets
if result.high_sensitivity_sheets:
st.markdown(
f"**Actuarially Sensitive Sheets:** "
+ ", ".join(f"`{s}`" for s in result.high_sensitivity_sheets)
)
st.divider()
# Apply severity visibility filter from sidebar
visible = st.session_state.get("visible_severities", ["High", "Medium", "Low"])
visible_findings = [f for f in result.findings if f.severity in visible]
# Top 5 findings (filtered by visible severities)
severity_order = {"High": 0, "Medium": 1, "Low": 2}
sorted_visible = sorted(
visible_findings,
key=lambda f: (severity_order.get(f.severity, 3), f.check_id),
)
top = sorted_visible[:5]
hidden_count = len(result.findings) - len(visible_findings)
if hidden_count > 0:
st.caption(
f"βΉοΈ {hidden_count} finding(s) hidden by severity filter. "
f"PDF report always includes all findings."
)
if top:
st.markdown("**Top Findings:**")
for f in top:
sev_html = _severity_html(f.severity)
location = f.sheet_name
if f.cell_ref:
location += f" β {f.cell_ref}"
st.markdown(
f'<div class="finding-card">'
f'{sev_html} '
f'<b>{f.name}</b> | '
f'<code>{location}</code><br>'
f'<small>{f.description[:200]}</small>'
f'</div>',
unsafe_allow_html=True,
)
elif not result.findings:
st.success("No findings detected β workbook passed all checks.")
else:
st.info("All findings are hidden by the current severity filter.")
st.divider()
# AI Commentary section
ai = result.ai_commentary
if ai is not None:
if ai.api_error:
st.warning(f"AI Commentary Error: {ai.api_error}")
has_content = any([
ai.workbook_purpose,
ai.sheet_narratives,
ai.assumption_commentary,
ai.formula_explanations,
ai.findings_narrative,
])
if has_content:
st.markdown("### π€ AI Workbook Intelligence")
st.caption("AI-Generated β Interpretive Only. Validate with the model owner.")
if ai.workbook_purpose:
st.markdown("**Workbook Purpose**")
st.info(ai.workbook_purpose)
if ai.sheet_narratives:
st.markdown("**Sheet-by-Sheet Narratives**")
for sn in ai.sheet_narratives:
st.markdown(
f"<details><summary><b>{sn.sheet_name}</b></summary>"
f"<p>{sn.narrative}</p></details>",
unsafe_allow_html=True,
)
if ai.assumption_commentary:
st.markdown("**Assumption & Input Commentary**")
st.info(ai.assumption_commentary)
if ai.formula_explanations:
st.markdown("**Key Formula Explanations**")
for fe in ai.formula_explanations:
label = f"{fe.sheet_name} β {fe.cell_address}" if fe.cell_address else fe.sheet_name
formula_block = f"<pre><code>{fe.formula}</code></pre>" if fe.formula else ""
st.markdown(
f"<details><summary><b>{label}</b></summary>"
f"{formula_block}<p>{fe.explanation}</p></details>",
unsafe_allow_html=True,
)
if ai.findings_narrative:
st.markdown("**Findings Narrative**")
st.info(ai.findings_narrative)
st.divider()
# PDF download button
try:
pdf_bytes = generate_pdf_bytes(result)
safe_name = result.filename.replace(" ", "_").rsplit(".", 1)[0]
st.download_button(
label="π₯ Download PDF Report",
data=pdf_bytes,
file_name=f"Risk_Report_{safe_name}.pdf",
mime="application/pdf",
key=f"download_{index}",
use_container_width=True,
type="primary",
)
except Exception as pdf_err:
st.error(f"PDF generation failed: {pdf_err}")
# ---------------------------------------------------------------------------
# Analysis runner
# ---------------------------------------------------------------------------
def _run_analysis(uploaded_files: list) -> None:
"""
Save uploaded files to temp paths and run analysis on each.
Displays a progress bar and status messages during processing.
Results are stored in st.session_state['results'].
Args:
uploaded_files: List of Streamlit UploadedFile objects.
"""
st.session_state["results"] = []
n = len(uploaded_files)
progress = st.progress(0, text="Starting analysis...")
status_container = st.empty()
# Build AI config from session state (api_key never logged or persisted)
ai_config: dict | None = None
if st.session_state.get("ai_enabled"):
ai_config = {
"enabled": True,
"api_key": st.session_state.get("ai_api_key", ""),
"findings_narrative": st.session_state.get("ai_feature_narrative", True),
"purpose_and_sheets": st.session_state.get("ai_feature_purpose", True),
"formula_explanations": st.session_state.get("ai_feature_formulas", True),
"assumption_commentary": st.session_state.get("ai_feature_assumptions", True),
}
for i, uploaded_file in enumerate(uploaded_files):
pct = int(i / n * 100)
progress.progress(pct, text=f"Analysing {uploaded_file.name} ({i+1}/{n})...")
try:
# Save to a temp file (openpyxl needs a real file path)
suffix = Path(uploaded_file.name).suffix
with tempfile.NamedTemporaryFile(
delete=False, suffix=suffix
) as tmp:
tmp.write(uploaded_file.getbuffer())
tmp_path = Path(tmp.name)
def make_callback(container, filename):
"""Create a closure capturing the status container."""
def callback(msg: str) -> None:
container.info(f"**{filename}:** {msg}")
return callback
cb = make_callback(status_container, uploaded_file.name)
result = analyse_workbook(tmp_path, status_callback=cb, ai_config=ai_config)
# Restore the original filename (temp path has a UUID name)
result.filename = uploaded_file.name
st.session_state["results"].append(result)
except Exception as exc:
from models import WorkbookAnalysisResult
from datetime import datetime, timezone
error_result = WorkbookAnalysisResult(
filename=uploaded_file.name,
file_path=Path(uploaded_file.name),
file_size_mb=0.0,
analysis_timestamp=datetime.now(tz=timezone.utc),
error_message=f"Unexpected error during analysis: {exc}",
)
st.session_state["results"].append(error_result)
finally:
# Clean up temp file
try:
tmp_path.unlink(missing_ok=True)
except Exception:
pass
progress.progress(100, text="Analysis complete.")
status_container.empty()
# ---------------------------------------------------------------------------
# Main app layout
# ---------------------------------------------------------------------------
_render_sidebar()
st.markdown(
'<p class="main-header">π Excel Workbook Risk Diagnostic Tool</p>',
unsafe_allow_html=True,
)
st.markdown(
'<p class="sub-header">'
"Upload one or more Excel workbooks to receive an automated risk "
"diagnostic report identifying formula errors, structural risks, VBA "
"vulnerabilities, and actuarial assumption issues."
"</p>",
unsafe_allow_html=True,
)
st.divider()
# File upload widget
uploaded_files = st.file_uploader(
"Upload Excel workbooks (.xlsx or .xlsm)",
type=["xlsx", "xlsm"],
accept_multiple_files=True,
help=(
"You can upload multiple files at once. "
"Password-protected files will be reported as unanalysable."
),
)
col_analyse, col_clear = st.columns([3, 1])
with col_analyse:
analyse_btn = st.button(
"π Analyse All Files",
disabled=not uploaded_files,
use_container_width=True,
type="primary",
)
with col_clear:
clear_btn = st.button(
"Clear Results",
disabled=not st.session_state.get("results"),
use_container_width=True,
)
if clear_btn:
st.session_state["results"] = []
st.rerun()
if analyse_btn and uploaded_files:
_run_analysis(uploaded_files)
# Display results
results: list[WorkbookAnalysisResult] = st.session_state.get("results", [])
if results:
st.divider()
st.markdown(f"### Analysis Results ({len(results)} workbook(s))")
for i, result in enumerate(results):
_display_result(result, i)
elif not uploaded_files:
# Onboarding hint
st.info(
"π Upload one or more **.xlsx** or **.xlsm** files above, then click "
"**Analyse All Files** to begin. Adjust thresholds in the sidebar "
"before running if needed."
)