Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,19 @@
|
|
| 1 |
-
|
| 2 |
-
from huggingface_hub import HfApi
|
| 3 |
-
import os
|
| 4 |
-
|
| 5 |
-
hf_token = userdata.get('HF_TOKEN').strip()
|
| 6 |
-
api = HfApi(token=hf_token)
|
| 7 |
-
repo_id = 'Saicharan21/CardioLab-AI'
|
| 8 |
-
|
| 9 |
-
# Write clean app
|
| 10 |
-
app = """import gradio as gr
|
| 11 |
import os
|
| 12 |
import requests
|
| 13 |
from groq import Groq
|
| 14 |
|
| 15 |
GROQ_KEY = os.environ.get("GROQ_API_KEY", "")
|
| 16 |
|
| 17 |
-
KNOWHOW =
|
| 18 |
-
TGT uses Arduino Uno Stepper Motor 150mL blood sampled at 0 20 40 60min measures TAT PF1.2 hemolysis platelets.
|
| 19 |
-
uPAD uses Jaffe reaction creatinine plus picric acid gives orange-red color normal 0.6-1.2 mg/dL CKD above 1.5.
|
| 20 |
-
FSI uses COMSOL ALE mesh blood 1060 kg/m3 0.0035 Pa.s St Jude geometry.
|
| 21 |
-
MHV is 27mm SJM Regent bileaflet also trileaflet monoleaflet pediatric designs.
|
| 22 |
-
Equipment: Heska HT5 hematology analyzer time-resolved PIV Tygon tubing Arduino Uno.\"\"\"
|
| 23 |
|
| 24 |
def get_pubmed(query):
|
| 25 |
try:
|
| 26 |
-
r = requests.get(
|
|
|
|
| 27 |
params={"db":"pubmed","term":query+" mechanical heart valve OR microfluidic OR CKD","retmax":3,"retmode":"json","sort":"date"},
|
| 28 |
-
timeout=8
|
|
|
|
| 29 |
ids = r.json()["esearchresult"]["idlist"]
|
| 30 |
if not ids:
|
| 31 |
return ""
|
|
@@ -37,20 +25,29 @@ def get_pubmed(query):
|
|
| 37 |
def respond(message, history):
|
| 38 |
if not GROQ_KEY:
|
| 39 |
history.append({"role":"user","content":message})
|
| 40 |
-
history.append({"role":"assistant","content":"Error: Add GROQ_API_KEY to Space
|
| 41 |
return "", history
|
| 42 |
try:
|
| 43 |
client = Groq(api_key=GROQ_KEY)
|
| 44 |
-
msgs = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
for msg in history:
|
| 46 |
if isinstance(msg, dict):
|
| 47 |
msgs.append({"role":msg["role"],"content":msg["content"]})
|
| 48 |
pubmed = get_pubmed(message)
|
| 49 |
-
msgs.append({"role":"user","content":message+"\
|
| 50 |
-
resp = client.chat.completions.create(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
answer = resp.choices[0].message.content
|
| 52 |
if pubmed:
|
| 53 |
-
answer += "\
|
| 54 |
history.append({"role":"user","content":message})
|
| 55 |
history.append({"role":"assistant","content":answer})
|
| 56 |
return "", history
|
|
@@ -62,24 +59,24 @@ def respond(message, history):
|
|
| 62 |
def piv_tool(velocity, shear, hr):
|
| 63 |
v = "HIGH - stenosis risk" if float(velocity)>2.0 else "NORMAL"
|
| 64 |
s = "HIGH - thrombosis risk" if float(shear)>10 else "ELEVATED" if float(shear)>5 else "NORMAL"
|
| 65 |
-
return "Velocity: "+str(velocity)+" m/s - "+v+"\
|
| 66 |
|
| 67 |
def tgt_tool(tat, pf12, hemo, platelets, time):
|
| 68 |
risk = sum([float(tat)>15,float(pf12)>2.0,float(hemo)>50,float(platelets)<150])
|
| 69 |
r = "HIGH THROMBOGENIC RISK" if risk>=3 else "MODERATE RISK" if risk>=2 else "LOW RISK"
|
| 70 |
-
return "TAT:"+str(tat)+" PF1.2:"+str(pf12)+"\
|
| 71 |
|
| 72 |
def upad_tool(r, g, b):
|
| 73 |
c = max(0,round(0.02*(float(r)-float(b))-0.5,2))
|
| 74 |
s = "Normal" if c<1.2 else "Borderline" if c<1.5 else "Stage 2 CKD" if c<3.0 else "Stage 3-4 CKD" if c<6.0 else "Stage 5 CKD"
|
| 75 |
-
return "Creatinine: "+str(c)+" mg/dL\
|
| 76 |
|
| 77 |
with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
| 78 |
gr.Markdown("# CardioLab AI Agent")
|
| 79 |
gr.Markdown("### SJSU Biomedical Engineering | Biomni Stanford + Llama 70B + Chat Memory + PubMed")
|
| 80 |
gr.Markdown("Open Source: github.com/pranatechsol/Cardio-Lab-Ai")
|
| 81 |
with gr.Tab("Research Chat"):
|
| 82 |
-
gr.Markdown("### Chat with memory like ChatGPT
|
| 83 |
chatbot = gr.Chatbot(label="CardioLab AI", height=500, type="messages")
|
| 84 |
msg = gr.Textbox(label="Ask anything about CardioLab research", placeholder="e.g. How does the TGT measure thrombogenicity?", lines=2)
|
| 85 |
with gr.Row():
|
|
@@ -112,27 +109,4 @@ with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
|
| 112 |
out3 = gr.Textbox(label="Result", lines=4)
|
| 113 |
gr.Button("Analyze uPAD").click(upad_tool, inputs=[r,g,b], outputs=out3)
|
| 114 |
|
| 115 |
-
demo.launch()
|
| 116 |
-
"""
|
| 117 |
-
|
| 118 |
-
req = "gradio\ngroq\nrequests\n"
|
| 119 |
-
|
| 120 |
-
with open('/content/final_app.py', 'w') as f:
|
| 121 |
-
f.write(app)
|
| 122 |
-
|
| 123 |
-
with open('/content/final_req.txt', 'w') as f:
|
| 124 |
-
f.write(req)
|
| 125 |
-
|
| 126 |
-
# Verify line count
|
| 127 |
-
with open('/content/final_app.py') as f:
|
| 128 |
-
lines = f.readlines()
|
| 129 |
-
print("Lines in app:", len(lines))
|
| 130 |
-
print("First line:", lines[0])
|
| 131 |
-
print("Last line:", lines[-1])
|
| 132 |
-
|
| 133 |
-
api.upload_file(path_or_fileobj='/content/final_app.py', path_in_repo='app.py', repo_id=repo_id, repo_type='space', token=hf_token)
|
| 134 |
-
api.upload_file(path_or_fileobj='/content/final_req.txt', path_in_repo='requirements.txt', repo_id=repo_id, repo_type='space', token=hf_token)
|
| 135 |
-
|
| 136 |
-
print("\nFINAL CLEAN APP DEPLOYED!")
|
| 137 |
-
print("URL: https://huggingface.co/spaces/Saicharan21/CardioLab-AI")
|
| 138 |
-
print("Wait 3 minutes then test!")
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
GROQ_KEY = os.environ.get("GROQ_API_KEY", "")
|
| 7 |
|
| 8 |
+
KNOWHOW = """SJSU CardioLab: MCL uses Sylgard 184 PDMS 10:1 ratio 48hr cure green laser PIV 70bpm 5L/min. TGT uses Arduino Uno Stepper Motor 150mL blood sampled at 0 20 40 60min measures TAT PF1.2 hemolysis platelets. uPAD uses Jaffe reaction creatinine plus picric acid gives orange-red color normal 0.6-1.2 mg/dL CKD above 1.5. FSI uses COMSOL ALE mesh blood 1060 kg/m3 0.0035 Pa.s St Jude geometry. MHV is 27mm SJM Regent bileaflet also trileaflet monoleaflet pediatric. Equipment Heska HT5 hematology analyzer time-resolved PIV Tygon tubing Arduino Uno."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def get_pubmed(query):
|
| 11 |
try:
|
| 12 |
+
r = requests.get(
|
| 13 |
+
"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi",
|
| 14 |
params={"db":"pubmed","term":query+" mechanical heart valve OR microfluidic OR CKD","retmax":3,"retmode":"json","sort":"date"},
|
| 15 |
+
timeout=8
|
| 16 |
+
)
|
| 17 |
ids = r.json()["esearchresult"]["idlist"]
|
| 18 |
if not ids:
|
| 19 |
return ""
|
|
|
|
| 25 |
def respond(message, history):
|
| 26 |
if not GROQ_KEY:
|
| 27 |
history.append({"role":"user","content":message})
|
| 28 |
+
history.append({"role":"assistant","content":"Error: Add GROQ_API_KEY to Space Settings tab under Secrets."})
|
| 29 |
return "", history
|
| 30 |
try:
|
| 31 |
client = Groq(api_key=GROQ_KEY)
|
| 32 |
+
msgs = [
|
| 33 |
+
{
|
| 34 |
+
"role":"system",
|
| 35 |
+
"content":"You are CardioLab AI from SJSU Biomedical Engineering built on Biomni Stanford SNAP Lab. Expert in MHV MCL PIV TGT uPAD CKD FSI COMSOL. Remember the full conversation. Never invent paper URLs.\n\n"+KNOWHOW
|
| 36 |
+
}
|
| 37 |
+
]
|
| 38 |
for msg in history:
|
| 39 |
if isinstance(msg, dict):
|
| 40 |
msgs.append({"role":msg["role"],"content":msg["content"]})
|
| 41 |
pubmed = get_pubmed(message)
|
| 42 |
+
msgs.append({"role":"user","content":message+"\n\n"+pubmed})
|
| 43 |
+
resp = client.chat.completions.create(
|
| 44 |
+
model="llama-3.3-70b-versatile",
|
| 45 |
+
messages=msgs,
|
| 46 |
+
max_tokens=600
|
| 47 |
+
)
|
| 48 |
answer = resp.choices[0].message.content
|
| 49 |
if pubmed:
|
| 50 |
+
answer += "\n\n📚 "+pubmed
|
| 51 |
history.append({"role":"user","content":message})
|
| 52 |
history.append({"role":"assistant","content":answer})
|
| 53 |
return "", history
|
|
|
|
| 59 |
def piv_tool(velocity, shear, hr):
|
| 60 |
v = "HIGH - stenosis risk" if float(velocity)>2.0 else "NORMAL"
|
| 61 |
s = "HIGH - thrombosis risk" if float(shear)>10 else "ELEVATED" if float(shear)>5 else "NORMAL"
|
| 62 |
+
return "Velocity: "+str(velocity)+" m/s - "+v+"\nShear: "+str(shear)+" Pa - "+s+"\nHeart Rate: "+str(hr)+" bpm"
|
| 63 |
|
| 64 |
def tgt_tool(tat, pf12, hemo, platelets, time):
|
| 65 |
risk = sum([float(tat)>15,float(pf12)>2.0,float(hemo)>50,float(platelets)<150])
|
| 66 |
r = "HIGH THROMBOGENIC RISK" if risk>=3 else "MODERATE RISK" if risk>=2 else "LOW RISK"
|
| 67 |
+
return "TAT:"+str(tat)+" PF1.2:"+str(pf12)+"\nHemo:"+str(hemo)+" Platelets:"+str(platelets)+"\nTime:"+str(time)+" min\nResult: "+r
|
| 68 |
|
| 69 |
def upad_tool(r, g, b):
|
| 70 |
c = max(0,round(0.02*(float(r)-float(b))-0.5,2))
|
| 71 |
s = "Normal" if c<1.2 else "Borderline" if c<1.5 else "Stage 2 CKD" if c<3.0 else "Stage 3-4 CKD" if c<6.0 else "Stage 5 CKD"
|
| 72 |
+
return "Creatinine: "+str(c)+" mg/dL\nStage: "+s+"\nConfirm with: Heska Element HT5"
|
| 73 |
|
| 74 |
with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
| 75 |
gr.Markdown("# CardioLab AI Agent")
|
| 76 |
gr.Markdown("### SJSU Biomedical Engineering | Biomni Stanford + Llama 70B + Chat Memory + PubMed")
|
| 77 |
gr.Markdown("Open Source: github.com/pranatechsol/Cardio-Lab-Ai")
|
| 78 |
with gr.Tab("Research Chat"):
|
| 79 |
+
gr.Markdown("### Chat with memory like ChatGPT for CardioLab research")
|
| 80 |
chatbot = gr.Chatbot(label="CardioLab AI", height=500, type="messages")
|
| 81 |
msg = gr.Textbox(label="Ask anything about CardioLab research", placeholder="e.g. How does the TGT measure thrombogenicity?", lines=2)
|
| 82 |
with gr.Row():
|
|
|
|
| 109 |
out3 = gr.Textbox(label="Result", lines=4)
|
| 110 |
gr.Button("Analyze uPAD").click(upad_tool, inputs=[r,g,b], outputs=out3)
|
| 111 |
|
| 112 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|