Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import PyPDF2
|
| 3 |
import requests
|
|
@@ -5,10 +6,10 @@ import json
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import numpy as np
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
-
import os
|
| 9 |
from datetime import datetime
|
|
|
|
| 10 |
|
| 11 |
-
# Page
|
| 12 |
st.set_page_config(
|
| 13 |
page_title="π¨ First Aid Emergency Assistant",
|
| 14 |
page_icon="π¨",
|
|
@@ -16,7 +17,7 @@ st.set_page_config(
|
|
| 16 |
initial_sidebar_state="collapsed"
|
| 17 |
)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
st.markdown("""
|
| 21 |
<style>
|
| 22 |
/* Main App Background */
|
|
@@ -47,13 +48,6 @@ st.markdown("""
|
|
| 47 |
background-clip: text;
|
| 48 |
}
|
| 49 |
|
| 50 |
-
.main-header p {
|
| 51 |
-
margin: 0.5rem 0 0 0;
|
| 52 |
-
font-size: 1.2rem;
|
| 53 |
-
opacity: 0.8;
|
| 54 |
-
color: #a0aec0;
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
/* Chat Container */
|
| 58 |
.chat-container {
|
| 59 |
background: rgba(26, 32, 46, 0.8);
|
|
@@ -185,6 +179,26 @@ st.markdown("""
|
|
| 185 |
transform: translateY(0) !important;
|
| 186 |
}
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
/* Warning Box */
|
| 189 |
.warning-box {
|
| 190 |
background: linear-gradient(135deg, rgba(255, 107, 107, 0.1) 0%, rgba(255, 154, 158, 0.1) 100%);
|
|
@@ -197,24 +211,25 @@ st.markdown("""
|
|
| 197 |
backdrop-filter: blur(10px);
|
| 198 |
}
|
| 199 |
|
| 200 |
-
/*
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
color: #
|
| 208 |
}
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
}
|
| 214 |
|
| 215 |
-
/* Typing
|
| 216 |
.typing-indicator {
|
| 217 |
background: rgba(45, 55, 72, 0.9);
|
|
|
|
| 218 |
padding: 16px 20px;
|
| 219 |
border-radius: 20px 20px 20px 8px;
|
| 220 |
margin: 16px auto 16px 0;
|
|
@@ -230,349 +245,164 @@ st.markdown("""
|
|
| 230 |
50% { opacity: 1; }
|
| 231 |
100% { opacity: 0.6; }
|
| 232 |
}
|
| 233 |
-
|
| 234 |
-
/* Custom scrollbar for main app */
|
| 235 |
-
.main .block-container {
|
| 236 |
-
padding-top: 2rem;
|
| 237 |
-
}
|
| 238 |
-
|
| 239 |
-
/* Hide Streamlit default elements */
|
| 240 |
-
#MainMenu {visibility: hidden;}
|
| 241 |
-
footer {visibility: hidden;}
|
| 242 |
-
header {visibility: hidden;}
|
| 243 |
-
|
| 244 |
-
/* Custom markdown styling */
|
| 245 |
-
.bot-message h1, .bot-message h2, .bot-message h3 {
|
| 246 |
-
color: #4FACFE !important;
|
| 247 |
-
margin-top: 1rem !important;
|
| 248 |
-
}
|
| 249 |
-
|
| 250 |
-
.bot-message ul, .bot-message ol {
|
| 251 |
-
padding-left: 1.5rem !important;
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
.bot-message strong {
|
| 255 |
-
color: #00F2FE !important;
|
| 256 |
-
}
|
| 257 |
-
|
| 258 |
-
.bot-message code {
|
| 259 |
-
background: rgba(79, 172, 254, 0.1) !important;
|
| 260 |
-
padding: 2px 6px !important;
|
| 261 |
-
border-radius: 4px !important;
|
| 262 |
-
color: #4FACFE !important;
|
| 263 |
-
}
|
| 264 |
</style>
|
| 265 |
""", unsafe_allow_html=True)
|
| 266 |
|
| 267 |
-
#
|
| 268 |
@st.cache_resource
|
| 269 |
def setup_groq():
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
|
|
|
| 273 |
st.stop()
|
| 274 |
-
return groq_api_key
|
| 275 |
|
| 276 |
-
|
|
|
|
|
|
|
| 277 |
@st.cache_resource
|
| 278 |
def load_pdf():
|
| 279 |
try:
|
| 280 |
-
with open("First-Aid.pdf", "rb") as
|
| 281 |
-
|
| 282 |
-
text = ""
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
except FileNotFoundError:
|
| 287 |
-
st.error("π First-Aid.pdf not found! Please upload the PDF file to your space.")
|
| 288 |
-
st.stop()
|
| 289 |
-
except Exception as e:
|
| 290 |
-
st.error(f"β Error loading PDF: {str(e)}")
|
| 291 |
st.stop()
|
| 292 |
|
| 293 |
-
#
|
| 294 |
@st.cache_resource
|
| 295 |
def setup_knowledge_base():
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
# Split text into chunks
|
| 300 |
-
chunks = []
|
| 301 |
-
sentences = pdf_text.split('\n')
|
| 302 |
-
current_chunk = ""
|
| 303 |
-
|
| 304 |
-
for sentence in sentences:
|
| 305 |
if len(current_chunk + sentence) < 1000:
|
| 306 |
current_chunk += sentence + "\n"
|
| 307 |
else:
|
| 308 |
-
if current_chunk.strip():
|
| 309 |
-
chunks.append(current_chunk.strip())
|
| 310 |
current_chunk = sentence + "\n"
|
| 311 |
-
|
| 312 |
-
if current_chunk.strip():
|
| 313 |
-
chunks.append(current_chunk.strip())
|
| 314 |
-
|
| 315 |
-
# Load sentence transformer model
|
| 316 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
chunk_embeddings = model.encode(chunks)
|
| 320 |
-
|
| 321 |
-
return chunks, chunk_embeddings, model
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
query_embedding = model.encode([query])
|
| 326 |
-
similarities = cosine_similarity(query_embedding,
|
| 327 |
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
| 328 |
-
|
| 329 |
-
relevant_chunks = [chunks[i] for i in top_indices]
|
| 330 |
-
return "\n\n".join(relevant_chunks)
|
| 331 |
|
|
|
|
| 332 |
def query_groq(prompt, api_key):
|
| 333 |
-
"""Query GROQ API"""
|
| 334 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 335 |
-
|
| 336 |
-
headers = {
|
| 337 |
-
"Authorization": f"Bearer {api_key}",
|
| 338 |
-
"Content-Type": "application/json"
|
| 339 |
-
}
|
| 340 |
-
|
| 341 |
data = {
|
| 342 |
-
"model": "
|
| 343 |
"messages": [
|
| 344 |
{
|
| 345 |
"role": "system",
|
| 346 |
-
"content": """You are a First Aid Emergency Assistant.
|
| 347 |
-
|
| 348 |
-
IMPORTANT RULES:
|
| 349 |
-
1. Only answer questions related to first aid, medical emergencies, and health safety
|
| 350 |
-
2. If asked about non-medical topics, politely redirect to first aid topics
|
| 351 |
-
3. For serious emergencies, always remind users to call emergency services first
|
| 352 |
-
4. Provide clear, numbered steps when giving instructions
|
| 353 |
-
5. Keep responses focused and practical
|
| 354 |
-
|
| 355 |
-
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."
|
| 356 |
-
"""
|
| 357 |
},
|
| 358 |
-
{
|
| 359 |
-
"role": "user",
|
| 360 |
-
"content": prompt
|
| 361 |
-
}
|
| 362 |
],
|
| 363 |
"temperature": 0.3,
|
| 364 |
"max_tokens": 1000
|
| 365 |
}
|
| 366 |
-
|
| 367 |
try:
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
return
|
| 371 |
-
except requests.exceptions.RequestException as e:
|
| 372 |
-
return f"β Error connecting to GROQ API: {str(e)}"
|
| 373 |
except Exception as e:
|
| 374 |
-
return f"β
|
| 375 |
|
| 376 |
-
#
|
| 377 |
if "messages" not in st.session_state:
|
| 378 |
-
st.session_state.messages = [
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
}
|
| 383 |
-
]
|
| 384 |
|
| 385 |
if "knowledge_base" not in st.session_state:
|
| 386 |
-
with st.spinner("
|
| 387 |
chunks, embeddings, model = setup_knowledge_base()
|
| 388 |
st.session_state.knowledge_base = {
|
| 389 |
-
"chunks": chunks,
|
| 390 |
-
"embeddings": embeddings,
|
| 391 |
-
"model": model
|
| 392 |
}
|
| 393 |
|
| 394 |
-
|
| 395 |
-
|
| 396 |
|
| 397 |
-
#
|
| 398 |
-
st.markdown("""
|
| 399 |
-
<div class="main-header">
|
| 400 |
-
<h1>π¨ First Aid Emergency Assistant</h1>
|
| 401 |
-
<p style="margin: 0; font-size: 18px; opacity: 0.9;">Your AI-powered emergency response guide</p>
|
| 402 |
-
</div>
|
| 403 |
-
""", unsafe_allow_html=True)
|
| 404 |
-
|
| 405 |
-
# Warning disclaimer
|
| 406 |
st.markdown("""
|
| 407 |
<div class="warning-box">
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
This tool is not a substitute for professional medical advice, diagnosis, or treatment.
|
| 411 |
</div>
|
| 412 |
""", unsafe_allow_html=True)
|
| 413 |
|
| 414 |
-
# Chat
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
with chat_container:
|
| 418 |
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
if message["role"] == "user":
|
| 423 |
-
st.markdown(f"""
|
| 424 |
-
<div class="user-message">
|
| 425 |
-
{message["content"]}
|
| 426 |
-
</div>
|
| 427 |
-
<div style="clear: both;"></div>
|
| 428 |
-
""", unsafe_allow_html=True)
|
| 429 |
else:
|
| 430 |
-
st.markdown(f"""
|
| 431 |
-
<div class="bot-message">
|
| 432 |
-
<strong>π€ First Aid Assistant</strong><br>
|
| 433 |
-
{message["content"]}
|
| 434 |
-
</div>
|
| 435 |
-
<div style="clear: both;"></div>
|
| 436 |
-
""", unsafe_allow_html=True)
|
| 437 |
-
|
| 438 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 439 |
|
| 440 |
-
# Input
|
| 441 |
st.markdown('<div class="input-container">', unsafe_allow_html=True)
|
| 442 |
|
| 443 |
-
#
|
| 444 |
with st.form(key="chat_form", clear_on_submit=True):
|
| 445 |
col1, col2 = st.columns([4, 1])
|
| 446 |
-
|
| 447 |
with col1:
|
| 448 |
-
user_input = st.text_input(
|
| 449 |
-
"",
|
| 450 |
-
placeholder="Ask me about first aid emergencies... (Press Enter or click Send)",
|
| 451 |
-
key="user_input_form",
|
| 452 |
-
label_visibility="collapsed"
|
| 453 |
-
)
|
| 454 |
-
|
| 455 |
with col2:
|
| 456 |
-
|
| 457 |
|
| 458 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 459 |
|
| 460 |
-
# Process
|
| 461 |
-
if
|
| 462 |
-
# Add user message
|
| 463 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 464 |
|
| 465 |
-
# Show typing indicator
|
| 466 |
-
with st.
|
| 467 |
-
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 468 |
-
# Display all previous messages
|
| 469 |
-
for message in st.session_state.messages:
|
| 470 |
-
if message["role"] == "user":
|
| 471 |
-
st.markdown(f"""
|
| 472 |
-
<div class="user-message">
|
| 473 |
-
{message["content"]}
|
| 474 |
-
</div>
|
| 475 |
-
<div style="clear: both;"></div>
|
| 476 |
-
""", unsafe_allow_html=True)
|
| 477 |
-
else:
|
| 478 |
-
st.markdown(f"""
|
| 479 |
-
<div class="bot-message">
|
| 480 |
-
<strong>π€ First Aid Assistant</strong><br>
|
| 481 |
-
{message["content"]}
|
| 482 |
-
</div>
|
| 483 |
-
<div style="clear: both;"></div>
|
| 484 |
-
""", unsafe_allow_html=True)
|
| 485 |
-
|
| 486 |
-
# Show typing indicator
|
| 487 |
st.markdown('''
|
| 488 |
-
<div class="
|
| 489 |
-
<
|
| 490 |
-
|
|
|
|
|
|
|
|
|
|
| 491 |
</div>
|
| 492 |
-
<div style="clear: both;"></div>
|
| 493 |
''', unsafe_allow_html=True)
|
| 494 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 495 |
|
| 496 |
-
# Get
|
| 497 |
try:
|
| 498 |
-
# Find relevant context from PDF
|
| 499 |
kb = st.session_state.knowledge_base
|
| 500 |
-
context = find_relevant_context(
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
kb["embeddings"],
|
| 504 |
-
kb["model"]
|
| 505 |
-
)
|
| 506 |
-
|
| 507 |
-
# Create enhanced prompt with context
|
| 508 |
-
enhanced_prompt = f"""
|
| 509 |
-
Based on the following first aid manual content, answer this question: {user_input}
|
| 510 |
|
| 511 |
-
Context
|
| 512 |
{context}
|
| 513 |
-
|
| 514 |
-
Please provide a clear, helpful response based on this information. If this is a serious emergency, remind the user to call emergency services first.
|
| 515 |
"""
|
| 516 |
-
|
| 517 |
-
# Query GROQ API
|
| 518 |
-
response = query_groq(enhanced_prompt, st.session_state.groq_api_key)
|
| 519 |
-
|
| 520 |
-
# Enhance response with emergency reminder for serious cases
|
| 521 |
-
serious_keywords = ['heart attack', 'stroke', 'unconscious', 'not breathing', 'severe bleeding', 'poisoning', 'choking']
|
| 522 |
-
if any(keyword in user_input.lower() for keyword in serious_keywords):
|
| 523 |
-
response = f"π¨ **CALL EMERGENCY SERVICES IMMEDIATELY!**\n\n{response}"
|
| 524 |
-
|
| 525 |
-
except Exception as e:
|
| 526 |
-
response = f"β Sorry, I encountered an error: {str(e)}. Please try asking your question differently."
|
| 527 |
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
# Rerun to show new message without typing indicator
|
| 532 |
-
st.rerun()
|
| 533 |
|
| 534 |
-
#
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
<strong>π¨ Emergency Services:</strong><br>
|
| 540 |
-
β’ General Emergency: 911<br>
|
| 541 |
-
β’ Poison Control: 1-800-222-1222<br>
|
| 542 |
-
β’ Mental Health Crisis: 988
|
| 543 |
-
</div>
|
| 544 |
-
""", unsafe_allow_html=True)
|
| 545 |
-
|
| 546 |
-
st.markdown("## π― What I Can Help With")
|
| 547 |
-
st.markdown("""
|
| 548 |
-
<div class="sidebar-info">
|
| 549 |
-
β’ CPR and rescue breathing<br>
|
| 550 |
-
β’ Wound care and bleeding<br>
|
| 551 |
-
β’ Burns and scalds<br>
|
| 552 |
-
β’ Fractures and sprains<br>
|
| 553 |
-
β’ Choking procedures<br>
|
| 554 |
-
β’ Poisoning emergencies<br>
|
| 555 |
-
β’ Heart attack signs<br>
|
| 556 |
-
β’ Snake and animal bites<br>
|
| 557 |
-
β’ Drowning response<br>
|
| 558 |
-
β’ And much more!
|
| 559 |
-
</div>
|
| 560 |
-
""", unsafe_allow_html=True)
|
| 561 |
|
| 562 |
-
|
| 563 |
-
st.
|
| 564 |
-
<div class="sidebar-info">
|
| 565 |
-
1. Type your first aid question<br>
|
| 566 |
-
2. Get instant step-by-step guidance<br>
|
| 567 |
-
3. Follow instructions carefully<br>
|
| 568 |
-
4. Seek professional help for serious emergencies
|
| 569 |
-
</div>
|
| 570 |
-
""", unsafe_allow_html=True)
|
| 571 |
-
|
| 572 |
-
# Footer
|
| 573 |
-
st.markdown("---")
|
| 574 |
-
st.markdown("""
|
| 575 |
-
<div style="text-align: center; opacity: 0.7; padding: 1rem;">
|
| 576 |
-
π€ First Aid Emergency Assistant | Powered by GROQ AI | Always consult medical professionals for serious emergencies
|
| 577 |
-
</div>
|
| 578 |
-
""", unsafe_allow_html=True)
|
|
|
|
| 1 |
+
# --- Imports ---
|
| 2 |
import streamlit as st
|
| 3 |
import PyPDF2
|
| 4 |
import requests
|
|
|
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
import numpy as np
|
| 8 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
| 9 |
from datetime import datetime
|
| 10 |
+
from groq import Groq
|
| 11 |
|
| 12 |
+
# --- Page config ---
|
| 13 |
st.set_page_config(
|
| 14 |
page_title="π¨ First Aid Emergency Assistant",
|
| 15 |
page_icon="π¨",
|
|
|
|
| 17 |
initial_sidebar_state="collapsed"
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# --- Enhanced Professional UI CSS ---
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
/* Main App Background */
|
|
|
|
| 48 |
background-clip: text;
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
/* Chat Container */
|
| 52 |
.chat-container {
|
| 53 |
background: rgba(26, 32, 46, 0.8);
|
|
|
|
| 179 |
transform: translateY(0) !important;
|
| 180 |
}
|
| 181 |
|
| 182 |
+
/* Form Submit Button Styling */
|
| 183 |
+
.stForm > div > div > button {
|
| 184 |
+
background: linear-gradient(135deg, #4FACFE 0%, #00F2FE 100%) !important;
|
| 185 |
+
color: white !important;
|
| 186 |
+
border: none !important;
|
| 187 |
+
border-radius: 30px !important;
|
| 188 |
+
padding: 16px 32px !important;
|
| 189 |
+
font-weight: 600 !important;
|
| 190 |
+
font-size: 16px !important;
|
| 191 |
+
transition: all 0.3s ease !important;
|
| 192 |
+
box-shadow: 0 4px 20px rgba(79, 172, 254, 0.3) !important;
|
| 193 |
+
width: 100% !important;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
.stForm > div > div > button:hover {
|
| 197 |
+
transform: translateY(-2px) !important;
|
| 198 |
+
box-shadow: 0 8px 30px rgba(79, 172, 254, 0.5) !important;
|
| 199 |
+
background: linear-gradient(135deg, #00F2FE 0%, #4FACFE 100%) !important;
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
/* Warning Box */
|
| 203 |
.warning-box {
|
| 204 |
background: linear-gradient(135deg, rgba(255, 107, 107, 0.1) 0%, rgba(255, 154, 158, 0.1) 100%);
|
|
|
|
| 211 |
backdrop-filter: blur(10px);
|
| 212 |
}
|
| 213 |
|
| 214 |
+
/* Hide Streamlit default elements */
|
| 215 |
+
#MainMenu {visibility: hidden;}
|
| 216 |
+
footer {visibility: hidden;}
|
| 217 |
+
header {visibility: hidden;}
|
| 218 |
+
|
| 219 |
+
/* Bot message formatting */
|
| 220 |
+
.bot-message strong {
|
| 221 |
+
color: #00F2FE !important;
|
| 222 |
}
|
| 223 |
|
| 224 |
+
.bot-message h1, .bot-message h2, .bot-message h3 {
|
| 225 |
+
color: #4FACFE !important;
|
| 226 |
+
margin-top: 1rem !important;
|
| 227 |
}
|
| 228 |
|
| 229 |
+
/* Typing indicator */
|
| 230 |
.typing-indicator {
|
| 231 |
background: rgba(45, 55, 72, 0.9);
|
| 232 |
+
color: #a0aec0;
|
| 233 |
padding: 16px 20px;
|
| 234 |
border-radius: 20px 20px 20px 8px;
|
| 235 |
margin: 16px auto 16px 0;
|
|
|
|
| 245 |
50% { opacity: 1; }
|
| 246 |
100% { opacity: 0.6; }
|
| 247 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
</style>
|
| 249 |
""", unsafe_allow_html=True)
|
| 250 |
|
| 251 |
+
# --- GROQ API setup ---
|
| 252 |
@st.cache_resource
|
| 253 |
def setup_groq():
|
| 254 |
+
try:
|
| 255 |
+
return st.secrets["GROQ_API_KEY"]
|
| 256 |
+
except:
|
| 257 |
+
st.error("β Missing GROQ_API_KEY in `.streamlit/secrets.toml`")
|
| 258 |
st.stop()
|
|
|
|
| 259 |
|
| 260 |
+
client = Groq(api_key=setup_groq())
|
| 261 |
+
|
| 262 |
+
# --- PDF Processing ---
|
| 263 |
@st.cache_resource
|
| 264 |
def load_pdf():
|
| 265 |
try:
|
| 266 |
+
with open("First-Aid.pdf", "rb") as f:
|
| 267 |
+
reader = PyPDF2.PdfReader(f)
|
| 268 |
+
text = "".join(page.extract_text() + "\n" for page in reader.pages)
|
| 269 |
+
return text
|
| 270 |
+
except:
|
| 271 |
+
st.error("β Failed to load 'First-Aid.pdf'. Upload it to the app root.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
st.stop()
|
| 273 |
|
| 274 |
+
# --- Knowledge Base Setup ---
|
| 275 |
@st.cache_resource
|
| 276 |
def setup_knowledge_base():
|
| 277 |
+
text = load_pdf()
|
| 278 |
+
chunks, current_chunk = [], ""
|
| 279 |
+
for sentence in text.split('\n'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
if len(current_chunk + sentence) < 1000:
|
| 281 |
current_chunk += sentence + "\n"
|
| 282 |
else:
|
| 283 |
+
if current_chunk.strip(): chunks.append(current_chunk.strip())
|
|
|
|
| 284 |
current_chunk = sentence + "\n"
|
| 285 |
+
if current_chunk.strip(): chunks.append(current_chunk.strip())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 287 |
+
embeddings = model.encode(chunks)
|
| 288 |
+
return chunks, embeddings, model
|
|
|
|
|
|
|
|
|
|
| 289 |
|
| 290 |
+
# --- Context Matching ---
|
| 291 |
+
def find_relevant_context(query, chunks, embeddings, model, top_k=3):
|
| 292 |
query_embedding = model.encode([query])
|
| 293 |
+
similarities = cosine_similarity(query_embedding, embeddings)[0]
|
| 294 |
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
| 295 |
+
return "\n\n".join([chunks[i] for i in top_indices])
|
|
|
|
|
|
|
| 296 |
|
| 297 |
+
# --- Query GROQ ---
|
| 298 |
def query_groq(prompt, api_key):
|
|
|
|
| 299 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 300 |
+
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
data = {
|
| 302 |
+
"model": "llama3-70b-8192",
|
| 303 |
"messages": [
|
| 304 |
{
|
| 305 |
"role": "system",
|
| 306 |
+
"content": """You are a First Aid Emergency Assistant. Answer only first aid-related queries. Be clear, concise, and step-by-step. For serious emergencies, advise calling 911. If asked off-topic, respond with: "π¨ I'm specialized in First Aid emergencies only!"."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
},
|
| 308 |
+
{"role": "user", "content": prompt}
|
|
|
|
|
|
|
|
|
|
| 309 |
],
|
| 310 |
"temperature": 0.3,
|
| 311 |
"max_tokens": 1000
|
| 312 |
}
|
|
|
|
| 313 |
try:
|
| 314 |
+
r = requests.post(url, headers=headers, json=data, timeout=30)
|
| 315 |
+
r.raise_for_status()
|
| 316 |
+
return r.json()["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
| 317 |
except Exception as e:
|
| 318 |
+
return f"β GROQ API Error: {str(e)}"
|
| 319 |
|
| 320 |
+
# --- Session State ---
|
| 321 |
if "messages" not in st.session_state:
|
| 322 |
+
st.session_state.messages = [{
|
| 323 |
+
"role": "assistant",
|
| 324 |
+
"content": "π¨ **Hello! I'm your First Aid Emergency Assistant.**\n\nAsk me about CPR, bleeding, choking, burns, or any first aid emergency."
|
| 325 |
+
}]
|
|
|
|
|
|
|
| 326 |
|
| 327 |
if "knowledge_base" not in st.session_state:
|
| 328 |
+
with st.spinner("Loading knowledge base..."):
|
| 329 |
chunks, embeddings, model = setup_knowledge_base()
|
| 330 |
st.session_state.knowledge_base = {
|
| 331 |
+
"chunks": chunks, "embeddings": embeddings, "model": model
|
|
|
|
|
|
|
| 332 |
}
|
| 333 |
|
| 334 |
+
# --- Chat UI ---
|
| 335 |
+
st.markdown('<div class="main-header"><h1>π¨ First Aid Emergency Assistant</h1><p style="margin: 0.5rem 0 0 0; font-size: 1.2rem; opacity: 0.8; color: #a0aec0;">Your AI-powered emergency response guide</p></div>', unsafe_allow_html=True)
|
| 336 |
|
| 337 |
+
# Disclaimer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
st.markdown("""
|
| 339 |
<div class="warning-box">
|
| 340 |
+
<strong>β οΈ MEDICAL DISCLAIMER:</strong><br>
|
| 341 |
+
This tool is for informational purposes only. Always call emergency services in real situations.
|
|
|
|
| 342 |
</div>
|
| 343 |
""", unsafe_allow_html=True)
|
| 344 |
|
| 345 |
+
# Chat display
|
| 346 |
+
with st.container():
|
|
|
|
|
|
|
| 347 |
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 348 |
+
for msg in st.session_state.messages:
|
| 349 |
+
if msg["role"] == "user":
|
| 350 |
+
st.markdown(f'<div class="user-message">{msg["content"]}</div><div style="clear: both;"></div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
else:
|
| 352 |
+
st.markdown(f'<div class="bot-message"><strong>π€ First Aid Assistant</strong><br>{msg["content"]}</div><div style="clear: both;"></div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 354 |
|
| 355 |
+
# Enhanced Input UI with Enter key support and auto-clear
|
| 356 |
st.markdown('<div class="input-container">', unsafe_allow_html=True)
|
| 357 |
|
| 358 |
+
# Use form for Enter key support and auto-clear
|
| 359 |
with st.form(key="chat_form", clear_on_submit=True):
|
| 360 |
col1, col2 = st.columns([4, 1])
|
|
|
|
| 361 |
with col1:
|
| 362 |
+
user_input = st.text_input("", placeholder="Ask about a first aid emergency... (Press Enter or click Send)", key="user_input_form", label_visibility="collapsed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
with col2:
|
| 364 |
+
send = st.form_submit_button("Send π")
|
| 365 |
|
| 366 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 367 |
|
| 368 |
+
# Process Input (works with both Enter key and button click)
|
| 369 |
+
if send and user_input and user_input.strip():
|
| 370 |
+
# Add user message
|
| 371 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 372 |
|
| 373 |
+
# Show typing indicator temporarily
|
| 374 |
+
with st.empty():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
st.markdown('''
|
| 376 |
+
<div class="chat-container">
|
| 377 |
+
<div class="typing-indicator">
|
| 378 |
+
<strong>π€ First Aid Assistant</strong><br>
|
| 379 |
+
<em>Typing...</em>
|
| 380 |
+
</div>
|
| 381 |
+
<div style="clear: both;"></div>
|
| 382 |
</div>
|
|
|
|
| 383 |
''', unsafe_allow_html=True)
|
|
|
|
| 384 |
|
| 385 |
+
# Get response
|
| 386 |
try:
|
|
|
|
| 387 |
kb = st.session_state.knowledge_base
|
| 388 |
+
context = find_relevant_context(user_input, kb["chunks"], kb["embeddings"], kb["model"])
|
| 389 |
+
full_prompt = f"""
|
| 390 |
+
Based on this manual content, answer the question: {user_input}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
|
| 392 |
+
Context:
|
| 393 |
{context}
|
|
|
|
|
|
|
| 394 |
"""
|
| 395 |
+
response = query_groq(full_prompt, setup_groq())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
+
# Add emergency warning for serious cases
|
| 398 |
+
if any(x in user_input.lower() for x in ["heart attack", "stroke", "not breathing", "unconscious", "severe bleeding"]):
|
| 399 |
+
response = f"π¨ **CALL EMERGENCY SERVICES IMMEDIATELY!**\n\n{response}"
|
|
|
|
|
|
|
| 400 |
|
| 401 |
+
# Add bot response
|
| 402 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 403 |
+
|
| 404 |
+
except Exception as e:
|
| 405 |
+
st.session_state.messages.append({"role": "assistant", "content": f"β Sorry, I encountered an error: {str(e)}. Please try again."})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
+
# Rerun to show updated chat
|
| 408 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|