Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
| 9 |
from datetime import datetime
|
| 10 |
from groq import Groq
|
| 11 |
|
| 12 |
-
#
|
| 13 |
st.set_page_config(
|
| 14 |
page_title="π¨ First Aid Emergency Assistant",
|
| 15 |
page_icon="π¨",
|
|
@@ -17,8 +17,112 @@ st.set_page_config(
|
|
| 17 |
initial_sidebar_state="collapsed"
|
| 18 |
)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
st.markdown("""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# --- GROQ API setup ---
|
| 24 |
@st.cache_resource
|
|
@@ -114,40 +218,136 @@ This tool is for informational purposes only. Always call emergency services in
|
|
| 114 |
</div>
|
| 115 |
""", unsafe_allow_html=True)
|
| 116 |
|
| 117 |
-
# Chat
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
else:
|
| 124 |
-
st.markdown(f
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 126 |
|
| 127 |
-
# Input
|
| 128 |
st.markdown('<div class="input-container">', unsafe_allow_html=True)
|
|
|
|
| 129 |
col1, col2 = st.columns([4, 1])
|
|
|
|
| 130 |
with col1:
|
| 131 |
-
user_input = st.text_input(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
with col2:
|
| 133 |
-
|
|
|
|
| 134 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 135 |
|
| 136 |
-
# Process
|
| 137 |
-
if
|
|
|
|
| 138 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
| 153 |
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from datetime import datetime
|
| 10 |
from groq import Groq
|
| 11 |
|
| 12 |
+
# Page configuration
|
| 13 |
st.set_page_config(
|
| 14 |
page_title="π¨ First Aid Emergency Assistant",
|
| 15 |
page_icon="π¨",
|
|
|
|
| 17 |
initial_sidebar_state="collapsed"
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Custom CSS for ChatGPT-like interface
|
| 21 |
+
st.markdown("""
|
| 22 |
+
<style>
|
| 23 |
+
.stApp {
|
| 24 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
.main-header {
|
| 28 |
+
text-align: center;
|
| 29 |
+
padding: 1rem 0;
|
| 30 |
+
background: rgba(255, 255, 255, 0.1);
|
| 31 |
+
border-radius: 15px;
|
| 32 |
+
margin-bottom: 2rem;
|
| 33 |
+
backdrop-filter: blur(10px);
|
| 34 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
.chat-container {
|
| 38 |
+
background: white;
|
| 39 |
+
border-radius: 15px;
|
| 40 |
+
padding: 1rem;
|
| 41 |
+
margin: 1rem 0;
|
| 42 |
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
| 43 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 44 |
+
max-height: 500px;
|
| 45 |
+
overflow-y: auto;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
.user-message {
|
| 49 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 50 |
+
color: white;
|
| 51 |
+
padding: 12px 16px;
|
| 52 |
+
border-radius: 18px 18px 5px 18px;
|
| 53 |
+
margin: 8px 0 8px 20%;
|
| 54 |
+
max-width: 80%;
|
| 55 |
+
float: right;
|
| 56 |
+
clear: both;
|
| 57 |
+
box-shadow: 0 2px 10px rgba(102, 126, 234, 0.3);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
.bot-message {
|
| 61 |
+
background: #f8f9fa;
|
| 62 |
+
color: #333;
|
| 63 |
+
padding: 12px 16px;
|
| 64 |
+
border-radius: 18px 18px 18px 5px;
|
| 65 |
+
margin: 8px 20% 8px 0;
|
| 66 |
+
max-width: 80%;
|
| 67 |
+
float: left;
|
| 68 |
+
clear: both;
|
| 69 |
+
border: 1px solid #e9ecef;
|
| 70 |
+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
.input-container {
|
| 74 |
+
position: sticky;
|
| 75 |
+
bottom: 0;
|
| 76 |
+
background: white;
|
| 77 |
+
padding: 1rem;
|
| 78 |
+
border-radius: 15px;
|
| 79 |
+
margin-top: 2rem;
|
| 80 |
+
box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
.warning-box {
|
| 84 |
+
background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
|
| 85 |
+
padding: 1rem;
|
| 86 |
+
border-radius: 10px;
|
| 87 |
+
margin: 1rem 0;
|
| 88 |
+
border-left: 4px solid #ff6b6b;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
.stTextInput input {
|
| 92 |
+
border-radius: 25px !important;
|
| 93 |
+
border: 2px solid #e9ecef !important;
|
| 94 |
+
padding: 12px 20px !important;
|
| 95 |
+
font-size: 16px !important;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
.stTextInput input:focus {
|
| 99 |
+
border-color: #667eea !important;
|
| 100 |
+
box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25) !important;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
.stButton button {
|
| 104 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 105 |
+
color: white !important;
|
| 106 |
+
border: none !important;
|
| 107 |
+
border-radius: 25px !important;
|
| 108 |
+
padding: 12px 30px !important;
|
| 109 |
+
font-weight: 600 !important;
|
| 110 |
+
transition: all 0.3s ease !important;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
.stButton button:hover {
|
| 114 |
+
transform: translateY(-2px) !important;
|
| 115 |
+
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4) !important;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
.sidebar-info {
|
| 119 |
+
background: rgba(255, 255, 255, 0.1);
|
| 120 |
+
padding: 1rem;
|
| 121 |
+
border-radius: 10px;
|
| 122 |
+
margin: 1rem 0;
|
| 123 |
+
}
|
| 124 |
+
</style>
|
| 125 |
+
""", unsafe_allow_html=True)
|
| 126 |
|
| 127 |
# --- GROQ API setup ---
|
| 128 |
@st.cache_resource
|
|
|
|
| 218 |
</div>
|
| 219 |
""", unsafe_allow_html=True)
|
| 220 |
|
| 221 |
+
# Chat container
|
| 222 |
+
chat_container = st.container()
|
| 223 |
+
|
| 224 |
+
with chat_container:
|
| 225 |
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 226 |
+
|
| 227 |
+
# Display chat messages
|
| 228 |
+
for message in st.session_state.messages:
|
| 229 |
+
if message["role"] == "user":
|
| 230 |
+
st.markdown(f"""
|
| 231 |
+
<div class="user-message">
|
| 232 |
+
<strong>You:</strong> {message["content"]}
|
| 233 |
+
</div>
|
| 234 |
+
<div style="clear: both;"></div>
|
| 235 |
+
""", unsafe_allow_html=True)
|
| 236 |
else:
|
| 237 |
+
st.markdown(f"""
|
| 238 |
+
<div class="bot-message">
|
| 239 |
+
<strong>π€ First Aid Assistant:</strong><br>
|
| 240 |
+
{message["content"]}
|
| 241 |
+
</div>
|
| 242 |
+
<div style="clear: both;"></div>
|
| 243 |
+
""", unsafe_allow_html=True)
|
| 244 |
+
|
| 245 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 246 |
|
| 247 |
+
# Input section
|
| 248 |
st.markdown('<div class="input-container">', unsafe_allow_html=True)
|
| 249 |
+
|
| 250 |
col1, col2 = st.columns([4, 1])
|
| 251 |
+
|
| 252 |
with col1:
|
| 253 |
+
user_input = st.text_input(
|
| 254 |
+
"",
|
| 255 |
+
placeholder="Ask me about first aid emergencies... (e.g., 'How to treat burns?')",
|
| 256 |
+
key="user_input",
|
| 257 |
+
label_visibility="collapsed"
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
with col2:
|
| 261 |
+
send_button = st.button("Send π", key="send_button")
|
| 262 |
+
|
| 263 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 264 |
|
| 265 |
+
# Process user input
|
| 266 |
+
if send_button and user_input.strip():
|
| 267 |
+
# Add user message to chat
|
| 268 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 269 |
+
|
| 270 |
+
# Get bot response
|
| 271 |
+
with st.spinner("π€ Thinking..."):
|
| 272 |
+
try:
|
| 273 |
+
# Find relevant context from PDF
|
| 274 |
+
kb = st.session_state.knowledge_base
|
| 275 |
+
context = find_relevant_context(
|
| 276 |
+
user_input,
|
| 277 |
+
kb["chunks"],
|
| 278 |
+
kb["embeddings"],
|
| 279 |
+
kb["model"]
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
# Create enhanced prompt with context
|
| 283 |
+
enhanced_prompt = f"""
|
| 284 |
+
Based on the following first aid manual content, answer this question: {user_input}
|
| 285 |
|
| 286 |
+
Context from First Aid Manual:
|
| 287 |
+
{context}
|
| 288 |
|
| 289 |
+
Please provide a clear, helpful response based on this information. If this is a serious emergency, remind the user to call emergency services first.
|
| 290 |
+
"""
|
| 291 |
+
|
| 292 |
+
# Query GROQ API
|
| 293 |
+
response = query_groq(enhanced_prompt, st.session_state.groq_api_key)
|
| 294 |
+
|
| 295 |
+
# Enhance response with emergency reminder for serious cases
|
| 296 |
+
serious_keywords = ['heart attack', 'stroke', 'unconscious', 'not breathing', 'severe bleeding', 'poisoning', 'choking']
|
| 297 |
+
if any(keyword in user_input.lower() for keyword in serious_keywords):
|
| 298 |
+
response = f"π¨ **CALL EMERGENCY SERVICES IMMEDIATELY!**\n\n{response}"
|
| 299 |
+
|
| 300 |
+
except Exception as e:
|
| 301 |
+
response = f"β Sorry, I encountered an error: {str(e)}. Please try asking your question differently."
|
| 302 |
+
|
| 303 |
+
# Add bot response to chat
|
| 304 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 305 |
+
|
| 306 |
+
# Rerun to show new message
|
| 307 |
st.rerun()
|
| 308 |
+
|
| 309 |
+
# Sidebar with helpful information
|
| 310 |
+
with st.sidebar:
|
| 311 |
+
st.markdown("## π Quick Emergency Numbers")
|
| 312 |
+
st.markdown("""
|
| 313 |
+
<div class="sidebar-info">
|
| 314 |
+
<strong>π¨ Emergency Services:</strong><br>
|
| 315 |
+
β’ General Emergency: 911<br>
|
| 316 |
+
β’ Poison Control: 1-800-222-1222<br>
|
| 317 |
+
β’ Mental Health Crisis: 988
|
| 318 |
+
</div>
|
| 319 |
+
""", unsafe_allow_html=True)
|
| 320 |
+
|
| 321 |
+
st.markdown("## π― What I Can Help With")
|
| 322 |
+
st.markdown("""
|
| 323 |
+
<div class="sidebar-info">
|
| 324 |
+
β’ CPR and rescue breathing<br>
|
| 325 |
+
β’ Wound care and bleeding<br>
|
| 326 |
+
β’ Burns and scalds<br>
|
| 327 |
+
β’ Fractures and sprains<br>
|
| 328 |
+
β’ Choking procedures<br>
|
| 329 |
+
β’ Poisoning emergencies<br>
|
| 330 |
+
β’ Heart attack signs<br>
|
| 331 |
+
β’ Snake and animal bites<br>
|
| 332 |
+
β’ Drowning response<br>
|
| 333 |
+
β’ And much more!
|
| 334 |
+
</div>
|
| 335 |
+
""", unsafe_allow_html=True)
|
| 336 |
+
|
| 337 |
+
st.markdown("## βΉοΈ How to Use")
|
| 338 |
+
st.markdown("""
|
| 339 |
+
<div class="sidebar-info">
|
| 340 |
+
1. Type your first aid question<br>
|
| 341 |
+
2. Get instant step-by-step guidance<br>
|
| 342 |
+
3. Follow instructions carefully<br>
|
| 343 |
+
4. Seek professional help for serious emergencies
|
| 344 |
+
</div>
|
| 345 |
+
""", unsafe_allow_html=True)
|
| 346 |
+
|
| 347 |
+
# Footer
|
| 348 |
+
st.markdown("---")
|
| 349 |
+
st.markdown("""
|
| 350 |
+
<div style="text-align: center; opacity: 0.7; padding: 1rem;">
|
| 351 |
+
π€ First Aid Emergency Assistant | Powered by GROQ AI | Always consult medical professionals for serious emergencies
|
| 352 |
+
</div>
|
| 353 |
+
""", unsafe_allow_html=True)
|