Update callbackmanager.py
Browse files- callbackmanager.py +41 -4
callbackmanager.py
CHANGED
|
@@ -7,6 +7,7 @@ from datetime import datetime
|
|
| 7 |
import traceback
|
| 8 |
import logging
|
| 9 |
from huggingface_hub import InferenceClient # Import InferenceClient
|
|
|
|
| 10 |
|
| 11 |
# Set up logging
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -737,6 +738,17 @@ def generate_pdf_from_meldrx_with_ai_content(patient_data, llm_content):
|
|
| 737 |
except Exception as e:
|
| 738 |
return None, f"Error generating PDF with AI content: {str(e)}"
|
| 739 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 740 |
|
| 741 |
# Create a simplified interface to avoid complex component interactions
|
| 742 |
CALLBACK_MANAGER = CallbackManager(
|
|
@@ -751,9 +763,13 @@ with gr.Blocks() as demo:
|
|
| 751 |
with gr.Tab("Authenticate with MeldRx"):
|
| 752 |
gr.Markdown("## SMART on FHIR Authentication")
|
| 753 |
auth_url_output = gr.Textbox(label="Authorization URL", value=CALLBACK_MANAGER.get_auth_url(), interactive=False)
|
| 754 |
-
gr.Markdown("Copy the URL above, open it in a browser, log in, and paste the
|
| 755 |
-
|
| 756 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
auth_result = gr.Textbox(label="Authentication Result")
|
| 758 |
|
| 759 |
patient_data_button = gr.Button("Fetch Patient Data")
|
|
@@ -764,7 +780,28 @@ with gr.Blocks() as demo:
|
|
| 764 |
meldrx_pdf_status = gr.Textbox(label="PDF Generation Status (No AI)") # Renamed status
|
| 765 |
meldrx_pdf_download = gr.File(label="Download Generated PDF (No AI)") # Renamed download
|
| 766 |
|
| 767 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 768 |
|
| 769 |
with gr.Tab("Patient Dashboard"):
|
| 770 |
gr.Markdown("## Patient Data")
|
|
|
|
| 7 |
import traceback
|
| 8 |
import logging
|
| 9 |
from huggingface_hub import InferenceClient # Import InferenceClient
|
| 10 |
+
from urllib.parse import urlparse, parse_qs # Import URL parsing utilities
|
| 11 |
|
| 12 |
# Set up logging
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 738 |
except Exception as e:
|
| 739 |
return None, f"Error generating PDF with AI content: {str(e)}"
|
| 740 |
|
| 741 |
+
def extract_auth_code_from_url(redirected_url):
|
| 742 |
+
"""Extracts the authorization code from the redirected URL."""
|
| 743 |
+
try:
|
| 744 |
+
parsed_url = urlparse(redirected_url)
|
| 745 |
+
query_params = parse_qs(parsed_url.query)
|
| 746 |
+
if "code" in query_params:
|
| 747 |
+
return query_params["code"][0], None # Return code and no error
|
| 748 |
+
else:
|
| 749 |
+
return None, "Authorization code not found in URL." # Return None and error message
|
| 750 |
+
except Exception as e:
|
| 751 |
+
return None, f"Error parsing URL: {e}" # Return None and error message
|
| 752 |
|
| 753 |
# Create a simplified interface to avoid complex component interactions
|
| 754 |
CALLBACK_MANAGER = CallbackManager(
|
|
|
|
| 763 |
with gr.Tab("Authenticate with MeldRx"):
|
| 764 |
gr.Markdown("## SMART on FHIR Authentication")
|
| 765 |
auth_url_output = gr.Textbox(label="Authorization URL", value=CALLBACK_MANAGER.get_auth_url(), interactive=False)
|
| 766 |
+
gr.Markdown("Copy the URL above, open it in a browser, log in, and paste the **entire redirected URL** from your browser's address bar below.") # Updated instructions
|
| 767 |
+
redirected_url_input = gr.Textbox(label="Redirected URL") # New textbox for redirected URL
|
| 768 |
+
extract_code_button = gr.Button("Extract Authorization Code") # Button to extract code
|
| 769 |
+
extracted_code_output = gr.Textbox(label="Extracted Authorization Code", interactive=False) # Textbox to show extracted code
|
| 770 |
+
|
| 771 |
+
auth_code_input = gr.Textbox(label="Authorization Code (from above, or paste manually if extraction fails)", interactive=True) # Updated label to be clearer
|
| 772 |
+
auth_submit = gr.Button("Submit Code for Authentication")
|
| 773 |
auth_result = gr.Textbox(label="Authentication Result")
|
| 774 |
|
| 775 |
patient_data_button = gr.Button("Fetch Patient Data")
|
|
|
|
| 780 |
meldrx_pdf_status = gr.Textbox(label="PDF Generation Status (No AI)") # Renamed status
|
| 781 |
meldrx_pdf_download = gr.File(label="Download Generated PDF (No AI)") # Renamed download
|
| 782 |
|
| 783 |
+
def process_redirected_url(redirected_url):
|
| 784 |
+
"""Processes the redirected URL to extract and display the authorization code."""
|
| 785 |
+
auth_code, error_message = extract_auth_code_from_url(redirected_url)
|
| 786 |
+
if auth_code:
|
| 787 |
+
return auth_code, "Authorization code extracted!"
|
| 788 |
+
else:
|
| 789 |
+
return "", error_message or "Could not extract authorization code."
|
| 790 |
+
|
| 791 |
+
|
| 792 |
+
extract_code_button.click(
|
| 793 |
+
fn=process_redirected_url,
|
| 794 |
+
inputs=redirected_url_input,
|
| 795 |
+
outputs=[extracted_code_output, auth_result] # Reusing auth_result for extraction status
|
| 796 |
+
)
|
| 797 |
+
|
| 798 |
+
|
| 799 |
+
auth_submit.click(
|
| 800 |
+
fn=CALLBACK_MANAGER.set_auth_code,
|
| 801 |
+
inputs=extracted_code_output, # Using extracted code as input for authentication
|
| 802 |
+
outputs=auth_result
|
| 803 |
+
)
|
| 804 |
+
|
| 805 |
|
| 806 |
with gr.Tab("Patient Dashboard"):
|
| 807 |
gr.Markdown("## Patient Data")
|