mgbam commited on
Commit
5417053
Β·
verified Β·
1 Parent(s): f797a9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -37
app.py CHANGED
@@ -13,7 +13,7 @@ from mcp.orchestrator import orchestrate_search, answer_ai_question
13
  from mcp.schemas import UnifiedSearchInput, UnifiedSearchResult
14
  from mcp.workspace import get_workspace, save_query
15
  from mcp.knowledge_graph import build_agraph
16
- from streamlit_agraph import agraph, Node, Edge, Config
17
 
18
  ROOT = Path(__file__).parent
19
  LOGO = ROOT / "assets" / "logo.png"
@@ -35,17 +35,21 @@ def generate_pdf(papers):
35
  def render_ui():
36
  st.set_page_config(page_title="MedGenesis AI", layout="wide")
37
 
 
38
  with st.sidebar:
39
  st.header("πŸ—‚οΈ Workspace")
40
- for i, item in enumerate(get_workspace(), 1):
41
- with st.expander(f"{i}. {item['query']}"):
42
- st.write("**AI Summary:**", item["result"]["ai_summary"])
43
- st.write("**Top Paper:**", item["result"]["papers"][0]["title"] if item["result"]["papers"] else "None")
44
- df = pd.DataFrame(item["result"]["papers"])
45
- st.download_button("πŸ“₯ CSV", df.to_csv(index=False), f"workspace_{i}.csv", "text/csv")
46
- if not get_workspace():
47
- st.info("Run and save searches here.")
48
-
 
 
 
49
  col1, col2 = st.columns([0.15, 0.85])
50
  with col1:
51
  if LOGO.exists():
@@ -55,69 +59,76 @@ def render_ui():
55
  st.caption("Built by Oluwafemi Idiakhoa β€’ Hugging Face Spaces")
56
 
57
  st.markdown("---")
58
- query = st.text_input("πŸ” Enter research question:", placeholder="e.g. CRISPR treatments for glioblastoma")
59
 
60
  results = None
61
  if st.button("Run Search πŸš€"):
62
- with st.spinner("Analyzing..."):
63
  results = asyncio.run(orchestrate_search(query))
64
- st.success("Search complete")
65
 
66
  if results:
67
  tabs = st.tabs(["πŸ“ Results", "πŸ—ΊοΈ Knowledge Graph", "πŸ“Š Visualizations"])
68
 
 
69
  with tabs[0]:
70
- st.header("πŸ“š Papers")
71
  for i, p in enumerate(results["papers"], 1):
72
- st.markdown(f"**{i}. [{p['title']}]({p['link']})** *{p['authors']}*")
73
  st.markdown(f"<div style='color:gray'>{p['summary']}</div>", unsafe_allow_html=True)
74
  if st.button("Save to Workspace"):
75
- save_query(query, results); st.success("Saved!")
 
76
  df = pd.DataFrame(results["papers"])
77
- st.download_button("πŸ“₯ CSV", df.to_csv(index=False), "results.csv", "text/csv")
78
  pdf = generate_pdf(results["papers"])
79
- st.download_button("πŸ“„ PDF", pdf, "results.pdf", "application/pdf")
80
  st.subheader("🧠 UMLS Concepts")
81
  for c in results["umls"]:
82
  if c.get("cui"):
83
- st.markdown(f"- **{c['name']}** (CUI:{c['cui']}): {c.get('definition','')}")
84
- st.subheader("πŸ’Š Drug Safety")
85
  for d in results["drug_safety"]:
86
  st.json(d)
87
- st.subheader("πŸ€– AI Summary")
88
  st.info(results["ai_summary"])
89
 
 
90
  with tabs[1]:
91
- st.header("πŸ”Ž Knowledge Graph")
92
- search_term = st.text_input("Highlight node containing text:", value="")
93
  try:
94
  nodes, edges, config = build_agraph(results["papers"], results["umls"], results["drug_safety"])
95
- if search_term:
96
- pat = re.compile(re.escape(search_term), re.IGNORECASE)
97
  for node in nodes:
98
- if pat.search(node.label) or (hasattr(node, "tooltip") and pat.search(getattr(node, "tooltip", ""))):
99
- node.color = "#f1c40f"; node.size = max(node.size, 30)
 
100
  else:
101
  node.color = "#ddd"
102
  agraph(nodes=nodes, edges=edges, config=config)
103
  except Exception as e:
104
- st.warning("Graph unavailable: " + str(e))
105
 
 
106
  with tabs[2]:
107
- years = [p["published"] for p in results["papers"] if p.get("published")]
108
- if years:
109
- st.plotly_chart(px.histogram(years, title="Pub Year"))
 
110
 
 
111
  st.markdown("---")
112
- st.subheader("πŸ’¬ Ask AI a Follow-up")
113
- fq = st.text_input("Ask based on above:", "")
114
  if st.button("Ask AI"):
115
- with st.spinner("Thinking..."):
116
- ans = asyncio.run(answer_ai_question(fq, context=query))
117
- st.write(ans.get("answer", ans))
118
 
119
  st.markdown("---")
120
- st.caption("✨ Built by Oluwafemi Idiakhoa β€’ Powered by Streamlit & AI")
121
 
122
  if __name__ == "__main__":
123
  render_ui()
 
13
  from mcp.schemas import UnifiedSearchInput, UnifiedSearchResult
14
  from mcp.workspace import get_workspace, save_query
15
  from mcp.knowledge_graph import build_agraph
16
+ from streamlit_agraph import agraph
17
 
18
  ROOT = Path(__file__).parent
19
  LOGO = ROOT / "assets" / "logo.png"
 
35
  def render_ui():
36
  st.set_page_config(page_title="MedGenesis AI", layout="wide")
37
 
38
+ # Workspace in sidebar
39
  with st.sidebar:
40
  st.header("πŸ—‚οΈ Workspace")
41
+ ws = get_workspace()
42
+ if ws:
43
+ for i, item in enumerate(ws, 1):
44
+ with st.expander(f"{i}. {item['query']}"):
45
+ st.write("**AI Summary:**", item["result"]["ai_summary"])
46
+ st.write("**Top Paper:**", item["result"]["papers"][0]["title"] if item["result"]["papers"] else "None")
47
+ df = pd.DataFrame(item["result"]["papers"])
48
+ st.download_button("πŸ“₯ CSV", df.to_csv(index=False), f"query_{i}.csv", "text/csv")
49
+ else:
50
+ st.info("Run & save searches here.")
51
+
52
+ # Header and logo
53
  col1, col2 = st.columns([0.15, 0.85])
54
  with col1:
55
  if LOGO.exists():
 
59
  st.caption("Built by Oluwafemi Idiakhoa β€’ Hugging Face Spaces")
60
 
61
  st.markdown("---")
62
+ query = st.text_input("πŸ” Enter biomedical research question:", placeholder="e.g. CRISPR treatments for glioblastoma")
63
 
64
  results = None
65
  if st.button("Run Search πŸš€"):
66
+ with st.spinner("Gathering and analyzing data..."):
67
  results = asyncio.run(orchestrate_search(query))
68
+ st.success("βœ… Search complete")
69
 
70
  if results:
71
  tabs = st.tabs(["πŸ“ Results", "πŸ—ΊοΈ Knowledge Graph", "πŸ“Š Visualizations"])
72
 
73
+ # Tab 1: Results
74
  with tabs[0]:
75
+ st.header("πŸ“š Most Relevant Papers")
76
  for i, p in enumerate(results["papers"], 1):
77
+ st.markdown(f"**{i}. [{p['title']}]({p['link']})** \n*{p['authors']}* ({p['source']})")
78
  st.markdown(f"<div style='color:gray'>{p['summary']}</div>", unsafe_allow_html=True)
79
  if st.button("Save to Workspace"):
80
+ save_query(query, results)
81
+ st.success("Saved to workspace!")
82
  df = pd.DataFrame(results["papers"])
83
+ st.download_button("πŸ“₯ Download CSV", df.to_csv(index=False), "results.csv", "text/csv")
84
  pdf = generate_pdf(results["papers"])
85
+ st.download_button("πŸ“„ Download PDF", pdf, "results.pdf", "application/pdf")
86
  st.subheader("🧠 UMLS Concepts")
87
  for c in results["umls"]:
88
  if c.get("cui"):
89
+ st.markdown(f"- **{c['name']}** (CUI: `{c['cui']}`): {c.get('definition','No definition')}")
90
+ st.subheader("πŸ’Š Drug Safety (OpenFDA)")
91
  for d in results["drug_safety"]:
92
  st.json(d)
93
+ st.subheader("πŸ€– AI-Powered Summary")
94
  st.info(results["ai_summary"])
95
 
96
+ # Tab 2: Knowledge Graph (with Node Search Highlight)
97
  with tabs[1]:
98
+ st.header("πŸ—ΊοΈ Knowledge Graph Explorer")
99
+ search_term = st.text_input("πŸ”Ž Highlight nodes containing:", value="")
100
  try:
101
  nodes, edges, config = build_agraph(results["papers"], results["umls"], results["drug_safety"])
102
+ if search_term.strip():
103
+ pattern = re.compile(re.escape(search_term), re.IGNORECASE)
104
  for node in nodes:
105
+ if pattern.search(node.label) or (hasattr(node, "tooltip") and pattern.search(getattr(node, "tooltip", ""))):
106
+ node.color = "#f1c40f"
107
+ node.size = max(node.size, 30)
108
  else:
109
  node.color = "#ddd"
110
  agraph(nodes=nodes, edges=edges, config=config)
111
  except Exception as e:
112
+ st.warning("Knowledge graph unavailable: " + str(e))
113
 
114
+ # Tab 3: Visualizations
115
  with tabs[2]:
116
+ pub_years = [p["published"] for p in results["papers"] if p.get("published")]
117
+ if pub_years:
118
+ fig = px.histogram(pub_years, nbins=10, title="Publication Year Distribution")
119
+ st.plotly_chart(fig)
120
 
121
+ # Follow-up AI Q&A
122
  st.markdown("---")
123
+ st.subheader("πŸ’¬ Ask a Follow-up Question")
124
+ follow_up = st.text_input("Based on above:", placeholder="e.g. What's the most promising therapy?")
125
  if st.button("Ask AI"):
126
+ with st.spinner("Analyzing..."):
127
+ ai_ans = asyncio.run(answer_ai_question(follow_up, context=query))
128
+ st.write(ai_ans.get("answer", ai_ans))
129
 
130
  st.markdown("---")
131
+ st.caption("✨ Built by Oluwafemi Idiakhoa β€’ Powered by FastAPI, Streamlit, OpenAI, UMLS, OpenFDA, NCBI")
132
 
133
  if __name__ == "__main__":
134
  render_ui()