File size: 4,116 Bytes
d923822 738d6d3 d923822 738d6d3 d923822 738d6d3 813bfe7 d923822 813bfe7 d923822 738d6d3 d923822 738d6d3 d923822 738d6d3 d923822 738d6d3 d923822 738d6d3 d923822 738d6d3 d923822 738d6d3 d923822 738d6d3 d923822 738d6d3 d923822 738d6d3 d923822 738d6d3 d923822 |
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 |
# app.py
import os
import platform
import time
from pathlib import Path
from typing import Dict
import streamlit as st
import pandas as pd
from src.paths import (
base_dir,
guidelines_dir,
faiss_index_dir,
exports_dir,
cases_dir,
audit_dir,
hf_cache_dir,
initialize_environment,
describe_paths,
)
st.set_page_config(page_title="AI‑Native E‑Consult — Health Check", page_icon="🩺", layout="wide")
st.title("AI‑Native E‑Consult Prototype (V1)")
st.caption("Step 0 — Environment Setup & Health Check")
st.warning("Demo only — de‑identified data. Prototype for feedback; **not for clinical use**.", icon="🛑")
# ---------- Initialize env & log ----------
env = initialize_environment()
st.session_state.setdefault("_app_env", env)
with st.expander("Environment variables (runtime)", expanded=False):
st.json(env)
# ---------- Dependency checks ----------
def _probe_import(modname: str):
try:
m = __import__(modname)
ver = getattr(m, "__version__", "")
# faiss exposes version differently sometimes
if modname == "faiss" and not ver:
ver = getattr(m, "FAISS_VERSION", "") or ""
return True, ver, ""
except Exception as e:
return False, "", f"{type(e).__name__}: {e}"
mods = [
"torch", "accelerate", "transformers", "bitsandbytes", "faiss",
"sentence_transformers", "pypdf", "huggingface_hub", "numpy", "pandas"
]
rows = []
for name in mods:
ok, ver, err = _probe_import(name)
rows.append({
"package": name,
"status": "✅" if ok else "❌",
"version": ver,
"error": err,
})
st.subheader("Python packages")
st.dataframe(pd.DataFrame(rows), use_container_width=True)
# ---------- CUDA ----------
cuda_txt = "Not checked"
gpu_name = ""
try:
import torch
has_cuda = torch.cuda.is_available()
cuda_txt = "✅ Available" if has_cuda else "❌ Not available"
if has_cuda:
try:
gpu_name = torch.cuda.get_device_name(0)
except Exception:
gpu_name = "CUDA detected (name unavailable)"
except Exception as e:
has_cuda = False
cuda_txt = f"⚠️ Torch import error: {e}"
colA, colB = st.columns(2)
with colA:
st.subheader("System")
st.write({
"python": platform.python_version(),
"platform": platform.platform(),
"cwd": str(Path.cwd()),
"time": time.strftime("%Y-%m-%d %H:%M:%S"),
"CUDA": cuda_txt,
"GPU": gpu_name,
})
with colB:
st.subheader("Paths")
pinfo: Dict[str, str] = describe_paths()
st.write(pinfo)
# basic directory status
def _count_pdfs(p: Path) -> int:
return sum(1 for _ in p.glob("**/*.pdf"))
def _human_bytes(n: int) -> str:
for u in ["B", "KB", "MB", "GB", "TB"]:
if n < 1024:
return f"{n:.1f} {u}"
n /= 1024
return f"{n:.1f} PB"
# quick cache dir size
cache = Path(pinfo["hf_cache_dir"])
size = 0
try:
for root, _, files in os.walk(cache):
for f in files:
try:
size += (Path(root) / f).stat().st_size
except Exception:
pass
except Exception:
pass
st.write({
"guideline_pdfs": _count_pdfs(Path(pinfo["guidelines_dir"])),
"index_present": (
(Path(pinfo["faiss_index_dir"]) / "faiss.index").exists()
and (Path(pinfo["faiss_index_dir"]) / "chunks.jsonl").exists()
and (Path(pinfo["faiss_index_dir"]) / "index_info.json").exists()
),
"hf_cache_size": _human_bytes(size),
})
st.info(
"**Model selection**\n\n"
f"- Primary: `{os.getenv('MODEL_ID', 'google/medgemma-27b-text-it')}` (GPU / 4-bit)\n"
f"- Fallback: `{os.getenv('MODEL_FALLBACK_ID', 'google/medgemma-4b-it')}` (CPU)\n"
f"- Stub mode (`E2E_STUB=1`): returns deterministic output for UI tests.",
icon="⚙️"
)
st.success("Health page loaded. Proceed to **Step 1 — RAG Corpus Prep** from the sidebar when ready.", icon="➡️")
|