Saicharan21 commited on
Commit
e931ae7
·
verified ·
1 Parent(s): 5c82f86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -24,20 +24,21 @@ def get_pubmed(query):
24
 
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(
@@ -47,13 +48,11 @@ def respond(message, history):
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
54
  except Exception as e:
55
- history.append({"role":"user","content":message})
56
- history.append({"role":"assistant","content":"Error: "+str(e)})
57
  return "", history
58
 
59
  def piv_tool(velocity, shear, hr):
@@ -77,7 +76,7 @@ with gr.Blocks(title="CardioLab AI - SJSU") as demo:
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():
83
  send = gr.Button("Send", variant="primary")
 
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 = [
32
  {
33
+ "role": "system",
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(
 
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):
 
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")