Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os, requests
|
| 3 |
from groq import Groq
|
| 4 |
-
from datetime import datetime
|
| 5 |
|
| 6 |
GROQ_KEY = os.environ.get("GROQ_API_KEY","")
|
| 7 |
client = Groq(api_key=GROQ_KEY)
|
|
@@ -67,9 +66,9 @@ def search_scholar(query, n=3):
|
|
| 67 |
|
| 68 |
def ask_with_memory(message, history):
|
| 69 |
if not GROQ_KEY:
|
| 70 |
-
return "Error: GROQ_API_KEY not set."
|
| 71 |
|
| 72 |
-
# Build
|
| 73 |
messages = [
|
| 74 |
{
|
| 75 |
"role": "system",
|
|
@@ -78,28 +77,30 @@ Expert in SJSU Biomedical Engineering research.
|
|
| 78 |
You remember everything said in this conversation.
|
| 79 |
NEVER invent paper titles or URLs.
|
| 80 |
ONLY cite papers from the search results provided.
|
| 81 |
-
Always be helpful, detailed and accurate.
|
| 82 |
|
| 83 |
CARDIOLAB KNOW-HOW:
|
| 84 |
""" + KNOWHOW
|
| 85 |
}
|
| 86 |
]
|
| 87 |
|
| 88 |
-
# Add
|
| 89 |
-
for
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
# Search
|
| 94 |
cardio_query = message + " mechanical heart valve OR microfluidic OR CKD creatinine OR PIV OR thrombogenicity"
|
| 95 |
pubmed_links, pubmed_context = search_pubmed(cardio_query, n=3)
|
| 96 |
scholar_links, scholar_context = search_scholar(message + " biomedical", n=3)
|
| 97 |
sources = pubmed_context + scholar_context
|
| 98 |
|
| 99 |
-
# Add current question with search results
|
| 100 |
messages.append({
|
| 101 |
"role": "user",
|
| 102 |
-
"content": message + "\n\nReal papers
|
| 103 |
})
|
| 104 |
|
| 105 |
response = client.chat.completions.create(
|
|
@@ -110,7 +111,6 @@ CARDIOLAB KNOW-HOW:
|
|
| 110 |
|
| 111 |
answer = response.choices[0].message.content
|
| 112 |
|
| 113 |
-
# Add verified links
|
| 114 |
links = ""
|
| 115 |
if pubmed_links:
|
| 116 |
links += "\n\n📚 VERIFIED PUBMED LINKS:\n" + "\n".join(pubmed_links[:3])
|
|
@@ -144,21 +144,22 @@ with gr.Blocks(title="CardioLab AI - SJSU") as demo:
|
|
| 144 |
chatbot = gr.Chatbot(
|
| 145 |
label="CardioLab AI",
|
| 146 |
height=500,
|
| 147 |
-
|
| 148 |
)
|
| 149 |
msg = gr.Textbox(
|
| 150 |
label="Your message",
|
| 151 |
-
placeholder="Ask anything about CardioLab research...
|
| 152 |
lines=2
|
| 153 |
)
|
| 154 |
with gr.Row():
|
| 155 |
send = gr.Button("Send", variant="primary")
|
| 156 |
clear = gr.Button("Clear Chat")
|
| 157 |
|
| 158 |
-
def respond(message,
|
| 159 |
-
bot_message = ask_with_memory(message,
|
| 160 |
-
|
| 161 |
-
|
|
|
|
| 162 |
|
| 163 |
send.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 164 |
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os, requests
|
| 3 |
from groq import Groq
|
|
|
|
| 4 |
|
| 5 |
GROQ_KEY = os.environ.get("GROQ_API_KEY","")
|
| 6 |
client = Groq(api_key=GROQ_KEY)
|
|
|
|
| 66 |
|
| 67 |
def ask_with_memory(message, history):
|
| 68 |
if not GROQ_KEY:
|
| 69 |
+
return "Error: GROQ_API_KEY not set in Space secrets."
|
| 70 |
|
| 71 |
+
# Build messages with full history for memory
|
| 72 |
messages = [
|
| 73 |
{
|
| 74 |
"role": "system",
|
|
|
|
| 77 |
You remember everything said in this conversation.
|
| 78 |
NEVER invent paper titles or URLs.
|
| 79 |
ONLY cite papers from the search results provided.
|
|
|
|
| 80 |
|
| 81 |
CARDIOLAB KNOW-HOW:
|
| 82 |
""" + KNOWHOW
|
| 83 |
}
|
| 84 |
]
|
| 85 |
|
| 86 |
+
# Add chat history — new Gradio format uses dicts
|
| 87 |
+
for msg in history:
|
| 88 |
+
if isinstance(msg, dict):
|
| 89 |
+
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 90 |
+
else:
|
| 91 |
+
# fallback for tuple format
|
| 92 |
+
messages.append({"role": "user", "content": str(msg[0])})
|
| 93 |
+
messages.append({"role": "assistant", "content": str(msg[1])})
|
| 94 |
|
| 95 |
+
# Search papers
|
| 96 |
cardio_query = message + " mechanical heart valve OR microfluidic OR CKD creatinine OR PIV OR thrombogenicity"
|
| 97 |
pubmed_links, pubmed_context = search_pubmed(cardio_query, n=3)
|
| 98 |
scholar_links, scholar_context = search_scholar(message + " biomedical", n=3)
|
| 99 |
sources = pubmed_context + scholar_context
|
| 100 |
|
|
|
|
| 101 |
messages.append({
|
| 102 |
"role": "user",
|
| 103 |
+
"content": message + "\n\nReal papers (ONLY use these):\n" + sources[:3000]
|
| 104 |
})
|
| 105 |
|
| 106 |
response = client.chat.completions.create(
|
|
|
|
| 111 |
|
| 112 |
answer = response.choices[0].message.content
|
| 113 |
|
|
|
|
| 114 |
links = ""
|
| 115 |
if pubmed_links:
|
| 116 |
links += "\n\n📚 VERIFIED PUBMED LINKS:\n" + "\n".join(pubmed_links[:3])
|
|
|
|
| 144 |
chatbot = gr.Chatbot(
|
| 145 |
label="CardioLab AI",
|
| 146 |
height=500,
|
| 147 |
+
type="messages"
|
| 148 |
)
|
| 149 |
msg = gr.Textbox(
|
| 150 |
label="Your message",
|
| 151 |
+
placeholder="Ask anything about CardioLab research...",
|
| 152 |
lines=2
|
| 153 |
)
|
| 154 |
with gr.Row():
|
| 155 |
send = gr.Button("Send", variant="primary")
|
| 156 |
clear = gr.Button("Clear Chat")
|
| 157 |
|
| 158 |
+
def respond(message, history):
|
| 159 |
+
bot_message = ask_with_memory(message, history)
|
| 160 |
+
history.append({"role": "user", "content": message})
|
| 161 |
+
history.append({"role": "assistant", "content": bot_message})
|
| 162 |
+
return "", history
|
| 163 |
|
| 164 |
send.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 165 |
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|