Spaces:
Sleeping
Sleeping
File size: 22,021 Bytes
ceaa691 42e73f2 ceaa691 42e73f2 ceaa691 a35c9e2 ceaa691 42e73f2 ceaa691 5c0eb1b 42e73f2 ceaa691 42e73f2 57042f5 42e73f2 ceaa691 42e73f2 47c5ad7 42e73f2 ceaa691 42e73f2 ceaa691 42e73f2 099031b 47c5ad7 5c0eb1b 099031b 42e73f2 47c5ad7 42e73f2 ceaa691 5c0eb1b 099031b 5c0eb1b 42e73f2 ceaa691 42e73f2 ceaa691 42e73f2 ceaa691 099031b 42e73f2 ceaa691 ab97c26 42e73f2 5c0eb1b ceaa691 47c5ad7 aed0c9b 47c5ad7 42e73f2 b2ae2a5 42e73f2 099031b 5c0eb1b 42e73f2 5c0eb1b 42e73f2 47c5ad7 5c0eb1b 099031b 5c0eb1b 47c5ad7 5c0eb1b df21ba0 099031b df21ba0 eb12761 aed0c9b 47c5ad7 eb12761 df21ba0 eb12761 df21ba0 eb12761 42e73f2 df21ba0 eb12761 df21ba0 5c0eb1b 099031b 5c0eb1b 42e73f2 5c0eb1b 42e73f2 5c0eb1b 42e73f2 ceaa691 42e73f2 5c0eb1b ceaa691 42e73f2 ceaa691 42e73f2 5c0eb1b ceaa691 42e73f2 5c0eb1b 099031b 42e73f2 5c0eb1b 42e73f2 5c0eb1b 42e73f2 04342e7 f795335 5b29aec 389c056 ceaa691 10400ea 5c0eb1b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
#!/usr/bin/env python3
import os
import json
import logging
import re
from typing import Dict, Any
from pathlib import Path
from unstructured.partition.pdf import partition_pdf
from flask import Flask, request, jsonify
from flask_cors import CORS
from dotenv import load_dotenv
from flask import send_from_directory, abort
from bloatectomy import bloatectomy
from werkzeug.utils import secure_filename
from langchain_groq import ChatGroq
from typing_extensions import TypedDict, NotRequired
# --- Logging ---
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger("patient-assistant")
ALLOWED_EXTENSIONS = {"pdf"}
# --- Load environment ---
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
logger.error("GROQ_API_KEY not set in environment")
exit(1)
# --- Flask app setup ---
BASE_DIR = Path(__file__).resolve().parent
REPORTS_ROOT = Path(os.getenv("REPORTS_ROOT", str(BASE_DIR / "reports")))
static_folder = BASE_DIR / "static"
app = Flask(__name__, static_folder=str(static_folder), static_url_path="/static")
CORS(app)
# Ensure the reports directory exists
os.makedirs(REPORTS_ROOT, exist_ok=True)
# --- LLM setup ---
llm = ChatGroq(
model=os.getenv("LLM_MODEL", "meta-llama/llama-4-scout-17b-16e-instruct"),
temperature=0.0,
max_tokens=1024,
api_key=GROQ_API_KEY,
)
def clean_notes_with_bloatectomy(text: str, style: str = "remov") -> str:
try:
b = bloatectomy(text, style=style, output="html")
tokens = getattr(b, "tokens", None)
if not tokens:
return text
return "\n".join(tokens)
except Exception:
logger.exception("Bloatectomy cleaning failed; returning original text")
return text
PATIENT_ASSISTANT_PROMPT = """
You are a helpful medical assistant acting as a doctor. You respond naturally to greetings and general medical questions without asking for patient ID unless the user requests information about prior medical records
Behavior rules (follow these strictly):
- Do NOT ask for the patient ID at the start of every conversation. Only request the PID when the user's question explicitly requires accessing prior medical records (for example: "show my previous lab report", "what does my thyroid test from last month say", "what was my doctor's note for PID 12345", etc.).
- When you do ask for a PID, be concise and ask only for the PID (e.g., "Please provide the patient ID (PID) to retrieve previous records."). Do not request name/DOB/other verification unless the user explicitly asks for an extra verification step.
- If the user supplies a PID in their message (patterns like "pid 5678", "p5678", "patient id: 5678"), accept and use it — do not ask again.
- Never ask for the PID if it is already known. If the user provides a different PID later, update it and proceed accordingly.
- Avoid repeating unnecessary clarifying questions. If you previously asked for the PID and the user didn't provide it, ask once more succinctly and then offer to help with general guidance without records.
- When analyzing medical reports, trust the patient ID from the folder or query context as the source of truth.
- **If the report text mentions a different patient ID or name, do not refuse to answer but mention the discrepancy politely and proceed to answer based on the available data.**
- **Always protect patient privacy and avoid sharing information from reports not matching the current PID unless explicitly requested and with a clear disclaimer.**
STRICT OUTPUT FORMAT (JSON ONLY):
Return a single JSON object with the following keys:
- assistant_reply: string // a natural language reply to the user (short, helpful, always present)
- patientDetails: object // keys may include name, problem, pid (patient ID), city, contact (update if user shared info)
- conversationSummary: string (optional) // short summary of conversation + relevant patient docs
Rules:
- ALWAYS include `assistant_reply` as a non-empty string.
- Do NOT produce any text outside the JSON object.
- Be concise in `assistant_reply`. If you need more details, ask a targeted follow-up question.
- Do not make up information that is not present in the provided medical reports or conversation history.
"""
PID_PATTERN = re.compile(r"(?:\bpid\b|\bpatient\s*id\b|\bp\b)\s*[:#\-]?\s*(p?\d+)", re.IGNORECASE)
DIGIT_PATTERN = re.compile(r"\b(p?\d{3,})\b")
RECORD_KEYWORDS = [
"report", "lab", "result", "results", "previous", "history", "record", "records",
"test", "tests", "scan", "imaging", "radiology", "thyroid", "tsh", "t3", "t4",
"prescription", "doctor", "referral", "visit", "consultation",
]
def extract_pid_from_text(text: str) -> str | None:
if not text:
return None
m = PID_PATTERN.search(text)
if m:
return m.group(1).lstrip('pP')
if any(k in text.lower() for k in RECORD_KEYWORDS):
m2 = DIGIT_PATTERN.search(text)
if m2:
return m2.group(1).lstrip('pP')
return None
def needs_pid_for_query(text: str) -> bool:
if not text:
return False
lower = text.lower()
phrases = ["previous report", "previous lab", "my report", "my records", "past report", "last report", "previous test", "previous results"]
if any(p in lower for p in phrases):
return True
if any(k in lower for k in RECORD_KEYWORDS):
return True
return False
def extract_json_from_llm_response(raw_response: str) -> dict:
default = {
"assistant_reply": "I'm sorry — I couldn't understand that. Could you please rephrase?",
"patientDetails": {},
"conversationSummary": "",
}
if not raw_response or not isinstance(raw_response, str):
return default
m = re.search(r"```(?:json)?\s*([\s\S]*?)\s*```", raw_response)
json_string = m.group(1).strip() if m else raw_response
first = json_string.find('{')
last = json_string.rfind('}')
if first == -1 or last == -1 or first >= last:
try:
return json.loads(json_string)
except Exception:
logger.warning("Could not locate JSON braces in LLM output. Falling back to default.")
return default
candidate = json_string[first:last+1]
candidate = re.sub(r',\s*(?=[}\]])', '', candidate)
try:
parsed = json.loads(candidate)
except Exception as e:
logger.warning("Failed to parse JSON from LLM output: %s", e)
return default
if isinstance(parsed, dict) and "assistant_reply" in parsed and isinstance(parsed["assistant_reply"], str) and parsed["assistant_reply"].strip():
parsed.setdefault("patientDetails", {})
parsed.setdefault("conversationSummary", "")
return parsed
else:
logger.warning("Parsed JSON missing 'assistant_reply' or invalid format. Returning default.")
return default
def extract_details_from_user_message(user_message: str) -> dict:
"""
Use the LLM to extract patient details (name, contact, city, problem) from the user's last message.
Returns a dict with any found fields.
"""
extraction_prompt = f"""
Extract any patient details from the following user message. Return a JSON object with keys name, contact, city, problem.
If a field is not present, omit it.
User message:
\"\"\"{user_message}\"\"\"
"""
messages = [
{"role": "system", "content": "You are a helpful assistant that extracts patient details from user messages."},
{"role": "user", "content": extraction_prompt}
]
try:
response = llm.invoke(messages)
content = response.content if hasattr(response, "content") else str(response)
extracted = extract_json_from_llm_response(content)
return extracted.get("patientDetails", extracted) # support both keys
except Exception as e:
logger.warning(f"Detail extraction failed: {e}")
return {}
# --- Flask routes ---
@app.route("/", methods=["GET"])
def serve_frontend():
try:
return app.send_static_file("frontend.html")
except Exception:
return "<h3>frontend.html not found in static/ — please add your frontend.html there.</h3>", 404
@app.route("/upload_report", methods=["POST"])
def upload_report():
if 'report' not in request.files:
return jsonify({"error": "No file part in the request"}), 400
file = request.files['report']
patient_id = request.form.get("patient_id")
if file.filename == '' or not patient_id:
return jsonify({"error": "No selected file or patient ID"}), 400
if file:
filename = secure_filename(file.filename)
patient_folder = REPORTS_ROOT / f"{patient_id}"
os.makedirs(patient_folder, exist_ok=True)
file_path = patient_folder / filename
file.save(file_path)
return jsonify({"message": f"File '{filename}' uploaded successfully for patient ID '{patient_id}'."}), 200
@app.route("/chat", methods=["POST"])
def chat():
data = request.get_json(force=True)
if not isinstance(data, dict):
return jsonify({"error": "invalid request body"}), 400
chat_history = data.get("chat_history") or []
patient_state = data.get("patient_state") or {}
patient_details = patient_state.get("patientDetails", {})
patient_id = patient_details.get("pid")
state = patient_state.copy()
state.setdefault("asked_for_pid", False)
state.setdefault("conversationSummary", state.get("conversationSummary", ""))
state["lastUserMessage"] = ""
if chat_history:
for msg in reversed(chat_history):
if msg.get("role") == "user" and msg.get("content"):
state["lastUserMessage"] = msg["content"]
break
inferred_pid = extract_pid_from_text(state.get("lastUserMessage", "") or "")
patient_id_str = str(patient_id) if patient_id is not None else ""
if (not patient_id_str or patient_id_str.strip() == "") and inferred_pid:
logger.info("Inferred PID from user message: %s", inferred_pid)
state.setdefault("patientDetails", {})["pid"] = inferred_pid
patient_id = inferred_pid
combined_text_parts = []
wants_records = needs_pid_for_query(state.get("lastUserMessage", "") or "")
# If user wants records but no PID yet, ask for PID (same behavior as before)
if wants_records and (not patient_id or patient_id_str.strip() == ""):
if not state.get("asked_for_pid", False):
assistant_reply = "Please provide the patient ID (PID) to retrieve previous records."
state["asked_for_pid"] = True
response_payload = {
"assistant_reply": assistant_reply,
"updated_state": state,
}
return jsonify(response_payload)
else:
assistant_reply = (
"I still need your Patient ID (PID) to access your records. "
"If you prefer, I can help with general medical questions instead."
)
response_payload = {
"assistant_reply": assistant_reply,
"updated_state": state,
}
return jsonify(response_payload)
# If we have a PID, check whether any allowed files exist for that PID.
has_allowed_files = False
if patient_id and str(patient_id).strip() != "":
patient_folder = REPORTS_ROOT / f"{patient_id}"
if patient_folder.exists() and patient_folder.is_dir():
for f in patient_folder.iterdir():
if f.is_file():
ext = f.suffix.lower().lstrip(".")
if ext in ALLOWED_EXTENSIONS:
has_allowed_files = True
break
# IMPORTANT: do NOT short-circuit here.
# If the user explicitly asked for previous records (wants_records == True)
# and we have no files, we will tell the LLM that there are no uploaded records
# via the SYSTEM_HINT (so LLM can respond appropriately). We DO NOT return early,
# and we DO NOT add any extra JSON fields to the response.
if has_allowed_files:
# read files and build combined_text_parts (existing behavior)
for fname in sorted(os.listdir(patient_folder)):
file_path = patient_folder / fname
page_text = ""
if partition_pdf is not None and str(file_path).lower().endswith('.pdf'):
try:
elements = partition_pdf(filename=str(file_path))
page_text = "\n".join([el.text for el in elements if hasattr(el, 'text') and el.text])
except Exception:
logger.exception("Failed to parse PDF %s", file_path)
else:
try:
page_text = file_path.read_text(encoding='utf-8', errors='ignore')
except Exception:
page_text = ""
if page_text:
cleaned = clean_notes_with_bloatectomy(page_text, style="remov")
if cleaned:
combined_text_parts.append(cleaned)
else:
# no files: do not modify state or return. We'll include a hint for the LLM below
logger.info("No uploaded files found for PID %s. Will inform LLM only if user asked for records.", patient_id)
# Build conversationSummary from any docs we read (unchanged)
base_summary = state.get("conversationSummary", "") or ""
docs_summary = "\n\n".join(combined_text_parts)
if docs_summary:
state["conversationSummary"] = (base_summary + "\n\n" + docs_summary).strip()
else:
state["conversationSummary"] = base_summary
# Prepare the action hint. If user asked for records but there are no uploaded files,
# explicitly tell the LLM so it can respond like "No records available for PID X".
if patient_id and str(patient_id).strip() != "":
if wants_records and not has_allowed_files:
action_hint = (
f"User asked about prior records. NOTE: there are NO uploaded medical records for patient ID {patient_id}."
)
else:
action_hint = f"Use the patient ID {patient_id} to retrieve and summarize any relevant reports."
else:
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."
user_prompt = f"""
Current patientDetails: {json.dumps(state.get("patientDetails", {}))}
Current conversationSummary: {state.get("conversationSummary", "")[:4000]}
Last user message: {state.get("lastUserMessage", "")}
SYSTEM_HINT: {action_hint}
Return ONLY valid JSON with keys: assistant_reply, patientDetails, conversationSummary.
"""
messages = [
{"role": "system", "content": PATIENT_ASSISTANT_PROMPT},
{"role": "user", "content": user_prompt}
]
try:
logger.info("Invoking LLM with prepared state and prompt...")
llm_response = llm.invoke(messages)
raw_response = ""
if hasattr(llm_response, "content"):
raw_response = llm_response.content
else:
raw_response = str(llm_response)
logger.info(f"Raw LLM response: {raw_response}")
parsed_result = extract_json_from_llm_response(raw_response)
except Exception as e:
logger.exception("LLM invocation failed")
return jsonify({"error": "LLM invocation failed", "detail": str(e)}), 500
updated_state = parsed_result or {}
# Merge patientDetails back into state (but avoid overwriting asked_for_pid)
state.setdefault("patientDetails", {}).update(updated_state.get("patientDetails", {}))
state["conversationSummary"] = updated_state.get("conversationSummary", state.get("conversationSummary", ""))
# --- New: Extract details from last user message to update patientDetails ---
REQUIRED_DETAILS = ["name", "contact", "city", "problem"]
booking_intent_keywords = ["book appointment", "schedule appointment", "make appointment", "appointment"]
last_msg_lower = state.get("lastUserMessage", "").lower()
conversation_summary_lower = state.get("conversationSummary", "").lower()
wants_to_book = any(kw in last_msg_lower for kw in booking_intent_keywords) or \
any(kw in conversation_summary_lower for kw in booking_intent_keywords)
if wants_to_book:
# Extract details from last user message
extracted_details = extract_details_from_user_message(state.get("lastUserMessage", ""))
patient_details = state.setdefault("patientDetails", {})
# Update patientDetails with any newly extracted info
for key in REQUIRED_DETAILS:
if key in extracted_details and extracted_details[key]:
patient_details[key] = extracted_details[key]
missing_fields = [field for field in REQUIRED_DETAILS if not patient_details.get(field)]
if missing_fields:
missing_field = missing_fields[0]
field_prompts = {
"name": "Could you please provide your full name?",
"contact": "May I have your contact number?",
"city": "What city are you located in?",
"problem": "Please briefly describe your medical problem or reason for the appointment.",
}
assistant_reply = field_prompts.get(missing_field, f"Please provide your {missing_field}.")
response_payload = {
"assistant_reply": assistant_reply,
"updated_state": state,
}
return jsonify(response_payload)
assistant_reply = updated_state.get("assistant_reply")
if not assistant_reply or not isinstance(assistant_reply, str) or not assistant_reply.strip():
assistant_reply = "I'm here to help — could you tell me more about your symptoms or provide a Patient ID (PID) if you want me to fetch past reports?"
response_payload = {
"assistant_reply": assistant_reply,
"updated_state": state,
}
return jsonify(response_payload)
@app.route("/upload_reports", methods=["POST"])
def upload_reports():
try:
patient_id = request.form.get("patient_id") or request.args.get("patient_id")
if not patient_id:
return jsonify({"error": "patient_id form field required"}), 400
uploaded_files = request.files.getlist("files")
if not uploaded_files:
single = request.files.get("file")
if single:
uploaded_files = [single]
if not uploaded_files:
return jsonify({"error": "no files uploaded (use form field 'files')"}), 400
patient_folder = REPORTS_ROOT / str(patient_id)
patient_folder.mkdir(parents=True, exist_ok=True)
saved = []
skipped = []
for file_storage in uploaded_files:
orig_name = getattr(file_storage, "filename", "") or ""
filename = secure_filename(orig_name)
if not filename:
skipped.append({"filename": orig_name, "reason": "invalid filename"})
continue
ext = filename.rsplit(".", 1)[1].lower() if "." in filename else ""
if ext not in ALLOWED_EXTENSIONS:
skipped.append({"filename": filename, "reason": f"extension '{ext}' not allowed"})
continue
dest = patient_folder / filename
if dest.exists():
base, dot, extension = filename.rpartition(".")
base = base or filename
i = 1
while True:
candidate = f"{base}__{i}.{extension}" if extension else f"{base}__{i}"
dest = patient_folder / candidate
if not dest.exists():
filename = candidate
break
i += 1
try:
file_storage.save(str(dest))
saved.append(filename)
except Exception as e:
logger.exception("Failed to save uploaded file %s: %s", filename, e)
skipped.append({"filename": filename, "reason": f"save failed: {e}"})
return jsonify({
"patient_id": str(patient_id),
"saved": saved,
"skipped": skipped,
"patient_folder": str(patient_folder)
}), 200
except Exception as exc:
logger.exception("Upload failed: %s", exc)
return jsonify({"error": "upload failed", "detail": str(exc)}), 500
@app.route("/<patient_id>/<filename>")
def serve_report(patient_id, filename):
"""
Serve a specific uploaded PDF (or other allowed file) for a patient.
URL format: /<patient_id>/<filename>
Example: /p14562/report1.pdf
"""
try:
patient_folder = REPORTS_ROOT / str(patient_id)
if not patient_folder.exists():
abort(404, description=f"Patient folder not found: {patient_id}")
# security check: only allow files with allowed extensions
ext = filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
if ext not in ALLOWED_EXTENSIONS:
abort(403, description=f"Extension '{ext}' not allowed")
return send_from_directory(
directory=str(patient_folder),
path=filename,
as_attachment=False # set True if you want download instead of inline view
)
except Exception as e:
logger.exception("Failed to serve file %s/%s: %s", patient_id, filename, e)
abort(500, description=f"Failed to serve file: {e}")
@app.route("/ping", methods=["GET"])
def ping():
return jsonify({"status": "ok"})
if __name__ == "__main__":
port = int(os.getenv("PORT", 7860))
app.run(host="0.0.0.0", port=port, debug=True)
|