Attempt to fix, fingers crossed
Browse files- callbackmanager.py +141 -109
callbackmanager.py
CHANGED
|
@@ -183,8 +183,8 @@ def process_fhir_bundle(fhir_bundle):
|
|
| 183 |
patients.append(resource)
|
| 184 |
return patients
|
| 185 |
|
| 186 |
-
def
|
| 187 |
-
"""
|
| 188 |
# Safely extract name components
|
| 189 |
official_name = next(
|
| 190 |
(n for n in patient.get("name", []) if n.get("use") == "official"),
|
|
@@ -204,75 +204,32 @@ def create_patient_stripe(patient):
|
|
| 204 |
state = address.get("state", "")
|
| 205 |
postal_code = address.get("postalCode", "")
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
status = gr.Dropdown(
|
| 218 |
-
choices=["Pending", "Reviewed", "Approved", "Rejected"],
|
| 219 |
-
value="Pending",
|
| 220 |
-
label="Review Status"
|
| 221 |
-
)
|
| 222 |
-
|
| 223 |
-
with gr.Column(scale=1):
|
| 224 |
-
generate_btn = gr.Button("Generate Report", visible=False)
|
| 225 |
-
probability = gr.Label("Risk Probability", visible=False)
|
| 226 |
-
documents_btn = gr.Button("View Documents", visible=True)
|
| 227 |
-
|
| 228 |
-
doc_output = gr.JSON(label="Patient Documents", visible=False)
|
| 229 |
-
|
| 230 |
-
# Dynamic visibility and document handling
|
| 231 |
-
def update_components(selected_status):
|
| 232 |
-
visible = selected_status in ["Reviewed", "Approved"]
|
| 233 |
-
return (
|
| 234 |
-
{"visible": visible}, # For generate_btn
|
| 235 |
-
{"visible": visible, "value": {"Low": 0.3, "Medium": 0.6, "High": 0.9}.get(selected_status, 0.5)} # For probability
|
| 236 |
-
)
|
| 237 |
-
|
| 238 |
-
def show_documents(patient_id):
|
| 239 |
-
docs = CALLBACK_MANAGER.get_patient_documents(patient_id)
|
| 240 |
-
return {"value": docs, "visible": True} # For doc_output
|
| 241 |
-
|
| 242 |
-
status.change(
|
| 243 |
-
update_components,
|
| 244 |
-
inputs=status,
|
| 245 |
-
outputs=[generate_btn, probability]
|
| 246 |
-
)
|
| 247 |
-
|
| 248 |
-
documents_btn.click(
|
| 249 |
-
fn=show_documents,
|
| 250 |
-
inputs=patient_id_box,
|
| 251 |
-
outputs=doc_output
|
| 252 |
-
)
|
| 253 |
-
|
| 254 |
-
return stripe
|
| 255 |
|
| 256 |
def update_patient_display(patient_data_json):
|
| 257 |
"""
|
| 258 |
-
Update the patient display with parsed FHIR data
|
| 259 |
-
|
| 260 |
-
Args:
|
| 261 |
-
patient_data_json: JSON string of FHIR patient data
|
| 262 |
-
|
| 263 |
-
Returns:
|
| 264 |
-
Tuple of updates for the UI components
|
| 265 |
"""
|
| 266 |
try:
|
| 267 |
# Handle the case where patient_data_json is an error message
|
| 268 |
if isinstance(patient_data_json, str) and (patient_data_json.startswith("Not authenticated") or patient_data_json.startswith("Failed")):
|
| 269 |
logger.warning(f"Error in patient data: {patient_data_json}")
|
| 270 |
-
return
|
| 271 |
-
|
| 272 |
-
[],
|
| 273 |
-
|
| 274 |
-
None
|
| 275 |
-
|
| 276 |
|
| 277 |
# Parse JSON if it's a string
|
| 278 |
if isinstance(patient_data_json, str):
|
|
@@ -280,72 +237,147 @@ def update_patient_display(patient_data_json):
|
|
| 280 |
fhir_bundle = json.loads(patient_data_json)
|
| 281 |
except json.JSONDecodeError as e:
|
| 282 |
logger.error(f"JSON decode error: {str(e)} - Data: {patient_data_json[:100]}...")
|
| 283 |
-
return
|
| 284 |
-
|
| 285 |
-
[],
|
| 286 |
-
|
| 287 |
-
None
|
| 288 |
-
|
| 289 |
else:
|
| 290 |
fhir_bundle = patient_data_json
|
| 291 |
|
| 292 |
# Safety check
|
| 293 |
if not isinstance(fhir_bundle, dict):
|
| 294 |
logger.error(f"Unexpected data type: {type(fhir_bundle)}")
|
| 295 |
-
return
|
| 296 |
-
|
| 297 |
-
[],
|
| 298 |
-
|
| 299 |
-
None
|
| 300 |
-
|
| 301 |
|
| 302 |
patients = process_fhir_bundle(fhir_bundle)
|
| 303 |
|
| 304 |
if not patients:
|
| 305 |
logger.warning("No patients found in data")
|
| 306 |
-
return
|
| 307 |
-
|
| 308 |
-
[],
|
| 309 |
-
|
| 310 |
-
None
|
| 311 |
-
|
| 312 |
|
| 313 |
-
#
|
| 314 |
has_prev = any(link.get("relation") == "previous" for link in fhir_bundle.get("link", []))
|
| 315 |
has_next = any(link.get("relation") == "next" for link in fhir_bundle.get("link", []))
|
| 316 |
|
| 317 |
-
# Create
|
| 318 |
-
|
| 319 |
for patient in patients:
|
| 320 |
try:
|
| 321 |
-
|
| 322 |
-
|
| 323 |
except Exception as e:
|
| 324 |
-
logger.error(f"Error
|
| 325 |
# Continue with other patients if one fails
|
| 326 |
|
| 327 |
-
logger.info(f"
|
| 328 |
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
|
|
|
|
|
|
|
|
|
| 340 |
except Exception as e:
|
| 341 |
error_msg = f"Error loading patient data: {str(e)}\n{traceback.format_exc()}"
|
| 342 |
logger.error(error_msg)
|
| 343 |
-
return
|
| 344 |
-
|
| 345 |
-
[],
|
| 346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
None
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
|
| 350 |
def generate_pdf_from_form(
|
| 351 |
first_name, last_name, middle_initial, dob, age, sex, address, city, state, zip_code,
|
|
@@ -497,12 +529,12 @@ with gr.Blocks() as demo:
|
|
| 497 |
patient_data_state = gr.State()
|
| 498 |
|
| 499 |
# Pagination controls
|
| 500 |
-
with gr.Row(
|
| 501 |
-
prev_btn = gr.Button("Previous Page")
|
| 502 |
-
next_btn = gr.Button("Next Page")
|
| 503 |
|
| 504 |
-
#
|
| 505 |
-
|
| 506 |
|
| 507 |
# Metadata display
|
| 508 |
metadata_md = gr.Markdown()
|
|
|
|
| 183 |
patients.append(resource)
|
| 184 |
return patients
|
| 185 |
|
| 186 |
+
def create_patient_stripe_data(patient):
|
| 187 |
+
"""Extract data from patient to create a stripe (without components or event handlers)"""
|
| 188 |
# Safely extract name components
|
| 189 |
official_name = next(
|
| 190 |
(n for n in patient.get("name", []) if n.get("use") == "official"),
|
|
|
|
| 204 |
state = address.get("state", "")
|
| 205 |
postal_code = address.get("postalCode", "")
|
| 206 |
|
| 207 |
+
# Return structured data that can be used to display the patient info
|
| 208 |
+
return {
|
| 209 |
+
"name": f"{given_names} {family_name}",
|
| 210 |
+
"gender": gender,
|
| 211 |
+
"birth_date": birth_date,
|
| 212 |
+
"patient_id": patient_id,
|
| 213 |
+
"city": city,
|
| 214 |
+
"state": state,
|
| 215 |
+
"postal_code": postal_code
|
| 216 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
def update_patient_display(patient_data_json):
|
| 219 |
"""
|
| 220 |
+
Update the patient display with parsed FHIR data.
|
| 221 |
+
Returns processed data without trying to update UI components directly.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
"""
|
| 223 |
try:
|
| 224 |
# Handle the case where patient_data_json is an error message
|
| 225 |
if isinstance(patient_data_json, str) and (patient_data_json.startswith("Not authenticated") or patient_data_json.startswith("Failed")):
|
| 226 |
logger.warning(f"Error in patient data: {patient_data_json}")
|
| 227 |
+
return {
|
| 228 |
+
"has_pagination": False,
|
| 229 |
+
"patients": [],
|
| 230 |
+
"metadata": f"Error: {patient_data_json}",
|
| 231 |
+
"raw_data": None
|
| 232 |
+
}
|
| 233 |
|
| 234 |
# Parse JSON if it's a string
|
| 235 |
if isinstance(patient_data_json, str):
|
|
|
|
| 237 |
fhir_bundle = json.loads(patient_data_json)
|
| 238 |
except json.JSONDecodeError as e:
|
| 239 |
logger.error(f"JSON decode error: {str(e)} - Data: {patient_data_json[:100]}...")
|
| 240 |
+
return {
|
| 241 |
+
"has_pagination": False,
|
| 242 |
+
"patients": [],
|
| 243 |
+
"metadata": f"Invalid JSON data: {str(e)}",
|
| 244 |
+
"raw_data": None
|
| 245 |
+
}
|
| 246 |
else:
|
| 247 |
fhir_bundle = patient_data_json
|
| 248 |
|
| 249 |
# Safety check
|
| 250 |
if not isinstance(fhir_bundle, dict):
|
| 251 |
logger.error(f"Unexpected data type: {type(fhir_bundle)}")
|
| 252 |
+
return {
|
| 253 |
+
"has_pagination": False,
|
| 254 |
+
"patients": [],
|
| 255 |
+
"metadata": f"Expected dictionary but got {type(fhir_bundle)}",
|
| 256 |
+
"raw_data": None
|
| 257 |
+
}
|
| 258 |
|
| 259 |
patients = process_fhir_bundle(fhir_bundle)
|
| 260 |
|
| 261 |
if not patients:
|
| 262 |
logger.warning("No patients found in data")
|
| 263 |
+
return {
|
| 264 |
+
"has_pagination": False,
|
| 265 |
+
"patients": [],
|
| 266 |
+
"metadata": "No patients found in the data",
|
| 267 |
+
"raw_data": None
|
| 268 |
+
}
|
| 269 |
|
| 270 |
+
# Check pagination
|
| 271 |
has_prev = any(link.get("relation") == "previous" for link in fhir_bundle.get("link", []))
|
| 272 |
has_next = any(link.get("relation") == "next" for link in fhir_bundle.get("link", []))
|
| 273 |
|
| 274 |
+
# Create patient data for display
|
| 275 |
+
patient_data_list = []
|
| 276 |
for patient in patients:
|
| 277 |
try:
|
| 278 |
+
patient_data = create_patient_stripe_data(patient)
|
| 279 |
+
patient_data_list.append(patient_data)
|
| 280 |
except Exception as e:
|
| 281 |
+
logger.error(f"Error processing patient data: {str(e)}")
|
| 282 |
# Continue with other patients if one fails
|
| 283 |
|
| 284 |
+
logger.info(f"Processed {len(patient_data_list)} patients successfully")
|
| 285 |
|
| 286 |
+
# Generate metadata string
|
| 287 |
+
metadata_text = f"""
|
| 288 |
+
**Bundle Metadata**
|
| 289 |
+
Type: {fhir_bundle.get('type', 'Unknown')}
|
| 290 |
+
Total Patients: {fhir_bundle.get('total', len(patients))}
|
| 291 |
+
Last Updated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 292 |
+
"""
|
| 293 |
+
|
| 294 |
+
return {
|
| 295 |
+
"has_pagination": has_prev or has_next,
|
| 296 |
+
"patients": patient_data_list,
|
| 297 |
+
"metadata": metadata_text,
|
| 298 |
+
"raw_data": {"patients": patients, "bundle": fhir_bundle}
|
| 299 |
+
}
|
| 300 |
except Exception as e:
|
| 301 |
error_msg = f"Error loading patient data: {str(e)}\n{traceback.format_exc()}"
|
| 302 |
logger.error(error_msg)
|
| 303 |
+
return {
|
| 304 |
+
"has_pagination": False,
|
| 305 |
+
"patients": [],
|
| 306 |
+
"metadata": error_msg,
|
| 307 |
+
"raw_data": None
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
def fetch_and_display_patients():
|
| 311 |
+
try:
|
| 312 |
+
logger.info("Fetching patient data...")
|
| 313 |
+
# Get data from API
|
| 314 |
+
data = CALLBACK_MANAGER.get_patient_data()
|
| 315 |
+
logger.info(f"Received patient data: {data[:100]}..." if isinstance(data, str) else "Received non-string data")
|
| 316 |
+
|
| 317 |
+
# Handle case when the data is just an error message
|
| 318 |
+
if data.startswith("Not authenticated") or data.startswith("Failed"):
|
| 319 |
+
logger.warning(f"Authentication issue: {data}")
|
| 320 |
+
processed_data = {
|
| 321 |
+
"has_pagination": False,
|
| 322 |
+
"patients": [],
|
| 323 |
+
"metadata": f"Error: {data}",
|
| 324 |
+
"raw_data": None
|
| 325 |
+
}
|
| 326 |
+
else:
|
| 327 |
+
# Process the results
|
| 328 |
+
try:
|
| 329 |
+
processed_data = update_patient_display(data)
|
| 330 |
+
logger.info("Successfully processed patient display data")
|
| 331 |
+
except Exception as e:
|
| 332 |
+
logger.error(f"Error in update_patient_display: {str(e)}\n{traceback.format_exc()}")
|
| 333 |
+
processed_data = {
|
| 334 |
+
"has_pagination": False,
|
| 335 |
+
"patients": [],
|
| 336 |
+
"metadata": f"Error processing patient data: {str(e)}",
|
| 337 |
+
"raw_data": None
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
# Return the raw JSON and processed data
|
| 341 |
+
return [
|
| 342 |
+
data, # For the raw JSON output
|
| 343 |
+
processed_data["has_pagination"], # Update pagination visibility
|
| 344 |
+
format_patient_html(processed_data["patients"]), # HTML for patients
|
| 345 |
+
processed_data["metadata"], # Metadata text
|
| 346 |
+
processed_data["raw_data"] # State data
|
| 347 |
+
]
|
| 348 |
+
|
| 349 |
+
except Exception as e:
|
| 350 |
+
error_msg = f"Unexpected error fetching patient data: {str(e)}\n{traceback.format_exc()}"
|
| 351 |
+
logger.error(error_msg)
|
| 352 |
+
return [
|
| 353 |
+
f"Error: {str(e)}",
|
| 354 |
+
False,
|
| 355 |
+
"",
|
| 356 |
+
error_msg,
|
| 357 |
None
|
| 358 |
+
]
|
| 359 |
+
|
| 360 |
+
def format_patient_html(patients):
|
| 361 |
+
"""Format patient list as HTML for display in markdown component"""
|
| 362 |
+
if not patients:
|
| 363 |
+
return "No patients found."
|
| 364 |
+
|
| 365 |
+
html = ""
|
| 366 |
+
for i, patient in enumerate(patients):
|
| 367 |
+
html += f"""
|
| 368 |
+
<div style="padding: 10px; margin: 10px 0; border: 1px solid #ccc; border-radius: 5px; background-color: #f9f9f9;">
|
| 369 |
+
<h3>{patient['name']}</h3>
|
| 370 |
+
<p>{patient['gender']} | Born: {patient['birth_date']}</p>
|
| 371 |
+
<p>{patient['city']}, {patient['state']} {patient['postal_code']}</p>
|
| 372 |
+
<p>ID: {patient['patient_id']}</p>
|
| 373 |
+
<hr/>
|
| 374 |
+
<div style="display: flex; justify-content: space-between;">
|
| 375 |
+
<span>Status: Pending Review</span>
|
| 376 |
+
<button onclick="alert('Documents for patient {patient['patient_id']}')">View Documents</button>
|
| 377 |
+
</div>
|
| 378 |
+
</div>
|
| 379 |
+
"""
|
| 380 |
+
return html
|
| 381 |
|
| 382 |
def generate_pdf_from_form(
|
| 383 |
first_name, last_name, middle_initial, dob, age, sex, address, city, state, zip_code,
|
|
|
|
| 529 |
patient_data_state = gr.State()
|
| 530 |
|
| 531 |
# Pagination controls
|
| 532 |
+
with gr.Row() as pagination_row:
|
| 533 |
+
prev_btn = gr.Button("Previous Page", visible=False)
|
| 534 |
+
next_btn = gr.Button("Next Page", visible=False)
|
| 535 |
|
| 536 |
+
# Replace patient stripes with a single markdown component
|
| 537 |
+
patient_display = gr.HTML("No patient data loaded.")
|
| 538 |
|
| 539 |
# Metadata display
|
| 540 |
metadata_md = gr.Markdown()
|