Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,57 +1,76 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def piv_tool(velocity, shear, hr):
|
| 4 |
v = "HIGH - stenosis risk" if float(velocity) > 2.0 else "NORMAL"
|
| 5 |
-
s = "HIGH - thrombosis risk" if float(shear) > 10 else "NORMAL"
|
| 6 |
-
return "Velocity: " + str(velocity) + " m/s - " + v + "\nShear: " + str(shear) + " Pa - " + s
|
| 7 |
|
| 8 |
def tgt_tool(tat, pf12, hemo, platelets, time):
|
| 9 |
risk = sum([float(tat)>15, float(pf12)>2.0, float(hemo)>50, float(platelets)<150])
|
| 10 |
overall = "HIGH THROMBOGENIC RISK" if risk>=3 else "MODERATE RISK" if risk>=2 else "LOW RISK"
|
| 11 |
-
return "TAT:" + str(tat) + "
|
| 12 |
|
| 13 |
def upad_tool(r, g, b):
|
| 14 |
creatinine = max(0, round(0.02*(float(r)-float(b))-0.5, 2))
|
| 15 |
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"
|
| 16 |
-
return "Creatinine: " + str(creatinine) + " mg/dL\
|
| 17 |
-
|
| 18 |
-
def ask(q):
|
| 19 |
-
answers = {
|
| 20 |
-
"sylgard": "Sylgard 184 is used in the MCL to create transparent test sections for PIV imaging. Mixed at 10:1 ratio, cured 48hrs.",
|
| 21 |
-
"tgt": "TGT uses Arduino Uno + Stepper Motor at 70 bpm. Blood sampled at 0,20,40,60 min. Measures TAT, PF1.2, hemolysis.",
|
| 22 |
-
"jaffe": "Jaffe reaction: creatinine + picric acid = orange-red color. Used in uPAD for CKD creatinine detection.",
|
| 23 |
-
"piv": "PIV = Particle Image Velocimetry. Green laser + high speed camera measures velocity and shear stress in MCL.",
|
| 24 |
-
"ckd": "CKD detected via uPAD Jaffe reaction. Normal creatinine < 1.2 mg/dL. CKD threshold > 1.5 mg/dL.",
|
| 25 |
-
"mhv": "Lab uses 27mm SJM Regent MHV. Tested in MCL for hemodynamics and TGT for thrombogenicity.",
|
| 26 |
-
}
|
| 27 |
-
for key, answer in answers.items():
|
| 28 |
-
if key in q.lower():
|
| 29 |
-
return answer
|
| 30 |
-
return "CardioLab AI covers MCL, PIV, TGT, uPAD, CKD, FSI, MHV. Ask about any topic!"
|
| 31 |
|
| 32 |
with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
| 33 |
gr.Markdown("# CardioLab AI Agent")
|
| 34 |
-
gr.Markdown("### SJSU Biomedical Engineering |
|
| 35 |
gr.Markdown("GitHub: github.com/pranatechsol/Cardio-Lab-Ai")
|
| 36 |
with gr.Tab("Ask Agent"):
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
gr.
|
|
|
|
| 40 |
with gr.Tab("PIV Analysis"):
|
|
|
|
| 41 |
v = gr.Number(label="Max Velocity m/s", value=1.8)
|
| 42 |
s = gr.Number(label="Shear Stress Pa", value=6.5)
|
| 43 |
h = gr.Number(label="Heart Rate bpm", value=72)
|
| 44 |
out = gr.Textbox(label="Result", lines=4)
|
| 45 |
gr.Button("Analyze PIV").click(piv_tool, inputs=[v,s,h], outputs=out)
|
| 46 |
with gr.Tab("TGT Results"):
|
|
|
|
| 47 |
t1 = gr.Number(label="TAT", value=18)
|
| 48 |
t2 = gr.Number(label="PF1.2", value=2.5)
|
| 49 |
-
t3 = gr.Number(label="Free Hemoglobin", value=60)
|
| 50 |
t4 = gr.Number(label="Platelet Count", value=140)
|
| 51 |
t5 = gr.Number(label="Time minutes", value=40)
|
| 52 |
out2 = gr.Textbox(label="Result", lines=5)
|
| 53 |
gr.Button("Analyze TGT").click(tgt_tool, inputs=[t1,t2,t3,t4,t5], outputs=out2)
|
| 54 |
with gr.Tab("uPAD CKD"):
|
|
|
|
| 55 |
r = gr.Number(label="R value", value=210)
|
| 56 |
g = gr.Number(label="G value", value=140)
|
| 57 |
b = gr.Number(label="B value", value=80)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 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 |
+
CardioLab SJSU Know-How:
|
| 10 |
+
MCL: Sylgard 184 at 10:1 ratio, 48hr cure, green laser PIV, 70bpm 5L/min flow
|
| 11 |
+
TGT: Arduino Uno + Stepper Motor, 150mL blood, sample at 0/20/40/60min, measure TAT PF1.2 hemolysis platelets
|
| 12 |
+
uPAD: Jaffe reaction creatinine + picric acid = orange-red, normal <1.2 mg/dL, CKD >1.5 mg/dL
|
| 13 |
+
FSI: COMSOL ALE mesh, blood density 1060 kg/m3, viscosity 0.0035 Pa.s, St Jude geometry
|
| 14 |
+
MHV: 27mm SJM Regent, bileaflet, trileaflet, monoleaflet, pediatric designs
|
| 15 |
+
CKD: Stages 1-5 by creatinine, uPAD image RGB analysis 64x64 pixel
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
def ask_biomni(question):
|
| 19 |
+
try:
|
| 20 |
+
response = client.chat.completions.create(
|
| 21 |
+
model="llama-3.3-70b-versatile",
|
| 22 |
+
messages=[
|
| 23 |
+
{"role": "system", "content": "You are CardioLab AI built on Biomni from Stanford SNAP Lab. Expert in SJSU Biomedical Engineering research. Always give detailed accurate answers.\n" + KNOWHOW},
|
| 24 |
+
{"role": "user", "content": question}
|
| 25 |
+
],
|
| 26 |
+
max_tokens=500
|
| 27 |
+
)
|
| 28 |
+
return response.choices[0].message.content
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return "Error: " + str(e)
|
| 31 |
|
| 32 |
def piv_tool(velocity, shear, hr):
|
| 33 |
v = "HIGH - stenosis risk" if float(velocity) > 2.0 else "NORMAL"
|
| 34 |
+
s = "HIGH - thrombosis risk" if float(shear) > 10 else "ELEVATED - monitor" if float(shear) > 5 else "NORMAL"
|
| 35 |
+
return "Velocity: " + str(velocity) + " m/s - " + v + "\nShear Stress: " + str(shear) + " Pa - " + s + "\nHeart Rate: " + str(hr) + " bpm"
|
| 36 |
|
| 37 |
def tgt_tool(tat, pf12, hemo, platelets, time):
|
| 38 |
risk = sum([float(tat)>15, float(pf12)>2.0, float(hemo)>50, float(platelets)<150])
|
| 39 |
overall = "HIGH THROMBOGENIC RISK" if risk>=3 else "MODERATE RISK" if risk>=2 else "LOW RISK"
|
| 40 |
+
return "TAT: " + str(tat) + "\nPF1.2: " + str(pf12) + "\nFree Hemoglobin: " + str(hemo) + "\nPlatelets: " + str(platelets) + "\nTime: " + str(time) + " min\nOverall: " + overall
|
| 41 |
|
| 42 |
def upad_tool(r, g, b):
|
| 43 |
creatinine = max(0, round(0.02*(float(r)-float(b))-0.5, 2))
|
| 44 |
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"
|
| 45 |
+
return "Creatinine: " + str(creatinine) + " mg/dL\nCKD Stage: " + stage + "\nConfirm with: Heska Element HT5"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
| 48 |
gr.Markdown("# CardioLab AI Agent")
|
| 49 |
+
gr.Markdown("### SJSU Biomedical Engineering | Built on Biomni (Stanford) | Powered by Llama 70B")
|
| 50 |
gr.Markdown("GitHub: github.com/pranatechsol/Cardio-Lab-Ai")
|
| 51 |
with gr.Tab("Ask Agent"):
|
| 52 |
+
gr.Markdown("Ask anything about MCL, PIV, TGT, uPAD, CKD, FSI, MHV")
|
| 53 |
+
q = gr.Textbox(label="Your question", placeholder="e.g. What is Sylgard 184 used for?")
|
| 54 |
+
a = gr.Textbox(label="Answer", lines=6)
|
| 55 |
+
gr.Button("Ask CardioLab AI").click(ask_biomni, inputs=q, outputs=a)
|
| 56 |
with gr.Tab("PIV Analysis"):
|
| 57 |
+
gr.Markdown("### Analyze PIV flow data from Mock Circulatory Loop")
|
| 58 |
v = gr.Number(label="Max Velocity m/s", value=1.8)
|
| 59 |
s = gr.Number(label="Shear Stress Pa", value=6.5)
|
| 60 |
h = gr.Number(label="Heart Rate bpm", value=72)
|
| 61 |
out = gr.Textbox(label="Result", lines=4)
|
| 62 |
gr.Button("Analyze PIV").click(piv_tool, inputs=[v,s,h], outputs=out)
|
| 63 |
with gr.Tab("TGT Results"):
|
| 64 |
+
gr.Markdown("### Interpret Thrombogenicity Tester blood results")
|
| 65 |
t1 = gr.Number(label="TAT", value=18)
|
| 66 |
t2 = gr.Number(label="PF1.2", value=2.5)
|
| 67 |
+
t3 = gr.Number(label="Free Hemoglobin mg/L", value=60)
|
| 68 |
t4 = gr.Number(label="Platelet Count", value=140)
|
| 69 |
t5 = gr.Number(label="Time minutes", value=40)
|
| 70 |
out2 = gr.Textbox(label="Result", lines=5)
|
| 71 |
gr.Button("Analyze TGT").click(tgt_tool, inputs=[t1,t2,t3,t4,t5], outputs=out2)
|
| 72 |
with gr.Tab("uPAD CKD"):
|
| 73 |
+
gr.Markdown("### Analyze uPAD colorimetric result - Jaffe Reaction")
|
| 74 |
r = gr.Number(label="R value", value=210)
|
| 75 |
g = gr.Number(label="G value", value=140)
|
| 76 |
b = gr.Number(label="B value", value=80)
|