Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import platform
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import time
|
| 5 |
+
import traceback
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
from src.paths import base_dir, guidelines_dir, faiss_index_dir, exports_dir
|
| 11 |
+
|
| 12 |
+
st.set_page_config(page_title="AI-Native E-Consult Prototype (V1)", page_icon="🩺", layout="wide")
|
| 13 |
+
st.title("AI‑Native E‑Consult Prototype (V1)")
|
| 14 |
+
st.caption("Step 0 — Environment Setup & Health Check")
|
| 15 |
+
|
| 16 |
+
# ---------- Helper ----------
|
| 17 |
+
def _try_import(modname: str):
|
| 18 |
+
try:
|
| 19 |
+
m = __import__(modname)
|
| 20 |
+
ver = getattr(m, "__version__", "n/a")
|
| 21 |
+
return True, ver, None
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return False, None, str(e)
|
| 24 |
+
|
| 25 |
+
def _hf_whoami():
|
| 26 |
+
try:
|
| 27 |
+
from huggingface_hub import whoami
|
| 28 |
+
token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 29 |
+
if not token:
|
| 30 |
+
return False, None, "No HF token found. Add HF_TOKEN in Space Settings → Variables."
|
| 31 |
+
me = whoami(token=token)
|
| 32 |
+
return True, me, None
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return False, None, str(e)
|
| 35 |
+
|
| 36 |
+
# ---------- Persistent dirs ----------
|
| 37 |
+
bdir = base_dir()
|
| 38 |
+
gdir = guidelines_dir()
|
| 39 |
+
idir = faiss_index_dir()
|
| 40 |
+
xdir = exports_dir()
|
| 41 |
+
|
| 42 |
+
with st.expander("📁 Storage locations (persistent)"):
|
| 43 |
+
st.write({
|
| 44 |
+
"base_dir": str(bdir),
|
| 45 |
+
"guidelines_dir": str(gdir),
|
| 46 |
+
"faiss_index_dir": str(idir),
|
| 47 |
+
"exports_dir": str(xdir),
|
| 48 |
+
})
|
| 49 |
+
st.caption("These live on the Space's persistent volume so your RAG index survives restarts.")
|
| 50 |
+
|
| 51 |
+
# ---------- Diagnostics ----------
|
| 52 |
+
colA, colB = st.columns(2)
|
| 53 |
+
|
| 54 |
+
with colA:
|
| 55 |
+
st.subheader("System")
|
| 56 |
+
st.write({
|
| 57 |
+
"python": platform.python_version(),
|
| 58 |
+
"platform": platform.platform(),
|
| 59 |
+
"cwd": str(Path.cwd()),
|
| 60 |
+
"time": time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 61 |
+
})
|
| 62 |
+
ok_torch, torch_ver, torch_err = _try_import("torch")
|
| 63 |
+
if ok_torch:
|
| 64 |
+
import torch
|
| 65 |
+
cuda = torch.cuda.is_available()
|
| 66 |
+
device = torch.cuda.get_device_name(0) if cuda else "CPU"
|
| 67 |
+
st.success(f"torch {torch_ver} — CUDA: {'✅' if cuda else '❌'} — device: {device}")
|
| 68 |
+
else:
|
| 69 |
+
st.error(f"torch import failed: {torch_err}")
|
| 70 |
+
|
| 71 |
+
with colB:
|
| 72 |
+
st.subheader("Core libraries")
|
| 73 |
+
rows = []
|
| 74 |
+
for name in ["transformers", "accelerate", "bitsandbytes", "faiss", "pypdf", "pandas", "numpy", "huggingface_hub", "sentence_transformers"]:
|
| 75 |
+
ok, ver, err = _try_import(name)
|
| 76 |
+
rows.append({"library": name, "status": "ok" if ok else "error", "version_or_error": ver if ok else err})
|
| 77 |
+
st.dataframe(pd.DataFrame(rows), hide_index=True, use_container_width=True)
|
| 78 |
+
|
| 79 |
+
st.divider()
|
| 80 |
+
st.subheader("Hugging Face auth check (for later model pulls)")
|
| 81 |
+
if st.button("Check HF token"):
|
| 82 |
+
ok, me, err = _hf_whoami()
|
| 83 |
+
if ok:
|
| 84 |
+
who = me.get("name") or me.get("email") or me.get("username", "unknown")
|
| 85 |
+
st.success(f"HF token valid ✅ — signed in as: {who}")
|
| 86 |
+
else:
|
| 87 |
+
st.warning(f"HF token not verified: {err}")
|
| 88 |
+
|
| 89 |
+
st.subheader("Quick functionality tests")
|
| 90 |
+
if st.button("Run health checks"):
|
| 91 |
+
results = []
|
| 92 |
+
|
| 93 |
+
# 1) Write to persistent storage
|
| 94 |
+
try:
|
| 95 |
+
testfile = bdir / "healthcheck.txt"
|
| 96 |
+
testfile.write_text("ok\n")
|
| 97 |
+
results.append(("write_persistent", True, f"wrote {testfile}"))
|
| 98 |
+
except Exception as e:
|
| 99 |
+
results.append(("write_persistent", False, str(e)))
|
| 100 |
+
|
| 101 |
+
# 2) FAISS in-memory index sanity test
|
| 102 |
+
try:
|
| 103 |
+
import numpy as np, faiss
|
| 104 |
+
xb = np.random.random((50, 8)).astype("float32")
|
| 105 |
+
idx = faiss.IndexFlatL2(8)
|
| 106 |
+
idx.add(xb)
|
| 107 |
+
D, I = idx.search(xb[:1], 5)
|
| 108 |
+
results.append(("faiss_search", True, f"top5 ids: {I[0].tolist()}"))
|
| 109 |
+
except Exception as e:
|
| 110 |
+
results.append(("faiss_search", False, str(e)))
|
| 111 |
+
|
| 112 |
+
# 3) bitsandbytes soft check (import + CUDA capability if torch has it)
|
| 113 |
+
try:
|
| 114 |
+
import bitsandbytes as bnb # noqa
|
| 115 |
+
cuda_msg = ""
|
| 116 |
+
try:
|
| 117 |
+
import torch
|
| 118 |
+
if torch.cuda.is_available():
|
| 119 |
+
# light test: allocate a tiny 4-bit linear layer if available
|
| 120 |
+
from bitsandbytes.nn import Linear4bit
|
| 121 |
+
_ = Linear4bit(8, 8, bias=False)
|
| 122 |
+
cuda_msg = "CUDA-backed 4-bit layer constructed."
|
| 123 |
+
except Exception:
|
| 124 |
+
pass
|
| 125 |
+
results.append(("bitsandbytes", True, f"import ok. {cuda_msg}"))
|
| 126 |
+
except Exception as e:
|
| 127 |
+
results.append(("bitsandbytes", False, str(e)))
|
| 128 |
+
|
| 129 |
+
st.success("Health checks complete.")
|
| 130 |
+
st.dataframe(pd.DataFrame([{"check": k, "ok": ok, "detail": d} for (k, ok, d) in results]),
|
| 131 |
+
hide_index=True, use_container_width=True)
|
| 132 |
+
|
| 133 |
+
st.info("If the checks are green, the Space is ready for Step 1 (RAG Corpus Prep).")
|