attempt to generate discharge paper with auth
Browse files- app.py +31 -88
- utils/oneclick.py +6 -9
app.py
CHANGED
|
@@ -71,68 +71,6 @@ CALLBACK_MANAGER = CallbackManager(
|
|
| 71 |
)
|
| 72 |
|
| 73 |
|
| 74 |
-
# class CallbackManager:
|
| 75 |
-
# def __init__(self, redirect_uri: str, client_secret: str = None):
|
| 76 |
-
# client_id = os.getenv("APPID")
|
| 77 |
-
# if not client_id:
|
| 78 |
-
# raise ValueError("APPID environment variable not set.")
|
| 79 |
-
# workspace_id = os.getenv("WORKSPACE_URL")
|
| 80 |
-
# if not workspace_id:
|
| 81 |
-
# raise ValueError("WORKSPACE_URL environment variable not set.")
|
| 82 |
-
# self.api = MeldRxAPI(client_id, client_secret, workspace_id, redirect_uri)
|
| 83 |
-
# self.auth_code = None
|
| 84 |
-
# self.access_token = None
|
| 85 |
-
|
| 86 |
-
# def handle_callback(self, callback_url: str) -> str:
|
| 87 |
-
# """Handles the callback URL and extracts the code automatically."""
|
| 88 |
-
# self.auth_code = extract_code_from_url(callback_url)
|
| 89 |
-
# if not self.auth_code:
|
| 90 |
-
# return "No authentication code found in URL."
|
| 91 |
-
|
| 92 |
-
# if self.api.authenticate_with_code(self.auth_code):
|
| 93 |
-
# self.access_token = self.api.access_token
|
| 94 |
-
# return f"Authentication successful! Access Token: {self.access_token[:10]}... (truncated)"
|
| 95 |
-
# return "Authentication failed. Please check the authorization code."
|
| 96 |
-
|
| 97 |
-
# def generate_discharge_paper_one_click():
|
| 98 |
-
# """One-click function to fetch patient data and generate discharge paper with AI Content."""
|
| 99 |
-
# patient_data_str = CALLBACK_MANAGER.get_patient_data()
|
| 100 |
-
# if (
|
| 101 |
-
# patient_data_str.startswith("Not authenticated")
|
| 102 |
-
# or patient_data_str.startswith("Failed")
|
| 103 |
-
# or patient_data_str.startswith("Error")
|
| 104 |
-
# ):
|
| 105 |
-
# return None, patient_data_str # Return error message if authentication or data fetch fails
|
| 106 |
-
|
| 107 |
-
# try:
|
| 108 |
-
# patient_data = json.loads(patient_data_str)
|
| 109 |
-
|
| 110 |
-
# # --- AI Content Generation for Discharge Summary ---
|
| 111 |
-
# # This is a placeholder - Replace with actual AI call using InferenceClient and patient_data to generate content
|
| 112 |
-
# ai_generated_content = generate_ai_discharge_content(
|
| 113 |
-
# patient_data
|
| 114 |
-
# ) # Placeholder AI function
|
| 115 |
-
|
| 116 |
-
# if not ai_generated_content:
|
| 117 |
-
# return None, "Error: AI content generation failed."
|
| 118 |
-
|
| 119 |
-
# # --- PDF Generation with AI Content ---
|
| 120 |
-
# pdf_path, status_message = generate_pdf_from_meldrx_with_ai_content(
|
| 121 |
-
# patient_data, ai_generated_content
|
| 122 |
-
# ) # Function to generate PDF with AI content
|
| 123 |
-
|
| 124 |
-
# if pdf_path:
|
| 125 |
-
# return pdf_path, status_message
|
| 126 |
-
# else:
|
| 127 |
-
# return None, status_message # Return status message if PDF generation fails
|
| 128 |
-
|
| 129 |
-
# except json.JSONDecodeError:
|
| 130 |
-
# return None, "Error: Patient data is not in valid JSON format."
|
| 131 |
-
# except Exception as e:
|
| 132 |
-
# return None, f"Error during discharge paper generation: {str(e)}"
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
# Define the cyberpunk theme - using a dark base and neon accents
|
| 137 |
cyberpunk_theme = gr.themes.Monochrome(
|
| 138 |
primary_hue="cyan",
|
|
@@ -395,33 +333,38 @@ with gr.Blocks(theme=cyberpunk_theme) as demo: # Apply the theme here
|
|
| 395 |
analyze_csv_file_with_ai, inputs=csv_file, outputs=csv_ai_output
|
| 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 |
-
# one_click_ai_button.click(
|
| 421 |
-
# fn=lambda pid, fname, lname: generate_discharge_paper_one_click(meldrx_api, pid, fname, lname),
|
| 422 |
-
# inputs=[patient_id_input, first_name_input, last_name_input],
|
| 423 |
-
# outputs=one_click_ai_output,
|
| 424 |
-
# )
|
| 425 |
|
| 426 |
# Connect the patient data buttons
|
| 427 |
patient_data_button.click(
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
# Define the cyberpunk theme - using a dark base and neon accents
|
| 75 |
cyberpunk_theme = gr.themes.Monochrome(
|
| 76 |
primary_hue="cyan",
|
|
|
|
| 333 |
analyze_csv_file_with_ai, inputs=csv_file, outputs=csv_ai_output
|
| 334 |
)
|
| 335 |
|
| 336 |
+
with gr.Tab("One-Click Discharge Paper (AI)", elem_classes="cyberpunk-tab"):
|
| 337 |
+
gr.Markdown("<h2 style='color:#00FFFF; text-shadow: 0 0 3px #00FFFF;'>One-Click Medical Discharge Summary with AI Content</h2>")
|
| 338 |
+
with gr.Row():
|
| 339 |
+
patient_id_input = gr.Textbox(label="Patient ID (Optional)", placeholder="Enter Patient ID")
|
| 340 |
+
first_name_input = gr.Textbox(label="First Name (Optional)", placeholder="Enter First Name")
|
| 341 |
+
last_name_input = gr.Textbox(label="Last Name (Optional)", placeholder="Enter Last Name")
|
| 342 |
+
one_click_ai_button = gr.Button("Generate Discharge Summary with AI (One-Click)", elem_classes="cyberpunk-button")
|
| 343 |
+
one_click_ai_output = gr.File(label="Download AI-Generated Discharge PDF") # Changed to File output for PDF
|
| 344 |
+
one_click_ai_status = gr.Textbox(label="Generation Status", lines=2, placeholder="Status will appear here...")
|
| 345 |
+
|
| 346 |
+
# Use the global CALLBACK_MANAGER instead of creating a new MeldRxAPI instance
|
| 347 |
+
def one_click_handler(patient_id, first_name, last_name):
|
| 348 |
+
try:
|
| 349 |
+
# Check if CALLBACK_MANAGER is authenticated
|
| 350 |
+
if not CALLBACK_MANAGER.access_token:
|
| 351 |
+
return None, "Error: Not authenticated. Please authenticate in the 'Authenticate with MeldRx' tab first."
|
| 352 |
+
|
| 353 |
+
# Call the one-click function with the existing authenticated CALLBACK_MANAGER.api
|
| 354 |
+
pdf_path, status = generate_discharge_paper_one_click(
|
| 355 |
+
CALLBACK_MANAGER.api, patient_id, first_name, last_name
|
| 356 |
+
)
|
| 357 |
+
return pdf_path, status
|
| 358 |
+
except Exception as e:
|
| 359 |
+
logger.error(f"One-click handler error: {str(e)}")
|
| 360 |
+
return None, f"Error: {str(e)}"
|
| 361 |
+
|
| 362 |
+
one_click_ai_button.click(
|
| 363 |
+
fn=one_click_handler,
|
| 364 |
+
inputs=[patient_id_input, first_name_input, last_name_input],
|
| 365 |
+
outputs=[one_click_ai_output, one_click_ai_status],
|
| 366 |
+
)
|
| 367 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
# Connect the patient data buttons
|
| 370 |
patient_data_button.click(
|
utils/oneclick.py
CHANGED
|
@@ -111,7 +111,7 @@ def generate_discharge_paper_one_click(
|
|
| 111 |
Generate a discharge paper with AI content in one click.
|
| 112 |
|
| 113 |
Args:
|
| 114 |
-
meldrx_api (MeldRxAPI): Initialized MeldRxAPI instance.
|
| 115 |
patient_id (str, optional): Patient ID to fetch specific patient data.
|
| 116 |
first_name (str, optional): First name for patient lookup if patient_id is not provided.
|
| 117 |
last_name (str, optional): Last name for patient lookup if patient_id is not provided.
|
|
@@ -120,14 +120,13 @@ def generate_discharge_paper_one_click(
|
|
| 120 |
tuple[Optional[str], str]: (PDF file path, Status message)
|
| 121 |
"""
|
| 122 |
try:
|
| 123 |
-
#
|
| 124 |
-
if not meldrx_api.access_token
|
| 125 |
-
return None, "Error:
|
| 126 |
|
| 127 |
# Fetch patient data
|
| 128 |
if patient_id:
|
| 129 |
-
|
| 130 |
-
patient_data = meldrx_api.get_patients() # Simplified; assumes ID filtering in real API
|
| 131 |
if not patient_data or "entry" not in patient_data:
|
| 132 |
return None, "Error: Failed to fetch patient data by ID."
|
| 133 |
patients = [entry["resource"] for entry in patient_data.get("entry", [])]
|
|
@@ -135,7 +134,6 @@ def generate_discharge_paper_one_click(
|
|
| 135 |
if not patient:
|
| 136 |
return None, f"Error: Patient with ID {patient_id} not found."
|
| 137 |
else:
|
| 138 |
-
# Fetch all patients and filter by name if provided
|
| 139 |
patient_data = meldrx_api.get_patients()
|
| 140 |
if not patient_data or "entry" not in patient_data:
|
| 141 |
return None, "Error: Failed to fetch patient data."
|
|
@@ -150,7 +148,6 @@ def generate_discharge_paper_one_click(
|
|
| 150 |
if not patient:
|
| 151 |
return None, f"Error: Patient with name {first_name} {last_name} not found."
|
| 152 |
else:
|
| 153 |
-
# Default to first patient if no specific ID or name provided
|
| 154 |
patient = patients[0] if patients else None
|
| 155 |
if not patient:
|
| 156 |
return None, "Error: No patients found in the workspace."
|
|
@@ -174,4 +171,4 @@ def generate_discharge_paper_one_click(
|
|
| 174 |
|
| 175 |
except Exception as e:
|
| 176 |
logger.error(f"Error in one-click discharge generation: {str(e)}")
|
| 177 |
-
return None, f"Error: {str(e)}"
|
|
|
|
| 111 |
Generate a discharge paper with AI content in one click.
|
| 112 |
|
| 113 |
Args:
|
| 114 |
+
meldrx_api (MeldRxAPI): Initialized and authenticated MeldRxAPI instance.
|
| 115 |
patient_id (str, optional): Patient ID to fetch specific patient data.
|
| 116 |
first_name (str, optional): First name for patient lookup if patient_id is not provided.
|
| 117 |
last_name (str, optional): Last name for patient lookup if patient_id is not provided.
|
|
|
|
| 120 |
tuple[Optional[str], str]: (PDF file path, Status message)
|
| 121 |
"""
|
| 122 |
try:
|
| 123 |
+
# Check if already authenticated
|
| 124 |
+
if not meldrx_api.access_token:
|
| 125 |
+
return None, "Error: Not authenticated. Please authenticate first in the 'Authenticate with MeldRx' tab."
|
| 126 |
|
| 127 |
# Fetch patient data
|
| 128 |
if patient_id:
|
| 129 |
+
patient_data = meldrx_api.get_patients()
|
|
|
|
| 130 |
if not patient_data or "entry" not in patient_data:
|
| 131 |
return None, "Error: Failed to fetch patient data by ID."
|
| 132 |
patients = [entry["resource"] for entry in patient_data.get("entry", [])]
|
|
|
|
| 134 |
if not patient:
|
| 135 |
return None, f"Error: Patient with ID {patient_id} not found."
|
| 136 |
else:
|
|
|
|
| 137 |
patient_data = meldrx_api.get_patients()
|
| 138 |
if not patient_data or "entry" not in patient_data:
|
| 139 |
return None, "Error: Failed to fetch patient data."
|
|
|
|
| 148 |
if not patient:
|
| 149 |
return None, f"Error: Patient with name {first_name} {last_name} not found."
|
| 150 |
else:
|
|
|
|
| 151 |
patient = patients[0] if patients else None
|
| 152 |
if not patient:
|
| 153 |
return None, "Error: No patients found in the workspace."
|
|
|
|
| 171 |
|
| 172 |
except Exception as e:
|
| 173 |
logger.error(f"Error in one-click discharge generation: {str(e)}")
|
| 174 |
+
return None, f"Error: {str(e)}"
|