Update app.py
Browse files
app.py
CHANGED
@@ -57,13 +57,39 @@ def extract_section(text: str, heading: str) -> str:
|
|
57 |
return ""
|
58 |
|
59 |
def structure_medical_response(text: str) -> Dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
return {
|
61 |
-
"summary":
|
62 |
-
|
63 |
-
"
|
64 |
-
|
|
|
|
|
|
|
|
|
65 |
}
|
66 |
-
|
67 |
def serialize_patient(patient: dict) -> dict:
|
68 |
patient_copy = patient.copy()
|
69 |
if "_id" in patient_copy:
|
|
|
57 |
return ""
|
58 |
|
59 |
def structure_medical_response(text: str) -> Dict:
|
60 |
+
"""Improved version that handles both markdown and plain text formats"""
|
61 |
+
def extract_improved(text: str, heading: str) -> str:
|
62 |
+
# Try multiple patterns to match different heading formats
|
63 |
+
patterns = [
|
64 |
+
rf"{re.escape(heading)}:\s*\n(.*?)(?=\n\s*\n|\Z)", # Heading followed by content until double newline
|
65 |
+
rf"\*\*{re.escape(heading)}\*\*:\s*\n(.*?)(?=\n\s*\n|\Z)", # Markdown bold heading
|
66 |
+
rf"{re.escape(heading)}[\s\-]+(.*?)(?=\n\s*\n|\Z)", # Heading with dashes
|
67 |
+
rf"\n{re.escape(heading)}\s*\n(.*?)(?=\n\s*\n|\Z)" # Heading on its own line
|
68 |
+
]
|
69 |
+
|
70 |
+
for pattern in patterns:
|
71 |
+
match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
|
72 |
+
if match:
|
73 |
+
content = match.group(1).strip()
|
74 |
+
# Clean up any remaining markdown or special characters
|
75 |
+
content = re.sub(r'^\s*[\-\*]\s*', '', content, flags=re.MULTILINE)
|
76 |
+
return content
|
77 |
+
|
78 |
+
return ""
|
79 |
+
|
80 |
+
# Normalize the text first
|
81 |
+
text = text.replace('**', '').replace('__', '')
|
82 |
+
|
83 |
return {
|
84 |
+
"summary": extract_improved(text, "Summary of Patient's Medical History") or
|
85 |
+
extract_improved(text, "Summarize the patient's medical history"),
|
86 |
+
"risks": extract_improved(text, "Identify Risks or Red Flags") or
|
87 |
+
extract_improved(text, "Risks or Red Flags"),
|
88 |
+
"missed_issues": extract_improved(text, "Missed Diagnoses or Treatments") or
|
89 |
+
extract_improved(text, "What the doctor might have missed"),
|
90 |
+
"recommendations": extract_improved(text, "Suggest Next Clinical Steps") or
|
91 |
+
extract_improved(text, "Suggested Clinical Actions")
|
92 |
}
|
|
|
93 |
def serialize_patient(patient: dict) -> dict:
|
94 |
patient_copy = patient.copy()
|
95 |
if "_id" in patient_copy:
|