Spaces:
Sleeping
Sleeping
Update app.py
#1
by
shayankhan7
- opened
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import PyPDF2
|
| 3 |
import requests
|
|
@@ -16,122 +17,17 @@ st.set_page_config(
|
|
| 16 |
initial_sidebar_state="collapsed"
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# Custom CSS for
|
| 20 |
-
|
| 21 |
-
<style>
|
| 22 |
-
.stApp {
|
| 23 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
.main-header {
|
| 27 |
-
text-align: center;
|
| 28 |
-
padding: 1rem 0;
|
| 29 |
-
background: rgba(255, 255, 255, 0.1);
|
| 30 |
-
border-radius: 15px;
|
| 31 |
-
margin-bottom: 2rem;
|
| 32 |
-
backdrop-filter: blur(10px);
|
| 33 |
-
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
.chat-container {
|
| 37 |
-
background: white;
|
| 38 |
-
border-radius: 15px;
|
| 39 |
-
padding: 1rem;
|
| 40 |
-
margin: 1rem 0;
|
| 41 |
-
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
| 42 |
-
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 43 |
-
max-height: 500px;
|
| 44 |
-
overflow-y: auto;
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
.user-message {
|
| 48 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 49 |
-
color: white;
|
| 50 |
-
padding: 12px 16px;
|
| 51 |
-
border-radius: 18px 18px 5px 18px;
|
| 52 |
-
margin: 8px 0 8px 20%;
|
| 53 |
-
max-width: 80%;
|
| 54 |
-
float: right;
|
| 55 |
-
clear: both;
|
| 56 |
-
box-shadow: 0 2px 10px rgba(102, 126, 234, 0.3);
|
| 57 |
-
}
|
| 58 |
-
|
| 59 |
-
.bot-message {
|
| 60 |
-
background: #f8f9fa;
|
| 61 |
-
color: #333;
|
| 62 |
-
padding: 12px 16px;
|
| 63 |
-
border-radius: 18px 18px 18px 5px;
|
| 64 |
-
margin: 8px 20% 8px 0;
|
| 65 |
-
max-width: 80%;
|
| 66 |
-
float: left;
|
| 67 |
-
clear: both;
|
| 68 |
-
border: 1px solid #e9ecef;
|
| 69 |
-
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
.input-container {
|
| 73 |
-
position: sticky;
|
| 74 |
-
bottom: 0;
|
| 75 |
-
background: white;
|
| 76 |
-
padding: 1rem;
|
| 77 |
-
border-radius: 15px;
|
| 78 |
-
margin-top: 2rem;
|
| 79 |
-
box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1);
|
| 80 |
-
}
|
| 81 |
-
|
| 82 |
-
.warning-box {
|
| 83 |
-
background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
|
| 84 |
-
padding: 1rem;
|
| 85 |
-
border-radius: 10px;
|
| 86 |
-
margin: 1rem 0;
|
| 87 |
-
border-left: 4px solid #ff6b6b;
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
.stTextInput input {
|
| 91 |
-
border-radius: 25px !important;
|
| 92 |
-
border: 2px solid #e9ecef !important;
|
| 93 |
-
padding: 12px 20px !important;
|
| 94 |
-
font-size: 16px !important;
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
.stTextInput input:focus {
|
| 98 |
-
border-color: #667eea !important;
|
| 99 |
-
box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25) !important;
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
.stButton button {
|
| 103 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 104 |
-
color: white !important;
|
| 105 |
-
border: none !important;
|
| 106 |
-
border-radius: 25px !important;
|
| 107 |
-
padding: 12px 30px !important;
|
| 108 |
-
font-weight: 600 !important;
|
| 109 |
-
transition: all 0.3s ease !important;
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
.stButton button:hover {
|
| 113 |
-
transform: translateY(-2px) !important;
|
| 114 |
-
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4) !important;
|
| 115 |
-
}
|
| 116 |
-
|
| 117 |
-
.sidebar-info {
|
| 118 |
-
background: rgba(255, 255, 255, 0.1);
|
| 119 |
-
padding: 1rem;
|
| 120 |
-
border-radius: 10px;
|
| 121 |
-
margin: 1rem 0;
|
| 122 |
-
}
|
| 123 |
-
</style>
|
| 124 |
-
""", unsafe_allow_html=True)
|
| 125 |
-
|
| 126 |
-
# Initialize GROQ API
|
| 127 |
|
|
|
|
| 128 |
@st.cache_resource
|
| 129 |
def setup_groq():
|
| 130 |
-
|
| 131 |
-
if not
|
| 132 |
-
st.error("⚠️ GROQ API key not found! Please add it to
|
| 133 |
st.stop()
|
| 134 |
-
return
|
| 135 |
|
| 136 |
# Load and process PDF
|
| 137 |
@st.cache_resource
|
|
@@ -144,7 +40,7 @@ def load_pdf():
|
|
| 144 |
text += page.extract_text() + "\n"
|
| 145 |
return text
|
| 146 |
except FileNotFoundError:
|
| 147 |
-
st.error("📄 First-Aid.pdf not found! Please upload the PDF file
|
| 148 |
st.stop()
|
| 149 |
except Exception as e:
|
| 150 |
st.error(f"❌ Error loading PDF: {str(e)}")
|
|
@@ -153,14 +49,10 @@ def load_pdf():
|
|
| 153 |
# Setup embeddings and knowledge base
|
| 154 |
@st.cache_resource
|
| 155 |
def setup_knowledge_base():
|
| 156 |
-
# Load PDF content
|
| 157 |
pdf_text = load_pdf()
|
| 158 |
-
|
| 159 |
-
# Split text into chunks
|
| 160 |
chunks = []
|
| 161 |
sentences = pdf_text.split('\n')
|
| 162 |
current_chunk = ""
|
| 163 |
-
|
| 164 |
for sentence in sentences:
|
| 165 |
if len(current_chunk + sentence) < 1000:
|
| 166 |
current_chunk += sentence + "\n"
|
|
@@ -168,52 +60,31 @@ def setup_knowledge_base():
|
|
| 168 |
if current_chunk.strip():
|
| 169 |
chunks.append(current_chunk.strip())
|
| 170 |
current_chunk = sentence + "\n"
|
| 171 |
-
|
| 172 |
if current_chunk.strip():
|
| 173 |
chunks.append(current_chunk.strip())
|
| 174 |
-
|
| 175 |
-
# Load sentence transformer model
|
| 176 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 177 |
-
|
| 178 |
-
# Create embeddings for chunks
|
| 179 |
chunk_embeddings = model.encode(chunks)
|
| 180 |
-
|
| 181 |
return chunks, chunk_embeddings, model
|
| 182 |
|
| 183 |
def find_relevant_context(query, chunks, chunk_embeddings, model, top_k=3):
|
| 184 |
-
"""Find most relevant chunks for the query"""
|
| 185 |
query_embedding = model.encode([query])
|
| 186 |
similarities = cosine_similarity(query_embedding, chunk_embeddings)[0]
|
| 187 |
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
| 188 |
-
|
| 189 |
-
relevant_chunks = [chunks[i] for i in top_indices]
|
| 190 |
-
return "\n\n".join(relevant_chunks)
|
| 191 |
|
| 192 |
def query_groq(prompt, api_key):
|
| 193 |
-
"""Query GROQ API"""
|
| 194 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 195 |
-
|
| 196 |
headers = {
|
| 197 |
"Authorization": f"Bearer {api_key}",
|
| 198 |
"Content-Type": "application/json"
|
| 199 |
}
|
| 200 |
-
|
| 201 |
data = {
|
| 202 |
"model": "mixtral-8x7b-32768",
|
| 203 |
"messages": [
|
| 204 |
{
|
| 205 |
"role": "system",
|
| 206 |
-
"content": """You are a First Aid Emergency Assistant. You provide clear, step-by-step first aid guidance based on the provided medical manual context.
|
| 207 |
-
|
| 208 |
-
IMPORTANT RULES:
|
| 209 |
-
1. Only answer questions related to first aid, medical emergencies, and health safety
|
| 210 |
-
2. If asked about non-medical topics, politely redirect to first aid topics
|
| 211 |
-
3. For serious emergencies, always remind users to call emergency services first
|
| 212 |
-
4. Provide clear, numbered steps when giving instructions
|
| 213 |
-
5. Keep responses focused and practical
|
| 214 |
-
|
| 215 |
-
If the question is not related to first aid or medical emergencies, respond with: "🚨 I'm specialized in First Aid emergencies only! Please ask me about medical emergencies, CPR, wounds, burns, fractures, or other first aid topics."
|
| 216 |
-
"""
|
| 217 |
},
|
| 218 |
{
|
| 219 |
"role": "user",
|
|
@@ -223,7 +94,6 @@ If the question is not related to first aid or medical emergencies, respond with
|
|
| 223 |
"temperature": 0.3,
|
| 224 |
"max_tokens": 1000
|
| 225 |
}
|
| 226 |
-
|
| 227 |
try:
|
| 228 |
response = requests.post(url, headers=headers, json=data, timeout=30)
|
| 229 |
response.raise_for_status()
|
|
@@ -237,7 +107,7 @@ If the question is not related to first aid or medical emergencies, respond with
|
|
| 237 |
if "messages" not in st.session_state:
|
| 238 |
st.session_state.messages = [
|
| 239 |
{
|
| 240 |
-
"role": "assistant",
|
| 241 |
"content": "🚨 **Hello! I'm your First Aid Emergency Assistant.**\n\nI can help you with:\n• CPR procedures\n• Bleeding control\n• Burns treatment\n• Choking response\n• Fracture management\n• Poisoning emergencies\n• And much more!\n\n💡 **Ask me anything about first aid emergencies!**"
|
| 242 |
}
|
| 243 |
]
|
|
@@ -254,153 +124,13 @@ if "knowledge_base" not in st.session_state:
|
|
| 254 |
if "groq_api_key" not in st.session_state:
|
| 255 |
st.session_state.groq_api_key = setup_groq()
|
| 256 |
|
| 257 |
-
# Header
|
| 258 |
-
|
| 259 |
-
<div class="main-header">
|
| 260 |
-
<h1>🚨 First Aid Emergency Assistant</h1>
|
| 261 |
-
<p style="margin: 0; font-size: 18px; opacity: 0.9;">Your AI-powered emergency response guide</p>
|
| 262 |
-
</div>
|
| 263 |
-
""", unsafe_allow_html=True)
|
| 264 |
-
|
| 265 |
-
# Warning disclaimer
|
| 266 |
-
st.markdown("""
|
| 267 |
-
<div class="warning-box">
|
| 268 |
-
<strong>⚠️ IMPORTANT MEDICAL DISCLAIMER:</strong><br>
|
| 269 |
-
This chatbot provides general first aid guidance only. In real emergencies, always call emergency services immediately.
|
| 270 |
-
This tool is not a substitute for professional medical advice, diagnosis, or treatment.
|
| 271 |
-
</div>
|
| 272 |
-
""", unsafe_allow_html=True)
|
| 273 |
-
|
| 274 |
-
# Chat container
|
| 275 |
-
chat_container = st.container()
|
| 276 |
-
|
| 277 |
-
with chat_container:
|
| 278 |
-
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 279 |
-
|
| 280 |
-
# Display chat messages
|
| 281 |
-
for message in st.session_state.messages:
|
| 282 |
-
if message["role"] == "user":
|
| 283 |
-
st.markdown(f"""
|
| 284 |
-
<div class="user-message">
|
| 285 |
-
<strong>You:</strong> {message["content"]}
|
| 286 |
-
</div>
|
| 287 |
-
<div style="clear: both;"></div>
|
| 288 |
-
""", unsafe_allow_html=True)
|
| 289 |
-
else:
|
| 290 |
-
st.markdown(f"""
|
| 291 |
-
<div class="bot-message">
|
| 292 |
-
<strong>🤖 First Aid Assistant:</strong><br>
|
| 293 |
-
{message["content"]}
|
| 294 |
-
</div>
|
| 295 |
-
<div style="clear: both;"></div>
|
| 296 |
-
""", unsafe_allow_html=True)
|
| 297 |
-
|
| 298 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 299 |
-
|
| 300 |
-
# Input section
|
| 301 |
-
st.markdown('<div class="input-container">', unsafe_allow_html=True)
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
user_input = st.text_input(
|
| 307 |
-
"",
|
| 308 |
-
placeholder="Ask me about first aid emergencies... (e.g., 'How to treat burns?')",
|
| 309 |
-
key="user_input",
|
| 310 |
-
label_visibility="collapsed"
|
| 311 |
-
)
|
| 312 |
-
|
| 313 |
-
with col2:
|
| 314 |
-
send_button = st.button("Send 🚀", key="send_button")
|
| 315 |
-
|
| 316 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 317 |
-
|
| 318 |
-
# Process user input
|
| 319 |
-
if send_button and user_input.strip():
|
| 320 |
-
# Add user message to chat
|
| 321 |
-
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 322 |
-
|
| 323 |
-
# Get bot response
|
| 324 |
-
with st.spinner("🤔 Thinking..."):
|
| 325 |
-
try:
|
| 326 |
-
# Find relevant context from PDF
|
| 327 |
-
kb = st.session_state.knowledge_base
|
| 328 |
-
context = find_relevant_context(
|
| 329 |
-
user_input,
|
| 330 |
-
kb["chunks"],
|
| 331 |
-
kb["embeddings"],
|
| 332 |
-
kb["model"]
|
| 333 |
-
)
|
| 334 |
-
|
| 335 |
-
# Create enhanced prompt with context
|
| 336 |
-
enhanced_prompt = f"""
|
| 337 |
-
Based on the following first aid manual content, answer this question: {user_input}
|
| 338 |
-
|
| 339 |
-
Context from First Aid Manual:
|
| 340 |
-
{context}
|
| 341 |
-
|
| 342 |
-
Please provide a clear, helpful response based on this information. If this is a serious emergency, remind the user to call emergency services first.
|
| 343 |
-
"""
|
| 344 |
-
|
| 345 |
-
# Query GROQ API
|
| 346 |
-
response = query_groq(enhanced_prompt, st.session_state.groq_api_key)
|
| 347 |
-
|
| 348 |
-
# Enhance response with emergency reminder for serious cases
|
| 349 |
-
serious_keywords = ['heart attack', 'stroke', 'unconscious', 'not breathing', 'severe bleeding', 'poisoning', 'choking']
|
| 350 |
-
if any(keyword in user_input.lower() for keyword in serious_keywords):
|
| 351 |
-
response = f"🚨 **CALL EMERGENCY SERVICES IMMEDIATELY!**\n\n{response}"
|
| 352 |
-
|
| 353 |
-
except Exception as e:
|
| 354 |
-
response = f"❌ Sorry, I encountered an error: {str(e)}. Please try asking your question differently."
|
| 355 |
-
|
| 356 |
-
# Add bot response to chat
|
| 357 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 358 |
-
|
| 359 |
-
# Rerun to show new message
|
| 360 |
st.rerun()
|
| 361 |
|
| 362 |
-
#
|
| 363 |
-
with
|
| 364 |
-
st.markdown("## 📋 Quick Emergency Numbers")
|
| 365 |
-
st.markdown("""
|
| 366 |
-
<div class="sidebar-info">
|
| 367 |
-
<strong>🚨 Emergency Services:</strong><br>
|
| 368 |
-
• General Emergency: 911<br>
|
| 369 |
-
• Poison Control: 1-800-222-1222<br>
|
| 370 |
-
• Mental Health Crisis: 988
|
| 371 |
-
</div>
|
| 372 |
-
""", unsafe_allow_html=True)
|
| 373 |
-
|
| 374 |
-
st.markdown("## 🎯 What I Can Help With")
|
| 375 |
-
st.markdown("""
|
| 376 |
-
<div class="sidebar-info">
|
| 377 |
-
• CPR and rescue breathing<br>
|
| 378 |
-
• Wound care and bleeding<br>
|
| 379 |
-
• Burns and scalds<br>
|
| 380 |
-
• Fractures and sprains<br>
|
| 381 |
-
• Choking procedures<br>
|
| 382 |
-
• Poisoning emergencies<br>
|
| 383 |
-
• Heart attack signs<br>
|
| 384 |
-
• Snake and animal bites<br>
|
| 385 |
-
• Drowning response<br>
|
| 386 |
-
• And much more!
|
| 387 |
-
</div>
|
| 388 |
-
""", unsafe_allow_html=True)
|
| 389 |
-
|
| 390 |
-
st.markdown("## ℹ️ How to Use")
|
| 391 |
-
st.markdown("""
|
| 392 |
-
<div class="sidebar-info">
|
| 393 |
-
1. Type your first aid question<br>
|
| 394 |
-
2. Get instant step-by-step guidance<br>
|
| 395 |
-
3. Follow instructions carefully<br>
|
| 396 |
-
4. Seek professional help for serious emergencies
|
| 397 |
-
</div>
|
| 398 |
-
""", unsafe_allow_html=True)
|
| 399 |
-
|
| 400 |
-
# Footer
|
| 401 |
-
st.markdown("---")
|
| 402 |
-
st.markdown("""
|
| 403 |
-
<div style="text-align: center; opacity: 0.7; padding: 1rem;">
|
| 404 |
-
🤖 First Aid Emergency Assistant | Powered by GROQ AI | Always consult medical professionals for serious emergencies
|
| 405 |
-
</div>
|
| 406 |
-
""", unsafe_allow_html=True)
|
|
|
|
| 1 |
+
# updated_app.py
|
| 2 |
import streamlit as st
|
| 3 |
import PyPDF2
|
| 4 |
import requests
|
|
|
|
| 17 |
initial_sidebar_state="collapsed"
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Custom CSS (unchanged for brevity, keep original styling from your code)
|
| 21 |
+
# ... [unchanged CSS block here] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Initialize GROQ API Key from secrets
|
| 24 |
@st.cache_resource
|
| 25 |
def setup_groq():
|
| 26 |
+
api_key = st.secrets.get("GROQ_API_KEY")
|
| 27 |
+
if not api_key:
|
| 28 |
+
st.error("⚠️ GROQ API key not found in secrets! Please add it to .streamlit/secrets.toml")
|
| 29 |
st.stop()
|
| 30 |
+
return api_key
|
| 31 |
|
| 32 |
# Load and process PDF
|
| 33 |
@st.cache_resource
|
|
|
|
| 40 |
text += page.extract_text() + "\n"
|
| 41 |
return text
|
| 42 |
except FileNotFoundError:
|
| 43 |
+
st.error("📄 First-Aid.pdf not found! Please upload the PDF file.")
|
| 44 |
st.stop()
|
| 45 |
except Exception as e:
|
| 46 |
st.error(f"❌ Error loading PDF: {str(e)}")
|
|
|
|
| 49 |
# Setup embeddings and knowledge base
|
| 50 |
@st.cache_resource
|
| 51 |
def setup_knowledge_base():
|
|
|
|
| 52 |
pdf_text = load_pdf()
|
|
|
|
|
|
|
| 53 |
chunks = []
|
| 54 |
sentences = pdf_text.split('\n')
|
| 55 |
current_chunk = ""
|
|
|
|
| 56 |
for sentence in sentences:
|
| 57 |
if len(current_chunk + sentence) < 1000:
|
| 58 |
current_chunk += sentence + "\n"
|
|
|
|
| 60 |
if current_chunk.strip():
|
| 61 |
chunks.append(current_chunk.strip())
|
| 62 |
current_chunk = sentence + "\n"
|
|
|
|
| 63 |
if current_chunk.strip():
|
| 64 |
chunks.append(current_chunk.strip())
|
| 65 |
+
|
|
|
|
| 66 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
|
|
|
| 67 |
chunk_embeddings = model.encode(chunks)
|
|
|
|
| 68 |
return chunks, chunk_embeddings, model
|
| 69 |
|
| 70 |
def find_relevant_context(query, chunks, chunk_embeddings, model, top_k=3):
|
|
|
|
| 71 |
query_embedding = model.encode([query])
|
| 72 |
similarities = cosine_similarity(query_embedding, chunk_embeddings)[0]
|
| 73 |
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
| 74 |
+
return "\n\n".join([chunks[i] for i in top_indices])
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def query_groq(prompt, api_key):
|
|
|
|
| 77 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
|
|
|
| 78 |
headers = {
|
| 79 |
"Authorization": f"Bearer {api_key}",
|
| 80 |
"Content-Type": "application/json"
|
| 81 |
}
|
|
|
|
| 82 |
data = {
|
| 83 |
"model": "mixtral-8x7b-32768",
|
| 84 |
"messages": [
|
| 85 |
{
|
| 86 |
"role": "system",
|
| 87 |
+
"content": """You are a First Aid Emergency Assistant. You provide clear, step-by-step first aid guidance based on the provided medical manual context. \nIMPORTANT RULES:\n1. Only answer questions related to first aid, medical emergencies, and health safety\n2. If asked about non-medical topics, politely redirect to first aid topics\n3. For serious emergencies, always remind users to call emergency services first\n4. Provide clear, numbered steps when giving instructions\n5. Keep responses focused and practical\nIf the question is not related to first aid or medical emergencies, respond with: \"🚨 I'm specialized in First Aid emergencies only! Please ask me about medical emergencies, CPR, wounds, burns, fractures, or other first aid topics.\""".strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
},
|
| 89 |
{
|
| 90 |
"role": "user",
|
|
|
|
| 94 |
"temperature": 0.3,
|
| 95 |
"max_tokens": 1000
|
| 96 |
}
|
|
|
|
| 97 |
try:
|
| 98 |
response = requests.post(url, headers=headers, json=data, timeout=30)
|
| 99 |
response.raise_for_status()
|
|
|
|
| 107 |
if "messages" not in st.session_state:
|
| 108 |
st.session_state.messages = [
|
| 109 |
{
|
| 110 |
+
"role": "assistant",
|
| 111 |
"content": "🚨 **Hello! I'm your First Aid Emergency Assistant.**\n\nI can help you with:\n• CPR procedures\n• Bleeding control\n• Burns treatment\n• Choking response\n• Fracture management\n• Poisoning emergencies\n• And much more!\n\n💡 **Ask me anything about first aid emergencies!**"
|
| 112 |
}
|
| 113 |
]
|
|
|
|
| 124 |
if "groq_api_key" not in st.session_state:
|
| 125 |
st.session_state.groq_api_key = setup_groq()
|
| 126 |
|
| 127 |
+
# Header UI, Sidebar, and Input box (unchanged from original)
|
| 128 |
+
# ... [keep the rest of your UI and chat input logic unchanged] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
# Add "Clear Chat" button
|
| 131 |
+
if st.sidebar.button("🧹 Clear Chat"):
|
| 132 |
+
st.session_state.messages = [st.session_state.messages[0]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
st.rerun()
|
| 134 |
|
| 135 |
+
# Handle user input and response (unchanged)
|
| 136 |
+
# ... [same as original block with improved exception handling if desired] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|