# app.py import os import streamlit as st import asyncio from pathlib import Path import pandas as pd from fpdf import FPDF import plotly.express as px import re from mcp.orchestrator import orchestrate_search, answer_ai_question from mcp.schemas import UnifiedSearchInput, UnifiedSearchResult from mcp.workspace import get_workspace, save_query from mcp.knowledge_graph import build_agraph from streamlit_agraph import agraph ROOT = Path(__file__).parent LOGO = ROOT / "assets" / "logo.png" def generate_pdf(papers): pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) pdf.cell(200, 10, "MedGenesis AI - Search Results", ln=True, align="C") pdf.ln(10) for i, p in enumerate(papers, 1): pdf.set_font("Arial", "B", 12) pdf.multi_cell(0, 10, f"{i}. {p['title']}") pdf.set_font("Arial", "", 10) pdf.multi_cell(0, 8, f"Authors: {p['authors']}\nLink: {p['link']}\nSummary: {p['summary']}\n") pdf.ln(2) return pdf.output(dest="S").encode("latin-1") def render_ui(): st.set_page_config(page_title="MedGenesis AI", layout="wide") # Workspace in sidebar with st.sidebar: st.header("ποΈ Workspace") ws = get_workspace() if ws: for i, item in enumerate(ws, 1): with st.expander(f"{i}. {item['query']}"): st.write("**AI Summary:**", item["result"]["ai_summary"]) st.write("**Top Paper:**", item["result"]["papers"][0]["title"] if item["result"]["papers"] else "None") df = pd.DataFrame(item["result"]["papers"]) st.download_button("π₯ CSV", df.to_csv(index=False), f"query_{i}.csv", "text/csv") else: st.info("Run & save searches here.") # Header and logo col1, col2 = st.columns([0.15, 0.85]) with col1: if LOGO.exists(): st.image(str(LOGO), width=100) with col2: st.markdown("## 𧬠MedGenesis AI\n*Unified Intelligence from PubMed, ArXiv, OpenFDA, UMLS, GPTβ4o*") st.caption("Built by Oluwafemi Idiakhoa β’ Hugging Face Spaces") st.markdown("---") query = st.text_input("π Enter biomedical research question:", placeholder="e.g. CRISPR treatments for glioblastoma") results = None if st.button("Run Search π"): with st.spinner("Gathering and analyzing data..."): results = asyncio.run(orchestrate_search(query)) st.success("β Search complete") if results: tabs = st.tabs(["π Results", "πΊοΈ Knowledge Graph", "π Visualizations"]) # Tab 1: Results with tabs[0]: st.header("π Most Relevant Papers") for i, p in enumerate(results["papers"], 1): st.markdown(f"**{i}. [{p['title']}]({p['link']})** \n*{p['authors']}* ({p['source']})") st.markdown(f"