Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 7 |
from mcp.orchestrator import orchestrate_search, answer_ai_question
|
| 8 |
from mcp.schemas import UnifiedSearchInput, UnifiedSearchResult
|
| 9 |
from pathlib import Path
|
|
|
|
|
|
|
| 10 |
import asyncio
|
| 11 |
|
| 12 |
ROOT_DIR = Path(__file__).resolve().parent
|
|
@@ -36,6 +38,23 @@ 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 |
# --- STREAMLIT UI ---
|
| 40 |
|
| 41 |
def render_ui():
|
|
@@ -61,37 +80,60 @@ def render_ui():
|
|
| 61 |
st.subheader("π Unified Semantic Search")
|
| 62 |
query = st.text_input("Enter your biomedical research question:", placeholder="e.g. New treatments for glioblastoma using CRISPR")
|
| 63 |
|
|
|
|
| 64 |
if st.button("Run Search π"):
|
| 65 |
with st.spinner("Thinking... Gathering and analyzing data across 5 systems..."):
|
| 66 |
results = asyncio.run(orchestrate_search(query))
|
| 67 |
st.success("Search complete! π")
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# Follow-up AI Q&A
|
| 97 |
st.markdown("---")
|
|
|
|
| 7 |
from mcp.orchestrator import orchestrate_search, answer_ai_question
|
| 8 |
from mcp.schemas import UnifiedSearchInput, UnifiedSearchResult
|
| 9 |
from pathlib import Path
|
| 10 |
+
import pandas as pd
|
| 11 |
+
from fpdf import FPDF
|
| 12 |
import asyncio
|
| 13 |
|
| 14 |
ROOT_DIR = Path(__file__).resolve().parent
|
|
|
|
| 38 |
async def ask_ai_endpoint(question: str, context: str = ""):
|
| 39 |
return await answer_ai_question(question, context)
|
| 40 |
|
| 41 |
+
# --- PDF Export Utility ---
|
| 42 |
+
|
| 43 |
+
def generate_pdf(papers):
|
| 44 |
+
pdf = FPDF()
|
| 45 |
+
pdf.add_page()
|
| 46 |
+
pdf.set_font("Arial", size=12)
|
| 47 |
+
pdf.cell(200, 10, txt="MedGenesis AI - Search Results", ln=True, align='C')
|
| 48 |
+
pdf.ln(10)
|
| 49 |
+
for i, paper in enumerate(papers, 1):
|
| 50 |
+
pdf.set_font("Arial", style="B", size=12)
|
| 51 |
+
pdf.multi_cell(0, 10, f"{i}. {paper['title']}")
|
| 52 |
+
pdf.set_font("Arial", style="", size=10)
|
| 53 |
+
pdf.multi_cell(0, 8, f"Authors: {paper['authors']}\nLink: {paper['link']}\nSummary: {paper['summary']}\n")
|
| 54 |
+
pdf.ln(2)
|
| 55 |
+
pdf_out = pdf.output(dest='S').encode('latin-1')
|
| 56 |
+
return pdf_out
|
| 57 |
+
|
| 58 |
# --- STREAMLIT UI ---
|
| 59 |
|
| 60 |
def render_ui():
|
|
|
|
| 80 |
st.subheader("π Unified Semantic Search")
|
| 81 |
query = st.text_input("Enter your biomedical research question:", placeholder="e.g. New treatments for glioblastoma using CRISPR")
|
| 82 |
|
| 83 |
+
results = None
|
| 84 |
if st.button("Run Search π"):
|
| 85 |
with st.spinner("Thinking... Gathering and analyzing data across 5 systems..."):
|
| 86 |
results = asyncio.run(orchestrate_search(query))
|
| 87 |
st.success("Search complete! π")
|
| 88 |
|
| 89 |
+
if results:
|
| 90 |
+
# Papers
|
| 91 |
+
st.markdown("### π Most Relevant Papers")
|
| 92 |
+
for i, paper in enumerate(results["papers"], 1):
|
| 93 |
+
st.markdown(f"**{i}. [{paper['title']}]({paper['link']})** \n*{paper['authors']}* ({paper['source']})")
|
| 94 |
+
st.markdown(f"<div style='font-size: 0.9em; color: gray'>{paper['summary']}</div>", unsafe_allow_html=True)
|
| 95 |
+
|
| 96 |
+
# Export as CSV
|
| 97 |
+
if results["papers"]:
|
| 98 |
+
df = pd.DataFrame(results["papers"])
|
| 99 |
+
csv = df.to_csv(index=False)
|
| 100 |
+
st.download_button(
|
| 101 |
+
label="π₯ Download results as CSV",
|
| 102 |
+
data=csv,
|
| 103 |
+
file_name="medgenesis_results.csv",
|
| 104 |
+
mime="text/csv",
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
# Export as PDF
|
| 108 |
+
if results["papers"]:
|
| 109 |
+
pdf_bytes = generate_pdf(results["papers"])
|
| 110 |
+
st.download_button(
|
| 111 |
+
label="π Download results as PDF",
|
| 112 |
+
data=pdf_bytes,
|
| 113 |
+
file_name="medgenesis_results.pdf",
|
| 114 |
+
mime="application/pdf",
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# UMLS Concepts
|
| 118 |
+
st.markdown("### π§ Biomedical Concept Enrichment (UMLS)")
|
| 119 |
+
for concept in results["umls"]:
|
| 120 |
+
if concept["cui"]:
|
| 121 |
+
st.markdown(f"πΉ **{concept['name']}** (CUI: `{concept['cui']}`): {concept['definition'] or 'No definition available'}")
|
| 122 |
+
|
| 123 |
+
# Drug Safety
|
| 124 |
+
st.markdown("### π Drug Safety Insights (OpenFDA)")
|
| 125 |
+
for drug_report in results["drug_safety"]:
|
| 126 |
+
if drug_report:
|
| 127 |
+
st.json(drug_report)
|
| 128 |
+
|
| 129 |
+
# AI Summary
|
| 130 |
+
st.markdown("### π€ AI-Powered Summary")
|
| 131 |
+
st.info(results["ai_summary"])
|
| 132 |
+
|
| 133 |
+
# Suggested Reading
|
| 134 |
+
st.markdown("### π Suggested Links")
|
| 135 |
+
for link in results["suggested_reading"]:
|
| 136 |
+
st.write(f"- {link}")
|
| 137 |
|
| 138 |
# Follow-up AI Q&A
|
| 139 |
st.markdown("---")
|