Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,103 @@
|
|
1 |
# app.py
|
2 |
-
import asyncio,
|
3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
st.set_page_config(layout="wide", page_title="MedGenesis AI")
|
6 |
-
if "res" not in st.session_state:
|
|
|
7 |
|
8 |
st.title("🧬 MedGenesis AI")
|
9 |
-
llm
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
|
|
|
|
15 |
res = st.session_state.res
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
st.write(p["summary"])
|
21 |
-
st.
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# app.py
|
2 |
+
import asyncio, re
|
3 |
+
from pathlib import Path
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
import pandas as pd
|
7 |
+
import plotly.express as px
|
8 |
+
from fpdf import FPDF
|
9 |
+
from streamlit_agraph import agraph
|
10 |
+
|
11 |
+
from mcp.orchestrator import orchestrate_search, answer_ai_question
|
12 |
+
from mcp.knowledge_graph import build_agraph
|
13 |
+
from mcp.graph_metrics import build_nx, get_top_hubs, get_density
|
14 |
|
15 |
st.set_page_config(layout="wide", page_title="MedGenesis AI")
|
16 |
+
if "res" not in st.session_state:
|
17 |
+
st.session_state.res = None
|
18 |
|
19 |
st.title("🧬 MedGenesis AI")
|
20 |
+
llm = st.radio("LLM engine", ["openai","gemini"], horizontal=True)
|
21 |
+
query= st.text_input("Enter biomedical question")
|
22 |
+
|
23 |
+
def _make_pdf(papers):
|
24 |
+
pdf = FPDF(); pdf.add_page(); pdf.set_font("Helvetica",size=12)
|
25 |
+
pdf.cell(0,10,"MedGenesis AI – Results",ln=True,align="C"); pdf.ln(5)
|
26 |
+
for i,p in enumerate(papers,1):
|
27 |
+
pdf.set_font("Helvetica","B",11); pdf.multi_cell(0,7,f"{i}. {p.get('title','')}")
|
28 |
+
pdf.set_font("Helvetica",size=9)
|
29 |
+
body = f"{p.get('authors','')}\n{p.get('summary','')}\n{p.get('link','')}"
|
30 |
+
pdf.multi_cell(0,6,body); pdf.ln(3)
|
31 |
+
return pdf.output(dest="S").encode("latin-1",errors="replace")
|
32 |
|
33 |
+
if st.button("Run Search 🚀") and query:
|
34 |
+
with st.spinner("Gathering data…"):
|
35 |
+
st.session_state.res = asyncio.run(orchestrate_search(query, llm))
|
36 |
res = st.session_state.res
|
37 |
+
|
38 |
+
if not res:
|
39 |
+
st.info("Enter a query and press Run Search")
|
40 |
+
st.stop()
|
41 |
+
|
42 |
+
# ── Results tab
|
43 |
+
tabs = st.tabs(["Results","Graph","Variants","Trials","Metrics","Visuals"])
|
44 |
+
with tabs[0]:
|
45 |
+
for i,p in enumerate(res["papers"],1):
|
46 |
+
st.markdown(f"**{i}. [{p['title']}]({p['link']})**")
|
47 |
st.write(p["summary"])
|
48 |
+
c1,c2 = st.columns(2)
|
49 |
+
c1.download_button("CSV", pd.DataFrame(res["papers"]).to_csv(index=False),
|
50 |
+
"papers.csv","text/csv")
|
51 |
+
c2.download_button("PDF", _make_pdf(res["papers"]),
|
52 |
+
"papers.pdf","application/pdf")
|
53 |
+
st.subheader("AI summary"); st.info(res["ai_summary"])
|
54 |
+
|
55 |
+
# ── Graph tab
|
56 |
+
with tabs[1]:
|
57 |
+
nodes,edges,cfg = build_agraph(
|
58 |
+
res["papers"], res["umls"], res["drug_safety"], res["umls_relations"]
|
59 |
+
)
|
60 |
+
hl = st.text_input("Highlight node:", key="hl")
|
61 |
+
if hl:
|
62 |
+
pat = re.compile(re.escape(hl), re.I)
|
63 |
+
for n in nodes:
|
64 |
+
n.color = "#f1c40f" if pat.search(n.label) else n.color
|
65 |
+
agraph(nodes, edges, cfg)
|
66 |
+
|
67 |
+
# ── Variants tab
|
68 |
+
with tabs[2]:
|
69 |
+
if res["variants"]:
|
70 |
+
st.json(res["variants"])
|
71 |
+
else:
|
72 |
+
st.warning("No variants found. Try ‘TP53’ or ‘BRCA1’.")
|
73 |
+
|
74 |
+
# ── Trials tab
|
75 |
+
with tabs[3]:
|
76 |
+
if res["clinical_trials"]:
|
77 |
+
st.json(res["clinical_trials"])
|
78 |
+
else:
|
79 |
+
st.warning("No trials found. Try a disease or drug.")
|
80 |
+
|
81 |
+
# ── Metrics tab
|
82 |
+
with tabs[4]:
|
83 |
+
G = build_nx([n.__dict__ for n in nodes],[e.__dict__ for e in edges])
|
84 |
+
st.metric("Density", f"{get_density(G):.3f}")
|
85 |
+
st.markdown("**Top hubs**")
|
86 |
+
for nid,sc in get_top_hubs(G):
|
87 |
+
lbl = next((n.label for n in nodes if n.id==nid), nid)
|
88 |
+
st.write(f"- {lbl}: {sc:.3f}")
|
89 |
+
|
90 |
+
# ── Visuals tab
|
91 |
+
with tabs[5]:
|
92 |
+
yrs = [p.get("published","")[:4] for p in res["papers"] if p.get("published")]
|
93 |
+
if yrs:
|
94 |
+
st.plotly_chart(px.histogram(yrs,nbins=10,title="Publication Year"))
|
95 |
+
|
96 |
+
# ── Follow-up QA
|
97 |
+
st.markdown("---")
|
98 |
+
q = st.text_input("Ask follow-up question:", key="followup_input")
|
99 |
+
if st.button("Ask AI"):
|
100 |
+
with st.spinner("Querying LLM…"):
|
101 |
+
ans = asyncio.run(answer_ai_question(
|
102 |
+
q, context=res["ai_summary"], llm=llm))
|
103 |
+
st.write(ans["answer"])
|