Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -248,37 +248,66 @@ def chat():
|
|
248 |
}
|
249 |
return jsonify(response_payload)
|
250 |
|
251 |
-
|
|
|
252 |
patient_folder = REPORTS_ROOT / f"{patient_id}"
|
|
|
|
|
|
|
253 |
if patient_folder.exists() and patient_folder.is_dir():
|
254 |
-
for
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
-
if patient_id and
|
282 |
action_hint = f"Use the patient ID {patient_id} to retrieve and summarize any relevant reports."
|
283 |
else:
|
284 |
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."
|
|
|
248 |
}
|
249 |
return jsonify(response_payload)
|
250 |
|
251 |
+
# If we have a patient_id, check for files — if none, return a clear assistant reply.
|
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():
|
260 |
+
ext = f.suffix.lower().lstrip(".")
|
261 |
+
if ext in ALLOWED_EXTENSIONS:
|
262 |
+
has_allowed_files = True
|
263 |
+
break
|
264 |
+
|
265 |
+
if not has_allowed_files:
|
266 |
+
# update state so caller knows we checked and found nothing
|
267 |
+
state.setdefault("patientDetails", {})["pid"] = str(patient_id)
|
268 |
+
state["no_records_found"] = True
|
269 |
+
# Optionally append to conversationSummary to keep a trace
|
270 |
+
base_summary = state.get("conversationSummary", "") or ""
|
271 |
+
note = f"No medical records found for patient ID {patient_id}."
|
272 |
+
state["conversationSummary"] = (base_summary + "\n\n" + note).strip()
|
273 |
+
|
274 |
+
assistant_reply = f"No medical records are available for patient ID {patient_id}."
|
275 |
+
response_payload = {
|
276 |
+
"assistant_reply": assistant_reply,
|
277 |
+
"updated_state": state,
|
278 |
+
}
|
279 |
+
return jsonify(response_payload)
|
280 |
+
|
281 |
+
# If we do have files, proceed to read them (existing behavior)
|
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:
|
306 |
+
state["conversationSummary"] = (base_summary + "\n\n" + docs_summary).strip()
|
307 |
+
else:
|
308 |
+
state["conversationSummary"] = base_summary
|
309 |
|
310 |
+
if patient_id and str(patient_id).strip() != "":
|
311 |
action_hint = f"Use the patient ID {patient_id} to retrieve and summarize any relevant reports."
|
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."
|