Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -228,6 +228,7 @@ def chat():
|
|
| 228 |
|
| 229 |
wants_records = needs_pid_for_query(state.get("lastUserMessage", "") or "")
|
| 230 |
|
|
|
|
| 231 |
if wants_records and (not patient_id or patient_id_str.strip() == ""):
|
| 232 |
if not state.get("asked_for_pid", False):
|
| 233 |
assistant_reply = "Please provide the patient ID (PID) to retrieve previous records."
|
|
@@ -248,12 +249,10 @@ def chat():
|
|
| 248 |
}
|
| 249 |
return jsonify(response_payload)
|
| 250 |
|
| 251 |
-
# If we have a
|
|
|
|
| 252 |
if patient_id and str(patient_id).strip() != "":
|
| 253 |
patient_folder = REPORTS_ROOT / f"{patient_id}"
|
| 254 |
-
|
| 255 |
-
# If folder does not exist or contains no allowed files, short-circuit with a friendly LLM-like reply.
|
| 256 |
-
has_allowed_files = False
|
| 257 |
if patient_folder.exists() and patient_folder.is_dir():
|
| 258 |
for f in patient_folder.iterdir():
|
| 259 |
if f.is_file():
|
|
@@ -262,44 +261,37 @@ def chat():
|
|
| 262 |
has_allowed_files = True
|
| 263 |
break
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
-
|
| 282 |
-
for fname in sorted(os.listdir(patient_folder)):
|
| 283 |
-
file_path = patient_folder / fname
|
| 284 |
-
page_text = ""
|
| 285 |
-
if partition_pdf is not None and str(file_path).lower().endswith('.pdf'):
|
| 286 |
-
try:
|
| 287 |
-
elements = partition_pdf(filename=str(file_path))
|
| 288 |
-
page_text = "\n".join([el.text for el in elements if hasattr(el, 'text') and el.text])
|
| 289 |
-
except Exception:
|
| 290 |
-
logger.exception("Failed to parse PDF %s", file_path)
|
| 291 |
-
else:
|
| 292 |
-
try:
|
| 293 |
-
page_text = file_path.read_text(encoding='utf-8', errors='ignore')
|
| 294 |
-
except Exception:
|
| 295 |
-
page_text = ""
|
| 296 |
-
|
| 297 |
-
if page_text:
|
| 298 |
-
cleaned = clean_notes_with_bloatectomy(page_text, style="remov")
|
| 299 |
-
if cleaned:
|
| 300 |
-
combined_text_parts.append(cleaned)
|
| 301 |
-
|
| 302 |
-
# (rest of the original function continues unchanged)
|
| 303 |
base_summary = state.get("conversationSummary", "") or ""
|
| 304 |
docs_summary = "\n\n".join(combined_text_parts)
|
| 305 |
if docs_summary:
|
|
@@ -307,8 +299,15 @@ def chat():
|
|
| 307 |
else:
|
| 308 |
state["conversationSummary"] = base_summary
|
| 309 |
|
|
|
|
|
|
|
| 310 |
if patient_id and str(patient_id).strip() != "":
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
else:
|
| 313 |
action_hint = "No PID provided and the user's request does not need prior records. Provide helpful, general medical guidance and offer to retrieve records if the user later supplies a PID."
|
| 314 |
|
|
|
|
| 228 |
|
| 229 |
wants_records = needs_pid_for_query(state.get("lastUserMessage", "") or "")
|
| 230 |
|
| 231 |
+
# If user wants records but no PID yet, ask for PID (same behavior as before)
|
| 232 |
if wants_records and (not patient_id or patient_id_str.strip() == ""):
|
| 233 |
if not state.get("asked_for_pid", False):
|
| 234 |
assistant_reply = "Please provide the patient ID (PID) to retrieve previous records."
|
|
|
|
| 249 |
}
|
| 250 |
return jsonify(response_payload)
|
| 251 |
|
| 252 |
+
# If we have a PID, check whether any allowed files exist for that PID.
|
| 253 |
+
has_allowed_files = False
|
| 254 |
if patient_id and str(patient_id).strip() != "":
|
| 255 |
patient_folder = REPORTS_ROOT / f"{patient_id}"
|
|
|
|
|
|
|
|
|
|
| 256 |
if patient_folder.exists() and patient_folder.is_dir():
|
| 257 |
for f in patient_folder.iterdir():
|
| 258 |
if f.is_file():
|
|
|
|
| 261 |
has_allowed_files = True
|
| 262 |
break
|
| 263 |
|
| 264 |
+
# IMPORTANT: do NOT short-circuit here.
|
| 265 |
+
# If the user explicitly asked for previous records (wants_records == True)
|
| 266 |
+
# and we have no files, we will tell the LLM that there are no uploaded records
|
| 267 |
+
# via the SYSTEM_HINT (so LLM can respond appropriately). We DO NOT return early,
|
| 268 |
+
# and we DO NOT add any extra JSON fields to the response.
|
| 269 |
+
if has_allowed_files:
|
| 270 |
+
# read files and build combined_text_parts (existing behavior)
|
| 271 |
+
for fname in sorted(os.listdir(patient_folder)):
|
| 272 |
+
file_path = patient_folder / fname
|
| 273 |
+
page_text = ""
|
| 274 |
+
if partition_pdf is not None and str(file_path).lower().endswith('.pdf'):
|
| 275 |
+
try:
|
| 276 |
+
elements = partition_pdf(filename=str(file_path))
|
| 277 |
+
page_text = "\n".join([el.text for el in elements if hasattr(el, 'text') and el.text])
|
| 278 |
+
except Exception:
|
| 279 |
+
logger.exception("Failed to parse PDF %s", file_path)
|
| 280 |
+
else:
|
| 281 |
+
try:
|
| 282 |
+
page_text = file_path.read_text(encoding='utf-8', errors='ignore')
|
| 283 |
+
except Exception:
|
| 284 |
+
page_text = ""
|
| 285 |
+
|
| 286 |
+
if page_text:
|
| 287 |
+
cleaned = clean_notes_with_bloatectomy(page_text, style="remov")
|
| 288 |
+
if cleaned:
|
| 289 |
+
combined_text_parts.append(cleaned)
|
| 290 |
+
else:
|
| 291 |
+
# no files: do not modify state or return. We'll include a hint for the LLM below
|
| 292 |
+
logger.info("No uploaded files found for PID %s. Will inform LLM only if user asked for records.", patient_id)
|
| 293 |
|
| 294 |
+
# Build conversationSummary from any docs we read (unchanged)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
base_summary = state.get("conversationSummary", "") or ""
|
| 296 |
docs_summary = "\n\n".join(combined_text_parts)
|
| 297 |
if docs_summary:
|
|
|
|
| 299 |
else:
|
| 300 |
state["conversationSummary"] = base_summary
|
| 301 |
|
| 302 |
+
# Prepare the action hint. If user asked for records but there are no uploaded files,
|
| 303 |
+
# explicitly tell the LLM so it can respond like "No records available for PID X".
|
| 304 |
if patient_id and str(patient_id).strip() != "":
|
| 305 |
+
if wants_records and not has_allowed_files:
|
| 306 |
+
action_hint = (
|
| 307 |
+
f"User asked about prior records. NOTE: there are NO uploaded medical records for patient ID {patient_id}."
|
| 308 |
+
)
|
| 309 |
+
else:
|
| 310 |
+
action_hint = f"Use the patient ID {patient_id} to retrieve and summarize any relevant reports."
|
| 311 |
else:
|
| 312 |
action_hint = "No PID provided and the user's request does not need prior records. Provide helpful, general medical guidance and offer to retrieve records if the user later supplies a PID."
|
| 313 |
|