File size: 6,288 Bytes
e51c241 |
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import os
import requests
import streamlit as st
st.set_page_config(page_title="Medical Assistant", page_icon="💊", layout="wide")
# -------------------------------
# Sidebar: Auth + Backend config
# -------------------------------
st.sidebar.header("🔐 Authentication")
# Prefer an env var default if you deploy multiple envs
default_base = os.getenv("BASE_URL", "https://mohitg012-medical-bot-agentic-ai.hf.space")
BASE_URL = st.sidebar.text_input("Backend URL", value=default_base, help="Your FastAPI Space URL")
hf_token = st.sidebar.text_input("Hugging Face Token", type="password", placeholder="hf_xxx...")
remember = st.sidebar.checkbox("Remember token for this browser", value=False)
if hf_token:
st.session_state["HF_TOKEN"] = hf_token
if remember:
# Warning: session_state is per-session; for true persistence, use st.session_state + st.experimental_set_query_params or a cookie/lib
st.session_state["REMEMBER"] = True
st.sidebar.success("Token saved.")
else:
st.sidebar.info("Enter your HF token to enable authenticated calls.")
# -------------------------------
# Header
# -------------------------------
left, right = st.columns([0.8, 0.2])
with left:
st.title("💊 Medical Assistant Portal")
st.write("🏠 Home – Enter your Hugging Face token and navigate to the Medical Assistant tools.")
st.write("RAG answers, agent workflows, and admin dashboard all in one place.")
with right:
st.image("https://img.icons8.com/color/96/medical-doctor.png", use_container_width=True)
st.markdown("---")
# -------------------------------
# Backend health check
# -------------------------------
st.markdown("### 🩺 Backend Status")
def check_backend(base_url: str, token: str | None):
try:
r = requests.get(f"{base_url.rstrip('/')}/", timeout=10)
ok_root = r.ok
r2 = requests.get(f"{base_url.rstrip('/')}/docs", timeout=10)
ok_docs = r2.ok
auth_ok = None
if token:
# Small authenticated call that doesn't mutate state
rq = requests.get(
f"{base_url.rstrip('/')}/medicine_availability",
params={"name": "Paracetamol"},
headers={"Authorization": f"Bearer {token}"},
timeout=15
)
auth_ok = rq.ok
return ok_root, ok_docs, auth_ok, r.json() if ok_root else None
except Exception as e:
return False, False, False, {"error": str(e)}
col1, col2, col3 = st.columns(3)
ok_root, ok_docs, auth_ok, root_payload = check_backend(BASE_URL, st.session_state.get("HF_TOKEN"))
with col1:
st.metric("Root", "OK" if ok_root else "Down")
with col2:
st.metric("Docs", "OK" if ok_docs else "Down")
with col3:
st.metric("Auth Test", ("OK" if auth_ok else ("Needs Token" if st.session_state.get("HF_TOKEN") is None else "Failed")))
if ok_root and isinstance(root_payload, dict):
with st.expander("ℹ️ Server Info"):
st.json(root_payload)
st.markdown("---")
# -------------------------------
# Navigation (one-click)
# -------------------------------
st.markdown("### 🧭 Navigate")
nav1, nav2, nav3, nav4 = st.columns(4)
with nav1:
st.page_link("pages/1_Medical_Chatbot.py", label="Medical Chatbot", icon="🧠")
with nav2:
st.page_link("pages/2_Registration_And_Operations.py", label="Registration And Operations", icon="📝")
with nav3:
st.page_link("pages/3_Agent_Bot.py", label="Agent Bot", icon="🤖")
with nav4:
st.page_link("pages/4_Dashboard.py", label="Dashboard", icon="📊")
st.markdown("---")
# -------------------------------
# Quick Actions (optional)
# -------------------------------
st.markdown("### ⚡ Quick Actions")
qa1, qa2 = st.columns(2)
with qa1:
st.subheader("Ask a sample question")
sample_q = st.text_input("Try a quick RAG query", value="What are common symptoms of jaundice?")
if st.button("Run sample RAG"):
if not st.session_state.get("HF_TOKEN"):
st.warning("Enter your HF token first (left sidebar).")
else:
with st.spinner("Contacting backend…"):
try:
r = requests.get(
f"{BASE_URL.rstrip('/')}/query",
params={"q": sample_q},
headers={"Authorization": f"Bearer {st.session_state['HF_TOKEN']}"},
timeout=30
)
if r.ok:
st.success("RAG response:")
st.json(r.json())
else:
st.error(f"{r.status_code}: {r.text}")
except Exception as e:
st.error(f"Error: {e}")
with qa2:
st.subheader("Release stale doctors")
st.caption("Frees any bookings older than 20 minutes.")
if st.button("Release"):
if not st.session_state.get("HF_TOKEN"):
st.warning("Enter your HF token first.")
else:
try:
r = requests.post(
f"{BASE_URL.rstrip('/')}/release_stale_doctors",
headers={"Authorization": f"Bearer {st.session_state['HF_TOKEN']}"},
timeout=20
)
st.success(r.json() if r.ok else f"Failed: {r.status_code} {r.text}")
except Exception as e:
st.error(f"Error: {e}")
st.markdown("---")
# -------------------------------
# Help / Tips
# -------------------------------
with st.expander("🧩 How this works", expanded=True):
st.markdown(
"""
- **Backend URL**: Your FastAPI service (on Spaces) that exposes `/query`, `/orchestrator_query`, etc.
- **HF Token**: Used to call HF models from the backend. We pass it in the `Authorization` header.
- **Pages**:
- *Medical Chatbot*: RAG QA over your medical corpus.
- *Agent Bot*: Patient registration, appointment confirmation, medicines, summaries.
- *Dashboard*: Live tables & charts via backend admin endpoints.
- **Troubleshooting**:
- If **Docs** is down, check your backend is running and exporting `/docs`.
- If **Auth Test** fails, verify your token and CORS.
"""
)
st.caption("© 2025 Medical Assistant • Built with Streamlit + FastAPI - By Mohit Gupta") |