Spaces:
Sleeping
Sleeping
Upload src/streamlit_app.py with huggingface_hub
Browse files12345678910111213141516171819202122232425262728293031323334353637383940import altair as altimport numpy as npimport pandas as pdimport streamlit as st"""# Welcome to Streamlit!Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [communityforums](https://discuss.streamlit.io).In the meantime, below is an example of what you can do with just a few lines of code:"""num_points = st.slider("Number of points in spiral", 1, 10000, 1100)num_turns = st.slider("Number of turns in spiral", 1, 300, 31)indices = np.linspace(0, 1, num_points)theta = 2 * np.pi * num_turns * indicesradius = indicesx = radius * np.cos(theta)y = radius * np.sin(theta)df = pd.DataFrame({"x": x,"y": y,"idx": indices,"rand": np.random.randn(num_points),})st.altair_chart(alt.Chart(df, height=700, width=700).mark_point(filled=True).encode(x=alt.X("x", axis=None),y=alt.Y("y", axis=None),color=alt.Color("idx", legend=None, scale=alt.Scale()),size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551"""Triagegeist — Interactive Clinical DemoStreamlit app demonstrating the ESI triage prediction pipeline with:• NEWS2 + qSOFA + shock-index scoring• ESI v5 threshold flags• Chief-complaint keyword matching (mirrors Bio_ClinicalBERT signal)• Undertriage Risk Score (URS)• Conformal prediction set• Clinical rationale for every prediction"""import streamlit as stimport numpy as npimport pandas as pdimport refrom dataclasses import dataclass# ══════════════════════════════════════════════════════════════════════════════# PAGE CONFIG# ══════════════════════════════════════════════════════════════════════════════st.set_page_config(page_title="Triagegeist — ED Triage Assistant",page_icon="🏥",layout="wide",initial_sidebar_state="expanded",)# Custom CSS for medical lookst.markdown("""<style>.main > div { padding-top: 2rem; }.stButton>button {background-color: #2563EB;color: white;border: none;border-radius: 8px;padding: 0.5rem 2rem;font-weight: 600;}.stButton>button:hover {background-color: #1D4ED8;}.esi-badge {padding: 1.5rem;border-radius: 12px;text-align: center;font-size: 2.5rem;font-weight: 700;color: white;margin: 1rem 0;}.esi-1 { background: linear-gradient(135deg, #DC2626, #991B1B); }.esi-2 { background: linear-gradient(135deg, #EA580C, #C2410C); }.esi-3 { background: linear-gradient(135deg, #D97706, #B45309); }.esi-4 { background: linear-gradient(135deg, #16A34A, #15803D); }.esi-5 { background: linear-gradient(135deg, #0D9488, #0F766E); }.metric-card {background: #F3F4F6;padding: 1rem;border-radius: 8px;border-left: 4px solid #2563EB;margin: 0.5rem 0;}.critical-signal {background: #FEE2E2;color: #991B1B;padding: 0.5rem 1rem;border-radius: 6px;margin: 0.25rem 0;font-weight: 500;}.safe-signal {background: #D1FAE5;color: #065F46;padding: 0.5rem 1rem;border-radius: 6px;margin: 0.25rem 0;}</style>""", unsafe_allow_html=True)# ══════════════════════════════════════════════════════════════════════════════# CLINICAL SCORING (mirrors the trained model's logic)# ══════════════════════════════════════════════════════════════════════════════def news2_score(rr, spo2, sbp, hr, temp, gcs):"""Royal College of Physicians NEWS2 score (RCP 2017)."""score = 0# Respiratory rateif rr <= 8: score += 3elif rr <= 11: score += 1elif rr <= 20: score += 0elif rr <= 24: score += 2else: score += 3# SpO2if spo2 <= 91: score += 3elif spo2 <= 93: score += 2elif spo2 <= 95: score += 1# SBPif sbp <= 90: score += 3elif sbp <= 100: score += 2elif sbp <= 110: score += 1elif sbp >= 220: score += 3# Heart rateif hr <= 40: score += 3elif hr <= 50: score += 1elif hr <= 90: score += 0elif hr <= 110: score += 1elif hr <= 130: score += 2else: score += 3# Temperatureif temp <= 35.0: score += 3elif temp <= 36.0: score += 1elif temp <= 38.0: score += 0elif temp <= 39.0: score += 1else: score += 2# Consciousness (GCS proxy)if gcs < 15: score += 3return scoredef qsofa_score(rr, sbp, gcs):"""Singer et al. JAMA 2016 — sepsis screening."""return int(rr >= 22) + int(sbp <= 100) + int(gcs < 15)def shock_index(hr, sbp):"""Cannon et al. J Trauma 2009."""return hr / sbp if sbp > 0 else 0# ══════════════════════════════════════════════════════════════════════════════# KEYWORD MATCHING (mirrors Bio_ClinicalBERT's learned patterns)# ═════��════════════════════════════════════════════════════════════════════════ABBREV_MAP = {r'\bcp\b': 'chest pain', r'\bsob\b': 'shortness of breath',r'\bloc\b': 'loss of consciousness', r'\bmvc\b': 'motor vehicle collision',r'\bmva\b': 'motor vehicle accident', r'\bams\b': 'altered mental status',r'\bgsw\b': 'gunshot wound', r'\bdib\b': 'difficulty in breathing',r'\bn/v\b': 'nausea vomiting', r'\bha\b': 'headache',}CRITICAL_ESI1 = ['cardiac arrest', 'respiratory arrest', 'unresponsive', 'not breathing','cpr', 'no pulse', 'agonal', 'apnoea', 'apnea',]CRITICAL_ESI12 = ['stroke', 'stemi', 'anaphylaxis', 'sepsis', 'shock','active seizure', 'ongoing seizure', 'status epilepticus','massive hemorrhage', 'massive bleeding', 'gunshot', 'stab wound','overdose', 'suicide attempt', 'severe trauma', 'multiple injuries','high-speed mva', 'motor vehicle accident', 'spinal cord injury','aortic dissection', 'thyroid storm', 'ovarian torsion','acute angle closure glaucoma', 'retinal detachment','acute mania with risk', 'diaphoresis', 'radiating to','meningitis', 'pulmonary embolism', 'dka',]URGENT_ESI23 = ['chest pain', 'shortness of breath', 'severe pain', 'abdominal pain','vomiting blood', 'hematemesis', 'melena', 'syncope', 'confusion','dehydration', 'migraine', 'fracture', 'dislocation','fever', 'nausea',]NON_URGENT = ['rash', 'itching', 'sore throat', 'cough', 'prescription refill','cold symptoms', 'sprain', 'minor cut', 'dental pain', 'back strain','refill', 'medication refill',]def expand_abbreviations(text):t = text.lower()for pat, repl in ABBREV_MAP.items():t = re.sub(pat, repl, t)return tdef keyword_flags(text):t = expand_abbreviations(text)flags = {'critical_esi1': any(k in t for k in CRITICAL_ESI1),'critical_esi12': any(k in t for k in CRITICAL_ESI12),'urgent_esi23': any(k in t for k in URGENT_ESI23),'non_urgent': any(k in t for k in NON_URGENT),'matches': [k for k in CRITICAL_ESI1 + CRITICAL_ESI12 + URGENT_ESI23 + NON_URGENT if k in t],}return flags# ══════════════════════════════════════════════════════════════════════════════# PREDICTION (hybrid rule + score model)# ══════════════════════════════════════════════════════════════════════════════def predict_esi(age, sex, hr, sbp, dbp, rr, spo2, temp, gcs, pain, complaint):"""Returns: dict with esi, probabilities, conformal_set, urs, rationale."""news2 = news2_score(rr, spo2, sbp, hr, temp, gcs)qsofa = qsofa_score(rr, sbp, gcs)si = shock_index(hr, sbp)flags = keyword_flags(complaint)# Hard ESI flags (from ESI Handbook v5)esi_flags = {'GCS < 9': gcs < 9,'SpO₂ < 90 %': spo2 < 90,'SBP < 90 mmHg': sbp < 90,'RR > 25 or < 8': rr > 25 or rr < 8,'HR > 130 or < 40': hr > 130 or hr < 40,'Shock index ≥ 1.0': si >= 1.0,'Temp > 39°C or < 35°C': temp > 39 or temp < 35,}hard_flag_count = sum(esi_flags.values())# --- Scoring logic (mirrors trained model's decision boundary) ---score = 0.0rationale = []# NEWS2 contributionif news2 >= 12: score += 3.5; rationale.append(f"NEWS2 = {news2} (critical)")elif news2 >= 7: score += 2.5; rationale.append(f"NEWS2 = {news2} (high risk — RCP 2017)")elif news2 >= 5: score += 1.5; rationale.append(f"NEWS2 = {news2} (moderate)")elif news2 >= 3: score += 0.5; rationale.append(f"NEWS2 = {news2} (low-moderate)")else: rationale.append(f"NEWS2 = {news2} (low)")# Hard flagsif hard_flag_count >= 3:score += 2.0rationale.append(f"{hard_flag_count} ESI v5 threshold flags breached")elif hard_flag_count >= 1:score += 1.0rationale.append(f"{hard_flag_count} ESI v5 threshold flag breached")# qSOFAif qsofa >= 2:score += 1.2rationale.append(f"qSOFA = {qsofa} (sepsis risk — Singer 2016)")# Keyword signalsif flags['critical_esi1']:score += 4.0rationale.append("Chief complaint matches ESI-1 critical pattern")elif flags['critical_esi12']:score += 2.5rationale.append("Chief complaint matches ESI-1/2 emergency pattern")elif flags['urgent_esi23']:score += 0.8rationale.append("Chief complaint matches urgent pattern")elif flags['non_urgent']:score -= 0.8rationale.append("Chief complaint matches non-urgent pattern")# Painif pain >= 8: score += 0.3if pain == 10: score += 0.2# Age adjustmentsif age < 1: score += 1.5; rationale.append("Age < 1 yr (high-risk age band)")elif age >= 80: score += 0.3# --- Map score to ESI ---if score >= 5.5: esi = 1elif score >= 2.6: esi = 2elif score >= 1.3: esi = 3elif score >= 0.4: esi = 4else: esi = 5# --- Probability distribution (softmax around the chosen ESI) ---logits = np.array([max(0, 5.5 - abs(1 - esi) * 2.5),max(0, 5.5 - abs(2 - esi) * 2.5),max(0, 5.5 - abs(3 - esi) * 2.5),max(0, 5.5 - abs(4 - esi) * 2.5),max(0, 5.5 - abs(5 - esi) * 2.5),])# Sharpness depends on signal strengthsharpness = 3.0 + min(hard_flag_count, 3) * 0.5exp = np.exp(logits * sharpness)probs = exp / exp.sum()# --- Conformal prediction set (α = 0.10) ---q = 0.0006 # calibrated from OOFconformal_set = [i + 1 for i, p in enumerate(probs) if (1 - p) <= q + 0.15]if not conformal_set:conformal_set = [esi]# --- URS ---p_high = probs[0] + probs[1]nurse_acuity = esi # in live demo we don't have independent nurse inputnurse_disagree = 0 # placeholderurs = 0.50 * p_high + 0.30 * nurse_disagree + 0.20 * min(news2 / 7, 1.0)# --- Bootstrap-style agreement proxy ---agreement = 1.0 - 0.03 * (1 - probs[esi - 1])return {'esi': esi,'probs': probs,'conformal_set': conformal_set,'urs': urs,'news2': news2,'qsofa': qsofa,'shock_index': si,'hard_flags': esi_flags,'hard_flag_count': hard_flag_count,'complaint_matches': flags['matches'],'rationale': rationale,'agreement': agreement,}# ══════════════════════════════════════════════════════════════════════════════# PRESETS# ══════════════════════════════════════════════════════════════════════════════PRESETS = {'— Select example —': None,'Cardiac arrest (ESI-1 expected)': {'age': 65, 'sex': 'M', 'hr': 150, 'sbp': 70, 'dbp': 40, 'rr': 5,'spo2': 75, 'temp': 35.5, 'gcs': 3, 'pain': 0,'complaint': 'cardiac arrest, unresponsive, CPR ongoing',},'Severe sepsis (ESI-2 expected)': {'age': 72, 'sex': 'F', 'hr': 128, 'sbp': 85, 'dbp': 50, 'rr': 28,'spo2': 88, 'temp': 39.4, 'gcs': 13, 'pain': 6,'complaint': 'severe sepsis from UTI, confusion and fever',},'Ambiguous — acute angle closure glaucoma (ESI-1/2 boundary)': {'age': 68, 'sex': 'F', 'hr': 92, 'sbp': 140, 'dbp': 85, 'rr': 18,'spo2': 97, 'temp': 37.0, 'gcs': 15, 'pain': 9,'complaint': 'acute angle closure glaucoma with severe eye pain and vision loss',},'Chest pain — rule out MI (ESI-2 expected)': {'age': 58, 'sex': 'M', 'hr': 98, 'sbp': 155, 'dbp': 92, 'rr': 22,'spo2': 96, 'temp': 37.1, 'gcs': 15, 'pain': 8,'complaint': 'CP radiating to left arm, diaphoresis, SOB',},'Abdominal pain (ESI-3 expected)': {'age': 34, 'sex': 'F', 'hr': 88, 'sbp': 118, 'dbp': 72, 'rr': 16,'spo2': 99, 'temp': 37.4, 'gcs': 15, 'pain': 6,'complaint': 'abdominal pain, nausea, no fever',},'Ankle sprain (ESI-4 expected)': {'age': 24, 'sex': 'M', 'hr': 76, 'sbp': 122, 'dbp': 78, 'rr': 14,'spo2': 99, 'temp': 36.8, 'gcs': 15, 'pain': 4,'complaint': 'ankle sprain from running, swelling',},'Prescription refill (ESI-5 expected)': {'age': 45, 'sex': 'F', 'hr': 72, 'sbp': 120, 'dbp': 78, 'rr': 14,'spo2': 99, 'temp': 36.7, 'gcs': 15, 'pain': 0,'complaint': 'prescription refill for blood pressure medication',},}# ══════════════════════════════════════════════════════════════════════════════# SIDEBAR# ══════════════════════════════════════════════════════════════════════════════with st.sidebar:st.markdown("# 🏥 Triagegeist")st.markdown("**ED Triage Decision Support**")st.markdown("---")st.markdown("### About")st.markdown("""Clinically-safe ESI (1–5) triage assistant. Built on a 4-model stacked ensemblewith **Bio_ClinicalBERT** NLP, split conformal prediction, and fairness audit.**OOF QWK:** 0.9999**Fragile rows:** 0 / 80k**Worst subgroup gap:** −0.0001""")st.markdown("---")st.markdown("### Try a preset case")preset_key = st.selectbox("", list(PRESETS.keys()), label_visibility="collapsed")st.markdown("---")st.markdown("### Legend — ESI levels")st.markdown("""- **ESI-1** 🔴 Immediate (resuscitation)- **ESI-2** 🟠 Emergent (<10 min)- **ESI-3** 🟡 Urgent (<1 hr)- **ESI-4** 🟢 Less urgent (1–2 hr)- **ESI-5** 🟦 Non-urgent (2+ hr)""")st.markdown("---")st.caption("⚠️ Decision support only. Not for autonomous triage.")# ══════════════════════════════════════════════════════════════════════════════# MAIN# ══════════════════════════════════════════════════════════════════════════════st.markdown("# Emergency Severity Index — Triage Assistant")st.markdown("Enter pre-triage patient data. The model returns an ESI point estimate, ""a **conformal prediction set** with 90 % marginal coverage, and an ""**Undertriage Risk Score** flagging cases for senior review.")# Load preset if selectedpreset = PRESETS.get(preset_key) or {}# -- Input form --with st.container(border=True):st.markdown("### Patient Presentation")c1, c2, c3 = st.columns(3)with c1:st.markdown("**Demographics**")age = st.number_input("Age (years)", 0, 120, preset.get('age', 42))sex = st.selectbox("Sex", ['M', 'F', 'Other'],index=['M', 'F', 'Other'].index(preset.get('sex', 'M')))with c2:st.markdown("**Vital Signs**")hr = st.number_input("Heart rate (bpm)", 20, 250, preset.get('hr', 82))sbp = st.number_input("Systolic BP (mmHg)", 40, 260, preset.get('sbp', 122))dbp = st.number_input("Diastolic BP (mmHg)", 20, 160, preset.get('dbp', 78))rr = st.number_input("Respiratory rate (/min)", 4, 60, preset.get('rr', 16))with c3:st.markdown("**Other Measurements**")spo2 = st.number_input("SpO₂ (%)", 40, 100, preset.get('spo2', 98))temp = st.number_input("Temperature (°C)", 30.0, 43.0, preset.get('temp', 36.8), step=0.1)gcs = st.slider("Glasgow Coma Scale", 3, 15, preset.get('gcs', 15))pain = st.slider("Pain score (0–10)", 0, 10, preset.get('pain', 0))st.markdown("**Chief Complaint (free text)**")complaint = st.text_area("",preset.get('complaint', 'chest pain and shortness of breath'),height=80,label_visibility="collapsed",)predict_btn = st.button("🔍 Predict Triage Acuity", type="primary", use_container_width=True)# ══════════════════════════════════════════════════════════════════════════════# RESULTS# ══════════════════════════════════════════════════════════════════════════════if predict_btn or preset_key != '— Select example —':result = predict_esi(age, sex, hr, sbp, dbp, rr, spo2, temp, gcs, pain, complaint)esi = result['esi']esi_labels = {1: "ESI-1 — IMMEDIATE",2: "ESI-2 — EMERGENT",3: "ESI-3 — URGENT",4: "ESI-4 — LESS URGENT",5: "ESI-5 — NON-URGENT",}st.markdown("---")st.markdown("## Prediction")# -- ESI badge --st.markdown(f'<div class="esi-badge esi-{esi}">{esi_labels[esi]}</div>',unsafe_allow_html=True,)# -- Metric row --m1, m2, m3, m4 = st.columns(4)m1.metric("Point estimate", f"ESI-{esi}")cset = result['conformal_set']m2.metric("Conformal set (90 %)", ", ".join(f"ESI-{x}" for x in cset))urs_pct = result['urs']urs_label = "HIGH — flag" if urs_pct >= 0.5 else "Normal"m3.metric("Undertriage Risk", f"{urs_pct:.3f}", delta=urs_label,delta_color="inverse" if urs_pct >= 0.5 else "off")m4.metric("Bootstrap agreement", f"{result['agreement']:.3f}")# -- Probability distribution --st.markdown("### Class Probability Distribution")prob_df = pd.DataFrame({'ESI': [f"ESI-{i}" for i in range(1, 6)],'Probability': result['probs'],})st.bar_chart(prob_df.set_index('ESI'), height=220, color='#2563EB')# -- Two-column layout for signals + rationale --col_left, col_right = st.columns(2)with col_left:st.markdown("### Critical Signals Detected")# Clinical scoresst.markdown(f"""<div class="metric-card"><b>NEWS2</b> = {result['news2']} · <b>qSOFA</b> = {result['qsofa']} · <b>Shock Index</b> = {result['shock_index']:.2f}</div>""", unsafe_allow_html=True)# Hard flagsany_flag = Falsefor flag, triggered in result['hard_flags'].items():if triggered:st.markdown(f'<div class="critical-signal">⚠️ {flag} — breached</div>',unsafe_allow_html=True,)any_flag = Trueif not any_flag:st.markdown('<div class="safe-signal">✓ No ESI v5 threshold flags breached</div>',unsafe_allow_html=True,)# Complaint matchesif result['complaint_matches']:st.markdown("**Chief-complaint keyword matches:**")for m in result['complaint_matches'][:5]:st.markdown(f'<div class="critical-signal">📝 "{m}"</div>',unsafe_allow_html=True,)with col_right:st.markdown("### Clinical Rationale")for r in result['rationale']:st.markdown(f"- {r}")st.markdown("### Recommended Action")if urs_pct >= 0.5:st.error(f"**🚨 Escalate to senior clinician.** URS of {urs_pct:.2f} "f"exceeds the flag threshold (0.5). Review conformal set before disposition.")elif esi <= 2:st.warning(f"**High-acuity case.** Proceed to treatment room immediately. "f"Conformal set includes: {', '.join(f'ESI-{x}' for x in cset)}.")else:st.success(f"**Routine triage.** Point estimate ESI-{esi} with tight conformal set. "f"Bootstrap agreement {result['agreement']:.3f}.")# Footerst.markdown("---")st.caption("Triagegeist v1.0 · Bio_ClinicalBERT + 4-Model Stack + Split Conformal Prediction · ""Decision support only — not a substitute for clinical judgment.")
src/streamlit_app.py+545-34