Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,6 @@ os.environ["XDG_STATE_HOME"] = "/tmp"
|
|
9 |
os.environ["STREAMLIT_BROWSER_GATHERUSAGESTATS"] = "false"
|
10 |
pathlib.Path("/tmp/.streamlit").mkdir(parents=True, exist_ok=True)
|
11 |
|
12 |
-
# ββ Std-lib / third-party imports ββββββββββββββββββββββββββββββββββββ
|
13 |
import asyncio, re
|
14 |
from pathlib import Path
|
15 |
import streamlit as st
|
@@ -18,17 +17,15 @@ import plotly.express as px
|
|
18 |
from fpdf import FPDF
|
19 |
from streamlit_agraph import agraph
|
20 |
|
21 |
-
|
22 |
-
from mcp.
|
23 |
-
from mcp.workspace import get_workspace, save_query
|
24 |
from mcp.knowledge_graph import build_agraph
|
25 |
-
from mcp.graph_metrics
|
26 |
-
from mcp.alerts
|
27 |
|
28 |
ROOT = Path(__file__).parent
|
29 |
LOGO = ROOT / "assets" / "logo.png"
|
30 |
|
31 |
-
# ββ PDF export helper (UTF-8 β Latin-1 βsafeβ) ββββββββββββββββββββββ
|
32 |
def _latin1_safe(txt: str) -> str:
|
33 |
return txt.encode("latin-1", "replace").decode("latin-1")
|
34 |
|
@@ -37,24 +34,17 @@ def _pdf(papers):
|
|
37 |
pdf.set_auto_page_break(auto=True, margin=15)
|
38 |
pdf.add_page()
|
39 |
pdf.set_font("Helvetica", size=11)
|
40 |
-
pdf.cell(200, 8, _latin1_safe("MedGenesis AI β Results"),
|
41 |
pdf.ln(3)
|
42 |
-
|
43 |
for i, p in enumerate(papers, 1):
|
44 |
pdf.set_font("Helvetica", "B", 11)
|
45 |
pdf.multi_cell(0, 7, _latin1_safe(f"{i}. {p['title']}"))
|
46 |
pdf.set_font("Helvetica", "", 9)
|
47 |
-
body =
|
48 |
-
f"{p['authors']}\n"
|
49 |
-
f"{p['summary']}\n"
|
50 |
-
f"{p['link']}\n"
|
51 |
-
)
|
52 |
pdf.multi_cell(0, 6, _latin1_safe(body))
|
53 |
pdf.ln(1)
|
54 |
-
|
55 |
return pdf.output(dest="S").encode("latin-1", "replace")
|
56 |
|
57 |
-
# ββ Sidebar workspace βββββββββββββββββββββββββββββββββββββββββββββββ
|
58 |
def _workspace_sidebar():
|
59 |
with st.sidebar:
|
60 |
st.header("ποΈ Workspace")
|
@@ -66,9 +56,15 @@ def _workspace_sidebar():
|
|
66 |
with st.expander(f"{i}. {item['query']}"):
|
67 |
st.write(item["result"]["ai_summary"])
|
68 |
|
69 |
-
# ββ Main UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
70 |
def render_ui():
|
71 |
st.set_page_config("MedGenesis AI", layout="wide")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
_workspace_sidebar()
|
73 |
|
74 |
c1, c2 = st.columns([0.15, 0.85])
|
@@ -79,7 +75,7 @@ def render_ui():
|
|
79 |
st.markdown("## 𧬠**MedGenesis AI**")
|
80 |
st.caption("Multi-source biomedical assistant Β· OpenAI / Gemini")
|
81 |
|
82 |
-
llm
|
83 |
query = st.text_input("Enter biomedical question", placeholder="e.g. CRISPR glioblastoma therapy")
|
84 |
|
85 |
# Alert check
|
@@ -94,98 +90,111 @@ def render_ui():
|
|
94 |
except Exception:
|
95 |
pass
|
96 |
|
97 |
-
#
|
98 |
if st.button("Run Search π") and query:
|
99 |
with st.spinner("Collecting literature & biomedical data β¦"):
|
100 |
res = asyncio.run(orchestrate_search(query, llm=llm))
|
101 |
st.success(f"Completed with **{res['llm_used'].title()}**")
|
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 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
st.markdown("---")
|
177 |
-
follow = st.text_input("Ask follow
|
|
|
|
|
178 |
if st.button("Ask AI"):
|
179 |
-
|
|
|
180 |
with st.spinner("Generating AI response..."):
|
181 |
-
ans = asyncio.run(answer_ai_question(
|
|
|
182 |
st.write(ans["answer"])
|
183 |
else:
|
184 |
-
st.warning("Please type a follow-up question before submitting.")
|
185 |
-
|
186 |
else:
|
187 |
st.info("Enter a question and press **Run Search π**")
|
188 |
|
189 |
-
# entry-point
|
190 |
if __name__ == "__main__":
|
191 |
render_ui()
|
|
|
9 |
os.environ["STREAMLIT_BROWSER_GATHERUSAGESTATS"] = "false"
|
10 |
pathlib.Path("/tmp/.streamlit").mkdir(parents=True, exist_ok=True)
|
11 |
|
|
|
12 |
import asyncio, re
|
13 |
from pathlib import Path
|
14 |
import streamlit as st
|
|
|
17 |
from fpdf import FPDF
|
18 |
from streamlit_agraph import agraph
|
19 |
|
20 |
+
from mcp.orchestrator import orchestrate_search, answer_ai_question
|
21 |
+
from mcp.workspace import get_workspace, save_query
|
|
|
22 |
from mcp.knowledge_graph import build_agraph
|
23 |
+
from mcp.graph_metrics import build_nx, get_top_hubs, get_density
|
24 |
+
from mcp.alerts import check_alerts
|
25 |
|
26 |
ROOT = Path(__file__).parent
|
27 |
LOGO = ROOT / "assets" / "logo.png"
|
28 |
|
|
|
29 |
def _latin1_safe(txt: str) -> str:
|
30 |
return txt.encode("latin-1", "replace").decode("latin-1")
|
31 |
|
|
|
34 |
pdf.set_auto_page_break(auto=True, margin=15)
|
35 |
pdf.add_page()
|
36 |
pdf.set_font("Helvetica", size=11)
|
37 |
+
pdf.cell(200, 8, _latin1_safe("MedGenesis AI β Results"), ln=True, align="C")
|
38 |
pdf.ln(3)
|
|
|
39 |
for i, p in enumerate(papers, 1):
|
40 |
pdf.set_font("Helvetica", "B", 11)
|
41 |
pdf.multi_cell(0, 7, _latin1_safe(f"{i}. {p['title']}"))
|
42 |
pdf.set_font("Helvetica", "", 9)
|
43 |
+
body = f"{p['authors']}\n{p['summary']}\n{p['link']}\n"
|
|
|
|
|
|
|
|
|
44 |
pdf.multi_cell(0, 6, _latin1_safe(body))
|
45 |
pdf.ln(1)
|
|
|
46 |
return pdf.output(dest="S").encode("latin-1", "replace")
|
47 |
|
|
|
48 |
def _workspace_sidebar():
|
49 |
with st.sidebar:
|
50 |
st.header("ποΈ Workspace")
|
|
|
56 |
with st.expander(f"{i}. {item['query']}"):
|
57 |
st.write(item["result"]["ai_summary"])
|
58 |
|
|
|
59 |
def render_ui():
|
60 |
st.set_page_config("MedGenesis AI", layout="wide")
|
61 |
+
|
62 |
+
# Initialize session-state
|
63 |
+
if "followup_input" not in st.session_state:
|
64 |
+
st.session_state.followup_input = ""
|
65 |
+
if "tab_index" not in st.session_state:
|
66 |
+
st.session_state.tab_index = 0
|
67 |
+
|
68 |
_workspace_sidebar()
|
69 |
|
70 |
c1, c2 = st.columns([0.15, 0.85])
|
|
|
75 |
st.markdown("## 𧬠**MedGenesis AI**")
|
76 |
st.caption("Multi-source biomedical assistant Β· OpenAI / Gemini")
|
77 |
|
78 |
+
llm = st.radio("LLM engine", ["openai", "gemini"], horizontal=True)
|
79 |
query = st.text_input("Enter biomedical question", placeholder="e.g. CRISPR glioblastoma therapy")
|
80 |
|
81 |
# Alert check
|
|
|
90 |
except Exception:
|
91 |
pass
|
92 |
|
93 |
+
# Trigger search
|
94 |
if st.button("Run Search π") and query:
|
95 |
with st.spinner("Collecting literature & biomedical data β¦"):
|
96 |
res = asyncio.run(orchestrate_search(query, llm=llm))
|
97 |
st.success(f"Completed with **{res['llm_used'].title()}**")
|
98 |
+
st.session_state.query_result = res
|
99 |
+
# Reset follow-up input
|
100 |
+
st.session_state.followup_input = ""
|
101 |
+
st.session_state.tab_index = 0
|
102 |
+
else:
|
103 |
+
res = st.session_state.get("query_result", None)
|
104 |
+
|
105 |
+
if res:
|
106 |
+
tabs_list = ["Results", "Genes", "Trials", "Graph", "Metrics", "Visuals"]
|
107 |
+
tabs = st.tabs(tabs_list, index=st.session_state.tab_index)
|
108 |
+
|
109 |
+
for idx, name in enumerate(tabs_list):
|
110 |
+
with tabs[idx]:
|
111 |
+
st.session_state.tab_index = idx
|
112 |
+
if name == "Results":
|
113 |
+
for i, p in enumerate(res["papers"], 1):
|
114 |
+
st.markdown(f"**{i}. [{p['title']}]({p['link']})** *{p['authors']}*")
|
115 |
+
st.write(p["summary"])
|
116 |
+
col1, col2 = st.columns(2)
|
117 |
+
with col1:
|
118 |
+
st.download_button("CSV", pd.DataFrame(res["papers"]).to_csv(index=False),
|
119 |
+
"papers.csv", "text/csv")
|
120 |
+
with col2:
|
121 |
+
st.download_button("PDF", _pdf(res["papers"]), "papers.pdf", "application/pdf")
|
122 |
+
if st.button("πΎ Save"):
|
123 |
+
save_query(query, res)
|
124 |
+
st.success("Saved to workspace")
|
125 |
+
st.subheader("UMLS concepts")
|
126 |
+
for c in res["umls"]:
|
127 |
+
if c.get("cui"):
|
128 |
+
st.write(f"- **{c['name']}** ({c['cui']})")
|
129 |
+
st.subheader("OpenFDA safety")
|
130 |
+
for d in res["drug_safety"]:
|
131 |
+
st.json(d)
|
132 |
+
st.subheader("AI summary")
|
133 |
+
st.info(res["ai_summary"])
|
134 |
+
|
135 |
+
elif name == "Genes":
|
136 |
+
st.header("Gene / Variant signals")
|
137 |
+
for g in res["genes"]:
|
138 |
+
st.write(f"- **{g.get('name', g.get('geneid'))}** "
|
139 |
+
f"{g.get('description', '')}")
|
140 |
+
if res["gene_disease"]:
|
141 |
+
st.markdown("### DisGeNET links")
|
142 |
+
st.json(res["gene_disease"][:15])
|
143 |
+
if res["mesh_defs"]:
|
144 |
+
st.markdown("### MeSH definitions")
|
145 |
+
for d in res["mesh_defs"]:
|
146 |
+
if d:
|
147 |
+
st.write("-", d)
|
148 |
+
|
149 |
+
elif name == "Trials":
|
150 |
+
st.header("Clinical trials")
|
151 |
+
if not res["clinical_trials"]:
|
152 |
+
st.info("No trials (rate-limited or none found).")
|
153 |
+
for t in res["clinical_trials"]:
|
154 |
+
st.markdown(f"**{t['NCTId'][0]}** β {t['BriefTitle'][0]}")
|
155 |
+
st.write(f"Phase {t.get('Phase',[''])[0]} | "
|
156 |
+
f"Status {t['OverallStatus'][0]}")
|
157 |
+
|
158 |
+
elif name == "Graph":
|
159 |
+
nodes, edges, cfg = build_agraph(res["papers"], res["umls"], res["drug_safety"])
|
160 |
+
hl = st.text_input("Highlight node:", key="hl")
|
161 |
+
if hl:
|
162 |
+
pat = re.compile(re.escape(hl), re.I)
|
163 |
+
for n in nodes:
|
164 |
+
n.color = "#f1c40f" if pat.search(n.label) else "#d3d3d3"
|
165 |
+
agraph(nodes, edges, cfg)
|
166 |
+
|
167 |
+
elif name == "Metrics":
|
168 |
+
nodes, edges, _ = build_agraph(res["papers"], res["umls"], res["drug_safety"])
|
169 |
+
G = build_nx([n.__dict__ for n in nodes], [e.__dict__ for e in edges])
|
170 |
+
st.metric("Density", f"{get_density(G):.3f}")
|
171 |
+
st.markdown("**Top hubs**")
|
172 |
+
for nid, sc in get_top_hubs(G):
|
173 |
+
lab = next((n.label for n in nodes if n.id == nid), nid)
|
174 |
+
st.write(f"- {lab} {sc:.3f}")
|
175 |
+
|
176 |
+
elif name == "Visuals":
|
177 |
+
years = [p["published"] for p in res["papers"] if p.get("published")]
|
178 |
+
if years:
|
179 |
+
st.plotly_chart(px.histogram(years, nbins=12,
|
180 |
+
title="Publication Year"))
|
181 |
+
|
182 |
+
# Follow-up Q-A persistently under tabs
|
183 |
st.markdown("---")
|
184 |
+
follow = st.text_input("Ask followβup question:",
|
185 |
+
value=st.session_state.followup_input,
|
186 |
+
key="followup_input")
|
187 |
if st.button("Ask AI"):
|
188 |
+
st.session_state.followup_input = follow
|
189 |
+
if follow.strip():
|
190 |
with st.spinner("Generating AI response..."):
|
191 |
+
ans = asyncio.run(answer_ai_question(
|
192 |
+
follow, context=query, llm=llm))
|
193 |
st.write(ans["answer"])
|
194 |
else:
|
195 |
+
st.warning("Please type a follow-up question before submitting.")
|
|
|
196 |
else:
|
197 |
st.info("Enter a question and press **Run Search π**")
|
198 |
|
|
|
199 |
if __name__ == "__main__":
|
200 |
render_ui()
|