Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -102,19 +102,28 @@ def determine_patient_status(original_plan, updated_plan, feedback):
|
|
| 102 |
"difficulty breathing", "severe shortness of breath", "wheezing",
|
| 103 |
"severe abdominal pain", "persistent vomiting", "uncontrolled bleeding",
|
| 104 |
"severe allergic reaction", "anaphylaxis", "immediate medical attention",
|
| 105 |
-
"emergency", "call 911", "urgent care", "hospital", "critical"
|
|
|
|
|
|
|
| 106 |
]
|
| 107 |
|
| 108 |
deteriorating_keywords = [
|
| 109 |
"worsening", "increased pain", "not improving", "deteriorating",
|
| 110 |
"elevated", "higher", "more frequent", "concerning", "monitor closely",
|
| 111 |
-
"decline", "decreased function", "less able", "more difficult"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
]
|
| 113 |
|
| 114 |
improvement_keywords = [
|
| 115 |
"improving", "better", "reduced", "lower", "less pain", "increased function",
|
| 116 |
"healing", "recovery", "progress", "stable", "maintained", "consistent",
|
| 117 |
-
"well-controlled", "responsive", "good progress"
|
|
|
|
|
|
|
|
|
|
| 118 |
]
|
| 119 |
|
| 120 |
original_plan_lower = original_plan.lower() if original_plan else ""
|
|
@@ -130,13 +139,19 @@ def determine_patient_status(original_plan, updated_plan, feedback):
|
|
| 130 |
return "emergency"
|
| 131 |
|
| 132 |
# Check for deteriorating keywords
|
| 133 |
-
if any(keyword in feedback_lower for keyword in deteriorating_keywords)
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
return "deteriorating"
|
| 136 |
|
| 137 |
# Check for improvement keywords
|
| 138 |
-
if any(keyword in feedback_lower for keyword in improvement_keywords)
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
| 140 |
return "improving"
|
| 141 |
|
| 142 |
# Default to stable if no clear indicators
|
|
@@ -252,23 +267,27 @@ def generate_care_plan_pdf(patient_info, care_plan_text, status):
|
|
| 252 |
# Process care plan text by sections
|
| 253 |
sections = re.findall(r'([A-Z][A-Z\s]+):(.*?)(?=\n[A-Z][A-Z\s]+:|$)', care_plan_text, re.DOTALL)
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
# Build the PDF
|
| 274 |
doc.build(story)
|
|
@@ -294,23 +313,27 @@ def send_whatsapp_care_plan(patient_name, care_plan_text, status):
|
|
| 294 |
# Extract and format sections
|
| 295 |
sections = re.findall(r'([A-Z][A-Z\s]+):(.*?)(?=\n[A-Z][A-Z\s]+:|$)', care_plan_text, re.DOTALL)
|
| 296 |
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
# Send WhatsApp message using hardcoded number
|
| 316 |
twilio_client.messages.create(
|
|
@@ -372,7 +395,6 @@ def submit_feedback():
|
|
| 372 |
care_plan_format = """
|
| 373 |
ASSESSMENT:
|
| 374 |
- [Assessment details]
|
| 375 |
-
|
| 376 |
DAILY CARE PLAN:
|
| 377 |
Morning:
|
| 378 |
- [Morning activities]
|
|
@@ -380,13 +402,10 @@ Afternoon:
|
|
| 380 |
- [Afternoon activities]
|
| 381 |
Evening:
|
| 382 |
- [Evening activities]
|
| 383 |
-
|
| 384 |
MEDICATIONS:
|
| 385 |
- [Medication details]
|
| 386 |
-
|
| 387 |
ADDITIONAL RECOMMENDATIONS:
|
| 388 |
- [Recommendations]
|
| 389 |
-
|
| 390 |
FOLLOW-UP:
|
| 391 |
- [Follow-up details]
|
| 392 |
"""
|
|
@@ -467,14 +486,11 @@ Patient Information:
|
|
| 467 |
Name: {name}
|
| 468 |
Age: {age}
|
| 469 |
Gender: {gender}
|
| 470 |
-
|
| 471 |
Patient Care Plan Update Request:
|
| 472 |
Current Symptoms and Feedback: {feedback}
|
| 473 |
Current Care Plan (extracted from PDF): {care_plan_text}
|
| 474 |
-
|
| 475 |
Based on the patient's feedback and current care plan, please provide an updated, perfect daily care plan in the exact following format:
|
| 476 |
{care_plan_format}
|
| 477 |
-
|
| 478 |
Please ensure the following:
|
| 479 |
- The response is well-structured with clear section headings.
|
| 480 |
- Use bullet points for list items.
|
|
@@ -536,22 +552,30 @@ Please ensure the following:
|
|
| 536 |
|
| 537 |
@app.route('/download_pdf/<patient_id>')
|
| 538 |
def download_pdf(patient_id):
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
|
| 556 |
|
| 557 |
@app.route('/get_emergency_notifications')
|
|
|
|
| 102 |
"difficulty breathing", "severe shortness of breath", "wheezing",
|
| 103 |
"severe abdominal pain", "persistent vomiting", "uncontrolled bleeding",
|
| 104 |
"severe allergic reaction", "anaphylaxis", "immediate medical attention",
|
| 105 |
+
"emergency", "call 911", "urgent care", "hospital", "critical", "ambulance",
|
| 106 |
+
"collapsed", "unconscious", "unresponsive", "stroke", "seizure", "convulsion",
|
| 107 |
+
"suffocating", "not breathing", "blue lips", "blue face", "cardiac arrest"
|
| 108 |
]
|
| 109 |
|
| 110 |
deteriorating_keywords = [
|
| 111 |
"worsening", "increased pain", "not improving", "deteriorating",
|
| 112 |
"elevated", "higher", "more frequent", "concerning", "monitor closely",
|
| 113 |
+
"decline", "decreased function", "less able", "more difficult", "worse",
|
| 114 |
+
"getting worse", "aggravated", "intensified", "escalating", "degrading",
|
| 115 |
+
"falling", "weakening", "relapse", "recurrence", "regressing", "not responding",
|
| 116 |
+
"increased symptoms", "more severe", "progressing", "progressive", "complicated",
|
| 117 |
+
"concerning development", "adverse change", "unstable", "needs attention"
|
| 118 |
]
|
| 119 |
|
| 120 |
improvement_keywords = [
|
| 121 |
"improving", "better", "reduced", "lower", "less pain", "increased function",
|
| 122 |
"healing", "recovery", "progress", "stable", "maintained", "consistent",
|
| 123 |
+
"well-controlled", "responsive", "good progress", "getting better", "positive",
|
| 124 |
+
"improved", "enhancement", "advancement", "resolving", "resolved", "recovering",
|
| 125 |
+
"normalized", "normal range", "responding well", "responding positively",
|
| 126 |
+
"effective treatment", "successful treatment", "managed well", "under control"
|
| 127 |
]
|
| 128 |
|
| 129 |
original_plan_lower = original_plan.lower() if original_plan else ""
|
|
|
|
| 139 |
return "emergency"
|
| 140 |
|
| 141 |
# Check for deteriorating keywords
|
| 142 |
+
if any(keyword in feedback_lower for keyword in deteriorating_keywords):
|
| 143 |
+
return "deteriorating"
|
| 144 |
+
|
| 145 |
+
# Further check for deteriorating indicators in updated plan
|
| 146 |
+
if any(keyword in updated_plan_lower for keyword in deteriorating_keywords):
|
| 147 |
return "deteriorating"
|
| 148 |
|
| 149 |
# Check for improvement keywords
|
| 150 |
+
if any(keyword in feedback_lower for keyword in improvement_keywords):
|
| 151 |
+
return "improving"
|
| 152 |
+
|
| 153 |
+
# Further check for improvement indicators in updated plan
|
| 154 |
+
if any(keyword in updated_plan_lower for keyword in improvement_keywords):
|
| 155 |
return "improving"
|
| 156 |
|
| 157 |
# Default to stable if no clear indicators
|
|
|
|
| 267 |
# Process care plan text by sections
|
| 268 |
sections = re.findall(r'([A-Z][A-Z\s]+):(.*?)(?=\n[A-Z][A-Z\s]+:|$)', care_plan_text, re.DOTALL)
|
| 269 |
|
| 270 |
+
if not sections:
|
| 271 |
+
# If no sections found, add the entire text as a paragraph
|
| 272 |
+
story.append(Paragraph(care_plan_text, normal_style))
|
| 273 |
+
else:
|
| 274 |
+
for section_title, section_content in sections:
|
| 275 |
+
story.append(Paragraph(section_title + ":", heading_style))
|
| 276 |
+
|
| 277 |
+
# Process bullet points if they exist
|
| 278 |
+
bullet_points = re.findall(r'[-•]\s*(.*?)(?=[-•]|\n|$)', section_content.strip())
|
| 279 |
+
|
| 280 |
+
if bullet_points:
|
| 281 |
+
for point in bullet_points:
|
| 282 |
+
if point.strip():
|
| 283 |
+
story.append(Paragraph(f"• {point.strip()}", bullet_style))
|
| 284 |
+
else:
|
| 285 |
+
# If no bullet points, add the content as a paragraph
|
| 286 |
+
cleaned_content = section_content.strip()
|
| 287 |
+
if cleaned_content:
|
| 288 |
+
story.append(Paragraph(cleaned_content, normal_style))
|
| 289 |
+
|
| 290 |
+
story.append(Spacer(1, 5))
|
| 291 |
|
| 292 |
# Build the PDF
|
| 293 |
doc.build(story)
|
|
|
|
| 313 |
# Extract and format sections
|
| 314 |
sections = re.findall(r'([A-Z][A-Z\s]+):(.*?)(?=\n[A-Z][A-Z\s]+:|$)', care_plan_text, re.DOTALL)
|
| 315 |
|
| 316 |
+
if not sections:
|
| 317 |
+
# If no sections found, add the entire text
|
| 318 |
+
message += care_plan_text
|
| 319 |
+
else:
|
| 320 |
+
for section_title, section_content in sections:
|
| 321 |
+
message += f"*{section_title}:*\n"
|
| 322 |
+
|
| 323 |
+
# Process bullet points if they exist
|
| 324 |
+
bullet_points = re.findall(r'[-•]\s*(.*?)(?=[-•]|\n|$)', section_content.strip())
|
| 325 |
+
|
| 326 |
+
if bullet_points:
|
| 327 |
+
for point in bullet_points:
|
| 328 |
+
if point.strip():
|
| 329 |
+
message += f"• {point.strip()}\n"
|
| 330 |
+
else:
|
| 331 |
+
# If no bullet points, add the content as a paragraph
|
| 332 |
+
cleaned_content = section_content.strip()
|
| 333 |
+
if cleaned_content:
|
| 334 |
+
message += f"{cleaned_content}\n"
|
| 335 |
+
|
| 336 |
+
message += "\n"
|
| 337 |
|
| 338 |
# Send WhatsApp message using hardcoded number
|
| 339 |
twilio_client.messages.create(
|
|
|
|
| 395 |
care_plan_format = """
|
| 396 |
ASSESSMENT:
|
| 397 |
- [Assessment details]
|
|
|
|
| 398 |
DAILY CARE PLAN:
|
| 399 |
Morning:
|
| 400 |
- [Morning activities]
|
|
|
|
| 402 |
- [Afternoon activities]
|
| 403 |
Evening:
|
| 404 |
- [Evening activities]
|
|
|
|
| 405 |
MEDICATIONS:
|
| 406 |
- [Medication details]
|
|
|
|
| 407 |
ADDITIONAL RECOMMENDATIONS:
|
| 408 |
- [Recommendations]
|
|
|
|
| 409 |
FOLLOW-UP:
|
| 410 |
- [Follow-up details]
|
| 411 |
"""
|
|
|
|
| 486 |
Name: {name}
|
| 487 |
Age: {age}
|
| 488 |
Gender: {gender}
|
|
|
|
| 489 |
Patient Care Plan Update Request:
|
| 490 |
Current Symptoms and Feedback: {feedback}
|
| 491 |
Current Care Plan (extracted from PDF): {care_plan_text}
|
|
|
|
| 492 |
Based on the patient's feedback and current care plan, please provide an updated, perfect daily care plan in the exact following format:
|
| 493 |
{care_plan_format}
|
|
|
|
| 494 |
Please ensure the following:
|
| 495 |
- The response is well-structured with clear section headings.
|
| 496 |
- Use bullet points for list items.
|
|
|
|
| 552 |
|
| 553 |
@app.route('/download_pdf/<patient_id>')
|
| 554 |
def download_pdf(patient_id):
|
| 555 |
+
try:
|
| 556 |
+
if patient_id not in patients_db:
|
| 557 |
+
return "Patient not found", 404
|
| 558 |
+
|
| 559 |
+
patient = patients_db[patient_id]
|
| 560 |
+
|
| 561 |
+
# Use the patient's stored information to generate the PDF
|
| 562 |
+
pdf_buffer = generate_care_plan_pdf({
|
| 563 |
+
'name': patient['name'],
|
| 564 |
+
'age': patient['age'],
|
| 565 |
+
'gender': patient['gender']
|
| 566 |
+
}, patient['updated_plan'], patient['status'])
|
| 567 |
+
|
| 568 |
+
pdf_buffer.seek(0) # Ensure buffer is at the start
|
| 569 |
+
|
| 570 |
+
return send_file(
|
| 571 |
+
pdf_buffer,
|
| 572 |
+
as_attachment=True,
|
| 573 |
+
download_name=f"care_plan_{patient['name'].replace(' ', '_')}.pdf",
|
| 574 |
+
mimetype='application/pdf'
|
| 575 |
+
)
|
| 576 |
+
except Exception as e:
|
| 577 |
+
print(f"PDF Download Error: {str(e)}")
|
| 578 |
+
return f"Error generating PDF: {str(e)}", 500
|
| 579 |
|
| 580 |
|
| 581 |
@app.route('/get_emergency_notifications')
|