Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -176,7 +176,7 @@ HEADER = """<div style="background:linear-gradient(135deg,#0a0f2e 0%,#1a0a0a 100
|
|
| 176 |
<svg width="100" height="28" viewBox="0 0 120 32"><polyline points="0,16 20,16 26,4 30,28 34,2 38,26 44,16 120,16" fill="none" stroke="#c1121f" stroke-width="2.5" stroke-linecap="round"/></svg>
|
| 177 |
<div style="font-size:2em;font-weight:900;letter-spacing:2px;"><span style="color:#ffffff;">Cardio</span><span style="color:#c1121f;">Lab</span><span style="color:#ffffff;"> AI</span></div>
|
| 178 |
<svg width="100" height="28" viewBox="0 0 120 32" style="transform:scaleX(-1);"><polyline points="0,16 20,16 26,4 30,28 34,2 38,26 44,16 120,16" fill="none" stroke="#c1121f" stroke-width="2.5" stroke-linecap="round"/></svg></div>
|
| 179 |
-
<div style="color:#9ca3af;font-size:0.68em;letter-spacing:2px;text-transform:uppercase;">Fine-tuned
|
| 180 |
<div style="display:flex;align-items:center;gap:14px;">
|
| 181 |
<div style="text-align:right;"><div style="color:#9ca3af;font-size:0.68em;text-transform:uppercase;">Research Pillars</div>
|
| 182 |
<div style="color:#ffffff;font-size:0.72em;margin-top:3px;">MHV CKD FSI</div>
|
|
@@ -240,6 +240,170 @@ def get_pubmed_chat(query, n=3):
|
|
| 240 |
return chr(10).join(["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids]) if ids else ""
|
| 241 |
except: return ""
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
def research_chat(message, history, chat_model="Llama 3.3 70B (Best)"):
|
| 244 |
if not message.strip(): return "", history
|
| 245 |
paper_context, paper_results = search_papers(message, n=4)
|
|
@@ -593,14 +757,14 @@ with gr.Blocks(title="CardioLab AI - SJSU", css=CSS) as demo:
|
|
| 593 |
voice_clear.click(lambda: [], outputs=voice_chatbot)
|
| 594 |
|
| 595 |
with gr.Tab("Papers"):
|
| 596 |
-
gr.Markdown("### Search PubMed +
|
| 597 |
with gr.Row():
|
| 598 |
search_input = gr.Textbox(placeholder="e.g. bileaflet mechanical heart valve thrombogenicity hemodynamics", label="Research Topic", scale=3)
|
| 599 |
search_model_dd = gr.Dropdown(choices=list(CHAT_MODELS.keys()), value="Llama 3.3 70B (Best)", label="AI Model", scale=1)
|
| 600 |
search_btn = gr.Button("Search", variant="primary", scale=1)
|
| 601 |
search_output = gr.Textbox(label="Results", lines=22)
|
| 602 |
-
search_btn.click(
|
| 603 |
-
search_input.submit(
|
| 604 |
|
| 605 |
with gr.Tab("PIV CSV"):
|
| 606 |
with gr.Row():
|
|
@@ -668,6 +832,6 @@ with gr.Blocks(title="CardioLab AI - SJSU", css=CSS) as demo:
|
|
| 668 |
gr.Button("Analyze TGT",variant="primary").click(tgt_manual,inputs=[t1,t2,t3,t4,t5],outputs=out2)
|
| 669 |
|
| 670 |
gr.HTML("""<div style="text-align:center;padding:10px;border-top:1px solid #e5e7eb;background:#f9fafb;">
|
| 671 |
-
<span style="color:#9ca3af;font-size:0.75em;">CardioLab AI
|
| 672 |
|
| 673 |
demo.launch()
|
|
|
|
| 176 |
<svg width="100" height="28" viewBox="0 0 120 32"><polyline points="0,16 20,16 26,4 30,28 34,2 38,26 44,16 120,16" fill="none" stroke="#c1121f" stroke-width="2.5" stroke-linecap="round"/></svg>
|
| 177 |
<div style="font-size:2em;font-weight:900;letter-spacing:2px;"><span style="color:#ffffff;">Cardio</span><span style="color:#c1121f;">Lab</span><span style="color:#ffffff;"> AI</span></div>
|
| 178 |
<svg width="100" height="28" viewBox="0 0 120 32" style="transform:scaleX(-1);"><polyline points="0,16 20,16 26,4 30,28 34,2 38,26 44,16 120,16" fill="none" stroke="#c1121f" stroke-width="2.5" stroke-linecap="round"/></svg></div>
|
| 179 |
+
<div style="color:#9ca3af;font-size:0.68em;letter-spacing:2px;text-transform:uppercase;">RAG + Fine-tuned | BioGPT | ClinicalTrials | Weekly Updates | 5 AI Models</div></div>
|
| 180 |
<div style="display:flex;align-items:center;gap:14px;">
|
| 181 |
<div style="text-align:right;"><div style="color:#9ca3af;font-size:0.68em;text-transform:uppercase;">Research Pillars</div>
|
| 182 |
<div style="color:#ffffff;font-size:0.72em;margin-top:3px;">MHV CKD FSI</div>
|
|
|
|
| 240 |
return chr(10).join(["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids]) if ids else ""
|
| 241 |
except: return ""
|
| 242 |
|
| 243 |
+
|
| 244 |
+
# ββ PHASE C: BIOGPT + CLINICALTRIALS + WEEKLY UPDATE ββββββββββββββ
|
| 245 |
+
|
| 246 |
+
def search_biogpt(query):
|
| 247 |
+
"""Search BioGPT β trained on 15M PubMed papers via HuggingFace API"""
|
| 248 |
+
if not HF_TOKEN: return ""
|
| 249 |
+
try:
|
| 250 |
+
headers = {"Authorization": "Bearer "+HF_TOKEN}
|
| 251 |
+
# Use BioGPT for biomedical question answering
|
| 252 |
+
payload = {"inputs": query+" [SEP] Answer based on biomedical literature:"}
|
| 253 |
+
r = requests.post(
|
| 254 |
+
"https://api-inference.huggingface.co/models/microsoft/BioGPT-Large-PubMedQA",
|
| 255 |
+
headers=headers, json=payload, timeout=20
|
| 256 |
+
)
|
| 257 |
+
if r.status_code == 200:
|
| 258 |
+
result = r.json()
|
| 259 |
+
if isinstance(result, list) and len(result) > 0:
|
| 260 |
+
text = result[0].get("generated_text","")
|
| 261 |
+
# Extract just the answer part
|
| 262 |
+
if "[SEP]" in text:
|
| 263 |
+
text = text.split("[SEP]")[-1].strip()
|
| 264 |
+
return text[:400] if text else ""
|
| 265 |
+
return ""
|
| 266 |
+
except: return ""
|
| 267 |
+
|
| 268 |
+
def search_clinical_trials(query, n=5):
|
| 269 |
+
"""Search ClinicalTrials.gov for heart valve and CKD trials"""
|
| 270 |
+
try:
|
| 271 |
+
r = requests.get(
|
| 272 |
+
"https://clinicaltrials.gov/api/v2/studies",
|
| 273 |
+
params={
|
| 274 |
+
"query.term": query,
|
| 275 |
+
"filter.overallStatus": "RECRUITING|COMPLETED",
|
| 276 |
+
"pageSize": n,
|
| 277 |
+
"format": "json",
|
| 278 |
+
"fields": "NCTId,BriefTitle,OverallStatus,Phase,StartDate,Condition"
|
| 279 |
+
},
|
| 280 |
+
timeout=12
|
| 281 |
+
)
|
| 282 |
+
if r.status_code != 200: return []
|
| 283 |
+
studies = r.json().get("studies",[])
|
| 284 |
+
results = []
|
| 285 |
+
for s in studies:
|
| 286 |
+
proto = s.get("protocolSection",{})
|
| 287 |
+
ident = proto.get("identificationModule",{})
|
| 288 |
+
status = proto.get("statusModule",{})
|
| 289 |
+
nct = ident.get("nctId","")
|
| 290 |
+
title = ident.get("briefTitle","")
|
| 291 |
+
phase = status.get("phase","")
|
| 292 |
+
overall = status.get("overallStatus","")
|
| 293 |
+
if nct and title:
|
| 294 |
+
results.append({
|
| 295 |
+
"nct": nct,
|
| 296 |
+
"title": title,
|
| 297 |
+
"status": overall,
|
| 298 |
+
"phase": phase,
|
| 299 |
+
"url": "https://clinicaltrials.gov/study/"+nct
|
| 300 |
+
})
|
| 301 |
+
return results
|
| 302 |
+
except: return []
|
| 303 |
+
|
| 304 |
+
def get_weekly_pubmed_update(topics=None):
|
| 305 |
+
"""Get papers published in last 7 days on CardioLab topics"""
|
| 306 |
+
if topics is None:
|
| 307 |
+
topics = [
|
| 308 |
+
"mechanical heart valve thrombogenicity",
|
| 309 |
+
"microfluidic creatinine CKD diagnosis",
|
| 310 |
+
"PIV hemodynamics prosthetic valve",
|
| 311 |
+
"Mock Circulatory Loop cardiac",
|
| 312 |
+
"bileaflet valve fluid structure interaction"
|
| 313 |
+
]
|
| 314 |
+
all_new = []
|
| 315 |
+
try:
|
| 316 |
+
from datetime import datetime, timedelta
|
| 317 |
+
week_ago = (datetime.now() - timedelta(days=7)).strftime("%Y/%m/%d")
|
| 318 |
+
for topic in topics:
|
| 319 |
+
r = requests.get(
|
| 320 |
+
"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi",
|
| 321 |
+
params={
|
| 322 |
+
"db":"pubmed",
|
| 323 |
+
"term":topic,
|
| 324 |
+
"mindate":week_ago,
|
| 325 |
+
"datetype":"pdat",
|
| 326 |
+
"retmax":3,
|
| 327 |
+
"retmode":"json",
|
| 328 |
+
"sort":"date"
|
| 329 |
+
},
|
| 330 |
+
timeout=10
|
| 331 |
+
)
|
| 332 |
+
ids = r.json()["esearchresult"]["idlist"]
|
| 333 |
+
for pmid in ids:
|
| 334 |
+
all_new.append({
|
| 335 |
+
"pmid": pmid,
|
| 336 |
+
"topic": topic,
|
| 337 |
+
"url": "https://pubmed.ncbi.nlm.nih.gov/"+pmid
|
| 338 |
+
})
|
| 339 |
+
return all_new
|
| 340 |
+
except: return []
|
| 341 |
+
|
| 342 |
+
def full_research_search(query, search_model="Llama 3.3 70B (Best)"):
|
| 343 |
+
"""Complete search across ALL sources including Phase C additions"""
|
| 344 |
+
if not query.strip(): return "Please enter a research topic."
|
| 345 |
+
|
| 346 |
+
model_id = CHAT_MODELS.get(search_model, "llama-3.3-70b-versatile")
|
| 347 |
+
expanded = expand_query_ai(query, model_id) if GROQ_KEY else query
|
| 348 |
+
|
| 349 |
+
# All search sources
|
| 350 |
+
pubmed = fetch_pubmed(expanded, n=6)
|
| 351 |
+
scholar = fetch_scholar(expanded, n=5)
|
| 352 |
+
europe = fetch_europe_pmc(expanded, n=4)
|
| 353 |
+
trials = search_clinical_trials(query, n=4)
|
| 354 |
+
weekly = get_weekly_pubmed_update()
|
| 355 |
+
biogpt_answer = search_biogpt(query)
|
| 356 |
+
|
| 357 |
+
# Format output
|
| 358 |
+
out = "QUERY: "+query+chr(10)
|
| 359 |
+
out += "AI EXPANDED: "+expanded+chr(10)
|
| 360 |
+
out += "SOURCES: PubMed + Scholar + EuropePMC + ClinicalTrials + SJSU + BioGPT"+chr(10)
|
| 361 |
+
out += "="*50+chr(10)+chr(10)
|
| 362 |
+
|
| 363 |
+
# BioGPT answer first
|
| 364 |
+
if biogpt_answer:
|
| 365 |
+
out += "BIOGPT ANSWER (trained on 15M PubMed papers):"+chr(10)
|
| 366 |
+
out += biogpt_answer+chr(10)+chr(10)
|
| 367 |
+
out += "="*50+chr(10)+chr(10)
|
| 368 |
+
|
| 369 |
+
# PubMed results
|
| 370 |
+
if pubmed:
|
| 371 |
+
out += "PUBMED ("+str(len(pubmed))+" papers):"+chr(10)
|
| 372 |
+
for p in pubmed[:6]:
|
| 373 |
+
out += p["title"][:85]+" ("+p["year"]+")"+chr(10)
|
| 374 |
+
out += " "+p["url"]+chr(10)+chr(10)
|
| 375 |
+
|
| 376 |
+
# Scholar results
|
| 377 |
+
if scholar:
|
| 378 |
+
out += "SEMANTIC SCHOLAR ("+str(len(scholar))+" papers):"+chr(10)
|
| 379 |
+
for p in scholar[:5]:
|
| 380 |
+
out += p["title"][:85]+" ("+p["year"]+")"
|
| 381 |
+
if p["citations"] not in ("N/A","","0"): out += " | "+p["citations"]+" citations"
|
| 382 |
+
out += chr(10)+" "+p["url"]+chr(10)+chr(10)
|
| 383 |
+
|
| 384 |
+
# Clinical trials
|
| 385 |
+
if trials:
|
| 386 |
+
out += "CLINICALTRIALS.GOV ("+str(len(trials))+" trials):"+chr(10)
|
| 387 |
+
for t in trials:
|
| 388 |
+
out += t["title"][:80]+" | "+t["status"]+" | "+t.get("phase","")+" "+chr(10)
|
| 389 |
+
out += " "+t["url"]+chr(10)+chr(10)
|
| 390 |
+
|
| 391 |
+
# Weekly updates
|
| 392 |
+
weekly_relevant = [w for w in weekly if any(
|
| 393 |
+
kw in query.lower() for kw in ["valve","heart","ckd","creatinine","piv","tgt","mcl"]
|
| 394 |
+
)]
|
| 395 |
+
if weekly_relevant:
|
| 396 |
+
out += "NEW THIS WEEK (last 7 days):"+chr(10)
|
| 397 |
+
for w in weekly_relevant[:5]:
|
| 398 |
+
out += " "+w["url"]+" ["+w["topic"][:40]+"]"+chr(10)
|
| 399 |
+
|
| 400 |
+
# SJSU ScholarWorks
|
| 401 |
+
out += chr(10)+"SJSU SCHOLARWORKS:"+chr(10)
|
| 402 |
+
out += " https://scholarworks.sjsu.edu/do/search/?q="+requests.utils.quote(query)+"&context=6781027"
|
| 403 |
+
|
| 404 |
+
return out
|
| 405 |
+
|
| 406 |
+
|
| 407 |
def research_chat(message, history, chat_model="Llama 3.3 70B (Best)"):
|
| 408 |
if not message.strip(): return "", history
|
| 409 |
paper_context, paper_results = search_papers(message, n=4)
|
|
|
|
| 757 |
voice_clear.click(lambda: [], outputs=voice_chatbot)
|
| 758 |
|
| 759 |
with gr.Tab("Papers"):
|
| 760 |
+
gr.Markdown("### Search PubMed + Scholar + EuropePMC + ClinicalTrials.gov + SJSU + BioGPT (15M papers)")
|
| 761 |
with gr.Row():
|
| 762 |
search_input = gr.Textbox(placeholder="e.g. bileaflet mechanical heart valve thrombogenicity hemodynamics", label="Research Topic", scale=3)
|
| 763 |
search_model_dd = gr.Dropdown(choices=list(CHAT_MODELS.keys()), value="Llama 3.3 70B (Best)", label="AI Model", scale=1)
|
| 764 |
search_btn = gr.Button("Search", variant="primary", scale=1)
|
| 765 |
search_output = gr.Textbox(label="Results", lines=22)
|
| 766 |
+
search_btn.click(full_research_search, inputs=[search_input, search_model_dd], outputs=search_output)
|
| 767 |
+
search_input.submit(full_research_search, inputs=[search_input, search_model_dd], outputs=search_output)
|
| 768 |
|
| 769 |
with gr.Tab("PIV CSV"):
|
| 770 |
with gr.Row():
|
|
|
|
| 832 |
gr.Button("Analyze TGT",variant="primary").click(tgt_manual,inputs=[t1,t2,t3,t4,t5],outputs=out2)
|
| 833 |
|
| 834 |
gr.HTML("""<div style="text-align:center;padding:10px;border-top:1px solid #e5e7eb;background:#f9fafb;">
|
| 835 |
+
<span style="color:#9ca3af;font-size:0.75em;">CardioLab AI v37 | SJSU Biomedical Engineering | Fine-tuned on 16 SJSU Papers | RAG + Custom Model | Inspired by <a href="https://github.com/snap-stanford/Biomni" style="color:#c1121f;">Biomni Stanford</a> | <a href="https://github.com/pranatechsol/Cardio-Lab-Ai" style="color:#0057a8;">GitHub</a> | Apache 2.0 | $0 Cost</span></div>""")
|
| 836 |
|
| 837 |
demo.launch()
|