Tonic's picture
attempt to generate discharge paper with auth
a82b00b unverified
raw
history blame
22.6 kB
import gradio as gr
from utils.meldrx import MeldRxAPI
import json
import os
import tempfile
from datetime import datetime
import traceback
import logging
from huggingface_hub import InferenceClient # Import InferenceClient
from urllib.parse import urlparse, parse_qs # Import URL parsing utilities
from utils.callbackmanager import CallbackManager
from utils.meldrx import MeldRxAPI
from utils.prompts import system_instructions
from old.extractcode import extract_code_from_url
from utils.generators import generate_pdf_from_meldrx, generate_ai_discharge_content, generate_pdf_from_meldrx_with_ai_content, extract_section, generate_pdf_from_form, generate_discharge_summary, generate_ai_discharge_content, analyze_dicom_file_with_ai, analyze_hl7_file_with_ai, analyze_cda_xml_file_with_ai, analyze_pdf_file_with_ai, analyze_csv_file_with_ai
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Import PDF utilities
from utils.pdfutils import PDFGenerator, generate_discharge_summary
from utils.oneclick import generate_discharge_paper_one_click
# Import necessary libraries for new file types and AI analysis functions
import pydicom # For DICOM
import hl7 # For HL7
from xml.etree import ElementTree # For XML and CCDA
from pypdf import PdfReader # For PDF
import csv # For CSV
import io # For IO operations
from PIL import Image # For image handling
from utils.callbackmanager import extract_auth_code_from_url, extract_code_from_url
from utils.generators import generate_pdf_from_meldrx, generate_ai_discharge_content, generate_pdf_from_meldrx_with_ai_content, extract_section, generate_pdf_from_form, generate_discharge_summary, generate_ai_discharge_content, analyze_dicom_file_with_ai, analyze_hl7_file_with_ai, analyze_cda_xml_file_with_ai, analyze_pdf_file_with_ai, analyze_csv_file_with_ai, generate_pdf_from_form , generate_ai_discharge_content , extract_section , generate_pdf_from_meldrx_with_ai_content
# Initialize Inference Client - Ensure YOUR_HF_TOKEN is set in environment variables or replace with your actual token
HF_TOKEN = os.getenv("HF_TOKEN") # Or replace with your actual token string
if not HF_TOKEN:
raise ValueError(
"HF_TOKEN environment variable not set. Please set your Hugging Face API token."
)
client = InferenceClient(api_key=HF_TOKEN)
model_name = "meta-llama/Llama-3.3-70B-Instruct" # Specify the model to use
def display_form(first_name, last_name, middle_initial, dob, age, sex, address, city, state, zip_code, doctor_first_name, doctor_last_name, doctor_middle_initial, hospital_name, doctor_address, doctor_city, doctor_state, doctor_zip, admission_date, referral_source, admission_method, discharge_date, discharge_reason, date_of_death, diagnosis, procedures, medications, preparer_name, preparer_job_title,):
form = f"""
<div style='color:#00FFFF; font-family: monospace;'>
**Patient Discharge Form** <br>
- Name: {first_name} {middle_initial} {last_name} <br>
- Date of Birth: {dob}, Age: {age}, Sex: {sex} <br>
- Address: {address}, {city}, {state}, {zip_code} <br>
- Doctor: {doctor_first_name} {doctor_middle_initial} {doctor_last_name} <br>
- Hospital/Clinic: {hospital_name} <br>
- Doctor Address: {doctor_address}, {doctor_city}, {doctor_state}, {doctor_zip} <br>
- Admission Date: {admission_date}, Source: {referral_source}, Method: {admission_method} <br>
- Discharge Date: {discharge_date}, Reason: {discharge_reason} <br>
- Date of Death: {date_of_death} <br>
- Diagnosis: {diagnosis} <br>
- Procedures: {procedures} <br>
- Medications: {medications} <br>
- Prepared By: {preparer_name}, {preparer_job_title}
</div>
"""
return form
CALLBACK_MANAGER = CallbackManager(
redirect_uri="https://multitransformer-discharge-guard.hf.space/callback",
client_secret=None,
)
# Define the cyberpunk theme - using a dark base and neon accents
cyberpunk_theme = gr.themes.Monochrome(
primary_hue="cyan",
secondary_hue="pink",
neutral_hue="slate",
font=["Source Code Pro", "monospace"], # Retro monospace font
font_mono=["Source Code Pro", "monospace"]
)
# Create the UI with the cyberpunk theme
with gr.Blocks(theme=cyberpunk_theme) as demo: # Apply the theme here
gr.Markdown("<h1 style='color:#00FFFF; text-shadow: 0 0 5px #00FFFF;'>Discharge Guard <span style='color:#FF00FF; text-shadow: 0 0 5px #FF00FF;'>Cyber</span></h1>") # Cyberpunk Title
with gr.Tab("Authenticate with MeldRx", elem_classes="cyberpunk-tab"): # Optional: Class for tab styling
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>SMART on FHIR Authentication</h2>") # Neon Tab Header
auth_url_output = gr.Textbox(label="Authorization URL", value=CALLBACK_MANAGER.get_auth_url(), interactive=False)
gr.Markdown("<p style='color:#A9A9A9;'>Copy the URL above, open it in a browser, log in, and paste the <span style='color:#00FFFF;'>entire redirected URL</span> from your browser's address bar below.</p>") # Subdued instructions with neon highlight
redirected_url_input = gr.Textbox(label="Redirected URL") # New textbox for redirected URL
extract_code_button = gr.Button("Extract Authorization Code", elem_classes="cyberpunk-button") # Cyberpunk button style
extracted_code_output = gr.Textbox(label="Extracted Authorization Code", interactive=False) # Textbox to show extracted code
auth_code_input = gr.Textbox(label="Authorization Code (from above, or paste manually if extraction fails)", interactive=True) # Updated label to be clearer
auth_submit = gr.Button("Submit Code for Authentication", elem_classes="cyberpunk-button") # Cyberpunk button style
auth_result = gr.HTML(label="Authentication Result") # Use HTML for styled result
patient_data_button = gr.Button("Fetch Patient Data", elem_classes="cyberpunk-button") # Cyberpunk button style
patient_data_output = gr.Textbox(label="Patient Data", lines=10)
# Add button to generate PDF from MeldRx data (No AI)
meldrx_pdf_button = gr.Button("Generate PDF from MeldRx Data (No AI)", elem_classes="cyberpunk-button") # Renamed button
meldrx_pdf_status = gr.Textbox(label="PDF Generation Status (No AI)") # Renamed status
meldrx_pdf_download = gr.File(label="Download Generated PDF (No AI)") # Renamed download
def process_redirected_url(redirected_url):
"""Processes the redirected URL to extract and display the authorization code."""
auth_code, error_message = extract_auth_code_from_url(redirected_url)
if auth_code:
return auth_code, "<span style='color:#00FF7F;'>Authorization code extracted!</span>" # Neon Green Success
else:
return "", f"<span style='color:#FF4500;'>Could not extract authorization code.</span> {error_message or ''}" # Neon Orange Error
extract_code_button.click(
fn=process_redirected_url,
inputs=redirected_url_input,
outputs=[extracted_code_output, auth_result],# Reusing auth_result for extraction status
)
auth_submit.click(
fn=CALLBACK_MANAGER.set_auth_code,
inputs=extracted_code_output, # Using extracted code as input for authentication
outputs=auth_result,
)
with gr.Tab("Patient Dashboard", elem_classes="cyberpunk-tab"): # Optional: Class for tab styling
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>Patient Data</h2>") # Neon Tab Header
dashboard_output = gr.HTML("<p style='color:#A9A9A9;'>Fetch patient data from the Authentication tab first.</p>") # Subdued placeholder text
refresh_btn = gr.Button("Refresh Data", elem_classes="cyberpunk-button") # Cyberpunk button style
# Simple function to update dashboard based on fetched data
def update_dashboard():
try:
data = CALLBACK_MANAGER.get_patient_data()
if (
data.startswith("<span style='color:#FF8C00;'>Not authenticated")
or data.startswith("<span style='color:#DC143C;'>Failed")
or data.startswith("<span style='color:#FF6347;'>Error")
):
return f"<p style='color:#FF8C00;'>{data}</p>" # Show auth errors in orange
try:
# Parse the data
patients_data = json.loads(data)
patients = []
# Extract patients from bundle
for entry in patients_data.get("entry", []):
resource = entry.get("resource", {})
if resource.get("resourceType") == "Patient":
patients.append(resource)
# Generate HTML card
html = "<h3 style='color:#00FFFF; text-shadow: 0 0 2px #00FFFF;'>Patients</h3>" # Neon Sub-header
for patient in patients:
# Extract name
name = patient.get("name", [{}])[0]
given = " ".join(name.get("given", ["Unknown"]))
family = name.get("family", "Unknown")
# Extract other details
gender = patient.get("gender", "unknown").capitalize()
birth_date = patient.get("birthDate", "Unknown")
# Generate HTML card with cyberpunk styling
html += f"""
<div style="border: 1px solid #00FFFF; padding: 10px; margin: 10px 0; border-radius: 5px; background-color: #222; box-shadow: 0 0 5px #00FFFF;">
<h4 style='color:#00FFFF;'>{given} {family}</h4>
<p style='color:#A9A9A9;'><strong>Gender:</strong> <span style='color:#00FFFF;'>{gender}</span></p>
<p style='color:#A9A9A9;'><strong>Birth Date:</strong> <span style='color:#00FFFF;'>{birth_date}</span></p>
<p style='color:#A9A9A9;'><strong>ID:</strong> <span style='color:#00FFFF;'>{patient.get("id", "Unknown")}</span></p>
</div>
"""
return html
except Exception as e:
return f"<p style='color:#FF6347;'>Error parsing patient data: {str(e)}</p>" # Tomato Error
except Exception as e:
return f"<p style='color:#FF6347;'>Error fetching patient data: {str(e)}</p>" # Tomato Error
with gr.Tab("Discharge Form", elem_classes="cyberpunk-tab"): # Optional: Class for tab styling
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>Patient Details</h2>") # Neon Tab Header
with gr.Row():
first_name = gr.Textbox(label="First Name")
last_name = gr.Textbox(label="Last Name")
middle_initial = gr.Textbox(label="Middle Initial")
with gr.Row():
dob = gr.Textbox(label="Date of Birth")
age = gr.Textbox(label="Age")
sex = gr.Textbox(label="Sex")
address = gr.Textbox(label="Address")
with gr.Row():
city = gr.Textbox(label="City")
state = gr.Textbox(label="State")
zip_code = gr.Textbox(label="Zip Code")
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>Primary Healthcare Professional Details</h2>") # Neon Sub-header
with gr.Row():
doctor_first_name = gr.Textbox(label="Doctor's First Name")
doctor_last_name = gr.Textbox(label="Doctor's Last Name")
doctor_middle_initial = gr.Textbox(label="Doctor's Middle Initial")
hospital_name = gr.Textbox(label="Hospital/Clinic Name")
doctor_address = gr.Textbox(label="Address")
with gr.Row():
doctor_city = gr.Textbox(label="City")
doctor_state = gr.Textbox(label="State")
doctor_zip = gr.Textbox(label="Zip Code")
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>Admission and Discharge Details</h2>") # Neon Sub-header
with gr.Row():
admission_date = gr.Textbox(label="Date of Admission")
referral_source = gr.Textbox(label="Source of Referral")
admission_method = gr.Textbox(label="Method of Admission")
with gr.Row():
discharge_date = gr.Textbox(label="Date of Discharge")
discharge_reason = gr.Radio(
["Treated", "Transferred", "Discharge Against Advice", "Patient Died"],
label="Discharge Reason",
)
date_of_death = gr.Textbox(label="Date of Death (if applicable)")
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>Diagnosis & Procedures</h2>") # Neon Sub-header
diagnosis = gr.Textbox(label="Diagnosis")
procedures = gr.Textbox(label="Operation & Procedures")
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>Medication Details</h2>") # Neon Sub-header
medications = gr.Textbox(label="Medication on Discharge")
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>Prepared By</h2>") # Neon Sub-header
with gr.Row():
preparer_name = gr.Textbox(label="Name")
preparer_job_title = gr.Textbox(label="Job Title")
# Add buttons for both display form and generate PDF
with gr.Row():
submit_display = gr.Button("Display Form", elem_classes="cyberpunk-button") # Cyberpunk button style
submit_pdf = gr.Button("Generate PDF (No AI)", elem_classes="cyberpunk-button") # Renamed button to clarify no AI and styled
# Output areas
form_output = gr.HTML() # Use HTML to render styled form
pdf_output = gr.File(label="Download PDF (No AI)") # Renamed output to clarify no AI
# Connect the display form button
submit_display.click(
display_form,
inputs=[ first_name, last_name, middle_initial, dob, age, sex, address, city, state, zip_code, doctor_first_name, doctor_last_name, doctor_middle_initial, hospital_name, doctor_address, doctor_city, doctor_state, doctor_zip, admission_date, referral_source, admission_method, discharge_date, discharge_reason, date_of_death, diagnosis, procedures, medications, preparer_name, preparer_job_title,],
outputs=form_output
)
# Connect the generate PDF button (No AI version)
submit_pdf.click(
generate_pdf_from_form,
inputs=[ first_name, last_name, middle_initial, dob, age, sex, address, city, state, zip_code, doctor_first_name, doctor_last_name, doctor_middle_initial, hospital_name, doctor_address, doctor_city, doctor_state, doctor_zip, admission_date, referral_source, admission_method, discharge_date, discharge_reason, date_of_death, diagnosis, procedures, medications, preparer_name, preparer_job_title,],
outputs=pdf_output
)
with gr.Tab("Medical File Analysis", elem_classes="cyberpunk-tab"): # Optional: Class for tab styling
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>Analyze Medical Files with Discharge Guard AI</h2>") # Neon Tab Header
with gr.Column():
dicom_file = gr.File(
file_types=[".dcm"], label="Upload DICOM File (.dcm)"
)
dicom_ai_output = gr.Textbox(label="DICOM Analysis Report", lines=5)
analyze_dicom_button = gr.Button("Analyze DICOM with AI", elem_classes="cyberpunk-button") # Cyberpunk button style
hl7_file = gr.File(
file_types=[".hl7"], label="Upload HL7 File (.hl7)"
)
hl7_ai_output = gr.Textbox(label="HL7 Analysis Report", lines=5)
analyze_hl7_button = gr.Button("Analyze HL7 with AI", elem_classes="cyberpunk-button") # Cyberpunk button style
xml_file = gr.File(
file_types=[".xml"], label="Upload XML File (.xml)"
)
xml_ai_output = gr.Textbox(label="XML Analysis Report", lines=5)
analyze_xml_button = gr.Button("Analyze XML with AI", elem_classes="cyberpunk-button") # Cyberpunk button style
ccda_file = gr.File(
file_types=[".xml", ".cda", ".ccd"], label="Upload CCDA File (.xml, .cda, .ccd)"
)
ccda_ai_output = gr.Textbox(label="CCDA Analysis Report", lines=5)
analyze_ccda_button = gr.Button("Analyze CCDA with AI", elem_classes="cyberpunk-button") # Cyberpunk button style
ccd_file = gr.File(
file_types=[".ccd"],
label="Upload CCD File (.ccd)",
) # Redundant, as CCDA also handles .ccd, but kept for clarity
ccd_ai_output = gr.Textbox(
label="CCD Analysis Report", lines=5
) # Redundant
analyze_ccd_button = gr.Button("Analyze CCD with AI", elem_classes="cyberpunk-button") # Cyberpunk button style # Redundant
pdf_file = gr.File(
file_types=[".pdf"], label="Upload PDF File (.pdf)"
)
pdf_ai_output = gr.Textbox(label="PDF Analysis Report", lines=5)
analyze_pdf_button = gr.Button("Analyze PDF with AI", elem_classes="cyberpunk-button") # Cyberpunk button style
csv_file = gr.File(
file_types=[".csv"], label="Upload CSV File (.csv)"
)
csv_ai_output = gr.Textbox(label="CSV Analysis Report", lines=5)
analyze_csv_button = gr.Button("Analyze CSV with AI", elem_classes="cyberpunk-button") # Cyberpunk button style
# Connect AI Analysis Buttons - using REAL AI functions now
analyze_dicom_button.click(
analyze_dicom_file_with_ai, # Call REAL AI function
inputs=dicom_file,
outputs=dicom_ai_output
)
analyze_hl7_button.click(
analyze_hl7_file_with_ai, # Call REAL AI function
inputs=hl7_file,
outputs=hl7_ai_output
)
analyze_xml_button.click(
analyze_cda_xml_file_with_ai, # Call REAL AI function
inputs=xml_file,
outputs=xml_ai_output
)
analyze_ccda_button.click(
analyze_cda_xml_file_with_ai, # Call REAL AI function
inputs=ccda_file,
outputs=ccda_ai_output
)
analyze_ccd_button.click( # Redundant button, but kept for UI if needed
analyze_cda_xml_file_with_ai, # Call REAL AI function
inputs=ccd_file,
outputs=ccd_ai_output
)
analyze_pdf_button.click(
analyze_pdf_file_with_ai, inputs=pdf_file, outputs=pdf_ai_output
)
analyze_csv_button.click(
analyze_csv_file_with_ai, inputs=csv_file, outputs=csv_ai_output
)
with gr.Tab("One-Click Discharge Paper (AI)", elem_classes="cyberpunk-tab"):
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>One-Click Medical Discharge Summary with AI Content</h2>")
with gr.Row():
patient_id_input = gr.Textbox(label="Patient ID (Optional)", placeholder="Enter Patient ID")
first_name_input = gr.Textbox(label="First Name (Optional)", placeholder="Enter First Name")
last_name_input = gr.Textbox(label="Last Name (Optional)", placeholder="Enter Last Name")
one_click_ai_button = gr.Button("Generate Discharge Summary with AI (One-Click)", elem_classes="cyberpunk-button")
one_click_ai_output = gr.Textbox(label="AI-Generated Discharge Summary", lines=20, placeholder="Discharge summary will appear here...")
# Initialize MeldRxAPI (ensure client_id, client_secret, workspace_id are set in environment variables)
client_id = os.getenv("APPID")
client_secret = os.getenv("CLIENT_SECRET")
workspace_id = os.getenv("WORKSPACE_URL")
redirect_uri = "https://multitransformer-discharge-guard.hf.space/callback"
meldrx_api = MeldRxAPI(client_id, client_secret, workspace_id, redirect_uri)
one_click_ai_button.click(
fn=lambda pid, fname, lname: generate_discharge_paper_one_click(meldrx_api, pid, fname, lname),
inputs=[patient_id_input, first_name_input, last_name_input],
outputs=one_click_ai_output,
)
# Use the global CALLBACK_MANAGER instead of creating a new MeldRxAPI instance
def one_click_handler(patient_id, first_name, last_name):
try:
# Check if CALLBACK_MANAGER is authenticated
if not CALLBACK_MANAGER.access_token:
return None, "Error: Not authenticated. Please authenticate in the 'Authenticate with MeldRx' tab first."
# Call the one-click function with the existing authenticated CALLBACK_MANAGER.api
pdf_path, status = generate_discharge_paper_one_click(
CALLBACK_MANAGER.api, patient_id, first_name, last_name
)
return pdf_path, status
except Exception as e:
logger.error(f"One-click handler error: {str(e)}")
return None, f"Error: {str(e)}"
# one_click_ai_button.click(
# fn=lambda pid, fname, lname: generate_discharge_paper_one_click(meldrx_api, pid, fname, lname),
# inputs=[patient_id_input, first_name_input, last_name_input],
# outputs=one_click_ai_output,
# )
# Connect the patient data buttons
patient_data_button.click(
fn=CALLBACK_MANAGER.get_patient_data,
inputs=None,
outputs=patient_data_output
)
# Connect refresh button to update dashboard
refresh_btn.click(
fn=update_dashboard, inputs=None, outputs=dashboard_output
)
# Corrected the button click function name here to `generate_pdf_from_meldrx` (No AI PDF)
meldrx_pdf_button.click(
fn=generate_pdf_from_meldrx,
inputs=patient_data_output,
outputs=[meldrx_pdf_download, meldrx_pdf_status]
)
# Connect patient data updates to dashboard
patient_data_button.click(
fn=update_dashboard, inputs=None, outputs=dashboard_output
)
# Launch with sharing enabled for public access
demo.launch(ssr_mode=False)