Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -21,22 +21,27 @@ def get_pubmed(query):
|
|
| 21 |
return "\n\nPubMed: "+" | ".join(["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids])
|
| 22 |
except: return ""
|
| 23 |
|
| 24 |
-
def
|
| 25 |
if not GROQ_KEY:
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
try:
|
| 28 |
client = Groq(api_key=GROQ_KEY)
|
| 29 |
msgs = [{"role":"system","content":"You are CardioLab AI from SJSU Biomedical Engineering built on Biomni Stanford. Expert in MHV MCL PIV TGT uPAD CKD FSI. Remember full conversation. Never invent URLs.\n\n"+KNOWHOW}]
|
| 30 |
for item in history:
|
| 31 |
if isinstance(item, dict):
|
| 32 |
msgs.append({"role":item["role"],"content":item["content"]})
|
| 33 |
-
elif isinstance(item,(list,tuple)) and len(item)==2:
|
| 34 |
-
if item[0]: msgs.append({"role":"user","content":str(item[0])})
|
| 35 |
-
if item[1]: msgs.append({"role":"assistant","content":str(item[1])})
|
| 36 |
msgs.append({"role":"user","content":message})
|
| 37 |
resp = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=msgs,max_tokens=600)
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def piv_tool(velocity, shear, hr):
|
| 42 |
v = "HIGH-stenosis" if float(velocity)>2.0 else "NORMAL"
|
|
@@ -57,26 +62,16 @@ with gr.Blocks(title="CardioLab AI SJSU") as demo:
|
|
| 57 |
gr.Markdown("# CardioLab AI Agent")
|
| 58 |
gr.Markdown("### SJSU Biomedical Engineering | Biomni Stanford + Llama 70B + PubMed")
|
| 59 |
gr.Markdown("github.com/pranatechsol/Cardio-Lab-Ai")
|
| 60 |
-
|
| 61 |
with gr.Tab("Research Chat"):
|
| 62 |
gr.Markdown("### Chat like ChatGPT for CardioLab research")
|
| 63 |
-
chatbot = gr.Chatbot(height=400, label="CardioLab AI")
|
| 64 |
msg_box = gr.Textbox(placeholder="Ask anything about CardioLab...", label="Your message", lines=2)
|
| 65 |
with gr.Row():
|
| 66 |
send_btn = gr.Button("Send", variant="primary", scale=3)
|
| 67 |
clear_btn = gr.Button("Clear", scale=1)
|
| 68 |
-
|
| 69 |
-
def respond(message, history):
|
| 70 |
-
if not message.strip():
|
| 71 |
-
return "", history
|
| 72 |
-
reply = chat(message, history)
|
| 73 |
-
history = history + [[message, reply]]
|
| 74 |
-
return "", history
|
| 75 |
-
|
| 76 |
send_btn.click(respond, inputs=[msg_box, chatbot], outputs=[msg_box, chatbot])
|
| 77 |
msg_box.submit(respond, inputs=[msg_box, chatbot], outputs=[msg_box, chatbot])
|
| 78 |
clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg_box])
|
| 79 |
-
|
| 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,7 +79,6 @@ with gr.Blocks(title="CardioLab AI SJSU") as demo:
|
|
| 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 |
-
|
| 88 |
with gr.Tab("TGT Results"):
|
| 89 |
gr.Markdown("### Interpret Thrombogenicity Tester blood results")
|
| 90 |
t1=gr.Number(label="TAT",value=18)
|
|
@@ -94,7 +88,6 @@ with gr.Blocks(title="CardioLab AI SJSU") as demo:
|
|
| 94 |
t5=gr.Number(label="Time minutes",value=40)
|
| 95 |
out2=gr.Textbox(label="Result",lines=5)
|
| 96 |
gr.Button("Analyze TGT").click(tgt_tool,inputs=[t1,t2,t3,t4,t5],outputs=out2)
|
| 97 |
-
|
| 98 |
with gr.Tab("uPAD CKD"):
|
| 99 |
gr.Markdown("### Analyze uPAD colorimetric result - Jaffe Reaction")
|
| 100 |
r=gr.Number(label="R value",value=210)
|
|
|
|
| 21 |
return "\n\nPubMed: "+" | ".join(["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids])
|
| 22 |
except: return ""
|
| 23 |
|
| 24 |
+
def respond(message, history):
|
| 25 |
if not GROQ_KEY:
|
| 26 |
+
history.append({"role":"user","content":message})
|
| 27 |
+
history.append({"role":"assistant","content":"Error: Add GROQ_API_KEY to Space Settings Secrets tab."})
|
| 28 |
+
return "", history
|
| 29 |
try:
|
| 30 |
client = Groq(api_key=GROQ_KEY)
|
| 31 |
msgs = [{"role":"system","content":"You are CardioLab AI from SJSU Biomedical Engineering built on Biomni Stanford. Expert in MHV MCL PIV TGT uPAD CKD FSI. Remember full conversation. Never invent URLs.\n\n"+KNOWHOW}]
|
| 32 |
for item in history:
|
| 33 |
if isinstance(item, dict):
|
| 34 |
msgs.append({"role":item["role"],"content":item["content"]})
|
|
|
|
|
|
|
|
|
|
| 35 |
msgs.append({"role":"user","content":message})
|
| 36 |
resp = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=msgs,max_tokens=600)
|
| 37 |
+
answer = resp.choices[0].message.content + get_pubmed(message)
|
| 38 |
+
history.append({"role":"user","content":message})
|
| 39 |
+
history.append({"role":"assistant","content":answer})
|
| 40 |
+
return "", history
|
| 41 |
+
except Exception as e:
|
| 42 |
+
history.append({"role":"user","content":message})
|
| 43 |
+
history.append({"role":"assistant","content":"Error: "+str(e)})
|
| 44 |
+
return "", history
|
| 45 |
|
| 46 |
def piv_tool(velocity, shear, hr):
|
| 47 |
v = "HIGH-stenosis" if float(velocity)>2.0 else "NORMAL"
|
|
|
|
| 62 |
gr.Markdown("# CardioLab AI Agent")
|
| 63 |
gr.Markdown("### SJSU Biomedical Engineering | Biomni Stanford + Llama 70B + PubMed")
|
| 64 |
gr.Markdown("github.com/pranatechsol/Cardio-Lab-Ai")
|
|
|
|
| 65 |
with gr.Tab("Research Chat"):
|
| 66 |
gr.Markdown("### Chat like ChatGPT for CardioLab research")
|
| 67 |
+
chatbot = gr.Chatbot(height=400, label="CardioLab AI", type="messages")
|
| 68 |
msg_box = gr.Textbox(placeholder="Ask anything about CardioLab...", label="Your message", lines=2)
|
| 69 |
with gr.Row():
|
| 70 |
send_btn = gr.Button("Send", variant="primary", scale=3)
|
| 71 |
clear_btn = gr.Button("Clear", scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
send_btn.click(respond, inputs=[msg_box, chatbot], outputs=[msg_box, chatbot])
|
| 73 |
msg_box.submit(respond, inputs=[msg_box, chatbot], outputs=[msg_box, chatbot])
|
| 74 |
clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg_box])
|
|
|
|
| 75 |
with gr.Tab("PIV Analysis"):
|
| 76 |
gr.Markdown("### Analyze PIV flow data from Mock Circulatory Loop")
|
| 77 |
v=gr.Number(label="Max Velocity m/s",value=1.8)
|
|
|
|
| 79 |
h=gr.Number(label="Heart Rate bpm",value=72)
|
| 80 |
out=gr.Textbox(label="Result",lines=4)
|
| 81 |
gr.Button("Analyze PIV").click(piv_tool,inputs=[v,s,h],outputs=out)
|
|
|
|
| 82 |
with gr.Tab("TGT Results"):
|
| 83 |
gr.Markdown("### Interpret Thrombogenicity Tester blood results")
|
| 84 |
t1=gr.Number(label="TAT",value=18)
|
|
|
|
| 88 |
t5=gr.Number(label="Time minutes",value=40)
|
| 89 |
out2=gr.Textbox(label="Result",lines=5)
|
| 90 |
gr.Button("Analyze TGT").click(tgt_tool,inputs=[t1,t2,t3,t4,t5],outputs=out2)
|
|
|
|
| 91 |
with gr.Tab("uPAD CKD"):
|
| 92 |
gr.Markdown("### Analyze uPAD colorimetric result - Jaffe Reaction")
|
| 93 |
r=gr.Number(label="R value",value=210)
|