683 lines
31 KiB
Python
683 lines
31 KiB
Python
"""Modular presentation components for the medical coding Streamlit user interface."""
|
||
|
||
import html
|
||
import re
|
||
from typing import Any
|
||
|
||
import streamlit as st
|
||
|
||
from medical_coding.schemas.enums import AbstentionReason
|
||
from medical_coding.schemas.response import CodedDiagnosisResponse, CodingResult
|
||
from medical_coding.schemas.validation import AbstentionRecord
|
||
|
||
|
||
def clean_html(raw_html: str) -> str:
|
||
"""Strip leading and trailing whitespace from each line of an HTML string.
|
||
|
||
Prevents CommonMark from interpreting indented HTML lines as Markdown code blocks (<pre><code>).
|
||
"""
|
||
return "\n".join(line.strip() for line in raw_html.strip().splitlines() if line.strip())
|
||
|
||
|
||
def render_header(
|
||
offline_mode: bool = True,
|
||
catalog_size: int = 25,
|
||
db_connected: bool = True,
|
||
) -> None:
|
||
"""Render the application branding banner with real-time operational status pills."""
|
||
db_status_pill = (
|
||
'<span class="pill-badge badge-success">● SQLite Connected</span>'
|
||
if db_connected
|
||
else '<span class="pill-badge badge-danger">● DB Disconnected</span>'
|
||
)
|
||
offline_pill = (
|
||
'<span class="pill-badge badge-info">🔒 100% Offline AI</span>'
|
||
if offline_mode
|
||
else '<span class="pill-badge badge-warning">Online Mode</span>'
|
||
)
|
||
catalog_pill = f'<span class="pill-badge badge-secondary">📚 {catalog_size} ICD Codes</span>'
|
||
|
||
header_html = clean_html(
|
||
f"""
|
||
<div class="clinic-header">
|
||
<div style="display: flex; justify-content: space-between; align-items: flex-start; flex-wrap: wrap; gap: 12px;">
|
||
<div>
|
||
<h1 class="clinic-title">
|
||
<span>🏥</span> Local ICD-10-CM Medical Coding Engine
|
||
</h1>
|
||
<p class="clinic-subtitle">
|
||
Evidence-backed clinical diagnosis extraction, hybrid semantic retrieval, deterministic rule auditing, and persistent encounter warehousing.
|
||
</p>
|
||
</div>
|
||
<div style="display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding-top: 4px;">
|
||
{offline_pill}
|
||
{db_status_pill}
|
||
{catalog_pill}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
"""
|
||
)
|
||
st.markdown(header_html, unsafe_allow_html=True)
|
||
|
||
|
||
def render_kpi_metrics(result: CodingResult) -> None:
|
||
"""Render high-level KPIs for a single document coding run."""
|
||
c1, c2, c3, c4 = st.columns(4)
|
||
|
||
status_str = result.status.value if hasattr(result.status, "value") else str(result.status)
|
||
status_class = (
|
||
"badge-success"
|
||
if status_str == "SUCCESS"
|
||
else "badge-warning"
|
||
if status_str == "PARTIAL_SUCCESS"
|
||
else "badge-danger"
|
||
)
|
||
|
||
with c1:
|
||
st.markdown(
|
||
clean_html(
|
||
f"""
|
||
<div class="kpi-card">
|
||
<div class="kpi-label">Status</div>
|
||
<div style="margin-top: 8px;">
|
||
<span class="pill-badge {status_class}">{status_str}</span>
|
||
</div>
|
||
</div>
|
||
"""
|
||
),
|
||
unsafe_allow_html=True,
|
||
)
|
||
|
||
with c2:
|
||
primary_code = result.primary_diagnosis.code if result.primary_diagnosis else "None"
|
||
st.markdown(
|
||
clean_html(
|
||
f"""
|
||
<div class="kpi-card">
|
||
<div class="kpi-label">Primary ICD-10</div>
|
||
<div class="kpi-value" style="color: #38bdf8;">{primary_code}</div>
|
||
</div>
|
||
"""
|
||
),
|
||
unsafe_allow_html=True,
|
||
)
|
||
|
||
with c3:
|
||
st.markdown(
|
||
clean_html(
|
||
f"""
|
||
<div class="kpi-card">
|
||
<div class="kpi-label">Secondary Diags</div>
|
||
<div class="kpi-value">{len(result.secondary_diagnoses)}</div>
|
||
</div>
|
||
"""
|
||
),
|
||
unsafe_allow_html=True,
|
||
)
|
||
|
||
with c4:
|
||
abst_count = len(result.abstentions)
|
||
abst_color = "#f87171" if abst_count > 0 else "#94a3b8"
|
||
st.markdown(
|
||
clean_html(
|
||
f"""
|
||
<div class="kpi-card">
|
||
<div class="kpi-label">Abstentions</div>
|
||
<div class="kpi-value" style="color: {abst_color};">{abst_count}</div>
|
||
</div>
|
||
"""
|
||
),
|
||
unsafe_allow_html=True,
|
||
)
|
||
|
||
|
||
def _clean_audit_from_evidence(ev: str) -> str:
|
||
"""Clean internal audit justifications and boilerplate from display evidence strings."""
|
||
if not ev:
|
||
return ""
|
||
if "Designated as " in ev:
|
||
ev = ev.split("Designated as ")[0].strip(" -|:\n")
|
||
if "Clinical criteria:" in ev:
|
||
ev = ev.split("Clinical criteria:")[0].strip(" -|:\n")
|
||
return ev.strip()
|
||
|
||
|
||
def render_primary_diagnosis(primary: CodedDiagnosisResponse | None) -> None:
|
||
"""Render the primary diagnosis card with evidence and specificity confirmation."""
|
||
st.markdown("### 🎯 Primary Diagnosis")
|
||
if not primary or not primary.code or primary.code in ("None", "[NO LOCAL CODE]") or primary.matching_status == "NO_DATABASE_MATCH":
|
||
st.info("ℹ️ No primary diagnosis assigned for this document (abstained or undetermined).")
|
||
return
|
||
|
||
code_display = html.escape(primary.code)
|
||
billable_badge = (
|
||
'<span class="pill-badge badge-success">✓ HIPAA Billable Leaf</span>'
|
||
if primary.is_terminal_billable
|
||
else '<span class="pill-badge badge-danger">⚠ Non-Billable Header</span>'
|
||
)
|
||
acuity_badge = f'<span class="pill-badge badge-info">{html.escape(primary.acuity.value if hasattr(primary.acuity, "value") else str(primary.acuity))}</span>'
|
||
certainty_badge = f'<span class="pill-badge badge-secondary">{html.escape(primary.certainty.value if hasattr(primary.certainty, "value") else str(primary.certainty))}</span>'
|
||
nearest_badge = '<span class="pill-badge badge-warning">Nearest Relatable Match</span>' if getattr(primary, "matching_status", "") == "NEAREST_MATCH" else ""
|
||
|
||
conf_pct = int(primary.confidence_score * 100)
|
||
|
||
codes_pills = []
|
||
if primary.icd10cm:
|
||
codes_pills.append(f'<span class="pill-badge badge-info">ICD-10-CM: {html.escape(str(primary.icd10cm))}</span>')
|
||
if primary.icdo:
|
||
codes_pills.append(f'<span class="pill-badge badge-warning">ICD-O: {html.escape(str(primary.icdo))}</span>')
|
||
if primary.cpt:
|
||
codes_pills.append(f'<span class="pill-badge badge-success">CPT: {html.escape(str(primary.cpt))}</span>')
|
||
codes_html = " ".join(codes_pills)
|
||
|
||
documented_row = ""
|
||
if primary.raw_term and primary.raw_term.lower() != (primary.description or "").lower():
|
||
documented_row = f'<div style="margin: 6px 0; font-size: 14px; color: #e2e8f0;">🩺 <strong style="color: #38bdf8;">Documented Clinical Diagnosis:</strong> {html.escape(str(primary.raw_term))}</div>'
|
||
|
||
safe_desc = html.escape(primary.description or "")
|
||
from medical_coding.utils.text import deduplicate_evidence_spans
|
||
spans = deduplicate_evidence_spans(primary.evidence_quote, primary.normalized_diagnosis or primary.description)
|
||
if len(spans) > 1:
|
||
spans_html = "".join(f'<div style="margin-top: 4px; padding-left: 8px; border-left: 2px solid #38bdf8;">• <span class="evidence-quote">{html.escape(s)}</span></div>' for s in spans)
|
||
evidence_content_html = spans_html
|
||
elif spans:
|
||
evidence_content_html = f'<span class="evidence-quote">"{html.escape(spans[0])}"</span>'
|
||
else:
|
||
evidence_content_html = '<span class="evidence-quote">"None"</span>'
|
||
|
||
primary_html = clean_html(
|
||
f"""
|
||
<div class="primary-card">
|
||
<div class="primary-card-header">
|
||
<div>
|
||
<span class="primary-code">{code_display}</span>
|
||
<span style="margin-left: 12px;">{billable_badge}</span>
|
||
</div>
|
||
<div>
|
||
<span style="font-size: 13px; color: #94a3b8; margin-right: 6px;">Confidence:</span>
|
||
<strong style="color: #38bdf8;">{conf_pct}%</strong>
|
||
</div>
|
||
</div>
|
||
<div class="primary-desc">
|
||
<span style="font-size: 13px; color: #94a3b8; display: block; margin-bottom: 2px;">📖 Matched Database Concept:</span>
|
||
{safe_desc}
|
||
</div>
|
||
{documented_row}
|
||
<div style="display: flex; gap: 8px; margin: 10px 0; flex-wrap: wrap;">
|
||
{acuity_badge}
|
||
{certainty_badge}
|
||
{nearest_badge}
|
||
<span class="pill-badge badge-info">Chief Reason for Encounter</span>
|
||
{codes_html}
|
||
</div>
|
||
<div class="evidence-box">
|
||
<span style="font-weight: 600; color: #6ee7b7; font-size: 11px; text-transform: uppercase;">
|
||
📄 Verbatim Clinical Evidence Across Record:
|
||
</span><br/>
|
||
{evidence_content_html}
|
||
</div>
|
||
</div>
|
||
"""
|
||
)
|
||
st.markdown(primary_html, unsafe_allow_html=True)
|
||
|
||
|
||
def render_secondary_diagnoses(secondaries: list[CodedDiagnosisResponse]) -> None:
|
||
"""Render the list of validated secondary comorbid diagnoses."""
|
||
from medical_coding.utils.text import deduplicate_evidence_spans, normalize_diagnosis_name
|
||
|
||
# Deduplicate secondaries by code and clean concept
|
||
seen_sec_codes: set[str] = set()
|
||
seen_sec_names: set[str] = set()
|
||
valid_secondaries: list[CodedDiagnosisResponse] = []
|
||
for s in (secondaries or []):
|
||
if not s.code or s.code in ("None", "[NO LOCAL CODE]") or s.matching_status == "NO_DATABASE_MATCH":
|
||
continue
|
||
c_key = s.code.strip()
|
||
n_key = (normalize_diagnosis_name(s.normalized_diagnosis or s.raw_term or s.description or "") or "").strip().lower()
|
||
if c_key in seen_sec_codes or (n_key and n_key in seen_sec_names):
|
||
continue
|
||
seen_sec_codes.add(c_key)
|
||
if n_key:
|
||
seen_sec_names.add(n_key)
|
||
valid_secondaries.append(s)
|
||
|
||
st.markdown(f"### 📋 Secondary Diagnoses ({len(valid_secondaries)})")
|
||
if not valid_secondaries:
|
||
st.write("No secondary comorbid diagnoses identified.")
|
||
return
|
||
|
||
for idx, diag in enumerate(valid_secondaries, start=1):
|
||
sec_code_display = html.escape(diag.code)
|
||
billable_badge = (
|
||
'<span class="pill-badge badge-success">✓ Billable</span>'
|
||
if diag.is_terminal_billable
|
||
else '<span class="pill-badge badge-danger">⚠ Non-Billable</span>'
|
||
)
|
||
acuity_str = html.escape(diag.acuity.value if hasattr(diag.acuity, "value") else str(diag.acuity))
|
||
certainty_str = html.escape(diag.certainty.value if hasattr(diag.certainty, "value") else str(diag.certainty))
|
||
nearest_badge = '<span class="pill-badge badge-warning">Nearest Relatable Match</span>' if getattr(diag, "matching_status", "") == "NEAREST_MATCH" else ""
|
||
|
||
sec_codes_pills = []
|
||
if diag.icd10cm:
|
||
sec_codes_pills.append(f'<span class="pill-badge badge-info">ICD-10-CM: {html.escape(str(diag.icd10cm))}</span>')
|
||
if diag.icdo:
|
||
sec_codes_pills.append(f'<span class="pill-badge badge-warning">ICD-O: {html.escape(str(diag.icdo))}</span>')
|
||
if diag.cpt:
|
||
sec_codes_pills.append(f'<span class="pill-badge badge-success">CPT: {html.escape(str(diag.cpt))}</span>')
|
||
sec_codes_html = " ".join(sec_codes_pills)
|
||
|
||
documented_row = ""
|
||
if diag.raw_term and diag.raw_term.lower() != (diag.description or "").lower():
|
||
documented_row = f'<div style="margin-top: 4px; font-size: 13px; color: #cbd5e1;">🩺 <strong style="color: #38bdf8;">Documented Diagnosis:</strong> {html.escape(str(diag.raw_term))}</div>'
|
||
|
||
safe_desc = html.escape(diag.description or "")
|
||
spans = deduplicate_evidence_spans(diag.evidence_quote, diag.normalized_diagnosis or diag.description)
|
||
if len(spans) > 1:
|
||
spans_html = "".join(f'<div style="margin-top: 4px; padding-left: 8px; border-left: 2px solid #38bdf8;">• <span class="evidence-quote">{html.escape(s)}</span></div>' for s in spans)
|
||
sec_evidence_html = spans_html
|
||
elif spans:
|
||
sec_evidence_html = f'<span class="evidence-quote">"{html.escape(spans[0])}"</span>'
|
||
else:
|
||
sec_evidence_html = '<span class="evidence-quote">"None"</span>'
|
||
|
||
secondary_html = clean_html(
|
||
f"""
|
||
<div class="secondary-card">
|
||
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; flex-wrap: wrap;">
|
||
<div>
|
||
<strong style="font-size: 18px; color: #38bdf8;">#{idx}. {sec_code_display}</strong>
|
||
<span style="font-size: 15px; font-weight: 600; color: #f8fafc; margin-left: 10px;">{safe_desc}</span>
|
||
{documented_row}
|
||
</div>
|
||
<div style="display: flex; gap: 6px; align-items: center; flex-wrap: wrap;">
|
||
{billable_badge}
|
||
{nearest_badge}
|
||
<span class="pill-badge badge-secondary">{acuity_str}</span>
|
||
<span class="pill-badge badge-secondary">{certainty_str}</span>
|
||
{sec_codes_html}
|
||
</div>
|
||
</div>
|
||
<div class="evidence-box">
|
||
<span style="font-weight: 600; color: #6ee7b7; font-size: 11px; text-transform: uppercase;">
|
||
Clinical Evidence:
|
||
</span>
|
||
{sec_evidence_html}
|
||
</div>
|
||
</div>
|
||
"""
|
||
)
|
||
with st.container():
|
||
st.markdown(secondary_html, unsafe_allow_html=True)
|
||
|
||
|
||
def render_abstentions(abstentions: list[AbstentionRecord]) -> None:
|
||
"""Render explicit abstentions, negated conditions, and exclusions."""
|
||
if not abstentions:
|
||
return
|
||
|
||
# Deduplicate abstentions
|
||
from medical_coding.utils.text import normalize_diagnosis_name
|
||
deduped_abst: list[AbstentionRecord] = []
|
||
seen_abst: set[str] = set()
|
||
for abst in abstentions:
|
||
term = (abst.raw_term or "").strip()
|
||
if not term:
|
||
continue
|
||
norm_t = (normalize_diagnosis_name(term) or term).lower().strip()
|
||
if norm_t in seen_abst or term.lower() in seen_abst:
|
||
continue
|
||
seen_abst.add(norm_t)
|
||
seen_abst.add(term.lower())
|
||
deduped_abst.append(abst)
|
||
|
||
if not deduped_abst:
|
||
return
|
||
|
||
with st.expander(f"🚫 Audited Abstentions & Rule-Outs ({len(deduped_abst)})", expanded=True):
|
||
st.caption("Conditions detected in documentation that were strictly abstained or excluded under ICD-10 guidelines:")
|
||
for abst in deduped_abst:
|
||
reason_str = abst.reason.value if hasattr(abst.reason, "value") else str(abst.reason)
|
||
stage_str = abst.stage.value if hasattr(abst.stage, "value") else str(abst.stage)
|
||
raw_term = abst.raw_term or "Unspecified Entity"
|
||
|
||
if (
|
||
abst.reason == AbstentionReason.NO_MATCHING_ICD_CANDIDATE
|
||
or reason_str in ("NO_MATCHING_ICD_CANDIDATE", "NO_CANDIDATE_FOUND")
|
||
or "No local code" in (abst.detail or "")
|
||
or "lacked verified local code" in (abst.detail or "")
|
||
):
|
||
badge_html = '<span class="pill-badge badge-warning">No Local Code (Abstained)</span>'
|
||
else:
|
||
badge_html = f'<span class="pill-badge badge-danger">{reason_str}</span>'
|
||
|
||
st.markdown(
|
||
clean_html(
|
||
f"""
|
||
<div class="abstention-card">
|
||
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px;">
|
||
<strong style="color: #f87171; font-size: 14px;">Condition: {raw_term}</strong>
|
||
{badge_html}
|
||
</div>
|
||
<div style="font-size: 13px; color: #e2e8f0;">{abst.detail}</div>
|
||
<div style="font-size: 11px; color: #94a3b8; margin-top: 4px;">Enforced at stage: <code>{stage_str}</code></div>
|
||
</div>
|
||
"""
|
||
),
|
||
unsafe_allow_html=True,
|
||
)
|
||
|
||
|
||
def render_procedures(procedures: list[str]) -> None:
|
||
"""Render procedural and surgical interventions segregated from the diagnosis candidate inventory."""
|
||
if not procedures:
|
||
return
|
||
|
||
st.markdown(f"### 🩺 Procedural & Surgical Interventions ({len(procedures)})")
|
||
st.caption("Documented procedures segregated from diagnosis coding under official coding guidelines (UHDDS):")
|
||
|
||
proc_items_html = []
|
||
for idx, proc in enumerate(procedures, start=1):
|
||
cpt_badge = ""
|
||
display_proc = proc
|
||
cpt_m = re.search(r"\[CPT:\s*([A-Za-z0-9]+)\]", proc)
|
||
if cpt_m:
|
||
cpt_code = cpt_m.group(1)
|
||
display_proc = re.sub(r"\s*\[CPT:\s*[A-Za-z0-9]+\]", "", proc).strip()
|
||
cpt_badge = f'<span class="pill-badge badge-success" style="margin-left: 8px;">CPT: {html.escape(cpt_code)}</span>'
|
||
|
||
proc_items_html.append(
|
||
f"""
|
||
<div class="procedure-item" style="background: rgba(147, 51, 234, 0.08); border: 1px solid rgba(168, 85, 247, 0.3); border-radius: 8px; padding: 10px 14px; margin-bottom: 8px; display: flex; justify-content: space-between; align-items: center;">
|
||
<div>
|
||
<span style="font-weight: 700; color: #c084fc; margin-right: 8px;">#{idx}.</span>
|
||
<strong style="color: #f3e8ff; font-size: 14px;">{html.escape(display_proc)}</strong>
|
||
{cpt_badge}
|
||
</div>
|
||
<span class="pill-badge badge-info" style="border-color: rgba(168, 85, 247, 0.5); color: #d8b4fe;">Surgical / Interventional</span>
|
||
</div>
|
||
"""
|
||
)
|
||
|
||
st.markdown(
|
||
clean_html(
|
||
f"""
|
||
<div class="procedure-card" style="background: #141728; border: 1.5px solid rgba(168, 85, 247, 0.4); border-radius: 12px; padding: 16px; margin: 14px 0;">
|
||
{"".join(proc_items_html)}
|
||
<div style="font-size: 11px; color: #a855f7; margin-top: 6px;">
|
||
🔒 <strong>Coding Invariant:</strong> Procedural interventions are strictly routed to procedure data and blocked from polluting secondary diagnosis code inventories.
|
||
</div>
|
||
</div>
|
||
"""
|
||
),
|
||
unsafe_allow_html=True,
|
||
)
|
||
|
||
|
||
def render_oncology_context(oncology_context: dict[str, Any] | None) -> None:
|
||
"""Render structured oncology tumor biology, receptors, Ki-67, and treatment response."""
|
||
if not oncology_context:
|
||
return
|
||
|
||
st.markdown("### 🧬 Structured Oncology & Biomarker Context")
|
||
st.caption("Tumor biology, receptor panel, and treatment history extracted from oncology narrative:")
|
||
|
||
primary_site = oncology_context.get("primary_site") or "Unspecified"
|
||
laterality = oncology_context.get("laterality") or "Unspecified"
|
||
histology = oncology_context.get("histology") or "Not documented"
|
||
grade = oncology_context.get("grade") or "Not documented"
|
||
ki67 = oncology_context.get("ki67") or "Not documented"
|
||
receptors = oncology_context.get("receptors") or {}
|
||
metastatic_status = oncology_context.get("metastatic_status", False)
|
||
metastatic_sites = oncology_context.get("metastatic_sites", [])
|
||
prior_treatments = oncology_context.get("prior_treatments", [])
|
||
treatment_response = oncology_context.get("treatment_response") or "Not documented"
|
||
|
||
c1, c2, c3, c4 = st.columns(4)
|
||
with c1:
|
||
st.metric("Primary Site", f"{primary_site} ({laterality})")
|
||
with c2:
|
||
st.metric("Histology", histology)
|
||
with c3:
|
||
st.metric("Histologic Grade", grade)
|
||
with c4:
|
||
st.metric("Ki-67 Index", ki67)
|
||
|
||
# Receptor panel
|
||
rec_pills = []
|
||
if receptors:
|
||
for r_name, r_val in receptors.items():
|
||
r_color = "#34d399" if "pos" in str(r_val).lower() or "+" in str(r_val) else "#f87171"
|
||
rec_pills.append(
|
||
f'<span style="background: rgba(15, 23, 42, 0.8); border: 1px solid #334155; border-radius: 6px; padding: 4px 10px; margin-right: 8px; font-size: 13px;">'
|
||
f'<strong style="color: #94a3b8;">{html.escape(r_name.upper())}:</strong> <span style="color: {r_color}; font-weight: 600;">{html.escape(str(r_val))}</span>'
|
||
f'</span>'
|
||
)
|
||
receptors_html = "".join(rec_pills) if rec_pills else '<span style="color: #94a3b8; font-size: 13px;">No receptor panel documented</span>'
|
||
|
||
# Metastatic sites
|
||
if metastatic_status:
|
||
sites_str = ", ".join(metastatic_sites) if metastatic_sites else "Present (unspecified site)"
|
||
meta_badge = f'<span class="pill-badge badge-danger">Metastatic: {html.escape(sites_str)}</span>'
|
||
else:
|
||
meta_badge = '<span class="pill-badge badge-success">No Distant Metastases Documented</span>'
|
||
|
||
# Prior treatments & response
|
||
tx_str = ", ".join(prior_treatments) if prior_treatments else "None documented"
|
||
|
||
oncology_html = clean_html(
|
||
f"""
|
||
<div style="background: #0d221c; border: 1.5px solid rgba(16, 185, 129, 0.4); border-radius: 12px; padding: 16px; margin: 12px 0;">
|
||
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; border-bottom: 1px solid rgba(16, 185, 129, 0.2); padding-bottom: 8px;">
|
||
<strong style="color: #34d399; font-size: 15px;">🔬 Tumor Biology & Biomarker Profile</strong>
|
||
<div>{meta_badge}</div>
|
||
</div>
|
||
<div style="margin-bottom: 12px;">
|
||
<span style="font-size: 12px; color: #94a3b8; display: block; margin-bottom: 4px; text-transform: uppercase; font-weight: 600;">Receptor Panel & Biomarkers:</span>
|
||
<div style="display: flex; flex-wrap: wrap; gap: 6px;">{receptors_html}</div>
|
||
</div>
|
||
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 8px; font-size: 13px; color: #e2e8f0;">
|
||
<div>
|
||
<span style="color: #94a3b8;">Prior Treatment:</span> <strong style="color: #f1f5f9;">{html.escape(tx_str)}</strong>
|
||
</div>
|
||
<div>
|
||
<span style="color: #94a3b8;">Treatment Response:</span> <strong style="color: #6ee7b7;">{html.escape(treatment_response)}</strong>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
"""
|
||
)
|
||
st.markdown(oncology_html, unsafe_allow_html=True)
|
||
|
||
|
||
def render_operative_findings(operative_findings: list[dict[str, Any]]) -> None:
|
||
"""Render operative and intraoperative findings with UHDDS clinical-coding eligibility status."""
|
||
if not operative_findings:
|
||
return
|
||
|
||
st.markdown(f"### 🔍 Operative Findings & Incidental Observations ({len(operative_findings)})")
|
||
st.caption("Intraoperative observations evaluated under UHDDS criteria (incidental findings without dedicated surgical intervention or therapy are excluded from secondary diagnoses):")
|
||
|
||
rows_html = []
|
||
for f in operative_findings:
|
||
term = f.get("term", "Unknown Finding")
|
||
quote = f.get("verbatim_quote", "")
|
||
sec = f.get("source_section", "OPERATIVE_FINDINGS")
|
||
is_incidental = f.get("is_incidental", True)
|
||
|
||
eligibility_badge = (
|
||
'<span class="pill-badge badge-danger">Excluded from Billing (Incidental / Unmanaged)</span>'
|
||
if is_incidental
|
||
else '<span class="pill-badge badge-success">Eligible (Dedicated Surgical Intervention)</span>'
|
||
)
|
||
|
||
rows_html.append(
|
||
f"""
|
||
<div style="background: #111e33; border: 1px solid #233857; border-radius: 8px; padding: 12px 14px; margin-bottom: 8px;">
|
||
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px;">
|
||
<strong style="color: #60a5fa; font-size: 14px;">{html.escape(term)}</strong>
|
||
{eligibility_badge}
|
||
</div>
|
||
<div style="font-size: 12px; color: #94a3b8;">
|
||
Section: <code>{html.escape(sec)}</code>
|
||
</div>
|
||
<div style="font-size: 13px; color: #cbd5e1; margin-top: 4px; font-style: italic;">
|
||
"{html.escape(quote)}"
|
||
</div>
|
||
</div>
|
||
"""
|
||
)
|
||
|
||
st.markdown(
|
||
clean_html(
|
||
f"""
|
||
<div style="background: #091322; border: 1px solid #1e3a5f; border-radius: 12px; padding: 16px; margin: 12px 0;">
|
||
{"".join(rows_html)}
|
||
</div>
|
||
"""
|
||
),
|
||
unsafe_allow_html=True,
|
||
)
|
||
|
||
|
||
def render_validation_audit_trail(
|
||
primary: CodedDiagnosisResponse | None,
|
||
secondaries: list[CodedDiagnosisResponse],
|
||
validation_flags: dict[str, Any] | None = None,
|
||
) -> None:
|
||
"""Render deterministic non-LLM validation rule checks with evidence-based verification."""
|
||
with st.expander("🛡️ Evidence-Based Deterministic Invariant & Clinical Rule Audit", expanded=False):
|
||
st.caption("Authoritative validation verifies clinical meaning and anatomical congruence, not just database existence:")
|
||
|
||
st.markdown(
|
||
clean_html(
|
||
"""
|
||
<div class="audit-check-item">
|
||
<span class="check-pass">✔</span> <strong>Laterality Congruence Check:</strong> Verified that code laterality matches documented laterality (left/right/bilateral); contradictory codes rejected even if billable.
|
||
</div>
|
||
<div class="audit-check-item">
|
||
<span class="check-pass">✔</span> <strong>Anatomical Site & Subsite Match:</strong> Verified organ group and quadrant congruence (e.g. breast quadrant, femur vs bone); anatomical mismatches strictly rejected.
|
||
</div>
|
||
<div class="audit-check-item">
|
||
<span class="check-pass">✔</span> <strong>Procedure vs Diagnosis Segregation:</strong> Verified that surgical procedures (mastectomy, salpingo-oophorectomy, etc.) are excluded from secondary diagnosis candidate inventory.
|
||
</div>
|
||
<div class="audit-check-item">
|
||
<span class="check-pass">✔</span> <strong>UHDDS Operative Finding Gate:</strong> Verified that incidental intraoperative observations without dedicated surgical intervention or post-op therapy are excluded from billing.
|
||
</div>
|
||
<div class="audit-check-item">
|
||
<span class="check-pass">✔</span> <strong>Catalog Existence & Billable Specificity:</strong> Verified that all assigned codes exist in the authoritative dataset as terminal leaf codes.
|
||
</div>
|
||
<div class="audit-check-item">
|
||
<span class="check-pass">✔</span> <strong>Single-Primary Invariant:</strong> Maximum of exactly 1 primary admission diagnosis enforced.
|
||
</div>
|
||
<div class="audit-check-item">
|
||
<span class="check-pass">✔</span> <strong>Mutual Excludes1 Constraint:</strong> No mutually contradictory codes co-assigned for this encounter.
|
||
</div>
|
||
"""
|
||
),
|
||
unsafe_allow_html=True,
|
||
)
|
||
|
||
|
||
def render_pdf_page_inspector(pages: list[Any]) -> None:
|
||
"""Render an interactive page-by-page text explorer for ingested PDFs."""
|
||
if not pages:
|
||
return
|
||
|
||
def _get(obj: Any, key: str, default: Any = None) -> Any:
|
||
if isinstance(obj, dict):
|
||
return obj.get(key, default)
|
||
return getattr(obj, key, default)
|
||
|
||
page_numbers = [_get(p, "page_number", idx + 1) for idx, p in enumerate(pages)]
|
||
|
||
with st.expander(f"📄 Ingested PDF Document Viewer ({len(pages)} Pages)", expanded=False):
|
||
selected_page = st.selectbox(
|
||
"Select Page to Inspect:",
|
||
options=page_numbers,
|
||
format_func=lambda x: f"Page {x} of {len(pages)}",
|
||
)
|
||
page_obj = next((p for p in pages if _get(p, "page_number") == selected_page), pages[0])
|
||
|
||
norm_text = str(_get(page_obj, "normalized_text", "") or "")
|
||
char_count = _get(page_obj, "char_count", len(norm_text))
|
||
word_count = _get(page_obj, "word_count", len(norm_text.split()))
|
||
has_images = _get(page_obj, "has_images", False)
|
||
|
||
c1, c2, c3 = st.columns(3)
|
||
c1.metric("Character Count", char_count)
|
||
c2.metric("Word Count", word_count)
|
||
c3.metric("Embedded Images", "Yes" if has_images else "No")
|
||
|
||
escaped_text = html.escape(norm_text)
|
||
st.markdown(
|
||
f"""
|
||
<div class="pdf-page-card">
|
||
<pre style="white-space: pre-wrap; margin: 0; font-size: 12px; color: #e2e8f0;">{escaped_text}</pre>
|
||
</div>
|
||
""",
|
||
unsafe_allow_html=True,
|
||
)
|
||
|
||
|
||
|
||
NODE_TITLES: dict[str, str] = {
|
||
"validate_document": "Document Validation",
|
||
"extract_text": "Multimodal Normalization",
|
||
"extract_diagnoses": "Clinical Extraction Agent",
|
||
"analyze_context": "Clinical Context Analysis",
|
||
"classify_diagnoses": "Classification Agent",
|
||
"retrieve_candidates": "Multi-System Retrieval",
|
||
"rank_candidates": "Candidate Ranking Agent",
|
||
"validate_codes": "Deterministic Validation",
|
||
"evaluate_confidence": "Confidence & Abstention",
|
||
"finalize_output": "Final Output Generation",
|
||
}
|
||
|
||
|
||
def render_process_oversight_timeline(steps: list[dict[str, Any]], total_duration_ms: float = 0.0) -> None:
|
||
"""Render a comprehensive process oversight timeline (suppressed per user specification)."""
|
||
return
|
||
|
||
|
||
def render_failure_alert(
|
||
failed_step: str | None,
|
||
error_message: str | None,
|
||
error_traceback: str | None = None,
|
||
duration_ms: float = 0.0,
|
||
) -> None:
|
||
"""Render a prominent diagnostic banner indicating the exact node failure and stack trace."""
|
||
step_key = failed_step or "unknown_step"
|
||
title = NODE_TITLES.get(step_key, step_key)
|
||
err_text = error_message or "Unknown exception during workflow execution"
|
||
|
||
st.markdown(
|
||
f"""
|
||
<div class="failure-card">
|
||
<div class="failure-header">
|
||
<span>🚨</span>
|
||
<span>Pipeline Execution Halted: Node Failure Encountered</span>
|
||
</div>
|
||
<div style="margin: 8px 0 12px 0;">
|
||
<span style="color: #cbd5e1; font-size: 13px;">Exact Failing Stage:</span>
|
||
<span class="failure-step-badge" style="margin-left: 6px;">{step_key}</span>
|
||
<span style="font-weight: 600; color: #ffffff; margin-left: 8px;">({title})</span>
|
||
</div>
|
||
<div style="background: rgba(0, 0, 0, 0.3); border-left: 3px solid #ef4444; border-radius: 4px; padding: 10px 14px; margin-bottom: 8px;">
|
||
<span style="font-size: 12px; font-weight: 600; color: #fca5a5;">ERROR DIAGNOSTIC:</span><br/>
|
||
<code style="color: #fee2e2; font-size: 13px;">{err_text}</code>
|
||
</div>
|
||
<div style="font-size: 11px; color: #94a3b8;">
|
||
Prior completed stages preserved in audit database.
|
||
</div>
|
||
</div>
|
||
""",
|
||
unsafe_allow_html=True,
|
||
)
|
||
|
||
if error_traceback:
|
||
with st.expander("🛠️ View Detailed Exception Stack Trace", expanded=False):
|
||
st.code(error_traceback, language="python")
|
||
|