Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,23 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from services.auth import authenticator, require_login
|
3 |
-
|
4 |
-
|
5 |
from agent.gemini_agent import chat_with_gemini
|
6 |
from clinical_nlp.umls_bioportal import lookup_umls, lookup_bioportal
|
7 |
from quantum.bf_dcqo import optimize_hubo
|
8 |
-
from services.pdf_report import generate_pdf
|
9 |
from repositories.chat_repo import ChatRepo
|
10 |
|
11 |
-
#
|
12 |
-
from
|
13 |
-
|
14 |
|
15 |
-
# UI
|
16 |
username = require_login()
|
17 |
st.set_page_config(page_title="Quantum Health AI", layout="wide")
|
18 |
st.image("assets/logo.png", width=64)
|
@@ -21,30 +26,42 @@ st.title(f"Welcome, {username}!")
|
|
21 |
tab1, tab2 = st.tabs(["π©Ί Consult", "π Reports"])
|
22 |
|
23 |
with tab1:
|
24 |
-
query = st.text_area("Enter clinical query or symptoms:")
|
|
|
25 |
if st.button("Ask Gemini"):
|
26 |
CHAT_COUNT.inc()
|
27 |
with st.spinner("Consulting AI..."):
|
28 |
response = chat_with_gemini(username, query)
|
29 |
-
|
30 |
-
|
31 |
-
ChatRepo().save(username, query, response)
|
32 |
-
|
33 |
-
with st.expander("UMLS
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
if st.button("Quantum Optimize"):
|
39 |
OPTIMIZE_COUNT.inc()
|
40 |
with st.spinner("Running quantum optimizer..."):
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
with tab2:
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
|
3 |
+
# 1) Initialize DB tables before anything else
|
4 |
+
from models.db import init_db
|
5 |
+
init_db()
|
6 |
+
|
7 |
+
# 2) Authentication (after DB is ready)
|
8 |
from services.auth import authenticator, require_login
|
9 |
+
|
10 |
+
# 3) Core services & repos
|
11 |
from agent.gemini_agent import chat_with_gemini
|
12 |
from clinical_nlp.umls_bioportal import lookup_umls, lookup_bioportal
|
13 |
from quantum.bf_dcqo import optimize_hubo
|
|
|
14 |
from repositories.chat_repo import ChatRepo
|
15 |
|
16 |
+
# 4) Monitoring & logging
|
17 |
+
from services.logger import logger
|
18 |
+
from services.metrics import CHAT_COUNT, OPTIMIZE_COUNT
|
19 |
|
20 |
+
# === UI Setup ===
|
21 |
username = require_login()
|
22 |
st.set_page_config(page_title="Quantum Health AI", layout="wide")
|
23 |
st.image("assets/logo.png", width=64)
|
|
|
26 |
tab1, tab2 = st.tabs(["π©Ί Consult", "π Reports"])
|
27 |
|
28 |
with tab1:
|
29 |
+
query = st.text_area("Enter clinical query or symptoms:", height=100)
|
30 |
+
|
31 |
if st.button("Ask Gemini"):
|
32 |
CHAT_COUNT.inc()
|
33 |
with st.spinner("Consulting AI..."):
|
34 |
response = chat_with_gemini(username, query)
|
35 |
+
logger.info(f"User={username} Prompt={query}")
|
36 |
+
st.markdown(f"**AI:** {response}")
|
37 |
+
ChatRepo().save(user=username, prompt=query, response=response)
|
38 |
+
|
39 |
+
with st.expander("π UMLS Lookup"):
|
40 |
+
umls_results = lookup_umls(query)
|
41 |
+
st.write(umls_results or "No UMLS concepts found.")
|
42 |
+
|
43 |
+
with st.expander("π¬ BioPortal Lookup"):
|
44 |
+
bio_results = lookup_bioportal(query)
|
45 |
+
st.write(bio_results or "No BioPortal concepts found.")
|
46 |
|
47 |
if st.button("Quantum Optimize"):
|
48 |
OPTIMIZE_COUNT.inc()
|
49 |
with st.spinner("Running quantum optimizer..."):
|
50 |
+
plan = optimize_hubo({"query": query})
|
51 |
+
logger.info(f"Quantum plan for {username}: {plan}")
|
52 |
+
st.markdown("### 𧬠Optimized Care Plan")
|
53 |
+
st.json(plan)
|
54 |
|
55 |
with tab2:
|
56 |
+
st.header("Generate PDF Report")
|
57 |
+
if st.button("Download Last 5 Chats"):
|
58 |
+
# Fetch last 5 messages for this user
|
59 |
+
recent = ChatRepo().get_recent(user=username, limit=5)
|
60 |
+
report_data = {"Recent Chats": recent}
|
61 |
+
from services.pdf_report import generate_pdf
|
62 |
+
pdf_file = generate_pdf(report_data)
|
63 |
+
with open(pdf_file, "rb") as f:
|
64 |
+
st.download_button("Download Report", f, file_name=pdf_file)
|
65 |
+
|
66 |
+
st.markdown("---")
|
67 |
+
st.caption("Powered by Gemini LLM, UMLS, BioPortal & quantum-inspired optimization. For research use only.")
|