Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -14,21 +14,15 @@ KNOWHOW = ('SJSU CardioLab: '
|
|
| 14 |
|
| 15 |
CSS = (
|
| 16 |
'body, .gradio-container { background: #0a0f1e !important; }'
|
| 17 |
-
'.
|
| 18 |
-
'.tab-nav button { color: #
|
| 19 |
-
'.tab-nav button.selected { color: #e63946 !important; border-bottom: 3px solid #e63946 !important; background: transparent !important; }'
|
| 20 |
'.tab-nav button:hover { color: #ffffff !important; background: #1a2744 !important; }'
|
| 21 |
-
'h1
|
| 22 |
-
'
|
| 23 |
'textarea, input[type=number], input[type=text] { background: #1a2744 !important; color: #e2e8f0 !important; border: 1px solid #4361ee !important; border-radius: 8px !important; }'
|
| 24 |
-
'
|
| 25 |
-
'
|
| 26 |
-
'button.primary { background: linear-gradient(135deg, #e63946 0%, #c1121f 100%) !important; color: white !important; border: none !important; }'
|
| 27 |
-
'button.secondary { background: #1d3461 !important; color: #a8b2d8 !important; border: 1px solid #4361ee !important; }'
|
| 28 |
-
'.message.bot { background: #1a2744 !important; border: 1px solid #2d4a8a !important; color: #e2e8f0 !important; border-radius: 12px !important; }'
|
| 29 |
-
'.message.user { background: linear-gradient(135deg, #e63946, #c1121f) !important; color: white !important; border-radius: 12px !important; }'
|
| 30 |
'.block { background: #0d1b3e !important; border: 1px solid #1e3a6e !important; border-radius: 12px !important; }'
|
| 31 |
-
'.chatbot { background: #0d1b3e !important; border: 1px solid #2d4a8a !important; border-radius: 12px !important; }'
|
| 32 |
)
|
| 33 |
|
| 34 |
def get_pubmed(query, n=5):
|
|
@@ -91,6 +85,44 @@ def research_chat(message, history):
|
|
| 91 |
history.append({'role':'assistant','content':'Error: '+str(e)})
|
| 92 |
return '', history
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
def piv_tool(velocity, shear, hr):
|
| 95 |
v = 'HIGH - stenosis risk' if float(velocity)>2.0 else 'NORMAL'
|
| 96 |
s = 'HIGH - thrombosis risk' if float(shear)>10 else 'ELEVATED' if float(shear)>5 else 'NORMAL'
|
|
@@ -120,8 +152,8 @@ with gr.Blocks(title='CardioLab AI', css=CSS) as demo:
|
|
| 120 |
gr.Markdown('# CardioLab AI')
|
| 121 |
|
| 122 |
with gr.Tabs():
|
| 123 |
-
with gr.Tab('
|
| 124 |
-
chatbot = gr.Chatbot(label='', height=
|
| 125 |
with gr.Row():
|
| 126 |
msg_box = gr.Textbox(placeholder='Ask anything about CardioLab research...', label='', lines=2, scale=4)
|
| 127 |
with gr.Column(scale=1):
|
|
@@ -131,14 +163,32 @@ with gr.Blocks(title='CardioLab AI', css=CSS) as demo:
|
|
| 131 |
msg_box.submit(research_chat, inputs=[msg_box, chatbot], outputs=[msg_box, chatbot])
|
| 132 |
clear_btn.click(lambda: ([], ''), outputs=[chatbot, msg_box])
|
| 133 |
|
| 134 |
-
with gr.Tab('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
search_input = gr.Textbox(placeholder='e.g. mechanical heart valve thrombogenicity', label='Research topic')
|
| 136 |
search_btn = gr.Button('Search Papers', variant='primary')
|
| 137 |
search_output = gr.Textbox(label='Results', lines=20)
|
| 138 |
search_btn.click(quick_search, inputs=search_input, outputs=search_output)
|
| 139 |
search_input.submit(quick_search, inputs=search_input, outputs=search_output)
|
| 140 |
|
| 141 |
-
with gr.Tab('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
with gr.Row():
|
| 143 |
with gr.Column():
|
| 144 |
v=gr.Number(label='Max Velocity m/s', value=1.8)
|
|
@@ -147,7 +197,7 @@ with gr.Blocks(title='CardioLab AI', css=CSS) as demo:
|
|
| 147 |
piv_out=gr.Textbox(label='Result', lines=5)
|
| 148 |
gr.Button('Analyze PIV', variant='primary').click(piv_tool,inputs=[v,s,h],outputs=piv_out)
|
| 149 |
|
| 150 |
-
with gr.Tab('
|
| 151 |
t1=gr.Number(label='TAT ng/mL', value=18)
|
| 152 |
t2=gr.Number(label='PF1.2 nmol/L', value=2.5)
|
| 153 |
t3=gr.Number(label='Free Hemoglobin mg/L', value=60)
|
|
@@ -156,7 +206,7 @@ with gr.Blocks(title='CardioLab AI', css=CSS) as demo:
|
|
| 156 |
out2=gr.Textbox(label='Result', lines=8)
|
| 157 |
gr.Button('Analyze TGT', variant='primary').click(tgt_tool,inputs=[t1,t2,t3,t4,t5],outputs=out2)
|
| 158 |
|
| 159 |
-
with gr.Tab('
|
| 160 |
r=gr.Number(label='R value', value=210)
|
| 161 |
g=gr.Number(label='G value', value=140)
|
| 162 |
b=gr.Number(label='B value', value=80)
|
|
|
|
| 14 |
|
| 15 |
CSS = (
|
| 16 |
'body, .gradio-container { background: #0a0f1e !important; }'
|
| 17 |
+
'.tab-nav button { color: #a8b2d8 !important; background: transparent !important; font-weight: 600 !important; padding: 10px 20px !important; border: none !important; border-bottom: 3px solid transparent !important; }'
|
| 18 |
+
'.tab-nav button.selected { color: #e63946 !important; border-bottom: 3px solid #e63946 !important; }'
|
|
|
|
| 19 |
'.tab-nav button:hover { color: #ffffff !important; background: #1a2744 !important; }'
|
| 20 |
+
'h1 { color: #e63946 !important; font-size: 2.8em !important; font-weight: 900 !important; text-align: center !important; padding: 25px 0 !important; }'
|
| 21 |
+
'h3, label, p, span { color: #e2e8f0 !important; }'
|
| 22 |
'textarea, input[type=number], input[type=text] { background: #1a2744 !important; color: #e2e8f0 !important; border: 1px solid #4361ee !important; border-radius: 8px !important; }'
|
| 23 |
+
'button.primary { background: linear-gradient(135deg, #e63946 0%, #c1121f 100%) !important; color: white !important; border: none !important; border-radius: 8px !important; }'
|
| 24 |
+
'button.secondary { background: #1d3461 !important; color: #a8b2d8 !important; border: 1px solid #4361ee !important; border-radius: 8px !important; }'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
'.block { background: #0d1b3e !important; border: 1px solid #1e3a6e !important; border-radius: 12px !important; }'
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
def get_pubmed(query, n=5):
|
|
|
|
| 85 |
history.append({'role':'assistant','content':'Error: '+str(e)})
|
| 86 |
return '', history
|
| 87 |
|
| 88 |
+
def voice_chat(audio, history):
|
| 89 |
+
if audio is None:
|
| 90 |
+
history.append({'role':'assistant','content':'Please record your question first.'})
|
| 91 |
+
return history
|
| 92 |
+
try:
|
| 93 |
+
client = Groq(api_key=GROQ_KEY)
|
| 94 |
+
with open(audio, 'rb') as f:
|
| 95 |
+
transcription = client.audio.transcriptions.create(
|
| 96 |
+
file=('audio.wav', f, 'audio/wav'),
|
| 97 |
+
model='whisper-large-v3'
|
| 98 |
+
)
|
| 99 |
+
text = transcription.text
|
| 100 |
+
system_msg = 'You are CardioLab AI assistant. Expert in MHV MCL PIV TGT uPAD CKD FSI. ' + KNOWHOW
|
| 101 |
+
msgs = [{'role':'system','content':system_msg}]
|
| 102 |
+
for item in history:
|
| 103 |
+
if isinstance(item, dict):
|
| 104 |
+
msgs.append({'role':item['role'],'content':item['content']})
|
| 105 |
+
msgs.append({'role':'user','content':text})
|
| 106 |
+
resp = client.chat.completions.create(model='llama-3.3-70b-versatile',messages=msgs,max_tokens=500)
|
| 107 |
+
answer = resp.choices[0].message.content
|
| 108 |
+
history.append({'role':'user','content':'[Voice]: '+text})
|
| 109 |
+
history.append({'role':'assistant','content':answer})
|
| 110 |
+
return history
|
| 111 |
+
except Exception as e:
|
| 112 |
+
history.append({'role':'assistant','content':'Voice error: '+str(e)})
|
| 113 |
+
return history
|
| 114 |
+
|
| 115 |
+
def generate_diagram(topic):
|
| 116 |
+
if not topic.strip(): return 'Please enter a topic.'
|
| 117 |
+
try:
|
| 118 |
+
client = Groq(api_key=GROQ_KEY)
|
| 119 |
+
msgs = [{'role':'system','content':'You are a biomedical engineering diagram expert for SJSU CardioLab. Generate a detailed ASCII diagram or technical schematic based on the topic. Make it clear, labeled, and useful for researchers.'}]
|
| 120 |
+
msgs.append({'role':'user','content':'Create a detailed labeled diagram for: '+topic})
|
| 121 |
+
resp = client.chat.completions.create(model='llama-3.3-70b-versatile',messages=msgs,max_tokens=600)
|
| 122 |
+
return resp.choices[0].message.content
|
| 123 |
+
except Exception as e:
|
| 124 |
+
return 'Error: '+str(e)
|
| 125 |
+
|
| 126 |
def piv_tool(velocity, shear, hr):
|
| 127 |
v = 'HIGH - stenosis risk' if float(velocity)>2.0 else 'NORMAL'
|
| 128 |
s = 'HIGH - thrombosis risk' if float(shear)>10 else 'ELEVATED' if float(shear)>5 else 'NORMAL'
|
|
|
|
| 152 |
gr.Markdown('# CardioLab AI')
|
| 153 |
|
| 154 |
with gr.Tabs():
|
| 155 |
+
with gr.Tab('Chat'):
|
| 156 |
+
chatbot = gr.Chatbot(label='', height=450)
|
| 157 |
with gr.Row():
|
| 158 |
msg_box = gr.Textbox(placeholder='Ask anything about CardioLab research...', label='', lines=2, scale=4)
|
| 159 |
with gr.Column(scale=1):
|
|
|
|
| 163 |
msg_box.submit(research_chat, inputs=[msg_box, chatbot], outputs=[msg_box, chatbot])
|
| 164 |
clear_btn.click(lambda: ([], ''), outputs=[chatbot, msg_box])
|
| 165 |
|
| 166 |
+
with gr.Tab('Voice'):
|
| 167 |
+
gr.Markdown('### Speak your question - powered by Groq Whisper')
|
| 168 |
+
voice_chatbot = gr.Chatbot(label='Voice Chat', height=350)
|
| 169 |
+
audio_input = gr.Audio(sources=['microphone'], type='filepath', label='Record your question')
|
| 170 |
+
with gr.Row():
|
| 171 |
+
voice_btn = gr.Button('Ask by Voice', variant='primary')
|
| 172 |
+
voice_clear = gr.Button('Clear', variant='secondary')
|
| 173 |
+
voice_btn.click(voice_chat, inputs=[audio_input, voice_chatbot], outputs=voice_chatbot)
|
| 174 |
+
voice_clear.click(lambda: [], outputs=voice_chatbot)
|
| 175 |
+
|
| 176 |
+
with gr.Tab('Paper Search'):
|
| 177 |
search_input = gr.Textbox(placeholder='e.g. mechanical heart valve thrombogenicity', label='Research topic')
|
| 178 |
search_btn = gr.Button('Search Papers', variant='primary')
|
| 179 |
search_output = gr.Textbox(label='Results', lines=20)
|
| 180 |
search_btn.click(quick_search, inputs=search_input, outputs=search_output)
|
| 181 |
search_input.submit(quick_search, inputs=search_input, outputs=search_output)
|
| 182 |
|
| 183 |
+
with gr.Tab('Diagram Generator'):
|
| 184 |
+
gr.Markdown('### Generate technical diagrams and schematics for CardioLab')
|
| 185 |
+
gr.Markdown('Examples: MCL setup | TGT circuit | uPAD fabrication flow | PIV system | valve design')
|
| 186 |
+
diagram_input = gr.Textbox(placeholder='e.g. Mock Circulatory Loop setup with PIV system', label='What to diagram')
|
| 187 |
+
diagram_btn = gr.Button('Generate Diagram', variant='primary')
|
| 188 |
+
diagram_output = gr.Textbox(label='Diagram', lines=20)
|
| 189 |
+
diagram_btn.click(generate_diagram, inputs=diagram_input, outputs=diagram_output)
|
| 190 |
+
|
| 191 |
+
with gr.Tab('PIV Analysis'):
|
| 192 |
with gr.Row():
|
| 193 |
with gr.Column():
|
| 194 |
v=gr.Number(label='Max Velocity m/s', value=1.8)
|
|
|
|
| 197 |
piv_out=gr.Textbox(label='Result', lines=5)
|
| 198 |
gr.Button('Analyze PIV', variant='primary').click(piv_tool,inputs=[v,s,h],outputs=piv_out)
|
| 199 |
|
| 200 |
+
with gr.Tab('TGT Results'):
|
| 201 |
t1=gr.Number(label='TAT ng/mL', value=18)
|
| 202 |
t2=gr.Number(label='PF1.2 nmol/L', value=2.5)
|
| 203 |
t3=gr.Number(label='Free Hemoglobin mg/L', value=60)
|
|
|
|
| 206 |
out2=gr.Textbox(label='Result', lines=8)
|
| 207 |
gr.Button('Analyze TGT', variant='primary').click(tgt_tool,inputs=[t1,t2,t3,t4,t5],outputs=out2)
|
| 208 |
|
| 209 |
+
with gr.Tab('uPAD CKD'):
|
| 210 |
r=gr.Number(label='R value', value=210)
|
| 211 |
g=gr.Number(label='G value', value=140)
|
| 212 |
b=gr.Number(label='B value', value=80)
|