Update app.py
Browse files
app.py
CHANGED
@@ -222,35 +222,44 @@ def log_system_usage(tag=""):
|
|
222 |
logger.error(f"[{tag}] Monitor failed: {e}")
|
223 |
|
224 |
def clean_response(text: str) -> str:
|
225 |
-
"""
|
226 |
if not text:
|
227 |
return ""
|
228 |
|
229 |
-
# Pre-compiled regex patterns
|
230 |
patterns = [
|
231 |
-
(re.compile(r"\[.*?\]|\bNone\b"), ""),
|
232 |
-
(re.compile(r"To analyze the patient record excerpt.*?medications\."), ""),
|
233 |
-
(re.compile(r"Since the previous attempts.*?\."), ""),
|
234 |
-
(re.compile(r"I need to.*?medications\."), ""),
|
235 |
-
(re.compile(r"Retrieving tools.*?\."), ""),
|
|
|
|
|
236 |
(re.compile(r"\s+"), " "),
|
237 |
-
(re.compile(r"[^\w\s\.\,\(\)\-]"), "")
|
|
|
238 |
]
|
239 |
|
240 |
for pattern, repl in patterns:
|
241 |
text = pattern.sub(repl, text)
|
242 |
|
243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
|
245 |
def summarize_findings(combined_response: str) -> str:
|
246 |
-
"""
|
247 |
if not combined_response:
|
248 |
return "No missed diagnoses were identified in the provided records."
|
249 |
|
250 |
# Pre-compiled regex patterns
|
251 |
diagnosis_pattern = re.compile(r"-\s*(.+)$")
|
252 |
section_pattern = re.compile(r"###\s*(Missed Diagnoses|Medication Conflicts|Incomplete Assessments|Urgent Follow-up)")
|
253 |
-
no_issues_pattern = re.compile(r"No issues identified", re.IGNORECASE)
|
254 |
|
255 |
diagnoses = []
|
256 |
current_section = None
|
@@ -266,7 +275,7 @@ def summarize_findings(combined_response: str) -> str:
|
|
266 |
current_section = "diagnoses" if section_match.group(1) == "Missed Diagnoses" else None
|
267 |
continue
|
268 |
|
269 |
-
#
|
270 |
if current_section == "diagnoses":
|
271 |
diagnosis_match = diagnosis_pattern.match(line)
|
272 |
if diagnosis_match and not no_issues_pattern.search(line):
|
@@ -274,6 +283,25 @@ def summarize_findings(combined_response: str) -> str:
|
|
274 |
if diagnosis:
|
275 |
diagnoses.append(diagnosis)
|
276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
if not diagnoses:
|
278 |
return "No missed diagnoses were identified in the provided records."
|
279 |
|
@@ -281,12 +309,11 @@ def summarize_findings(combined_response: str) -> str:
|
|
281 |
seen = set()
|
282 |
unique_diagnoses = [d for d in diagnoses if not (d in seen or seen.add(d))]
|
283 |
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
summary
|
288 |
-
summary +=
|
289 |
-
summary += ", all of which require urgent clinical review to prevent potential adverse outcomes."
|
290 |
|
291 |
return summary
|
292 |
|
@@ -312,6 +339,9 @@ def init_agent():
|
|
312 |
step_rag_num=4,
|
313 |
seed=100,
|
314 |
additional_default_tools=[],
|
|
|
|
|
|
|
315 |
)
|
316 |
agent.init_model()
|
317 |
|
@@ -322,7 +352,7 @@ def init_agent():
|
|
322 |
def create_ui(agent):
|
323 |
"""Optimized UI creation with pre-compiled templates"""
|
324 |
PROMPT_TEMPLATE = """
|
325 |
-
Analyze the patient record excerpt for missed diagnoses only. Provide a concise, evidence-based summary as a single paragraph without headings or bullet points. Include specific clinical findings (e.g., 'elevated blood pressure (160/95) on page 10'), their potential implications (e.g., 'may indicate untreated hypertension'), and a recommendation for urgent review. Do not
|
326 |
Patient Record Excerpt (Chunk {0} of {1}):
|
327 |
{chunk}
|
328 |
"""
|
@@ -352,7 +382,6 @@ Patient Record Excerpt (Chunk {0} of {1}):
|
|
352 |
file_hash_value = ""
|
353 |
|
354 |
if files:
|
355 |
-
# Use cached results when possible
|
356 |
for f in files:
|
357 |
file_type = f.name.split(".")[-1].lower()
|
358 |
cache_key = f"{file_hash(f.name)}_{file_type}"
|
@@ -426,7 +455,7 @@ Patient Record Excerpt (Chunk {0} of {1}):
|
|
426 |
elif isinstance(chunk_output, str):
|
427 |
content = clean_response(chunk_output)
|
428 |
|
429 |
-
if content:
|
430 |
chunk_response += content + " "
|
431 |
|
432 |
if chunk_response:
|
@@ -434,7 +463,6 @@ Patient Record Excerpt (Chunk {0} of {1}):
|
|
434 |
history[-1] = {"role": "assistant", "content": combined_response.strip()}
|
435 |
yield history, None, ""
|
436 |
finally:
|
437 |
-
# Ensure cleanup
|
438 |
del future
|
439 |
torch.cuda.empty_cache()
|
440 |
gc.collect()
|
@@ -458,7 +486,6 @@ Patient Record Excerpt (Chunk {0} of {1}):
|
|
458 |
history.append({"role": "assistant", "content": f"❌ Error occurred: {str(e)}"})
|
459 |
yield history, None, f"Error occurred during analysis: {str(e)}"
|
460 |
finally:
|
461 |
-
# Final cleanup
|
462 |
torch.cuda.empty_cache()
|
463 |
gc.collect()
|
464 |
|
|
|
222 |
logger.error(f"[{tag}] Monitor failed: {e}")
|
223 |
|
224 |
def clean_response(text: str) -> str:
|
225 |
+
"""Enhanced response cleaning with aggressive artifact removal"""
|
226 |
if not text:
|
227 |
return ""
|
228 |
|
229 |
+
# Pre-compiled regex patterns for cleaning
|
230 |
patterns = [
|
231 |
+
(re.compile(r"\[.*?\]|\bNone\b", re.IGNORECASE), ""),
|
232 |
+
(re.compile(r"To analyze the patient record excerpt.*?medications\.", re.IGNORECASE), ""),
|
233 |
+
(re.compile(r"Since the previous attempts.*?\.", re.IGNORECASE), ""),
|
234 |
+
(re.compile(r"I need to.*?medications\.", re.IGNORECASE), ""),
|
235 |
+
(re.compile(r"Retrieving tools.*?\.", re.IGNORECASE), ""),
|
236 |
+
(re.compile(r"I will start by retrieving.*?\.", re.IGNORECASE), ""),
|
237 |
+
(re.compile(r"This requires reviewing.*?\.", re.IGNORECASE), ""),
|
238 |
(re.compile(r"\s+"), " "),
|
239 |
+
(re.compile(r"[^\w\s\.\,\(\)\-]"), ""),
|
240 |
+
(re.compile(r"(No missed diagnoses identified\.)\s*\1+", re.IGNORECASE), r"\1"), # Deduplicate
|
241 |
]
|
242 |
|
243 |
for pattern, repl in patterns:
|
244 |
text = pattern.sub(repl, text)
|
245 |
|
246 |
+
# Deduplicate identical sentences
|
247 |
+
sentences = text.split(". ")
|
248 |
+
seen = set()
|
249 |
+
unique_sentences = [s for s in sentences if s and not (s in seen or seen.add(s))]
|
250 |
+
text = ". ".join(unique_sentences).strip()
|
251 |
+
|
252 |
+
return text if text else "No missed diagnoses identified."
|
253 |
|
254 |
def summarize_findings(combined_response: str) -> str:
|
255 |
+
"""Enhanced findings summarization for a single, detailed paragraph"""
|
256 |
if not combined_response:
|
257 |
return "No missed diagnoses were identified in the provided records."
|
258 |
|
259 |
# Pre-compiled regex patterns
|
260 |
diagnosis_pattern = re.compile(r"-\s*(.+)$")
|
261 |
section_pattern = re.compile(r"###\s*(Missed Diagnoses|Medication Conflicts|Incomplete Assessments|Urgent Follow-up)")
|
262 |
+
no_issues_pattern = re.compile(r"No issues identified|No missed diagnoses identified", re.IGNORECASE)
|
263 |
|
264 |
diagnoses = []
|
265 |
current_section = None
|
|
|
275 |
current_section = "diagnoses" if section_match.group(1) == "Missed Diagnoses" else None
|
276 |
continue
|
277 |
|
278 |
+
# Process diagnosis lines in the correct section
|
279 |
if current_section == "diagnoses":
|
280 |
diagnosis_match = diagnosis_pattern.match(line)
|
281 |
if diagnosis_match and not no_issues_pattern.search(line):
|
|
|
283 |
if diagnosis:
|
284 |
diagnoses.append(diagnosis)
|
285 |
|
286 |
+
# Extract findings from non-sectioned text (e.g., psychiatric evaluation, medications)
|
287 |
+
medication_pattern = re.compile(r"medications including ([^\.]+)", re.IGNORECASE)
|
288 |
+
evaluation_pattern = re.compile(r"psychiatric evaluation.*?mention of ([^\.]+)", re.IGNORECASE)
|
289 |
+
|
290 |
+
for line in combined_response.splitlines():
|
291 |
+
line = line.strip()
|
292 |
+
if not line or no_issues_pattern.search(line):
|
293 |
+
continue
|
294 |
+
|
295 |
+
med_match = medication_pattern.search(line)
|
296 |
+
if med_match:
|
297 |
+
meds = med_match.group(1).strip()
|
298 |
+
diagnoses.append(f"use of medications ({meds}), which may indicate an undiagnosed psychiatric condition requiring urgent review")
|
299 |
+
|
300 |
+
eval_match = evaluation_pattern.search(line)
|
301 |
+
if eval_match:
|
302 |
+
details = eval_match.group(1).strip()
|
303 |
+
diagnoses.append(f"psychiatric evaluation noting {details}, suggesting a potential missed psychiatric diagnosis requiring urgent review")
|
304 |
+
|
305 |
if not diagnoses:
|
306 |
return "No missed diagnoses were identified in the provided records."
|
307 |
|
|
|
309 |
seen = set()
|
310 |
unique_diagnoses = [d for d in diagnoses if not (d in seen or seen.add(d))]
|
311 |
|
312 |
+
# Create a single paragraph
|
313 |
+
summary = "The patient record indicates missed diagnoses including "
|
314 |
+
summary += ", ".join(unique_diagnoses[:-1])
|
315 |
+
summary += f", and {unique_diagnoses[-1]}" if len(unique_diagnoses) > 1 else unique_diagnoses[0]
|
316 |
+
summary += ". These findings, derived from the provided clinical data, suggest potential oversights in the patient's medical evaluation and require urgent clinical review to prevent adverse outcomes."
|
|
|
317 |
|
318 |
return summary
|
319 |
|
|
|
339 |
step_rag_num=4,
|
340 |
seed=100,
|
341 |
additional_default_tools=[],
|
342 |
+
disable_tools=True, # Disable tools to avoid unnecessary calls
|
343 |
+
max_retries=2, # Limit retries to prevent loops
|
344 |
+
max_tokens=4096, # Increase token limit for complex inputs
|
345 |
)
|
346 |
agent.init_model()
|
347 |
|
|
|
352 |
def create_ui(agent):
|
353 |
"""Optimized UI creation with pre-compiled templates"""
|
354 |
PROMPT_TEMPLATE = """
|
355 |
+
Analyze the patient record excerpt for missed diagnoses only, focusing on clinical findings such as symptoms, medications, or evaluation results. Provide a concise, evidence-based summary as a single paragraph without headings or bullet points. Include specific clinical findings (e.g., 'elevated blood pressure (160/95) on page 10'), their potential implications (e.g., 'may indicate untreated hypertension'), and a recommendation for urgent review. Do not use external tools unless explicitly required by the excerpt, and avoid mentioning other oversight categories like medication conflicts. If no missed diagnoses are found, state 'No missed diagnoses identified' in a single sentence. Use only the information provided in the excerpt below.
|
356 |
Patient Record Excerpt (Chunk {0} of {1}):
|
357 |
{chunk}
|
358 |
"""
|
|
|
382 |
file_hash_value = ""
|
383 |
|
384 |
if files:
|
|
|
385 |
for f in files:
|
386 |
file_type = f.name.split(".")[-1].lower()
|
387 |
cache_key = f"{file_hash(f.name)}_{file_type}"
|
|
|
455 |
elif isinstance(chunk_output, str):
|
456 |
content = clean_response(chunk_output)
|
457 |
|
458 |
+
if content and content != "No missed diagnoses identified.":
|
459 |
chunk_response += content + " "
|
460 |
|
461 |
if chunk_response:
|
|
|
463 |
history[-1] = {"role": "assistant", "content": combined_response.strip()}
|
464 |
yield history, None, ""
|
465 |
finally:
|
|
|
466 |
del future
|
467 |
torch.cuda.empty_cache()
|
468 |
gc.collect()
|
|
|
486 |
history.append({"role": "assistant", "content": f"❌ Error occurred: {str(e)}"})
|
487 |
yield history, None, f"Error occurred during analysis: {str(e)}"
|
488 |
finally:
|
|
|
489 |
torch.cuda.empty_cache()
|
490 |
gc.collect()
|
491 |
|