Update src/streamlit_app.py
Browse files- src/streamlit_app.py +41 -69
src/streamlit_app.py
CHANGED
|
@@ -3,6 +3,7 @@ import uuid
|
|
| 3 |
from datetime import datetime
|
| 4 |
import json
|
| 5 |
import os
|
|
|
|
| 6 |
from model import (
|
| 7 |
orchestrator_chat,
|
| 8 |
fetch_medical_evidence,
|
|
@@ -15,7 +16,7 @@ st.set_page_config(
|
|
| 15 |
page_title="Medical AI Assistant",
|
| 16 |
page_icon=None,
|
| 17 |
layout="wide",
|
| 18 |
-
initial_sidebar_state="
|
| 19 |
)
|
| 20 |
|
| 21 |
# Custom CSS for styling with purple->teal gradient and dark mode
|
|
@@ -45,6 +46,12 @@ st.markdown("""
|
|
| 45 |
background-color: #121212;
|
| 46 |
}
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
/* Chat container */
|
| 49 |
.stChatFloatingInputContainer {
|
| 50 |
border-radius: 16px;
|
|
@@ -140,52 +147,6 @@ st.markdown("""
|
|
| 140 |
color: rgba(255, 107, 107, 0.8);
|
| 141 |
}
|
| 142 |
|
| 143 |
-
/* New toggle switch */
|
| 144 |
-
.toggle-container {
|
| 145 |
-
display: flex;
|
| 146 |
-
align-items: center;
|
| 147 |
-
margin-bottom: 1rem;
|
| 148 |
-
background-color: #1e1e24;
|
| 149 |
-
padding: 8px 16px;
|
| 150 |
-
border-radius: 12px;
|
| 151 |
-
}
|
| 152 |
-
.toggle-label {
|
| 153 |
-
margin-right: 10px;
|
| 154 |
-
font-weight: 500;
|
| 155 |
-
color: #f0f0f0;
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
-
/* ChatGPT-like toggle style */
|
| 159 |
-
.chatgpt-toggle {
|
| 160 |
-
display: flex;
|
| 161 |
-
align-items: center;
|
| 162 |
-
justify-content: flex-end;
|
| 163 |
-
background-color: rgba(32, 33, 35, 0.5);
|
| 164 |
-
padding: 8px 16px;
|
| 165 |
-
border-radius: 8px;
|
| 166 |
-
margin-bottom: 10px;
|
| 167 |
-
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
.chatgpt-toggle .stToggle>label {
|
| 171 |
-
background: rgba(122, 95, 255, 0.2);
|
| 172 |
-
padding: 4px 10px;
|
| 173 |
-
font-size: 0.8rem;
|
| 174 |
-
}
|
| 175 |
-
|
| 176 |
-
/* Fixed bottom controls */
|
| 177 |
-
.controls-container {
|
| 178 |
-
position: fixed;
|
| 179 |
-
bottom: 80px;
|
| 180 |
-
left: 0;
|
| 181 |
-
right: 0;
|
| 182 |
-
z-index: 1000;
|
| 183 |
-
padding: 10px 50px;
|
| 184 |
-
background-color: rgba(18, 18, 18, 0.7);
|
| 185 |
-
backdrop-filter: blur(10px);
|
| 186 |
-
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
| 187 |
-
}
|
| 188 |
-
|
| 189 |
/* Legal disclaimer (small and muted) */
|
| 190 |
.footer-text {
|
| 191 |
font-size: 0.7rem;
|
|
@@ -195,6 +156,7 @@ st.markdown("""
|
|
| 195 |
position: fixed;
|
| 196 |
bottom: 0;
|
| 197 |
width: 100%;
|
|
|
|
| 198 |
}
|
| 199 |
</style>
|
| 200 |
""", unsafe_allow_html=True)
|
|
@@ -207,6 +169,31 @@ if 'consultation_id' not in st.session_state:
|
|
| 207 |
if 'use_rag' not in st.session_state:
|
| 208 |
st.session_state.use_rag = True
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
# Display chat history
|
| 211 |
for message in st.session_state.history:
|
| 212 |
if message["role"] == "user":
|
|
@@ -215,32 +202,14 @@ for message in st.session_state.history:
|
|
| 215 |
else:
|
| 216 |
with st.chat_message("assistant"):
|
| 217 |
st.markdown(message["content"])
|
| 218 |
-
# Only display the explanation in an expander if it exists
|
| 219 |
-
if message.get("explanation"):
|
| 220 |
with st.expander("Show Reasoning"):
|
| 221 |
st.markdown(message["explanation"])
|
| 222 |
if message.get("evidence"):
|
| 223 |
st.markdown("---")
|
| 224 |
st.markdown("**Legend:** 🔓 = Open Access (full text available)")
|
| 225 |
|
| 226 |
-
# Create a container at the bottom for the toggle and input
|
| 227 |
-
with st.container():
|
| 228 |
-
# Add spacing
|
| 229 |
-
st.markdown("<div style='height: 60px'></div>", unsafe_allow_html=True)
|
| 230 |
-
|
| 231 |
-
# Create a custom container for the toggle at the bottom
|
| 232 |
-
st.markdown("<div class='controls-container'>", unsafe_allow_html=True)
|
| 233 |
-
|
| 234 |
-
# ChatGPT-style toggle
|
| 235 |
-
col1, col2 = st.columns([3, 1])
|
| 236 |
-
with col2:
|
| 237 |
-
st.markdown("<div class='chatgpt-toggle'>", unsafe_allow_html=True)
|
| 238 |
-
st.session_state.use_rag = st.toggle("Database Search", value=st.session_state.use_rag,
|
| 239 |
-
help="Toggle to enable or disable medical database search")
|
| 240 |
-
st.markdown("</div>", unsafe_allow_html=True)
|
| 241 |
-
|
| 242 |
-
st.markdown("</div>", unsafe_allow_html=True)
|
| 243 |
-
|
| 244 |
# Chat input
|
| 245 |
if prompt := st.chat_input("Describe your symptoms or ask a medical question..."):
|
| 246 |
# Add user message to chat
|
|
@@ -270,8 +239,8 @@ if prompt := st.chat_input("Describe your symptoms or ask a medical question..."
|
|
| 270 |
# Display response
|
| 271 |
st.markdown(reply)
|
| 272 |
|
| 273 |
-
# Add explanation in an expander ONLY if explanation
|
| 274 |
-
if explanation:
|
| 275 |
with st.expander("Show Reasoning"):
|
| 276 |
st.markdown(explanation)
|
| 277 |
|
|
@@ -287,6 +256,9 @@ if prompt := st.chat_input("Describe your symptoms or ask a medical question..."
|
|
| 287 |
"explanation": explanation,
|
| 288 |
"evidence": evidence if evidence else []
|
| 289 |
})
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
# Small, unobtrusive legal disclaimer
|
| 292 |
st.markdown("""
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
+
import re
|
| 7 |
from model import (
|
| 8 |
orchestrator_chat,
|
| 9 |
fetch_medical_evidence,
|
|
|
|
| 16 |
page_title="Medical AI Assistant",
|
| 17 |
page_icon=None,
|
| 18 |
layout="wide",
|
| 19 |
+
initial_sidebar_state="expanded" # Keep sidebar expanded to show the toggle
|
| 20 |
)
|
| 21 |
|
| 22 |
# Custom CSS for styling with purple->teal gradient and dark mode
|
|
|
|
| 46 |
background-color: #121212;
|
| 47 |
}
|
| 48 |
|
| 49 |
+
/* Sidebar styling */
|
| 50 |
+
section[data-testid="stSidebar"] {
|
| 51 |
+
background-color: #1e1e24;
|
| 52 |
+
border-right: 1px solid rgba(122, 95, 255, 0.2);
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
/* Chat container */
|
| 56 |
.stChatFloatingInputContainer {
|
| 57 |
border-radius: 16px;
|
|
|
|
| 147 |
color: rgba(255, 107, 107, 0.8);
|
| 148 |
}
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
/* Legal disclaimer (small and muted) */
|
| 151 |
.footer-text {
|
| 152 |
font-size: 0.7rem;
|
|
|
|
| 156 |
position: fixed;
|
| 157 |
bottom: 0;
|
| 158 |
width: 100%;
|
| 159 |
+
z-index: 99;
|
| 160 |
}
|
| 161 |
</style>
|
| 162 |
""", unsafe_allow_html=True)
|
|
|
|
| 169 |
if 'use_rag' not in st.session_state:
|
| 170 |
st.session_state.use_rag = True
|
| 171 |
|
| 172 |
+
# Helper function to check if explanation has meaningful content
|
| 173 |
+
def has_meaningful_content(text):
|
| 174 |
+
if not text:
|
| 175 |
+
return False
|
| 176 |
+
|
| 177 |
+
# Check if the text is just equal signs or other separators
|
| 178 |
+
stripped_text = text.strip()
|
| 179 |
+
if re.match(r'^[=\-_*]+$', stripped_text.replace('\n', '')):
|
| 180 |
+
return False
|
| 181 |
+
|
| 182 |
+
# Check if the text only contains "## REASONING" with no actual content
|
| 183 |
+
if "## REASONING" in stripped_text and len(stripped_text) < 20:
|
| 184 |
+
return False
|
| 185 |
+
|
| 186 |
+
return True
|
| 187 |
+
|
| 188 |
+
# Put the toggle in the sidebar - this is the most reliable approach
|
| 189 |
+
with st.sidebar:
|
| 190 |
+
st.header("Settings")
|
| 191 |
+
st.session_state.use_rag = st.toggle("Database Search", value=st.session_state.use_rag,
|
| 192 |
+
help="Toggle to enable or disable medical database search")
|
| 193 |
+
|
| 194 |
+
# Main app area
|
| 195 |
+
st.title("Medical AI Assistant")
|
| 196 |
+
|
| 197 |
# Display chat history
|
| 198 |
for message in st.session_state.history:
|
| 199 |
if message["role"] == "user":
|
|
|
|
| 202 |
else:
|
| 203 |
with st.chat_message("assistant"):
|
| 204 |
st.markdown(message["content"])
|
| 205 |
+
# Only display the explanation in an expander if it exists AND has actual content
|
| 206 |
+
if message.get("explanation") and has_meaningful_content(message.get("explanation")):
|
| 207 |
with st.expander("Show Reasoning"):
|
| 208 |
st.markdown(message["explanation"])
|
| 209 |
if message.get("evidence"):
|
| 210 |
st.markdown("---")
|
| 211 |
st.markdown("**Legend:** 🔓 = Open Access (full text available)")
|
| 212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
# Chat input
|
| 214 |
if prompt := st.chat_input("Describe your symptoms or ask a medical question..."):
|
| 215 |
# Add user message to chat
|
|
|
|
| 239 |
# Display response
|
| 240 |
st.markdown(reply)
|
| 241 |
|
| 242 |
+
# Add explanation in an expander ONLY if explanation has meaningful content
|
| 243 |
+
if explanation and has_meaningful_content(explanation):
|
| 244 |
with st.expander("Show Reasoning"):
|
| 245 |
st.markdown(explanation)
|
| 246 |
|
|
|
|
| 256 |
"explanation": explanation,
|
| 257 |
"evidence": evidence if evidence else []
|
| 258 |
})
|
| 259 |
+
|
| 260 |
+
# Force refresh to update the UI
|
| 261 |
+
st.rerun()
|
| 262 |
|
| 263 |
# Small, unobtrusive legal disclaimer
|
| 264 |
st.markdown("""
|