Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -3,218 +3,425 @@ import os
|
|
| 3 |
import requests
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
-
GROQ_KEY = os.environ.get(
|
| 7 |
-
|
| 8 |
-
KNOWHOW = (
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
CSS =
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def get_pubmed(query, n=5):
|
| 33 |
try:
|
| 34 |
-
r = requests.get(
|
| 35 |
-
params={
|
| 36 |
-
|
| 37 |
-
ids = r.json()[
|
| 38 |
-
if not ids: return
|
| 39 |
-
return chr(10).join([
|
| 40 |
-
except: return
|
| 41 |
|
| 42 |
def get_scholar(query, n=5):
|
| 43 |
try:
|
| 44 |
-
r = requests.get(
|
| 45 |
-
params={
|
| 46 |
-
papers = r.json().get(
|
| 47 |
out = []
|
| 48 |
for p in papers:
|
| 49 |
-
title = p.get(
|
| 50 |
-
year = str(p.get(
|
| 51 |
-
url = p.get(
|
| 52 |
-
citations = str(p.get(
|
| 53 |
if url:
|
| 54 |
-
out.append(title[:80]+
|
| 55 |
return chr(10)+chr(10).join(out)
|
| 56 |
-
except: return
|
| 57 |
|
| 58 |
def quick_search(query):
|
| 59 |
-
if not query.strip(): return
|
| 60 |
pubmed = get_pubmed(query, n=8)
|
| 61 |
scholar = get_scholar(query, n=5)
|
| 62 |
-
result =
|
| 63 |
-
result +=
|
| 64 |
-
result +=
|
| 65 |
return result
|
| 66 |
|
| 67 |
def research_chat(message, history):
|
| 68 |
if not GROQ_KEY:
|
| 69 |
-
history.append({
|
| 70 |
-
history.append({
|
| 71 |
-
return
|
| 72 |
try:
|
| 73 |
client = Groq(api_key=GROQ_KEY)
|
| 74 |
pubmed = get_pubmed(message, n=3)
|
| 75 |
-
system_msg =
|
| 76 |
-
msgs = [{
|
| 77 |
for item in history:
|
| 78 |
if isinstance(item, dict):
|
| 79 |
-
msgs.append({
|
| 80 |
-
msgs.append({
|
| 81 |
-
resp = client.chat.completions.create(model=
|
| 82 |
answer = resp.choices[0].message.content
|
| 83 |
-
if pubmed: answer += chr(10)+chr(10)+
|
| 84 |
-
history.append({
|
| 85 |
-
history.append({
|
| 86 |
-
return
|
| 87 |
except Exception as e:
|
| 88 |
-
history.append({
|
| 89 |
-
history.append({
|
| 90 |
-
return
|
| 91 |
|
| 92 |
def voice_chat(audio, history):
|
| 93 |
if audio is None:
|
| 94 |
-
history.append({
|
| 95 |
return history
|
| 96 |
try:
|
| 97 |
client = Groq(api_key=GROQ_KEY)
|
| 98 |
-
with open(audio,
|
| 99 |
transcription = client.audio.transcriptions.create(
|
| 100 |
-
file=(
|
| 101 |
-
model=
|
| 102 |
)
|
| 103 |
text = transcription.text
|
| 104 |
-
|
| 105 |
-
msgs = [{'role':'system','content':system_msg}]
|
| 106 |
for item in history:
|
| 107 |
if isinstance(item, dict):
|
| 108 |
-
msgs.append({
|
| 109 |
-
msgs.append({
|
| 110 |
-
resp = client.chat.completions.create(model=
|
| 111 |
answer = resp.choices[0].message.content
|
| 112 |
-
history.append({
|
| 113 |
-
history.append({
|
| 114 |
return history
|
| 115 |
except Exception as e:
|
| 116 |
-
history.append({
|
| 117 |
return history
|
| 118 |
|
| 119 |
def generate_diagram(topic):
|
| 120 |
-
if not topic.strip(): return
|
| 121 |
try:
|
| 122 |
client = Groq(api_key=GROQ_KEY)
|
| 123 |
-
msgs = [{
|
| 124 |
-
msgs.append({
|
| 125 |
-
resp = client.chat.completions.create(model=
|
| 126 |
return resp.choices[0].message.content
|
| 127 |
except Exception as e:
|
| 128 |
-
return
|
| 129 |
|
| 130 |
def piv_tool(velocity, shear, hr):
|
| 131 |
-
v =
|
| 132 |
-
s =
|
| 133 |
-
hr_s =
|
| 134 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
def tgt_tool(tat,pf12,hemo,platelets,time):
|
| 137 |
risk=sum([float(tat)>15,float(pf12)>2.0,float(hemo)>50,float(platelets)<150])
|
| 138 |
-
r=
|
| 139 |
-
return (
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
def upad_tool(r,g,b):
|
| 147 |
c=max(0,round(0.02*(float(r)-float(b))-0.5,2))
|
| 148 |
-
if c<1.2: s=
|
| 149 |
-
elif c<1.5: s=
|
| 150 |
-
elif c<3.0: s=
|
| 151 |
-
elif c<6.0: s=
|
| 152 |
-
else: s=
|
| 153 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
with gr.Tabs():
|
| 159 |
-
with gr.Tab(
|
| 160 |
-
chatbot = gr.Chatbot(label=
|
| 161 |
with gr.Row():
|
| 162 |
-
msg_box = gr.Textbox(
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
send_btn.click(research_chat, inputs=[msg_box, chatbot], outputs=[msg_box, chatbot])
|
| 167 |
msg_box.submit(research_chat, inputs=[msg_box, chatbot], outputs=[msg_box, chatbot])
|
| 168 |
-
clear_btn.click(lambda: ([],
|
| 169 |
|
| 170 |
-
with gr.Tab(
|
| 171 |
-
gr.Markdown(
|
| 172 |
-
voice_chatbot = gr.Chatbot(label=
|
| 173 |
-
audio_input = gr.Audio(sources=[
|
| 174 |
with gr.Row():
|
| 175 |
-
voice_btn = gr.Button(
|
| 176 |
-
voice_clear = gr.Button(
|
| 177 |
voice_btn.click(voice_chat, inputs=[audio_input, voice_chatbot], outputs=voice_chatbot)
|
| 178 |
voice_clear.click(lambda: [], outputs=voice_chatbot)
|
| 179 |
|
| 180 |
-
with gr.Tab(
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
search_btn.click(quick_search, inputs=search_input, outputs=search_output)
|
| 185 |
search_input.submit(quick_search, inputs=search_input, outputs=search_output)
|
|
|
|
| 186 |
|
| 187 |
-
with gr.Tab(
|
| 188 |
-
gr.Markdown(
|
| 189 |
-
gr.
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
diagram_btn.click(generate_diagram, inputs=diagram_input, outputs=diagram_output)
|
|
|
|
| 194 |
|
| 195 |
-
with gr.Tab(
|
|
|
|
| 196 |
with gr.Row():
|
| 197 |
with gr.Column():
|
| 198 |
-
v=gr.Number(label=
|
| 199 |
-
s=gr.Number(label=
|
| 200 |
-
h=gr.Number(label=
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
demo.launch()
|
|
|
|
| 3 |
import requests
|
| 4 |
from groq import Groq
|
| 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 |
+
"MHV: 27mm SJM Regent bileaflet also trileaflet monoleaflet pediatric. "
|
| 13 |
+
"Equipment: Heska HT5 hematology analyzer time-resolved PIV Tygon tubing Arduino Uno.")
|
| 14 |
+
|
| 15 |
+
CSS = """
|
| 16 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;900&display=swap');
|
| 17 |
+
|
| 18 |
+
* { font-family: 'Inter', sans-serif !important; box-sizing: border-box; }
|
| 19 |
+
|
| 20 |
+
body, .gradio-container {
|
| 21 |
+
background: #050d1a !important;
|
| 22 |
+
color: #ffffff !important;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.gradio-container {
|
| 26 |
+
max-width: 1200px !important;
|
| 27 |
+
margin: 0 auto !important;
|
| 28 |
+
padding: 0 !important;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
/* HEADER */
|
| 32 |
+
.main-header {
|
| 33 |
+
background: linear-gradient(135deg, #0d1b3e 0%, #1a0533 50%, #0d1b3e 100%);
|
| 34 |
+
border-bottom: 3px solid #e63946;
|
| 35 |
+
padding: 30px 20px;
|
| 36 |
+
text-align: center;
|
| 37 |
+
margin-bottom: 0;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
/* TABS - ALWAYS VISIBLE */
|
| 41 |
+
.tab-nav {
|
| 42 |
+
background: #0d1b3e !important;
|
| 43 |
+
border-bottom: 2px solid #1e3a6e !important;
|
| 44 |
+
padding: 0 10px !important;
|
| 45 |
+
display: flex !important;
|
| 46 |
+
gap: 5px !important;
|
| 47 |
+
overflow-x: auto !important;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
.tab-nav button {
|
| 51 |
+
background: #132340 !important;
|
| 52 |
+
color: #a0b3d6 !important;
|
| 53 |
+
border: 1px solid #1e3a6e !important;
|
| 54 |
+
border-radius: 8px 8px 0 0 !important;
|
| 55 |
+
padding: 12px 20px !important;
|
| 56 |
+
font-size: 0.9em !important;
|
| 57 |
+
font-weight: 600 !important;
|
| 58 |
+
cursor: pointer !important;
|
| 59 |
+
transition: all 0.2s !important;
|
| 60 |
+
white-space: nowrap !important;
|
| 61 |
+
margin-top: 8px !important;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
.tab-nav button:hover {
|
| 65 |
+
background: #1e3a6e !important;
|
| 66 |
+
color: #ffffff !important;
|
| 67 |
+
border-color: #4361ee !important;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
.tab-nav button.selected {
|
| 71 |
+
background: linear-gradient(135deg, #e63946, #c1121f) !important;
|
| 72 |
+
color: #ffffff !important;
|
| 73 |
+
border-color: #e63946 !important;
|
| 74 |
+
font-weight: 700 !important;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
/* CONTENT AREA */
|
| 78 |
+
.tabitem {
|
| 79 |
+
background: #0a1628 !important;
|
| 80 |
+
padding: 20px !important;
|
| 81 |
+
border: 1px solid #1e3a6e !important;
|
| 82 |
+
border-top: none !important;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
/* INPUTS */
|
| 86 |
+
textarea, input[type=number], input[type=text] {
|
| 87 |
+
background: #132340 !important;
|
| 88 |
+
color: #ffffff !important;
|
| 89 |
+
border: 1px solid #2d4a8a !important;
|
| 90 |
+
border-radius: 8px !important;
|
| 91 |
+
font-size: 0.95em !important;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
textarea:focus, input:focus {
|
| 95 |
+
border-color: #e63946 !important;
|
| 96 |
+
outline: none !important;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
textarea::placeholder {
|
| 100 |
+
color: #5a7aaa !important;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
/* BUTTONS */
|
| 104 |
+
button.primary, .btn-primary {
|
| 105 |
+
background: linear-gradient(135deg, #e63946 0%, #c1121f 100%) !important;
|
| 106 |
+
color: white !important;
|
| 107 |
+
border: none !important;
|
| 108 |
+
border-radius: 8px !important;
|
| 109 |
+
font-weight: 700 !important;
|
| 110 |
+
padding: 12px 24px !important;
|
| 111 |
+
font-size: 0.95em !important;
|
| 112 |
+
cursor: pointer !important;
|
| 113 |
+
transition: transform 0.1s !important;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
button.primary:hover {
|
| 117 |
+
transform: translateY(-1px) !important;
|
| 118 |
+
box-shadow: 0 4px 15px rgba(230, 57, 70, 0.4) !important;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
button.secondary {
|
| 122 |
+
background: #1d3461 !important;
|
| 123 |
+
color: #a0b3d6 !important;
|
| 124 |
+
border: 1px solid #2d4a8a !important;
|
| 125 |
+
border-radius: 8px !important;
|
| 126 |
+
font-weight: 600 !important;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
/* LABELS */
|
| 130 |
+
label span, label, .label-wrap span {
|
| 131 |
+
color: #7eb8f7 !important;
|
| 132 |
+
font-weight: 600 !important;
|
| 133 |
+
font-size: 0.85em !important;
|
| 134 |
+
text-transform: uppercase !important;
|
| 135 |
+
letter-spacing: 0.5px !important;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
/* MARKDOWN TEXT */
|
| 139 |
+
.md h1, .md h2, .md h3 { color: #ffffff !important; }
|
| 140 |
+
.md p, .md li { color: #a0b3d6 !important; }
|
| 141 |
+
|
| 142 |
+
/* CHATBOT */
|
| 143 |
+
.chatbot {
|
| 144 |
+
background: #0a1628 !important;
|
| 145 |
+
border: 1px solid #1e3a6e !important;
|
| 146 |
+
border-radius: 12px !important;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
.message.user {
|
| 150 |
+
background: linear-gradient(135deg, #e63946, #c1121f) !important;
|
| 151 |
+
color: #ffffff !important;
|
| 152 |
+
border-radius: 12px 12px 4px 12px !important;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
.message.bot {
|
| 156 |
+
background: #132340 !important;
|
| 157 |
+
color: #e2e8f0 !important;
|
| 158 |
+
border: 1px solid #2d4a8a !important;
|
| 159 |
+
border-radius: 12px 12px 12px 4px !important;
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
/* RESULT BOXES */
|
| 163 |
+
.gr-box, .block {
|
| 164 |
+
background: #0d1b3e !important;
|
| 165 |
+
border: 1px solid #1e3a6e !important;
|
| 166 |
+
border-radius: 12px !important;
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
/* NUMBERS */
|
| 170 |
+
input[type=number] {
|
| 171 |
+
color: #7eb8f7 !important;
|
| 172 |
+
font-size: 1.1em !important;
|
| 173 |
+
font-weight: 600 !important;
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
/* INFO TEXT */
|
| 177 |
+
.info {
|
| 178 |
+
color: #5a7aaa !important;
|
| 179 |
+
font-size: 0.8em !important;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
/* SCROLLBAR */
|
| 183 |
+
::-webkit-scrollbar { width: 6px; }
|
| 184 |
+
::-webkit-scrollbar-track { background: #0a1628; }
|
| 185 |
+
::-webkit-scrollbar-thumb { background: #2d4a8a; border-radius: 3px; }
|
| 186 |
+
"""
|
| 187 |
|
| 188 |
def get_pubmed(query, n=5):
|
| 189 |
try:
|
| 190 |
+
r = requests.get("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi",
|
| 191 |
+
params={"db":"pubmed","term":query+" AND (mechanical heart valve OR microfluidic OR CKD OR thrombogenicity)",
|
| 192 |
+
"retmax":n,"retmode":"json","sort":"date"},timeout=10)
|
| 193 |
+
ids = r.json()["esearchresult"]["idlist"]
|
| 194 |
+
if not ids: return ""
|
| 195 |
+
return chr(10).join(["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids])
|
| 196 |
+
except: return ""
|
| 197 |
|
| 198 |
def get_scholar(query, n=5):
|
| 199 |
try:
|
| 200 |
+
r = requests.get("https://api.semanticscholar.org/graph/v1/paper/search",
|
| 201 |
+
params={"query":query+" biomedical","limit":n,"fields":"title,year,url,citationCount"},timeout=10)
|
| 202 |
+
papers = r.json().get("data",[])
|
| 203 |
out = []
|
| 204 |
for p in papers:
|
| 205 |
+
title = p.get("title","")
|
| 206 |
+
year = str(p.get("year",""))
|
| 207 |
+
url = p.get("url","")
|
| 208 |
+
citations = str(p.get("citationCount",0))
|
| 209 |
if url:
|
| 210 |
+
out.append(title[:80]+" ("+year+") - "+citations+" citations"+chr(10)+" "+url)
|
| 211 |
return chr(10)+chr(10).join(out)
|
| 212 |
+
except: return ""
|
| 213 |
|
| 214 |
def quick_search(query):
|
| 215 |
+
if not query.strip(): return "Please enter a research topic."
|
| 216 |
pubmed = get_pubmed(query, n=8)
|
| 217 |
scholar = get_scholar(query, n=5)
|
| 218 |
+
result = "PAPERS FOR: " + query + chr(10)+chr(10)
|
| 219 |
+
result += "PUBMED (verified links):"+chr(10)+pubmed+chr(10)+chr(10)
|
| 220 |
+
result += "SEMANTIC SCHOLAR:"+chr(10)+scholar
|
| 221 |
return result
|
| 222 |
|
| 223 |
def research_chat(message, history):
|
| 224 |
if not GROQ_KEY:
|
| 225 |
+
history.append({"role":"user","content":message})
|
| 226 |
+
history.append({"role":"assistant","content":"Error: Add GROQ_API_KEY to Space Settings Secrets tab."})
|
| 227 |
+
return "", history
|
| 228 |
try:
|
| 229 |
client = Groq(api_key=GROQ_KEY)
|
| 230 |
pubmed = get_pubmed(message, n=3)
|
| 231 |
+
system_msg = "You are CardioLab AI assistant. Expert in MHV MCL PIV TGT uPAD CKD FSI. Remember full conversation. Never invent URLs. " + KNOWHOW
|
| 232 |
+
msgs = [{"role":"system","content":system_msg}]
|
| 233 |
for item in history:
|
| 234 |
if isinstance(item, dict):
|
| 235 |
+
msgs.append({"role":item["role"],"content":item["content"]})
|
| 236 |
+
msgs.append({"role":"user","content":message})
|
| 237 |
+
resp = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=msgs,max_tokens=700)
|
| 238 |
answer = resp.choices[0].message.content
|
| 239 |
+
if pubmed: answer += chr(10)+chr(10)+"PUBMED LINKS:"+chr(10)+pubmed
|
| 240 |
+
history.append({"role":"user","content":message})
|
| 241 |
+
history.append({"role":"assistant","content":answer})
|
| 242 |
+
return "", history
|
| 243 |
except Exception as e:
|
| 244 |
+
history.append({"role":"user","content":message})
|
| 245 |
+
history.append({"role":"assistant","content":"Error: "+str(e)})
|
| 246 |
+
return "", history
|
| 247 |
|
| 248 |
def voice_chat(audio, history):
|
| 249 |
if audio is None:
|
| 250 |
+
history.append({"role":"assistant","content":"Please record your question first."})
|
| 251 |
return history
|
| 252 |
try:
|
| 253 |
client = Groq(api_key=GROQ_KEY)
|
| 254 |
+
with open(audio, "rb") as f:
|
| 255 |
transcription = client.audio.transcriptions.create(
|
| 256 |
+
file=("audio.wav", f, "audio/wav"),
|
| 257 |
+
model="whisper-large-v3"
|
| 258 |
)
|
| 259 |
text = transcription.text
|
| 260 |
+
msgs = [{"role":"system","content":"You are CardioLab AI assistant. Expert in MHV MCL PIV TGT uPAD CKD FSI. "+KNOWHOW}]
|
|
|
|
| 261 |
for item in history:
|
| 262 |
if isinstance(item, dict):
|
| 263 |
+
msgs.append({"role":item["role"],"content":item["content"]})
|
| 264 |
+
msgs.append({"role":"user","content":text})
|
| 265 |
+
resp = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=msgs,max_tokens=500)
|
| 266 |
answer = resp.choices[0].message.content
|
| 267 |
+
history.append({"role":"user","content":"[Voice] "+text})
|
| 268 |
+
history.append({"role":"assistant","content":answer})
|
| 269 |
return history
|
| 270 |
except Exception as e:
|
| 271 |
+
history.append({"role":"assistant","content":"Voice error: "+str(e)})
|
| 272 |
return history
|
| 273 |
|
| 274 |
def generate_diagram(topic):
|
| 275 |
+
if not topic.strip(): return "Please enter a topic."
|
| 276 |
try:
|
| 277 |
client = Groq(api_key=GROQ_KEY)
|
| 278 |
+
msgs = [{"role":"system","content":"You are a biomedical engineering expert for SJSU CardioLab. Generate a detailed ASCII diagram or technical schematic. Make it clear, labeled, and useful for researchers."}]
|
| 279 |
+
msgs.append({"role":"user","content":"Create a detailed labeled diagram for: "+topic})
|
| 280 |
+
resp = client.chat.completions.create(model="llama-3.3-70b-versatile",messages=msgs,max_tokens=600)
|
| 281 |
return resp.choices[0].message.content
|
| 282 |
except Exception as e:
|
| 283 |
+
return "Error: "+str(e)
|
| 284 |
|
| 285 |
def piv_tool(velocity, shear, hr):
|
| 286 |
+
v = "HIGH - stenosis risk" if float(velocity)>2.0 else "NORMAL"
|
| 287 |
+
s = "HIGH - thrombosis risk" if float(shear)>10 else "ELEVATED - monitor" if float(shear)>5 else "NORMAL"
|
| 288 |
+
hr_s = "ABNORMAL" if float(hr)<60 or float(hr)>100 else "NORMAL"
|
| 289 |
+
return ("PIV ANALYSIS RESULTS"+chr(10)+
|
| 290 |
+
"ββββββββββββββββββββ"+chr(10)+
|
| 291 |
+
"Velocity: "+str(velocity)+" m/s β "+v+chr(10)+
|
| 292 |
+
"Shear: "+str(shear)+" Pa β "+s+chr(10)+
|
| 293 |
+
"Heart Rate: "+str(hr)+" bpm β "+hr_s)
|
| 294 |
|
| 295 |
def tgt_tool(tat,pf12,hemo,platelets,time):
|
| 296 |
risk=sum([float(tat)>15,float(pf12)>2.0,float(hemo)>50,float(platelets)<150])
|
| 297 |
+
r="HIGH THROMBOGENIC RISK" if risk>=3 else "MODERATE RISK" if risk>=2 else "LOW RISK"
|
| 298 |
+
return ("TGT BLOOD ANALYSIS"+chr(10)+
|
| 299 |
+
"ββββββββββββββββββββ"+chr(10)+
|
| 300 |
+
"Time: "+str(time)+" min"+chr(10)+
|
| 301 |
+
"TAT: "+str(tat)+" β "+("HIGH" if float(tat)>15 else "NORMAL")+chr(10)+
|
| 302 |
+
"PF1.2: "+str(pf12)+" β "+("HIGH" if float(pf12)>2.0 else "NORMAL")+chr(10)+
|
| 303 |
+
"Hemoglobin: "+str(hemo)+" β "+("HIGH" if float(hemo)>50 else "NORMAL")+chr(10)+
|
| 304 |
+
"Platelets: "+str(platelets)+" β "+("LOW" if float(platelets)<150 else "NORMAL")+chr(10)+
|
| 305 |
+
"ββββββββββββββββββββ"+chr(10)+
|
| 306 |
+
"OVERALL: "+r)
|
| 307 |
|
| 308 |
def upad_tool(r,g,b):
|
| 309 |
c=max(0,round(0.02*(float(r)-float(b))-0.5,2))
|
| 310 |
+
if c<1.2: s="Normal - No CKD"
|
| 311 |
+
elif c<1.5: s="Borderline - Monitor"
|
| 312 |
+
elif c<3.0: s="Stage 2 CKD"
|
| 313 |
+
elif c<6.0: s="Stage 3-4 CKD"
|
| 314 |
+
else: s="Stage 5 CKD - Kidney Failure"
|
| 315 |
+
return ("uPAD CKD ANALYSIS"+chr(10)+
|
| 316 |
+
"ββββββββββββββββββββ"+chr(10)+
|
| 317 |
+
"RGB Input: R="+str(r)+" G="+str(g)+" B="+str(b)+chr(10)+
|
| 318 |
+
"Orange Score: "+str(int(float(r)-float(b)))+chr(10)+
|
| 319 |
+
"Creatinine: "+str(c)+" mg/dL"+chr(10)+
|
| 320 |
+
"ββββββββββββββββββββ"+chr(10)+
|
| 321 |
+
"CKD STAGE: "+s+chr(10)+
|
| 322 |
+
"Confirm with: Heska Element HT5")
|
| 323 |
+
|
| 324 |
+
with gr.Blocks(title="CardioLab AI", css=CSS) as demo:
|
| 325 |
|
| 326 |
+
gr.HTML("""
|
| 327 |
+
<div class="main-header">
|
| 328 |
+
<div style="font-size:2.8em; font-weight:900; color:#e63946; letter-spacing:3px; text-shadow: 0 0 30px rgba(230,57,70,0.5);">
|
| 329 |
+
β€οΈ CardioLab AI
|
| 330 |
+
</div>
|
| 331 |
+
</div>
|
| 332 |
+
""")
|
| 333 |
|
| 334 |
with gr.Tabs():
|
| 335 |
+
with gr.Tab("π¬ Chat"):
|
| 336 |
+
chatbot = gr.Chatbot(label="", height=450)
|
| 337 |
with gr.Row():
|
| 338 |
+
msg_box = gr.Textbox(
|
| 339 |
+
placeholder="Ask anything about CardioLab research...",
|
| 340 |
+
label="",
|
| 341 |
+
lines=2,
|
| 342 |
+
scale=4
|
| 343 |
+
)
|
| 344 |
+
with gr.Column(scale=1, min_width=100):
|
| 345 |
+
send_btn = gr.Button("Send β€", variant="primary")
|
| 346 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
| 347 |
send_btn.click(research_chat, inputs=[msg_box, chatbot], outputs=[msg_box, chatbot])
|
| 348 |
msg_box.submit(research_chat, inputs=[msg_box, chatbot], outputs=[msg_box, chatbot])
|
| 349 |
+
clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg_box])
|
| 350 |
|
| 351 |
+
with gr.Tab("ποΈ Voice"):
|
| 352 |
+
gr.Markdown("### Speak your research question β powered by Groq Whisper AI")
|
| 353 |
+
voice_chatbot = gr.Chatbot(label="", height=350)
|
| 354 |
+
audio_input = gr.Audio(sources=["microphone"], type="filepath", label="Record Question")
|
| 355 |
with gr.Row():
|
| 356 |
+
voice_btn = gr.Button("Ask by Voice β€", variant="primary")
|
| 357 |
+
voice_clear = gr.Button("Clear", variant="secondary")
|
| 358 |
voice_btn.click(voice_chat, inputs=[audio_input, voice_chatbot], outputs=voice_chatbot)
|
| 359 |
voice_clear.click(lambda: [], outputs=voice_chatbot)
|
| 360 |
|
| 361 |
+
with gr.Tab("π Papers"):
|
| 362 |
+
gr.Markdown("### Search latest research papers with verified working links")
|
| 363 |
+
with gr.Row():
|
| 364 |
+
search_input = gr.Textbox(
|
| 365 |
+
placeholder="e.g. mechanical heart valve thrombogenicity",
|
| 366 |
+
label="Research Topic",
|
| 367 |
+
scale=4
|
| 368 |
+
)
|
| 369 |
+
search_btn = gr.Button("Search β€", variant="primary", scale=1)
|
| 370 |
+
search_output = gr.Textbox(label="Results β Verified Links Only", lines=18)
|
| 371 |
search_btn.click(quick_search, inputs=search_input, outputs=search_output)
|
| 372 |
search_input.submit(quick_search, inputs=search_input, outputs=search_output)
|
| 373 |
+
gr.Markdown("**Try:** `mechanical heart valve PIV` | `creatinine uPAD microfluidic` | `bileaflet MHV thrombogenicity`")
|
| 374 |
|
| 375 |
+
with gr.Tab("π¨ Diagrams"):
|
| 376 |
+
gr.Markdown("### Generate technical diagrams and schematics for CardioLab")
|
| 377 |
+
with gr.Row():
|
| 378 |
+
diagram_input = gr.Textbox(
|
| 379 |
+
placeholder="e.g. Mock Circulatory Loop with PIV system",
|
| 380 |
+
label="What to diagram",
|
| 381 |
+
scale=4
|
| 382 |
+
)
|
| 383 |
+
diagram_btn = gr.Button("Generate β€", variant="primary", scale=1)
|
| 384 |
+
diagram_output = gr.Textbox(label="Technical Diagram", lines=18)
|
| 385 |
diagram_btn.click(generate_diagram, inputs=diagram_input, outputs=diagram_output)
|
| 386 |
+
gr.Markdown("**Try:** `TGT circuit diagram` | `uPAD fabrication steps` | `PIV optical setup` | `MCL flow loop`")
|
| 387 |
|
| 388 |
+
with gr.Tab("π PIV"):
|
| 389 |
+
gr.Markdown("### Analyze Particle Image Velocimetry data from Mock Circulatory Loop")
|
| 390 |
with gr.Row():
|
| 391 |
with gr.Column():
|
| 392 |
+
v=gr.Number(label="Max Velocity (m/s)", value=1.8, info="Normal range: 0.5 - 2.0 m/s")
|
| 393 |
+
s=gr.Number(label="Wall Shear Stress (Pa)", value=6.5, info="Normal: below 5 Pa")
|
| 394 |
+
h=gr.Number(label="Heart Rate (bpm)", value=72, info="Normal: 60 - 100 bpm")
|
| 395 |
+
piv_btn = gr.Button("Analyze PIV Data β€", variant="primary")
|
| 396 |
+
with gr.Column():
|
| 397 |
+
piv_out=gr.Textbox(label="Analysis Result", lines=8)
|
| 398 |
+
piv_btn.click(piv_tool, inputs=[v,s,h], outputs=piv_out)
|
| 399 |
+
|
| 400 |
+
with gr.Tab("π©Έ TGT"):
|
| 401 |
+
gr.Markdown("### Interpret Thrombogenicity Tester blood analysis results")
|
| 402 |
+
with gr.Row():
|
| 403 |
+
with gr.Column():
|
| 404 |
+
t1=gr.Number(label="TAT (ng/mL)", value=18, info="Normal: below 8")
|
| 405 |
+
t2=gr.Number(label="PF1.2 (nmol/L)", value=2.5, info="Normal: below 2.0")
|
| 406 |
+
t3=gr.Number(label="Free Hemoglobin (mg/L)", value=60, info="Normal: below 20")
|
| 407 |
+
t4=gr.Number(label="Platelet Count (10Β³/ΞΌL)", value=140, info="Normal: above 150")
|
| 408 |
+
t5=gr.Number(label="Time (minutes)", value=40)
|
| 409 |
+
tgt_btn = gr.Button("Analyze TGT Results β€", variant="primary")
|
| 410 |
+
with gr.Column():
|
| 411 |
+
out2=gr.Textbox(label="Analysis Result", lines=12)
|
| 412 |
+
tgt_btn.click(tgt_tool, inputs=[t1,t2,t3,t4,t5], outputs=out2)
|
| 413 |
+
|
| 414 |
+
with gr.Tab("π§ͺ uPAD"):
|
| 415 |
+
gr.Markdown("### Analyze uPAD colorimetric result using Jaffe Reaction for CKD diagnosis")
|
| 416 |
+
with gr.Row():
|
| 417 |
+
with gr.Column():
|
| 418 |
+
gr.Markdown("**Enter RGB values from 64x64 pixel detection zone**")
|
| 419 |
+
r=gr.Number(label="R (Red)", value=210, info="Range: 0 - 255")
|
| 420 |
+
g=gr.Number(label="G (Green)", value=140, info="Range: 0 - 255")
|
| 421 |
+
b=gr.Number(label="B (Blue)", value=80, info="Range: 0 - 255")
|
| 422 |
+
upad_btn = gr.Button("Analyze uPAD β€", variant="primary")
|
| 423 |
+
with gr.Column():
|
| 424 |
+
out3=gr.Textbox(label="CKD Analysis Result", lines=10)
|
| 425 |
+
upad_btn.click(upad_tool, inputs=[r,g,b], outputs=out3)
|
| 426 |
|
| 427 |
demo.launch()
|