rajkhanke commited on
Commit
95837f2
·
verified ·
1 Parent(s): d6712c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -83
app.py CHANGED
@@ -652,7 +652,7 @@ def doctor_dashboard():
652
  logger.info("Accessed doctor dashboard, setting role to doctor.")
653
  return render_template('doctor_dashboard.html')
654
 
655
- # --- START: Modified submit_feedback function (includes optimized prompt logic) ---
656
  @app.route('/submit_feedback', methods=['POST'])
657
  def submit_feedback():
658
  ai_enabled = bool(model) # Check if model is initialized
@@ -681,6 +681,7 @@ def submit_feedback():
681
  age = None # Store as None if not provided
682
 
683
  care_plan_text = "" # This will store the extracted text from PDF
 
684
 
685
  if 'care_plan_pdf' in request.files:
686
  pdf_file = request.files['care_plan_pdf']
@@ -693,35 +694,25 @@ def submit_feedback():
693
  logger.info(f"Processing uploaded PDF: {pdf_file.filename}")
694
  care_plan_text = extract_text_from_pdf(pdf_file)
695
 
696
- # If extraction resulted in an error message, the variable will contain it.
697
- # This error message will be passed to the AI as "Previous Care Plan Details".
698
- logger.info(f"Extracted text length: {len(care_plan_text)}. Begins with: {care_plan_text[:100]}...")
 
 
 
 
699
  else:
700
  logger.info("No PDF file uploaded or file is empty.")
701
 
702
- # Determine the initial status based on feedback and original plan text
703
  # Pass "" for updated_plan initially, as it hasn't been generated yet for status check
704
  initial_status = determine_patient_status(care_plan_text, "", feedback)
705
  logger.info(f"Initial status determined based on feedback/original plan: {initial_status}")
706
 
 
707
  generated_plan_text = "" # This will store the AI-generated or fallback plan
708
  final_status_to_save = initial_status # Start with initial status
709
 
710
- # --- Define the NEW, FIXED Care Plan Structure for the AI Output ---
711
- # Removed 'Emergency Contacts' and 'Red Flags' sections, added 'PHYSICAL ACTIVITY AND EXERCISE'
712
- required_care_plan_structure = """
713
- PATIENT INFORMATION:
714
- ASSESSMENT:
715
- DAILY CARE PLAN:
716
- Morning:
717
- Afternoon:
718
- Night:
719
- MEDICATIONS:
720
- DIET AND HYDRATION:
721
- PHYSICAL ACTIVITY AND EXERCISE:
722
- SYMPTOM MANAGEMENT:
723
- """
724
-
725
  # Only generate AI plan if AI is enabled AND status isn't immediate emergency based on feedback
726
  # If feedback triggers "emergency", the generated_plan_text is a fixed emergency plan.
727
  if final_status_to_save == 'emergency':
@@ -734,69 +725,82 @@ SYMPTOM MANAGEMENT:
734
  f"- Gender: {gender}\n\n"
735
  "ASSESSMENT:\n"
736
  f"- Emergency symptoms reported: {feedback}. Immediate medical attention required.\n\n"
737
- "DAILY CARE PLAN:\n" # Using the new structure headings
738
- "Morning:\n"
739
- "- Seek immediate medical evaluation.\n"
740
- "Afternoon:\n"
741
- "- Follow instructions from emergency services.\n"
742
- "Night:\n"
743
- "- Remain under medical supervision if advised.\n\n"
744
- "MEDICATIONS:\n"
745
- "- Specific medicines and dosage usage based on symptoms (ex : for fever paramcetamol 1-1-1)\n\n"
746
- "DIET AND HYDRATION:\n"
747
- "- Do not consume food or drink unless advised by medical professionals.\n\n"
748
- "PHYSICAL ACTIVITY AND EXERCISE:\n"
749
- "- Rest completely and avoid any physical activity until evaluated by medical professionals.\n\n"
750
- "SYMPTOM MANAGEMENT:\n"
751
- "- Focus on seeking professional help. Do not attempt home management of emergency symptoms.\n"
752
  )
 
753
  # Status remains 'emergency', which was already set as final_status_to_save.
754
 
755
 
756
  elif ai_enabled: # AI is enabled and initial status is not emergency
757
- logger.info("AI is enabled and status is not emergency. Generating plan via AI.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
758
 
759
- # --- MODIFIED AND OPTIMIZED AI PROMPT (This is the key change) ---
760
  prompt = f"""
761
- You are an expert AI assistant specialized in generating concise, structured patient care plans.
762
-
763
- Based on the following Patient Information, Patient Feedback, and Previous Care Plan (if available), generate a NEW care plan.
764
-
765
- --- Patient Information ---
766
  Name: {name}
767
  Age: {age if age is not None else 'N/A'}
768
  Gender: {gender}
769
-
770
- --- Patient Feedback/Symptoms Update ---
771
  {feedback}
772
-
773
- --- Previous Care Plan Details (if available) ---
774
  {care_plan_text if care_plan_text and "[No readable text found" not in care_plan_text and "[Error extracting PDF text" not in care_plan_text and "[PDF Content Unavailable" not in care_plan_text else "No previous care plan provided or could not be read."}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
775
 
776
- --- Instructions for Generating the New Care Plan ---
777
- 1. Generate the care plan using ONLY the exact section headings and sub-sections listed in the "Required Care Plan Structure" below. Maintain the order.
778
- 2. Fill in the details for each section based on the provided information. Prioritize addressing the latest feedback while incorporating relevant, SAFE, and appropriate details from the previous plan.
779
- 3. Be specific, actionable, and tailored to the patient's situation as described.
780
- 4. Keep the content concise. Aim for a few sentences or bullet points per sub-section (e.g., Morning, Afternoon, Night) and per main section (e.g., ASSESSMENT, MEDICATIONS, DIET, EXERCISE, SYMPTOM MANAGEMENT). Do NOT exceed 4 sentences/points per section/sub-section if possible, but prioritize clarity and necessary instructions.
781
- 5. Regarding MEDICATIONS:
782
- - If the previous plan listed specific prescribed medications, include them with dosage and timing if possible.
783
- - If specific medications are not available in the previous plan, list the *types* of medications commonly used for the patient's condition (if implied by the feedback/previous plan, e.g., "Blood pressure medication", "Pain relief").
784
- - You MAY mention common, over-the-counter examples for specific symptoms if the feedback implies them (e.g., "Paracetamol for fever or mild pain"), but *always* emphasize consulting a doctor for specific prescriptions and dosage. Avoid recommending prescription drugs you are not explicitly told about.
785
- - *Crucially, do NOT make up prescription drug names or dosages.*
786
- - *Crucially, do NOT use phrases like "Medications to be determined by physician", "Doctor will provide", "Not provided", "(Unknown)", etc.* List what is known or infer likely categories.
787
- 6. For PHYSICAL ACTIVITY AND EXERCISE, provide concrete examples or recommendations suitable for someone with the patient's condition and current status (e.g., "Gentle walking 15 mins daily", "Light stretching exercises", "Avoid strenuous activity"). Tailor this to the patient's feedback (e.g., if deteriorating, recommend rest).
788
- 7. Do NOT include sections like "Red Flags", "Emergency Contacts", "Contact Number", "Doctor's Name", "Hospital Contact", or "Primary Caregiver".
789
- 8. Do NOT use any vague placeholders like "(To be added)", "(Not provided)", or "(Unknown)" anywhere in the plan. If information is truly unavailable and you cannot infer a reasonable default (like suggesting a type of medication), omit the specific detail rather than using a placeholder.
790
- 9. Do NOT include any introductory phrases (e.g., "Here is the plan", "Based on your feedback") or concluding remarks outside the plan structure. Provide ONLY the structured content.
791
- 10. Ensure the plan's tone and content are appropriate for the patient's determined status (Deteriorating, Improving, Stable). For deteriorating status, emphasize caution and monitoring. For improving, suggest gradual progression if appropriate.
792
- 11. Review the feedback and previous plan carefully to determine the most likely current STATUS (e.g., Emergency, Deteriorating, Improving, Stable) and ensure the plan content aligns with that status.
793
-
794
- --- Required Care Plan Structure (Use ONLY these exact sections and sub-sections in this order) ---
795
- {required_care_plan_structure}
796
  """
797
- # --- END OF MODIFIED AI PROMPT ---
798
-
799
-
800
  logger.info("Sending prompt to AI model...")
801
 
802
  try:
@@ -805,18 +809,22 @@ Gender: {gender}
805
  generated_plan_text = response.text.strip()
806
 
807
  # Remove markdown code block formatting if present
808
- if generated_plan_text.startswith('```'):
 
809
  first_newline_after_code = generated_plan_text.find('\n')
810
  if first_newline_after_code != -1:
811
- # Check if there's a language name before the newline
812
- potential_lang = generated_plan_text[3:first_newline_after_code].strip()
813
- if re.match(r'^[a-zA-Z0-9]+$', potential_lang): # Simple check for language name
814
- generated_plan_text = generated_plan_text[first_newline_after_code:].strip()
815
- else:
816
- generated_plan_text = generated_plan_text[3:].strip()
 
817
  else:
818
- generated_plan_text = generated_plan_text[3:].strip()
 
819
 
 
820
  if generated_plan_text.endswith('```'):
821
  generated_plan_text = generated_plan_text[:-3].strip()
822
 
@@ -824,6 +832,8 @@ Gender: {gender}
824
  logger.info(f"AI Response received. Length: {len(generated_plan_text)}")
825
 
826
  # Re-determine the final status using the generated plan as well.
 
 
827
  final_status_to_save = determine_patient_status(care_plan_text, generated_plan_text, feedback)
828
  logger.info(f"Final status determined after AI generation: {final_status_to_save}")
829
 
@@ -862,7 +872,6 @@ Gender: {gender}
862
 
863
 
864
  # Create and store patient record in the database
865
- # Ensure original_plan is stored even if it contains error messages from PDF extraction
866
  new_patient = Patient(
867
  name=name,
868
  age=age,
@@ -880,12 +889,15 @@ Gender: {gender}
880
  logger.info(f"Patient {patient_id} added to DB with status: {final_status_to_save}.")
881
 
882
  # Generate PDF for downloading using the stored data
 
 
883
  pdf_buffer = generate_care_plan_pdf(new_patient.to_dict(), new_patient.updated_plan, new_patient.status)
884
  pdf_buffer.seek(0) # Ensure buffer is at the start before base64 encoding
885
  pdf_base64 = base64.b64encode(pdf_buffer.getvalue()).decode('utf-8')
886
  logger.info("PDF generated and base64 encoded.")
887
 
888
  # Send care plan via WhatsApp (using the final saved data)
 
889
  whatsapp_sent, whatsapp_message = send_whatsapp_care_plan(new_patient.to_dict(), new_patient.updated_plan, new_patient.status)
890
  logger.info(f"WhatsApp message attempt sent: {whatsapp_sent}, message: {whatsapp_message}")
891
 
@@ -898,8 +910,7 @@ Gender: {gender}
898
  'status': new_patient.status, # Return the final determined status
899
  'whatsapp_sent': whatsapp_sent,
900
  'whatsapp_message': whatsapp_message,
901
- 'ai_error': not ai_enabled or (ai_error_message is not None), # Indicate if AI was not enabled or failed
902
- 'ai_error_message': ai_error_message # Include the specific error message if applicable
903
  })
904
 
905
  except Exception as e:
@@ -909,9 +920,6 @@ Gender: {gender}
909
  'success': False,
910
  'error': f'An unexpected server error occurred: {str(e)}'
911
  }), 500
912
- # --- END: Modified submit_feedback function ---
913
-
914
-
915
 
916
  # --- New routes for Doctor Dashboard actions ---
917
 
 
652
  logger.info("Accessed doctor dashboard, setting role to doctor.")
653
  return render_template('doctor_dashboard.html')
654
 
655
+
656
  @app.route('/submit_feedback', methods=['POST'])
657
  def submit_feedback():
658
  ai_enabled = bool(model) # Check if model is initialized
 
681
  age = None # Store as None if not provided
682
 
683
  care_plan_text = "" # This will store the extracted text from PDF
684
+ care_plan_format = None # This will store the detected format
685
 
686
  if 'care_plan_pdf' in request.files:
687
  pdf_file = request.files['care_plan_pdf']
 
694
  logger.info(f"Processing uploaded PDF: {pdf_file.filename}")
695
  care_plan_text = extract_text_from_pdf(pdf_file)
696
 
697
+ # If extraction resulted in an error message, set format to None
698
+ if "[Error extracting PDF text" in care_plan_text or "[No readable text found" in care_plan_text or "[PDF Content Unavailable" in care_plan_text:
699
+ care_plan_format = None
700
+ logger.warning(f"PDF text extraction failed or empty: {care_plan_text}")
701
+ else:
702
+ care_plan_format = extract_care_plan_format(care_plan_text)
703
+ logger.info(f"Extracted text length: {len(care_plan_text)}. Format found: {care_plan_format is not None}")
704
  else:
705
  logger.info("No PDF file uploaded or file is empty.")
706
 
707
+ # Determine the initial status based on feedback and original plan
708
  # Pass "" for updated_plan initially, as it hasn't been generated yet for status check
709
  initial_status = determine_patient_status(care_plan_text, "", feedback)
710
  logger.info(f"Initial status determined based on feedback/original plan: {initial_status}")
711
 
712
+
713
  generated_plan_text = "" # This will store the AI-generated or fallback plan
714
  final_status_to_save = initial_status # Start with initial status
715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
716
  # Only generate AI plan if AI is enabled AND status isn't immediate emergency based on feedback
717
  # If feedback triggers "emergency", the generated_plan_text is a fixed emergency plan.
718
  if final_status_to_save == 'emergency':
 
725
  f"- Gender: {gender}\n\n"
726
  "ASSESSMENT:\n"
727
  f"- Emergency symptoms reported: {feedback}. Immediate medical attention required.\n\n"
728
+ "EMERGENCY ACTION PLAN:\n"
729
+ "- *Call emergency services immediately* at your local emergency number (e.g., 104/108/109/112).\n"
730
+ "- Do not delay seeking medical help. If possible, have someone stay with the patient.\n"
731
+ "- If conscious, help the patient into a comfortable position (e.g., upright for breathing difficulties, on back with legs elevated for shock).\n"
732
+ "- Do not give food or drink until evaluated by medical professionals.\n"
733
+ "- Prepare relevant medical history, medication list, and previous care plan if available for emergency responders.\n"
734
+ "- Follow *all* instructions from emergency responders.\n\n"
735
+ "RED FLAGS / WHEN TO SEEK HELP:\n"
736
+ "- *Any* worsening of reported emergency symptoms requires urgent re-evaluation by medical professionals.\n"
737
+ "- Do not attempt to manage severe symptoms at home once emergency signs are present.\n\n"
738
+ "FOLLOW-UP:\n"
739
+ "- Immediate hospitalization or urgent medical evaluation is necessary.\n"
740
+ "- Inform your primary physician/care team as soon as medically stable.\n"
741
+ "- A new care plan will be developed after the emergency situation is resolved and evaluated by medical professionals.\n"
 
742
  )
743
+ # For emergency, the generated plan text *is* the final plan to save/send.
744
  # Status remains 'emergency', which was already set as final_status_to_save.
745
 
746
 
747
  elif ai_enabled: # AI is enabled and initial status is not emergency
748
+ # Define or get the format template
749
+ if not care_plan_format or not care_plan_format.strip():
750
+ logger.info("Using default care plan format as extraction failed or returned empty.")
751
+ care_plan_format = """
752
+ PATIENT INFORMATION:
753
+ - Name: [Patient Name]
754
+ - Age: [Age]
755
+ - Gender: [Gender]
756
+ ASSESSMENT:
757
+ - [Summary of patient's current condition based on feedback and previous plan]
758
+ DAILY CARE PLAN:
759
+ Morning:
760
+ - [Morning activities/medications/checks]
761
+ Afternoon:
762
+ - [Afternoon activities/medications/checks]
763
+ Night:
764
+ - [Night activities/medications/sleep instructions/checks]
765
+ MEDICATIONS:
766
+ - list of medicines (speciifc ones, based on user symptomms and dosage and frequency like (ex: fro fever paracetamol 500mg 3 times a day))
767
+ DIET AND HYDRATION
768
+ - [what to eat , what to not eat ,speciific one slike ex : ooat,green vegetables, Beetrroot for blood defieicneny, avoid brger,pizza,samosa junk food etc...just ex]
769
+ - [ how many litres of water to drink in day, and other hydrated things toi dirnk etc...]
770
+ Fitness , Exercise and Home Remedies :
771
+ - [ what exercise to do for fitness and increase immunity, any Indian Hoem remeides on disease like (haldi ka kadha for cold,clough just an example)]
772
+ SYMPTOM MANAGEMENT:
773
+ - [Short instructions for managing specific symptoms (e.g., pain, nausea, shortness of breath), non-pharmacological interventions]
774
+ """ # Enhanced default format
775
 
 
776
  prompt = f"""
777
+ You are a helpful and highly structured AI assistant generating updated patient care plans.
778
+ Your task is to create a NEW, comprehensive, and well-structured daily care plan based on the provided patient's personal information, their current symptoms/feedback, and their previous care plan (if available).
779
+ Patient Information:
 
 
780
  Name: {name}
781
  Age: {age if age is not None else 'N/A'}
782
  Gender: {gender}
783
+ Patient Feedback/Symptoms Update:
 
784
  {feedback}
785
+ Previous Care Plan Details (if available):
 
786
  {care_plan_text if care_plan_text and "[No readable text found" not in care_plan_text and "[Error extracting PDF text" not in care_plan_text and "[PDF Content Unavailable" not in care_plan_text else "No previous care plan provided or could not be read."}
787
+ Instructions:
788
+ 1. Generate the updated care plan strictly using the exact following format template.
789
+ 2. Populate each section of the template based on the patient's information, their *latest feedback/symptoms*, and integrate relevant, SAFE, and appropriate elements from the previous plan if they are still applicable and helpful given the feedback.
790
+ 3. Prioritize addressing the issues raised in the patient feedback and give only prp[er formated,allinged,jsutifed and spaced text only, no tbales should be there.
791
+ 4. Be specific, actionable, and realistic in your recommendations (e.g., specify *what* activities, *when* to take medications and *what" medications and diet to follow).
792
+ 5. Ensure the language is clear, empathetic, and easy for a patient or caregiver to understand. Avoid overly technical jargon where possible.
793
+ 7. Do NOT include any introductory phrases (like "Here is the updated plan:", "Based on your feedback...") or concluding sentences outside the plan structure. Provide ONLY the structured content that fits within the templateand dont follow ai ethics like as an ai i ammnot able to give ,tehcnical consulatation etc
794
+ 8. If the previous care plan was unavailable or unreadable, create the plan based solely on the patient information and feedback, still following the template.
795
+ 9. Ensure the plan is medically sound and reflects standard care principles. also give specific medications rexomemndations based on patient symptoms (ex : paracetamol for fever etc)
796
+ 10. If the feedback indicates significant improvement, the plan should reflect this (e.g., adjusting activity levels up, noting successful symptom management) while still including monitoring and red flags.
797
+ 11. Review the feedback and previous plan carefully to determine the most likely current STATUS (e.g., Emergency, Deteriorating, Improving, Stable) and ensure the plan content aligns with that status, especially the RED FLAGS section. ensure plan does not exist whatsapp message character limit
798
+ 12. kindly, do not produce emergency contact section drop it okay,also in any section dont give like not provided, not known,need to be added like stufff, cosnider youself super intellgient and real physician
799
+ 13. also ensure that only three patient info paprmaeters name ,age,gender provided and nothing else okay. do not give vague parameters like contact no, caretkae no,emegency no etc..also do not give nay useless or vague content.also dont mention that as an ai i cant give ans, consult to professional,if you dont know give near about answers
800
+ Care Plan Format Template (hey only give in text format do not use tbales okay ,a lso ensure that plan is short and no section great than 4 sentences okay):
801
+ {care_plan_format}
802
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
803
  """
 
 
 
804
  logger.info("Sending prompt to AI model...")
805
 
806
  try:
 
809
  generated_plan_text = response.text.strip()
810
 
811
  # Remove markdown code block formatting if present
812
+ if generated_plan_text.startswith('```') and generated_plan_text.endswith('```'):
813
+ # Find the first newline after ``` to potentially strip language name
814
  first_newline_after_code = generated_plan_text.find('\n')
815
  if first_newline_after_code != -1:
816
+ # Check if there's a language name before the newline
817
+ potential_lang = generated_plan_text[3:first_newline_after_code].strip()
818
+ if re.match(r'^[a-zA-Z0-9]+$', potential_lang): # Simple check for language name
819
+ generated_plan_text = generated_plan_text[first_newline_after_code:].strip()
820
+ else:
821
+ # No language name or unexpected format, just strip ```
822
+ generated_plan_text = generated_plan_text[3:].strip()
823
  else:
824
+ # Handle case where ``` is on the last line without newline
825
+ generated_plan_text = generated_plan_text[3:].strip()
826
 
827
+ # Strip ending ```
828
  if generated_plan_text.endswith('```'):
829
  generated_plan_text = generated_plan_text[:-3].strip()
830
 
 
832
  logger.info(f"AI Response received. Length: {len(generated_plan_text)}")
833
 
834
  # Re-determine the final status using the generated plan as well.
835
+ # This is important because the AI might infer severity the keyword matching missed initially,
836
+ # or the generated plan text itself might contain explicit strong status indicators.
837
  final_status_to_save = determine_patient_status(care_plan_text, generated_plan_text, feedback)
838
  logger.info(f"Final status determined after AI generation: {final_status_to_save}")
839
 
 
872
 
873
 
874
  # Create and store patient record in the database
 
875
  new_patient = Patient(
876
  name=name,
877
  age=age,
 
889
  logger.info(f"Patient {patient_id} added to DB with status: {final_status_to_save}.")
890
 
891
  # Generate PDF for downloading using the stored data
892
+ # Note: We pass the patient object directly to the PDF generator for simplicity
893
+ # and to include feedback in the PDF
894
  pdf_buffer = generate_care_plan_pdf(new_patient.to_dict(), new_patient.updated_plan, new_patient.status)
895
  pdf_buffer.seek(0) # Ensure buffer is at the start before base64 encoding
896
  pdf_base64 = base64.b64encode(pdf_buffer.getvalue()).decode('utf-8')
897
  logger.info("PDF generated and base64 encoded.")
898
 
899
  # Send care plan via WhatsApp (using the final saved data)
900
+ # Note: We pass the patient object directly to the WhatsApp function
901
  whatsapp_sent, whatsapp_message = send_whatsapp_care_plan(new_patient.to_dict(), new_patient.updated_plan, new_patient.status)
902
  logger.info(f"WhatsApp message attempt sent: {whatsapp_sent}, message: {whatsapp_message}")
903
 
 
910
  'status': new_patient.status, # Return the final determined status
911
  'whatsapp_sent': whatsapp_sent,
912
  'whatsapp_message': whatsapp_message,
913
+ 'ai_error': not ai_enabled or (ai_error_message is not None) # Indicate if AI was not enabled or failed
 
914
  })
915
 
916
  except Exception as e:
 
920
  'success': False,
921
  'error': f'An unexpected server error occurred: {str(e)}'
922
  }), 500
 
 
 
923
 
924
  # --- New routes for Doctor Dashboard actions ---
925