Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -5,20 +5,26 @@ from groq import Groq
|
|
| 5 |
|
| 6 |
GROQ_KEY = os.environ.get("GROQ_API_KEY", "")
|
| 7 |
|
| 8 |
-
KNOWHOW = """SJSU CardioLab:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 15 |
timeout=8
|
| 16 |
)
|
| 17 |
ids = r.json()["esearchresult"]["idlist"]
|
| 18 |
if not ids:
|
| 19 |
return ""
|
| 20 |
links = ["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids]
|
| 21 |
-
return "\n
|
| 22 |
except:
|
| 23 |
return ""
|
| 24 |
|
|
@@ -27,27 +33,13 @@ def chat(message, history):
|
|
| 27 |
return "Error: Add GROQ_API_KEY to Space Settings under Secrets tab."
|
| 28 |
try:
|
| 29 |
client = Groq(api_key=GROQ_KEY)
|
| 30 |
-
msgs = [
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
}
|
| 35 |
-
]
|
| 36 |
-
for item in history:
|
| 37 |
-
if isinstance(item, dict):
|
| 38 |
-
msgs.append({"role": item["role"], "content": item["content"]})
|
| 39 |
-
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
| 40 |
-
if item[0]:
|
| 41 |
-
msgs.append({"role": "user", "content": str(item[0])})
|
| 42 |
-
if item[1]:
|
| 43 |
-
msgs.append({"role": "assistant", "content": str(item[1])})
|
| 44 |
pubmed = get_pubmed(message)
|
| 45 |
-
msgs.append({"role":
|
| 46 |
-
resp = client.chat.completions.create(
|
| 47 |
-
model="llama-3.3-70b-versatile",
|
| 48 |
-
messages=msgs,
|
| 49 |
-
max_tokens=600
|
| 50 |
-
)
|
| 51 |
return resp.choices[0].message.content + pubmed
|
| 52 |
except Exception as e:
|
| 53 |
return "Error: " + str(e)
|
|
@@ -71,17 +63,20 @@ with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
|
| 71 |
gr.Markdown("# CardioLab AI Agent")
|
| 72 |
gr.Markdown("### SJSU Biomedical Engineering | Biomni Stanford + Llama 70B + Chat Memory + PubMed")
|
| 73 |
gr.Markdown("Open Source: github.com/pranatechsol/Cardio-Lab-Ai")
|
| 74 |
-
|
| 75 |
with gr.Tab("Research Chat"):
|
| 76 |
gr.Markdown("### Chat with memory like ChatGPT for CardioLab research")
|
| 77 |
-
gr.
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
with gr.Tab("PIV Analysis"):
|
| 86 |
gr.Markdown("### Analyze PIV flow data from Mock Circulatory Loop")
|
| 87 |
v = gr.Number(label="Max Velocity m/s", value=1.8)
|
|
@@ -89,7 +84,6 @@ with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
|
| 89 |
h = gr.Number(label="Heart Rate bpm", value=72)
|
| 90 |
out = gr.Textbox(label="Result", lines=4)
|
| 91 |
gr.Button("Analyze PIV").click(piv_tool, inputs=[v,s,h], outputs=out)
|
| 92 |
-
|
| 93 |
with gr.Tab("TGT Results"):
|
| 94 |
gr.Markdown("### Interpret Thrombogenicity Tester blood results")
|
| 95 |
t1 = gr.Number(label="TAT", value=18)
|
|
@@ -99,7 +93,6 @@ with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
|
| 99 |
t5 = gr.Number(label="Time minutes", value=40)
|
| 100 |
out2 = gr.Textbox(label="Result", lines=5)
|
| 101 |
gr.Button("Analyze TGT").click(tgt_tool, inputs=[t1,t2,t3,t4,t5], outputs=out2)
|
| 102 |
-
|
| 103 |
with gr.Tab("uPAD CKD"):
|
| 104 |
gr.Markdown("### Analyze uPAD colorimetric result - Jaffe Reaction")
|
| 105 |
r = gr.Number(label="R value", value=210)
|
|
@@ -108,4 +101,4 @@ with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
|
| 108 |
out3 = gr.Textbox(label="Result", lines=4)
|
| 109 |
gr.Button("Analyze uPAD").click(upad_tool, inputs=[r,g,b], outputs=out3)
|
| 110 |
|
| 111 |
-
demo.launch()
|
|
|
|
| 5 |
|
| 6 |
GROQ_KEY = os.environ.get("GROQ_API_KEY", "")
|
| 7 |
|
| 8 |
+
KNOWHOW = """SJSU CardioLab:
|
| 9 |
+
MCL: Sylgard 184 PDMS 10:1 ratio 48hr cure green laser PIV 70bpm 5L/min.
|
| 10 |
+
TGT: Arduino Uno Stepper Motor 150mL blood sampled at 0 20 40 60min measures TAT PF1.2 hemolysis platelets.
|
| 11 |
+
uPAD: Jaffe reaction creatinine plus picric acid gives orange-red color normal 0.6-1.2 mg/dL CKD above 1.5.
|
| 12 |
+
FSI: COMSOL ALE mesh blood 1060 kg/m3 0.0035 Pa.s St Jude geometry.
|
| 13 |
+
MHV: 27mm SJM Regent bileaflet also trileaflet monoleaflet pediatric.
|
| 14 |
+
Equipment: Heska HT5 hematology analyzer time-resolved PIV Tygon tubing Arduino Uno."""
|
| 15 |
|
| 16 |
def get_pubmed(query):
|
| 17 |
try:
|
| 18 |
r = requests.get(
|
| 19 |
"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi",
|
| 20 |
+
params={"db":"pubmed","term":query,"retmax":3,"retmode":"json","sort":"date"},
|
| 21 |
timeout=8
|
| 22 |
)
|
| 23 |
ids = r.json()["esearchresult"]["idlist"]
|
| 24 |
if not ids:
|
| 25 |
return ""
|
| 26 |
links = ["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids]
|
| 27 |
+
return "\n\nPubMed: " + " | ".join(links)
|
| 28 |
except:
|
| 29 |
return ""
|
| 30 |
|
|
|
|
| 33 |
return "Error: Add GROQ_API_KEY to Space Settings under Secrets tab."
|
| 34 |
try:
|
| 35 |
client = Groq(api_key=GROQ_KEY)
|
| 36 |
+
msgs = [{"role":"system","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}]
|
| 37 |
+
for h in history:
|
| 38 |
+
msgs.append({"role":"user","content":str(h[0])})
|
| 39 |
+
msgs.append({"role":"assistant","content":str(h[1])})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
pubmed = get_pubmed(message)
|
| 41 |
+
msgs.append({"role":"user","content":message})
|
| 42 |
+
resp = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=msgs,max_tokens=600)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return resp.choices[0].message.content + pubmed
|
| 44 |
except Exception as e:
|
| 45 |
return "Error: " + str(e)
|
|
|
|
| 63 |
gr.Markdown("# CardioLab AI Agent")
|
| 64 |
gr.Markdown("### SJSU Biomedical Engineering | Biomni Stanford + Llama 70B + Chat Memory + PubMed")
|
| 65 |
gr.Markdown("Open Source: github.com/pranatechsol/Cardio-Lab-Ai")
|
|
|
|
| 66 |
with gr.Tab("Research Chat"):
|
| 67 |
gr.Markdown("### Chat with memory like ChatGPT for CardioLab research")
|
| 68 |
+
chatbot = gr.Chatbot(label="CardioLab AI", height=450)
|
| 69 |
+
msg = gr.Textbox(label="Ask anything about CardioLab research", placeholder="e.g. How does the TGT measure thrombogenicity?", lines=2)
|
| 70 |
+
with gr.Row():
|
| 71 |
+
send = gr.Button("Send", variant="primary")
|
| 72 |
+
clear = gr.Button("Clear Chat")
|
| 73 |
+
def respond(message, history):
|
| 74 |
+
reply = chat(message, history)
|
| 75 |
+
history.append((message, reply))
|
| 76 |
+
return "", history
|
| 77 |
+
send.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 78 |
+
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 79 |
+
clear.click(lambda: [], None, chatbot)
|
| 80 |
with gr.Tab("PIV Analysis"):
|
| 81 |
gr.Markdown("### Analyze PIV flow data from Mock Circulatory Loop")
|
| 82 |
v = gr.Number(label="Max Velocity m/s", value=1.8)
|
|
|
|
| 84 |
h = gr.Number(label="Heart Rate bpm", value=72)
|
| 85 |
out = gr.Textbox(label="Result", lines=4)
|
| 86 |
gr.Button("Analyze PIV").click(piv_tool, inputs=[v,s,h], outputs=out)
|
|
|
|
| 87 |
with gr.Tab("TGT Results"):
|
| 88 |
gr.Markdown("### Interpret Thrombogenicity Tester blood results")
|
| 89 |
t1 = gr.Number(label="TAT", value=18)
|
|
|
|
| 93 |
t5 = gr.Number(label="Time minutes", value=40)
|
| 94 |
out2 = gr.Textbox(label="Result", lines=5)
|
| 95 |
gr.Button("Analyze TGT").click(tgt_tool, inputs=[t1,t2,t3,t4,t5], outputs=out2)
|
|
|
|
| 96 |
with gr.Tab("uPAD CKD"):
|
| 97 |
gr.Markdown("### Analyze uPAD colorimetric result - Jaffe Reaction")
|
| 98 |
r = gr.Number(label="R value", value=210)
|
|
|
|
| 101 |
out3 = gr.Textbox(label="Result", lines=4)
|
| 102 |
gr.Button("Analyze uPAD").click(upad_tool, inputs=[r,g,b], outputs=out3)
|
| 103 |
|
| 104 |
+
demo.launch()
|