Update app.py
Browse files
app.py
CHANGED
|
@@ -9,18 +9,17 @@ from pathlib import Path
|
|
| 9 |
import pandas as pd
|
| 10 |
from fpdf import FPDF
|
| 11 |
import plotly.express as px
|
| 12 |
-
import
|
| 13 |
|
| 14 |
from mcp.orchestrator import orchestrate_search, answer_ai_question
|
| 15 |
from mcp.schemas import UnifiedSearchInput, UnifiedSearchResult
|
| 16 |
from mcp.workspace import get_workspace, save_query
|
| 17 |
from mcp.knowledge_graph import build_agraph
|
|
|
|
| 18 |
|
| 19 |
-
# Paths
|
| 20 |
ROOT = Path(__file__).parent
|
| 21 |
LOGO = ROOT / "assets" / "logo.png"
|
| 22 |
|
| 23 |
-
# FastAPI setup
|
| 24 |
api = FastAPI(
|
| 25 |
title="MedGenesis MCP Server",
|
| 26 |
version="2.0.0",
|
|
@@ -36,7 +35,6 @@ async def unified_search_endpoint(data: UnifiedSearchInput):
|
|
| 36 |
async def ask_ai_endpoint(question: str, context: str = ""):
|
| 37 |
return await answer_ai_question(question, context)
|
| 38 |
|
| 39 |
-
# PDF Export
|
| 40 |
def generate_pdf(papers):
|
| 41 |
pdf = FPDF(); pdf.add_page(); pdf.set_font("Arial", size=12)
|
| 42 |
pdf.cell(200, 10, "MedGenesis AI - Search Results", ln=True, align="C")
|
|
@@ -48,7 +46,6 @@ def generate_pdf(papers):
|
|
| 48 |
pdf.ln(2)
|
| 49 |
return pdf.output(dest="S").encode("latin-1")
|
| 50 |
|
| 51 |
-
# UI
|
| 52 |
def render_ui():
|
| 53 |
st.set_page_config(page_title="MedGenesis AI", layout="wide")
|
| 54 |
|
|
@@ -114,12 +111,21 @@ def render_ui():
|
|
| 114 |
st.subheader("🤖 AI Summary")
|
| 115 |
st.info(results["ai_summary"])
|
| 116 |
|
| 117 |
-
# Tab 2: Knowledge Graph
|
| 118 |
with tabs[1]:
|
| 119 |
st.header("🗺️ Knowledge Graph Explorer")
|
|
|
|
| 120 |
try:
|
| 121 |
nodes, edges, config = build_agraph(results["papers"], results["umls"], results["drug_safety"])
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
agraph(nodes=nodes, edges=edges, config=config)
|
| 124 |
except Exception as e:
|
| 125 |
st.warning("Knowledge graph unavailable: " + str(e))
|
|
@@ -143,7 +149,6 @@ def render_ui():
|
|
| 143 |
st.markdown("---")
|
| 144 |
st.caption("✨ Built by Oluwafemi Idiakhoa • Powered by FastAPI, Streamlit, OpenAI, UMLS, OpenFDA, NCBI")
|
| 145 |
|
| 146 |
-
# Entry
|
| 147 |
if __name__ == "__main__":
|
| 148 |
import sys
|
| 149 |
if "runserver" in sys.argv:
|
|
|
|
| 9 |
import pandas as pd
|
| 10 |
from fpdf import FPDF
|
| 11 |
import plotly.express as px
|
| 12 |
+
import re
|
| 13 |
|
| 14 |
from mcp.orchestrator import orchestrate_search, answer_ai_question
|
| 15 |
from mcp.schemas import UnifiedSearchInput, UnifiedSearchResult
|
| 16 |
from mcp.workspace import get_workspace, save_query
|
| 17 |
from mcp.knowledge_graph import build_agraph
|
| 18 |
+
from streamlit_agraph import agraph, Node, Edge, Config
|
| 19 |
|
|
|
|
| 20 |
ROOT = Path(__file__).parent
|
| 21 |
LOGO = ROOT / "assets" / "logo.png"
|
| 22 |
|
|
|
|
| 23 |
api = FastAPI(
|
| 24 |
title="MedGenesis MCP Server",
|
| 25 |
version="2.0.0",
|
|
|
|
| 35 |
async def ask_ai_endpoint(question: str, context: str = ""):
|
| 36 |
return await answer_ai_question(question, context)
|
| 37 |
|
|
|
|
| 38 |
def generate_pdf(papers):
|
| 39 |
pdf = FPDF(); pdf.add_page(); pdf.set_font("Arial", size=12)
|
| 40 |
pdf.cell(200, 10, "MedGenesis AI - Search Results", ln=True, align="C")
|
|
|
|
| 46 |
pdf.ln(2)
|
| 47 |
return pdf.output(dest="S").encode("latin-1")
|
| 48 |
|
|
|
|
| 49 |
def render_ui():
|
| 50 |
st.set_page_config(page_title="MedGenesis AI", layout="wide")
|
| 51 |
|
|
|
|
| 111 |
st.subheader("🤖 AI Summary")
|
| 112 |
st.info(results["ai_summary"])
|
| 113 |
|
| 114 |
+
# Tab 2: Knowledge Graph with Search & Highlight
|
| 115 |
with tabs[1]:
|
| 116 |
st.header("🗺️ Knowledge Graph Explorer")
|
| 117 |
+
search_term = st.text_input("🔎 Highlight nodes containing:", value="")
|
| 118 |
try:
|
| 119 |
nodes, edges, config = build_agraph(results["papers"], results["umls"], results["drug_safety"])
|
| 120 |
+
# Highlight logic
|
| 121 |
+
if search_term.strip():
|
| 122 |
+
pattern = re.compile(re.escape(search_term), re.IGNORECASE)
|
| 123 |
+
for node in nodes:
|
| 124 |
+
if pattern.search(node.label) or (hasattr(node, "tooltip") and pattern.search(getattr(node, "tooltip", ""))):
|
| 125 |
+
node.color = "#f1c40f"
|
| 126 |
+
node.size = max(node.size, 30)
|
| 127 |
+
else:
|
| 128 |
+
node.color = "#ddd"
|
| 129 |
agraph(nodes=nodes, edges=edges, config=config)
|
| 130 |
except Exception as e:
|
| 131 |
st.warning("Knowledge graph unavailable: " + str(e))
|
|
|
|
| 149 |
st.markdown("---")
|
| 150 |
st.caption("✨ Built by Oluwafemi Idiakhoa • Powered by FastAPI, Streamlit, OpenAI, UMLS, OpenFDA, NCBI")
|
| 151 |
|
|
|
|
| 152 |
if __name__ == "__main__":
|
| 153 |
import sys
|
| 154 |
if "runserver" in sys.argv:
|