MedQA / app.py
mgbam's picture
Update app.py
84b93bb verified
raw
history blame
4.91 kB
import streamlit as st
# ──────────────────────────────────────────────────────────────────────────────
# 1) Initialize DB (imports models under the hood, then create_all)
from models.db import init_db
init_db()
# ──────────────────────────────────────────────────────────────────────────────
# 2) First-run admin signup (before any queries to the user table)
from repositories.user_repo import UserRepo
from config.settings import settings
repo = UserRepo(settings.database_url)
if not repo.get_all_users():
st.title("πŸš€ Welcome to Quantum Healthcare AI")
st.warning("No users exist yet. Create the first admin account below:")
new_user = st.text_input("Username")
new_name = st.text_input("Full name")
new_pw = st.text_input("Password", type="password")
if st.button("Create Admin User"):
if new_user and new_name and new_pw:
repo.add_user(new_user, new_name, new_pw)
st.success(f"βœ… Admin `{new_user}` created! Refresh the page to log in.")
else:
st.error("All fields are required.")
st.stop()
# ──────────────────────────────────────────────────────────────────────────────
# 3) Authentication
from services.auth import authenticator, require_login
username = require_login()
# ──────────────────────────────────────────────────────────────────────────────
# 4) Core services & repositories
from agent.gemini_agent import chat_with_gemini
from clinical_nlp.umls_bioportal import lookup_umls, lookup_bioportal
from quantum.optimizer import optimize_treatment
from repositories.chat_repo import ChatRepo
# ──────────────────────────────────────────────────────────────────────────────
# 5) Logging & metrics
from services.logger import logger
from services.metrics import CHAT_COUNT, OPTIMIZE_COUNT
# ──────────────────────────────────────────────────────────────────────────────
# 6) UI Layout
st.set_page_config(page_title="Quantum Healthcare AI", layout="wide")
st.image("assets/logo.png", width=64)
st.title(f"Hello, {username}!")
tab1, tab2 = st.tabs(["🩺 Consult", "πŸ“Š Reports"])
with tab1:
query = st.text_area("Describe your symptoms or ask a clinical question:", height=100)
if st.button("Ask Gemini"):
CHAT_COUNT.inc()
with st.spinner("πŸ€– Consulting Gemini..."):
response = chat_with_gemini(username, query)
logger.info(f"[Chat] user={username} prompt={query!r}")
st.markdown(f"**AI Response:** {response}")
ChatRepo().save(user=username, prompt=query, response=response)
with st.expander("πŸ”Ž UMLS Concept Lookup"):
umls_results = lookup_umls(query)
st.write(umls_results or "No concepts found in UMLS.")
with st.expander("πŸ”¬ BioPortal Concept Lookup"):
bio_results = lookup_bioportal(query)
st.write(bio_results or "No matches found in BioPortal.")
if st.button("🧬 Quantum Optimize Care Plan"):
OPTIMIZE_COUNT.inc()
with st.spinner("βš›οΈ Running quantum-inspired optimizer..."):
plan = optimize_treatment(query)
logger.info(f"[Optimize] user={username} plan={plan}")
st.markdown("### Optimized Treatment Plan")
st.json(plan)
with tab2:
st.header("Generate PDF Report of Recent Chats")
if st.button("Download Last 5 Chats"):
recent = ChatRepo().get_recent(user=username, limit=5)
from services.pdf_report import generate_pdf
pdf_path = generate_pdf({"Recent Chats": recent})
with open(pdf_path, "rb") as f:
st.download_button("Download PDF", f, file_name=pdf_path)
# ──────────────────────────────────────────────────────────────────────────────
st.markdown("---")
st.caption("Powered by Gemini LLM β€’ UMLS/BioPortal β€’ Quantum-inspired optimization")