mgbam commited on
Commit
84b93bb
Β·
verified Β·
1 Parent(s): 864b488

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -20
app.py CHANGED
@@ -1,74 +1,80 @@
1
  import streamlit as st
2
 
3
- # 1) Ensure DB tables exist
 
4
  from models.db import init_db
5
  init_db()
6
 
7
- # β€”β€”β€”β€”β€” First-Run Signup Flow β€”β€”β€”β€”β€”
 
8
  from repositories.user_repo import UserRepo
9
  from config.settings import settings
10
 
11
  repo = UserRepo(settings.database_url)
12
  if not repo.get_all_users():
13
  st.title("πŸš€ Welcome to Quantum Healthcare AI")
14
- st.warning("No users exist yet. Please create the first admin account:")
15
  new_user = st.text_input("Username")
16
  new_name = st.text_input("Full name")
17
  new_pw = st.text_input("Password", type="password")
18
  if st.button("Create Admin User"):
19
  if new_user and new_name and new_pw:
20
  repo.add_user(new_user, new_name, new_pw)
21
- st.success(f"Admin `{new_user}` created! Please refresh to log in.")
22
  else:
23
  st.error("All fields are required.")
24
- # Stop here until admin is created
25
  st.stop()
26
- # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
27
 
28
- # 2) Authentication (after at least one user exists)
 
29
  from services.auth import authenticator, require_login
30
  username = require_login()
31
 
32
- # 3) Core AI + Clinical NLP + Quantum
 
33
  from agent.gemini_agent import chat_with_gemini
34
  from clinical_nlp.umls_bioportal import lookup_umls, lookup_bioportal
35
  from quantum.optimizer import optimize_treatment
36
  from repositories.chat_repo import ChatRepo
37
 
38
- # 4) Monitoring & Logging
 
39
  from services.logger import logger
40
  from services.metrics import CHAT_COUNT, OPTIMIZE_COUNT
41
 
42
- # === UI Setup ===
43
- st.set_page_config(page_title="Quantum Health AI", layout="wide")
 
44
  st.image("assets/logo.png", width=64)
45
- st.title(f"Welcome, {username}!")
46
 
47
  tab1, tab2 = st.tabs(["🩺 Consult", "πŸ“Š Reports"])
48
 
49
  with tab1:
50
- query = st.text_area("Describe symptoms or ask a medical question:", height=100)
51
 
52
  if st.button("Ask Gemini"):
53
  CHAT_COUNT.inc()
54
- with st.spinner("Consulting AI..."):
55
  response = chat_with_gemini(username, query)
56
- logger.info(f"[Chat] user={username} prompt={query}")
57
  st.markdown(f"**AI Response:** {response}")
58
  ChatRepo().save(user=username, prompt=query, response=response)
59
 
60
  with st.expander("πŸ”Ž UMLS Concept Lookup"):
61
- st.write(lookup_umls(query) or "No UMLS concepts found.")
 
62
 
63
  with st.expander("πŸ”¬ BioPortal Concept Lookup"):
64
- st.write(lookup_bioportal(query) or "No BioPortal matches found.")
 
65
 
66
- if st.button("Quantum Optimize Care Plan"):
67
  OPTIMIZE_COUNT.inc()
68
- with st.spinner("Running quantum-inspired optimizer..."):
69
  plan = optimize_treatment(query)
70
  logger.info(f"[Optimize] user={username} plan={plan}")
71
- st.markdown("### 🧬 Optimized Treatment Plan")
72
  st.json(plan)
73
 
74
  with tab2:
@@ -80,5 +86,6 @@ with tab2:
80
  with open(pdf_path, "rb") as f:
81
  st.download_button("Download PDF", f, file_name=pdf_path)
82
 
 
83
  st.markdown("---")
84
  st.caption("Powered by Gemini LLM β€’ UMLS/BioPortal β€’ Quantum-inspired optimization")
 
1
  import streamlit as st
2
 
3
+ # ──────────────────────────────────────────────────────────────────────────────
4
+ # 1) Initialize DB (imports models under the hood, then create_all)
5
  from models.db import init_db
6
  init_db()
7
 
8
+ # ──────────────────────────────────────────────────────────────────────────────
9
+ # 2) First-run admin signup (before any queries to the user table)
10
  from repositories.user_repo import UserRepo
11
  from config.settings import settings
12
 
13
  repo = UserRepo(settings.database_url)
14
  if not repo.get_all_users():
15
  st.title("πŸš€ Welcome to Quantum Healthcare AI")
16
+ st.warning("No users exist yet. Create the first admin account below:")
17
  new_user = st.text_input("Username")
18
  new_name = st.text_input("Full name")
19
  new_pw = st.text_input("Password", type="password")
20
  if st.button("Create Admin User"):
21
  if new_user and new_name and new_pw:
22
  repo.add_user(new_user, new_name, new_pw)
23
+ st.success(f"βœ… Admin `{new_user}` created! Refresh the page to log in.")
24
  else:
25
  st.error("All fields are required.")
 
26
  st.stop()
 
27
 
28
+ # ──────────────────────────────────────────────────────────────────────────────
29
+ # 3) Authentication
30
  from services.auth import authenticator, require_login
31
  username = require_login()
32
 
33
+ # ──────────────────────────────────────────────────────────────────────────────
34
+ # 4) Core services & repositories
35
  from agent.gemini_agent import chat_with_gemini
36
  from clinical_nlp.umls_bioportal import lookup_umls, lookup_bioportal
37
  from quantum.optimizer import optimize_treatment
38
  from repositories.chat_repo import ChatRepo
39
 
40
+ # ──────────────────────────────────────────────────────────────────────────────
41
+ # 5) Logging & metrics
42
  from services.logger import logger
43
  from services.metrics import CHAT_COUNT, OPTIMIZE_COUNT
44
 
45
+ # ──────────────────────────────────────────────────────────────────────────────
46
+ # 6) UI Layout
47
+ st.set_page_config(page_title="Quantum Healthcare AI", layout="wide")
48
  st.image("assets/logo.png", width=64)
49
+ st.title(f"Hello, {username}!")
50
 
51
  tab1, tab2 = st.tabs(["🩺 Consult", "πŸ“Š Reports"])
52
 
53
  with tab1:
54
+ query = st.text_area("Describe your symptoms or ask a clinical question:", height=100)
55
 
56
  if st.button("Ask Gemini"):
57
  CHAT_COUNT.inc()
58
+ with st.spinner("πŸ€– Consulting Gemini..."):
59
  response = chat_with_gemini(username, query)
60
+ logger.info(f"[Chat] user={username} prompt={query!r}")
61
  st.markdown(f"**AI Response:** {response}")
62
  ChatRepo().save(user=username, prompt=query, response=response)
63
 
64
  with st.expander("πŸ”Ž UMLS Concept Lookup"):
65
+ umls_results = lookup_umls(query)
66
+ st.write(umls_results or "No concepts found in UMLS.")
67
 
68
  with st.expander("πŸ”¬ BioPortal Concept Lookup"):
69
+ bio_results = lookup_bioportal(query)
70
+ st.write(bio_results or "No matches found in BioPortal.")
71
 
72
+ if st.button("🧬 Quantum Optimize Care Plan"):
73
  OPTIMIZE_COUNT.inc()
74
+ with st.spinner("βš›οΈ Running quantum-inspired optimizer..."):
75
  plan = optimize_treatment(query)
76
  logger.info(f"[Optimize] user={username} plan={plan}")
77
+ st.markdown("### Optimized Treatment Plan")
78
  st.json(plan)
79
 
80
  with tab2:
 
86
  with open(pdf_path, "rb") as f:
87
  st.download_button("Download PDF", f, file_name=pdf_path)
88
 
89
+ # ──────────────────────────────────────────────────────────────────────────────
90
  st.markdown("---")
91
  st.caption("Powered by Gemini LLM β€’ UMLS/BioPortal β€’ Quantum-inspired optimization")