Saicharan21 commited on
Commit
821f3f0
·
verified ·
1 Parent(s): e931ae7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -18,14 +18,13 @@ def get_pubmed(query):
18
  if not ids:
19
  return ""
20
  links = ["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids]
21
- return "PubMed: " + " | ".join(links)
22
  except:
23
  return ""
24
 
25
- def respond(message, history):
26
  if not GROQ_KEY:
27
- history.append([message, "Error: Add GROQ_API_KEY to Space Settings under Secrets."])
28
- return "", history
29
  try:
30
  client = Groq(api_key=GROQ_KEY)
31
  msgs = [
@@ -34,26 +33,24 @@ def respond(message, history):
34
  "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
35
  }
36
  ]
37
- for human_msg, ai_msg in history:
38
- if human_msg:
39
- msgs.append({"role":"user","content":str(human_msg)})
40
- if ai_msg:
41
- msgs.append({"role":"assistant","content":str(ai_msg)})
 
 
 
42
  pubmed = get_pubmed(message)
43
- msgs.append({"role":"user","content":message+"\n\n"+pubmed})
44
  resp = client.chat.completions.create(
45
  model="llama-3.3-70b-versatile",
46
  messages=msgs,
47
  max_tokens=600
48
  )
49
- answer = resp.choices[0].message.content
50
- if pubmed:
51
- answer += "\n\n📚 " + pubmed
52
- history.append([message, answer])
53
- return "", history
54
  except Exception as e:
55
- history.append([message, "Error: " + str(e)])
56
- return "", history
57
 
58
  def piv_tool(velocity, shear, hr):
59
  v = "HIGH - stenosis risk" if float(velocity)>2.0 else "NORMAL"
@@ -74,16 +71,17 @@ with gr.Blocks(title="CardioLab AI - SJSU") as demo:
74
  gr.Markdown("# CardioLab AI Agent")
75
  gr.Markdown("### SJSU Biomedical Engineering | Biomni Stanford + Llama 70B + Chat Memory + PubMed")
76
  gr.Markdown("Open Source: github.com/pranatechsol/Cardio-Lab-Ai")
 
77
  with gr.Tab("Research Chat"):
78
  gr.Markdown("### Chat with memory like ChatGPT for CardioLab research")
79
- chatbot = gr.Chatbot(label="CardioLab AI", height=500)
80
- msg = gr.Textbox(label="Ask anything about CardioLab research", placeholder="e.g. How does the TGT measure thrombogenicity?", lines=2)
81
- with gr.Row():
82
- send = gr.Button("Send", variant="primary")
83
- clear = gr.Button("Clear Chat")
84
- send.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
85
- msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
86
- clear.click(lambda: [], None, chatbot)
87
  with gr.Tab("PIV Analysis"):
88
  gr.Markdown("### Analyze PIV flow data from Mock Circulatory Loop")
89
  v = gr.Number(label="Max Velocity m/s", value=1.8)
@@ -91,6 +89,7 @@ with gr.Blocks(title="CardioLab AI - SJSU") as demo:
91
  h = gr.Number(label="Heart Rate bpm", value=72)
92
  out = gr.Textbox(label="Result", lines=4)
93
  gr.Button("Analyze PIV").click(piv_tool, inputs=[v,s,h], outputs=out)
 
94
  with gr.Tab("TGT Results"):
95
  gr.Markdown("### Interpret Thrombogenicity Tester blood results")
96
  t1 = gr.Number(label="TAT", value=18)
@@ -100,6 +99,7 @@ with gr.Blocks(title="CardioLab AI - SJSU") as demo:
100
  t5 = gr.Number(label="Time minutes", value=40)
101
  out2 = gr.Textbox(label="Result", lines=5)
102
  gr.Button("Analyze TGT").click(tgt_tool, inputs=[t1,t2,t3,t4,t5], outputs=out2)
 
103
  with gr.Tab("uPAD CKD"):
104
  gr.Markdown("### Analyze uPAD colorimetric result - Jaffe Reaction")
105
  r = gr.Number(label="R value", value=210)
 
18
  if not ids:
19
  return ""
20
  links = ["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids]
21
+ return "\n📚 PubMed: " + " | ".join(links)
22
  except:
23
  return ""
24
 
25
+ def chat(message, history):
26
  if not GROQ_KEY:
27
+ return "Error: Add GROQ_API_KEY to Space Settings under Secrets tab."
 
28
  try:
29
  client = Groq(api_key=GROQ_KEY)
30
  msgs = [
 
33
  "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
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": "user", "content": message})
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)
 
54
 
55
  def piv_tool(velocity, shear, hr):
56
  v = "HIGH - stenosis risk" if float(velocity)>2.0 else "NORMAL"
 
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.ChatInterface(
78
+ fn=chat,
79
+ chatbot=gr.Chatbot(height=450, label="CardioLab AI"),
80
+ textbox=gr.Textbox(placeholder="Ask anything about CardioLab research...", lines=2),
81
+ submit_btn="Send",
82
+ clear_btn="Clear Chat"
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
  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
  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)