"""
Internal Medicine Discharge Letter Error-Check — Streamlit App
Prospective study: AI-assisted error detection in ED discharge letters
"""
import streamlit as st
import time
import json
import os
import tempfile
import threading
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from pathlib import Path
from backend import (
ModelResult,
build_medication_lookup_context,
translate_to_english,
call_model_a,
call_model_b,
)
FEEDBACK_FILE = Path(__file__).parent / "feedback_data.json"
HF_DATASET_REPO = "Vrda/im-error-check-data"
HF_DATASET_FILE = "feedback_data.json"
@st.cache_resource
def get_deepseek_job_manager():
return {
"executor": ThreadPoolExecutor(max_workers=2),
"jobs": {},
"lock": threading.Lock(),
}
def cleanup_deepseek_jobs(max_age_seconds: int = 1800):
manager = get_deepseek_job_manager()
now = time.time()
stale_job_ids = []
with manager["lock"]:
for job_id, job in manager["jobs"].items():
if now - job["created_at"] > max_age_seconds:
stale_job_ids.append(job_id)
for job_id in stale_job_ids:
manager["jobs"].pop(job_id, None)
def submit_deepseek_job(
job_id: str,
english_text: str,
original_text: str,
medication_lookup_context: str,
):
manager = get_deepseek_job_manager()
future = manager["executor"].submit(
call_model_a,
english_text,
original_text,
medication_lookup_context,
)
with manager["lock"]:
manager["jobs"][job_id] = {
"future": future,
"created_at": time.time(),
}
def get_deepseek_job_info(job_id: str):
if not job_id:
return None
manager = get_deepseek_job_manager()
with manager["lock"]:
job = manager["jobs"].get(job_id)
if not job:
return None
return {
"created_at": job["created_at"],
"done": job["future"].done(),
}
def consume_deepseek_job_result(job_id: str) -> ModelResult | None:
if not job_id:
return None
manager = get_deepseek_job_manager()
with manager["lock"]:
job = manager["jobs"].get(job_id)
if not job:
return None
future = job["future"]
if not future.done():
return None
try:
result = future.result()
except Exception as exc:
result = ModelResult(
model_name="DeepSeek Reasoner",
raw_response="",
success=False,
error_message=f"Background job failed: {exc}",
latency_seconds=0.0,
)
with manager["lock"]:
manager["jobs"].pop(job_id, None)
return result
# -------------------------------------------------------------------------
# Feedback persistence (local + HF Hub sync)
# -------------------------------------------------------------------------
def _sync_from_hub() -> list:
"""Pull the latest feedback_data.json from HF Hub if it exists."""
try:
from huggingface_hub import HfApi
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
if not token:
return []
api = HfApi(token=token)
local_path = api.hf_hub_download(
repo_id=HF_DATASET_REPO,
filename=HF_DATASET_FILE,
repo_type="dataset",
)
with open(local_path, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return []
def _sync_to_hub(data: list):
"""Push feedback_data.json to the HF dataset repo."""
try:
from huggingface_hub import HfApi
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
if not token:
return
api = HfApi(token=token)
with tempfile.NamedTemporaryFile(
mode="w", suffix=".json", delete=False, encoding="utf-8"
) as tmp:
json.dump(data, tmp, ensure_ascii=False, indent=2)
tmp_path = tmp.name
api.upload_file(
path_or_fileobj=tmp_path,
path_in_repo=HF_DATASET_FILE,
repo_id=HF_DATASET_REPO,
repo_type="dataset",
commit_message=f"feedback entry #{len(data)}",
)
os.unlink(tmp_path)
except Exception as e:
st.toast(f"Hub sync warning: {e}", icon="\u26A0\uFE0F")
def save_feedback(entry: dict) -> int:
hub_data = _sync_from_hub()
if FEEDBACK_FILE.exists():
with open(FEEDBACK_FILE, "r", encoding="utf-8") as f:
local_data = json.load(f)
else:
local_data = []
if len(hub_data) > len(local_data):
data = hub_data
else:
data = local_data
data.append(entry)
with open(FEEDBACK_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
_sync_to_hub(data)
return len(data)
# -------------------------------------------------------------------------
# Page config & CSS
# -------------------------------------------------------------------------
st.set_page_config(
page_title="IM Error-Check",
page_icon="\U0001FA7A",
layout="wide",
)
st.markdown("""
""", unsafe_allow_html=True)
SAMPLE = """Adresa: VUKOVARSKA 45, SPLIT
Datum dolaska: 10.03.2026. 14:22
Datum rođenja: 15.05.1958.
Datum otpusta: 10.03.2026. 18:45
Trijažna kategorija: 3
Dijagnoze
I21.0 Akutni transmuralni infarkt miokarda prednje stijenke
Podaci s trijaže
Trijaž.kat:3; Puls:92/min; RR:155/95 mmHg; SpO2:94%; Tax: 36.8C; GCS:15;
Razlog dolaska
Bolovi u prsištu od jutros, stezajućeg karaktera s propagacijom u lijevu ruku. Trajanje > 30 min. Uzeo 2x NTG sprej bez učinka.
Anamneza
Osobna: arterijska hipertenzija, DM tip 2, dislipidemija. Terapija: Ramipril 5mg, Metformin 1000mg 2x1, Atorvastatin 20mg.
Status
Pri svijesti, blijed, znojav. Auskultatorno: srčana akcija ritmična, tonovi tiši, bez šumova. Pluća: bazalno obostrano oslabljen šum disanja.
Laboratorij
Troponin I: 2.8 ng/mL (ref <0.04), CK-MB: 45 U/L, L: 12.3, CRP: 8.5
Na: 138, K: 4.2, Kreatinin: 128 umol/L (eGFR 52), GUK: 14.2 mmol/L
EKG: ST elevacija V1-V4, recipročne promjene II, III, aVF
Terapija
Aspirin 300mg stat, zatim 100mg 1x1
Klopidogrel 300mg stat, zatim 75mg 1x1
Heparin 5000 IU i.v. bolus
Morphin 4mg i.v.
Metformin 1000mg nastaviti 2x1
Atorvastatin 40mg 1x1
Zaključak
Pacijent s akutnim STEMI prednje stijenke. Transportiran u Kath lab.
Preporučen kontrolni pregled za 14 dana."""
# -------------------------------------------------------------------------
# Session state
# -------------------------------------------------------------------------
for key, default in [
("input_text", ""),
("translated_text", None),
("medication_lookup_context", ""),
("model_a_result", None),
("model_b_result", None),
("translation_latency", 0),
("total_elapsed", 0),
("analysis_started_at", 0.0),
("deepseek_job_id", None),
("run_analysis", False),
("physician_id", ""),
]:
if key not in st.session_state:
st.session_state[key] = default
if "session_key" not in st.session_state:
import uuid
st.session_state.session_key = str(uuid.uuid4())
cleanup_deepseek_jobs()
def poll_deepseek_job():
job_id = st.session_state.deepseek_job_id
if not job_id or st.session_state.model_a_result is not None:
return
result = consume_deepseek_job_result(job_id)
if result is None:
return
st.session_state.model_a_result = result
st.session_state.deepseek_job_id = None
st.session_state.total_elapsed = round(
time.time() - st.session_state.analysis_started_at, 2
)
def load_sample():
st.session_state.input_text = SAMPLE
def trigger_analysis():
st.session_state.run_analysis = True
# -------------------------------------------------------------------------
# Header
# -------------------------------------------------------------------------
st.title("\U0001FA7A Internal Medicine — Discharge Letter Error-Check")
st.markdown("*AI-assisted error detection for Internal Medicine Emergency Department*")
st.warning(
"\u26A0\uFE0F **RESEARCH TOOL**: AI-generated findings require physician verification. "
"Do not use as sole basis for clinical decisions."
)
# Sidebar
with st.sidebar:
st.header("About")
st.markdown(
"Compares **DeepSeek Reasoner** and **GPT-OSS-120B** for detecting errors "
"in discharge letters."
)
st.markdown("---")
st.markdown("**Steps:** Paste letter \u2192 Analyze \u2192 Review \u2192 Rate")
st.markdown(
"Independent second-pass grading for two other physicians is on the "
"**Independent Grading** page in the sidebar."
)
st.markdown("---")
st.text_input(
"Physician ID (anonymous):",
placeholder="e.g. Physician A",
key="physician_id",
)
if FEEDBACK_FILE.exists():
with open(FEEDBACK_FILE, "r", encoding="utf-8") as f:
count = len(json.load(f))
st.metric("Cases collected", count)
# -------------------------------------------------------------------------
# Input
# -------------------------------------------------------------------------
st.header("Discharge Letter Input")
st.button("Load Sample Case", on_click=load_sample)
st.text_area(
"Paste discharge letter (Croatian):",
height=220,
placeholder="Zalijepite otpusno pismo ovdje...",
key="input_text",
)
st.button("Analyze", type="primary", on_click=trigger_analysis)
# -------------------------------------------------------------------------
# Run analysis (progressive: show GPT-OSS first, DeepSeek when ready)
# -------------------------------------------------------------------------
if st.session_state.run_analysis and st.session_state.input_text.strip():
st.session_state.run_analysis = False
st.session_state.model_a_result = None
st.session_state.model_b_result = None
st.session_state.total_elapsed = 0
st.session_state.analysis_started_at = time.time()
st.session_state.deepseek_job_id = None
with st.spinner("Translating discharge letter..."):
t0 = time.time()
st.session_state.translated_text = translate_to_english(st.session_state.input_text)
st.session_state.translation_latency = round(time.time() - t0, 2)
english = st.session_state.translated_text
st.session_state.medication_lookup_context = build_medication_lookup_context(
st.session_state.input_text
)
job_id = f"{st.session_state.session_key}:{int(time.time() * 1000)}"
submit_deepseek_job(
job_id,
english,
st.session_state.input_text,
st.session_state.medication_lookup_context,
)
st.session_state.deepseek_job_id = job_id
with st.spinner("GPT-OSS-120B responding (~5s)..."):
st.session_state.model_b_result = call_model_b(
english,
st.session_state.input_text,
st.session_state.medication_lookup_context,
)
st.rerun()
poll_deepseek_job()
# -------------------------------------------------------------------------
# Helper: render a model's output
# -------------------------------------------------------------------------
SEVERITY_LABELS = {
"critical": "\U0001F534 Critical",
"high": "\U0001F7E0 High",
"medium": "\U0001F7E1 Medium",
"low": "\U0001F7E2 Low",
}
CATEGORY_LABELS = {
"medication_error": "Medication",
"diagnostic_error": "Diagnostic",
"dosing_error": "Dosing",
"documentation_error": "Documentation",
"lab_interpretation_error": "Lab Interpretation",
"contraindication": "Contraindication",
"omission": "Omission",
"disposition_safety": "Disposition Safety",
"other": "Other",
"documentation_quality": "Documentation Quality",
"clinical_workflow": "Clinical Workflow",
"patient_safety": "Patient Safety",
"continuity_of_care": "Continuity of Care",
"patient_education": "Patient Education",
"followup_quality": "Follow-up Quality",
"medication_safety": "Medication Safety",
"completeness": "Completeness",
}
PRIORITY_LABELS = {
"high": "\U0001F534 High",
"medium": "\U0001F7E1 Medium",
"low": "\U0001F7E2 Low",
}
SAFETY_RISK_LABELS = {
"low": "\U0001F7E2 Low",
"moderate": "\U0001F7E1 Moderate",
"high": "\U0001F534 High",
}
DOC_QUALITY_LABELS = {
"excellent": "Excellent",
"good": "Good",
"adequate": "Adequate",
"needs_improvement": "Needs Improvement",
"poor": "Poor",
}
def render_model_output(result, header_class: str):
if not result.success:
st.error(f"Model error: {result.error_message}")
return
st.caption(f"Response time: {result.latency_seconds}s")
if result.summary.overall_assessment:
summary_parts = [f"**Summary:** {result.summary.overall_assessment}"]
if result.summary.safety_risk:
risk = SAFETY_RISK_LABELS.get(
result.summary.safety_risk, result.summary.safety_risk
)
summary_parts.append(f"**Safety risk:** {risk}")
if result.summary.documentation_quality:
doc_q = DOC_QUALITY_LABELS.get(
result.summary.documentation_quality,
result.summary.documentation_quality,
)
summary_parts.append(f"**Documentation quality:** {doc_q}")
st.markdown(" | ".join(summary_parts))
# Errors
if result.errors:
for i, err in enumerate(result.errors, 1):
sev = SEVERITY_LABELS.get(err.severity, err.severity)
cat = CATEGORY_LABELS.get(err.category, err.category)
quote = f'
Quote: "{err.quote}"' if err.quote else ""
why = ""
if getattr(err, "why_it_matters", ""):
why = f"
Why it matters: {err.why_it_matters}"
st.markdown(
f'