Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -3,19 +3,18 @@ import os, requests, glob
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
GROQ_KEY = os.environ.get("GROQ_API_KEY","")
|
|
|
|
| 6 |
client = Groq(api_key=GROQ_KEY)
|
| 7 |
|
| 8 |
KNOWHOW = """
|
| 9 |
SJSU CardioLab Know-How:
|
| 10 |
-
MCL:
|
| 11 |
-
TGT:
|
| 12 |
-
uPAD:
|
| 13 |
-
FSI: COMSOL
|
| 14 |
-
MHV: 27mm SJM Regent bileaflet
|
| 15 |
-
CKD Stages:
|
| 16 |
-
Equipment: Heska
|
| 17 |
-
Acoustic detection: microphone on valve housing detects clot formation via frequency shift
|
| 18 |
-
Projects: 13 total, 3 pillars - MHV hemodynamics, CKD diagnostics, FSI simulations
|
| 19 |
"""
|
| 20 |
|
| 21 |
def search_pubmed(query, n=3):
|
|
@@ -39,7 +38,7 @@ def search_pubmed(query, n=3):
|
|
| 39 |
if isinstance(abstract, list): abstract = " ".join([str(x) for x in abstract])
|
| 40 |
if isinstance(abstract, dict): abstract = str(abstract.get("#text",""))
|
| 41 |
pmid = str(c["PMID"]["#text"] if isinstance(c["PMID"],dict) else c["PMID"])
|
| 42 |
-
out.append("[PubMed: "
|
| 43 |
except: continue
|
| 44 |
return "\n\n".join(out)
|
| 45 |
except: return ""
|
|
@@ -51,22 +50,50 @@ def search_scholar(query, n=3):
|
|
| 51 |
papers = r.json().get("data",[])
|
| 52 |
out = []
|
| 53 |
for p in papers:
|
| 54 |
-
out.append("[Scholar "
|
| 55 |
return "\n\n".join(out)
|
| 56 |
except: return ""
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
def ask_agent(question):
|
| 59 |
if not GROQ_KEY:
|
| 60 |
-
return "Error: GROQ_API_KEY not set
|
| 61 |
cardio_query = question + " AND (mechanical heart valve OR microfluidic OR CKD creatinine OR PIV hemodynamics OR thrombogenicity)"
|
| 62 |
pubmed = search_pubmed(cardio_query, n=3)
|
| 63 |
scholar = search_scholar(question + " biomedical", n=3)
|
| 64 |
-
|
|
|
|
| 65 |
response = client.chat.completions.create(
|
| 66 |
model="llama-3.3-70b-versatile",
|
| 67 |
messages=[
|
| 68 |
-
{"role":"system","content":"You are CardioLab AI built on Biomni from Stanford SNAP Lab. Expert in SJSU Biomedical Engineering. Always cite sources.\n\nCARDIOLAB KNOW-HOW:\n" + KNOWHOW},
|
| 69 |
-
{"role":"user","content":"Research question: " + question + "\n\
|
| 70 |
],
|
| 71 |
max_tokens=800
|
| 72 |
)
|
|
@@ -80,21 +107,21 @@ def piv_tool(velocity, shear, hr):
|
|
| 80 |
def tgt_tool(tat, pf12, hemo, platelets, time):
|
| 81 |
risk = sum([float(tat)>15, float(pf12)>2.0, float(hemo)>50, float(platelets)<150])
|
| 82 |
overall = "HIGH THROMBOGENIC RISK" if risk>=3 else "MODERATE RISK" if risk>=2 else "LOW RISK"
|
| 83 |
-
return "TAT:
|
| 84 |
|
| 85 |
def upad_tool(r, g, b):
|
| 86 |
creatinine = max(0, round(0.02*(float(r)-float(b))-0.5, 2))
|
| 87 |
stage = "Normal" if creatinine<1.2 else "Borderline" if creatinine<1.5 else "Stage 2 CKD" if creatinine<3.0 else "Stage 3-4 CKD" if creatinine<6.0 else "Stage 5 CKD"
|
| 88 |
-
return "Creatinine: "+str(creatinine)+" mg/dL\
|
| 89 |
|
| 90 |
with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
| 91 |
gr.Markdown("# CardioLab AI Agent")
|
| 92 |
-
gr.Markdown("### SJSU Biomedical Engineering |
|
| 93 |
-
gr.Markdown("
|
| 94 |
with gr.Tab("Research Assistant"):
|
| 95 |
-
gr.Markdown("###
|
| 96 |
-
q = gr.Textbox(label="Research question", placeholder="e.g.
|
| 97 |
-
a = gr.Textbox(label="Answer with citations", lines=
|
| 98 |
gr.Button("Search & Answer").click(ask_agent, inputs=q, outputs=a)
|
| 99 |
with gr.Tab("PIV Analysis"):
|
| 100 |
gr.Markdown("### Analyze PIV flow data from Mock Circulatory Loop")
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
GROQ_KEY = os.environ.get("GROQ_API_KEY","")
|
| 6 |
+
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN","")
|
| 7 |
client = Groq(api_key=GROQ_KEY)
|
| 8 |
|
| 9 |
KNOWHOW = """
|
| 10 |
SJSU CardioLab Know-How:
|
| 11 |
+
MCL: Sylgard 184 PDMS 10:1 ratio 48hr cure, green laser PIV, 70bpm 5L/min flow
|
| 12 |
+
TGT: Arduino Uno + Stepper Motor, 150mL blood, sample 0/20/40/60min, TAT PF1.2 hemolysis platelets
|
| 13 |
+
uPAD: Jaffe reaction creatinine + picric acid = orange-red, normal 0.6-1.2 mg/dL, CKD above 1.5
|
| 14 |
+
FSI: COMSOL ALE mesh, blood 1060 kg/m3, 0.0035 Pa.s, St Jude geometry
|
| 15 |
+
MHV: 27mm SJM Regent, bileaflet trileaflet monoleaflet pediatric
|
| 16 |
+
CKD Stages: 1 below 1.5, 2 1.5-3.0, 3-4 3.0-6.0, 5 above 6.0 mg/dL
|
| 17 |
+
Equipment: Heska HT5, time-resolved PIV, Tygon tubing, Arduino
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
|
| 20 |
def search_pubmed(query, n=3):
|
|
|
|
| 38 |
if isinstance(abstract, list): abstract = " ".join([str(x) for x in abstract])
|
| 39 |
if isinstance(abstract, dict): abstract = str(abstract.get("#text",""))
|
| 40 |
pmid = str(c["PMID"]["#text"] if isinstance(c["PMID"],dict) else c["PMID"])
|
| 41 |
+
out.append("[PubMed: "+title[:80]+"]\n"+str(abstract)[:300]+"\nURL: https://pubmed.ncbi.nlm.nih.gov/"+pmid)
|
| 42 |
except: continue
|
| 43 |
return "\n\n".join(out)
|
| 44 |
except: return ""
|
|
|
|
| 50 |
papers = r.json().get("data",[])
|
| 51 |
out = []
|
| 52 |
for p in papers:
|
| 53 |
+
out.append("[Scholar "+str(p.get("year",""))+": "+p.get("title","")[:80]+"]\n"+(p.get("abstract") or "")[:300]+"\nURL: "+p.get("url",""))
|
| 54 |
return "\n\n".join(out)
|
| 55 |
except: return ""
|
| 56 |
|
| 57 |
+
def search_web_realtime(query):
|
| 58 |
+
if not GITHUB_TOKEN:
|
| 59 |
+
return ""
|
| 60 |
+
try:
|
| 61 |
+
headers = {
|
| 62 |
+
"Authorization": "Bearer " + GITHUB_TOKEN,
|
| 63 |
+
"Content-Type": "application/json"
|
| 64 |
+
}
|
| 65 |
+
payload = {
|
| 66 |
+
"messages": [{"role": "user", "content": query}],
|
| 67 |
+
"model": "gpt-4o",
|
| 68 |
+
"tools": [{"type": "bing_search"}]
|
| 69 |
+
}
|
| 70 |
+
r = requests.post(
|
| 71 |
+
"https://api.githubcopilot.com/chat/completions",
|
| 72 |
+
headers=headers,
|
| 73 |
+
json=payload,
|
| 74 |
+
timeout=15
|
| 75 |
+
)
|
| 76 |
+
if r.status_code == 200:
|
| 77 |
+
data = r.json()
|
| 78 |
+
content = data["choices"][0]["message"]["content"]
|
| 79 |
+
return "[Real-time Web Search]\n" + str(content)[:600]
|
| 80 |
+
return ""
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return ""
|
| 83 |
+
|
| 84 |
def ask_agent(question):
|
| 85 |
if not GROQ_KEY:
|
| 86 |
+
return "Error: GROQ_API_KEY not set."
|
| 87 |
cardio_query = question + " AND (mechanical heart valve OR microfluidic OR CKD creatinine OR PIV hemodynamics OR thrombogenicity)"
|
| 88 |
pubmed = search_pubmed(cardio_query, n=3)
|
| 89 |
scholar = search_scholar(question + " biomedical", n=3)
|
| 90 |
+
web = search_web_realtime("Latest research on: " + question + " biomedical engineering 2024 2025")
|
| 91 |
+
all_sources = pubmed + "\n\n" + scholar + "\n\n" + web
|
| 92 |
response = client.chat.completions.create(
|
| 93 |
model="llama-3.3-70b-versatile",
|
| 94 |
messages=[
|
| 95 |
+
{"role":"system","content":"You are CardioLab AI built on Biomni from Stanford SNAP Lab. Expert in SJSU Biomedical Engineering. Always cite sources with URLs when available.\n\nCARDIOLAB KNOW-HOW:\n" + KNOWHOW},
|
| 96 |
+
{"role":"user","content":"Research question: " + question + "\n\nSources found:\n" + all_sources[:4000]}
|
| 97 |
],
|
| 98 |
max_tokens=800
|
| 99 |
)
|
|
|
|
| 107 |
def tgt_tool(tat, pf12, hemo, platelets, time):
|
| 108 |
risk = sum([float(tat)>15, float(pf12)>2.0, float(hemo)>50, float(platelets)<150])
|
| 109 |
overall = "HIGH THROMBOGENIC RISK" if risk>=3 else "MODERATE RISK" if risk>=2 else "LOW RISK"
|
| 110 |
+
return "TAT:"+str(tat)+" PF1.2:"+str(pf12)+" Hemo:"+str(hemo)+" Platelets:"+str(platelets)+"\nTime: "+str(time)+" min\nResult: "+overall
|
| 111 |
|
| 112 |
def upad_tool(r, g, b):
|
| 113 |
creatinine = max(0, round(0.02*(float(r)-float(b))-0.5, 2))
|
| 114 |
stage = "Normal" if creatinine<1.2 else "Borderline" if creatinine<1.5 else "Stage 2 CKD" if creatinine<3.0 else "Stage 3-4 CKD" if creatinine<6.0 else "Stage 5 CKD"
|
| 115 |
+
return "Creatinine: "+str(creatinine)+" mg/dL\nStage: "+stage+"\nConfirm with: Heska Element HT5"
|
| 116 |
|
| 117 |
with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
| 118 |
gr.Markdown("# CardioLab AI Agent")
|
| 119 |
+
gr.Markdown("### SJSU Biomedical Engineering | Biomni + Llama 70B + PubMed + Web Search")
|
| 120 |
+
gr.Markdown("GitHub: github.com/pranatechsol/Cardio-Lab-Ai")
|
| 121 |
with gr.Tab("Research Assistant"):
|
| 122 |
+
gr.Markdown("### Searches CardioLab papers + PubMed + Semantic Scholar + Real-time Web")
|
| 123 |
+
q = gr.Textbox(label="Research question", placeholder="e.g. Latest methods for MHV thrombogenicity detection 2025")
|
| 124 |
+
a = gr.Textbox(label="Answer with citations", lines=12)
|
| 125 |
gr.Button("Search & Answer").click(ask_agent, inputs=q, outputs=a)
|
| 126 |
with gr.Tab("PIV Analysis"):
|
| 127 |
gr.Markdown("### Analyze PIV flow data from Mock Circulatory Loop")
|