add callback manager , model app
Browse files- callbackmanager.py +11 -28
- meldrx.py +4 -8
callbackmanager.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from meldrx import MeldRxAPI
|
3 |
import json
|
@@ -7,10 +8,10 @@ class CallbackManager:
|
|
7 |
def __init__(self, redirect_uri: str, client_secret: str = None):
|
8 |
client_id = os.getenv("APPID")
|
9 |
if not client_id:
|
10 |
-
raise ValueError("APPID environment variable not set.
|
11 |
workspace_id = os.getenv("WORKSPACE_URL")
|
12 |
if not workspace_id:
|
13 |
-
raise ValueError("WORKSPACE_URL environment variable not set.
|
14 |
self.api = MeldRxAPI(client_id, client_secret, workspace_id, redirect_uri)
|
15 |
self.auth_code = None
|
16 |
self.access_token = None
|
@@ -42,45 +43,27 @@ def display_form(
|
|
42 |
):
|
43 |
form = f"""
|
44 |
**Patient Discharge Form**
|
45 |
-
|
46 |
-
**Patient Details:**
|
47 |
- Name: {first_name} {middle_initial} {last_name}
|
48 |
- Date of Birth: {dob}, Age: {age}, Sex: {sex}
|
49 |
- Address: {address}, {city}, {state}, {zip_code}
|
50 |
-
|
51 |
-
**Primary Healthcare Professional Details:**
|
52 |
-
- Name: {doctor_first_name} {doctor_middle_initial} {doctor_last_name}
|
53 |
- Hospital/Clinic: {hospital_name}
|
54 |
-
- Address: {doctor_address}, {doctor_city}, {doctor_state}, {doctor_zip}
|
55 |
-
|
56 |
-
|
57 |
-
- Date of
|
58 |
-
- Source of Referral: {referral_source}
|
59 |
-
- Method of Admission: {admission_method}
|
60 |
-
- Date of Discharge: {discharge_date}
|
61 |
-
- Discharge Reason: {discharge_reason}
|
62 |
-
- Date of Death (if applicable): {date_of_death}
|
63 |
-
|
64 |
-
**Diagnosis & Procedures:**
|
65 |
- Diagnosis: {diagnosis}
|
66 |
- Procedures: {procedures}
|
67 |
-
|
68 |
-
|
69 |
-
- Medications on Discharge: {medications}
|
70 |
-
|
71 |
-
**Prepared By:**
|
72 |
-
- Name: {preparer_name}, Job Title: {preparer_job_title}
|
73 |
"""
|
74 |
return form
|
75 |
|
76 |
-
# Initialize CallbackManager
|
77 |
CALLBACK_MANAGER = CallbackManager(
|
78 |
redirect_uri="https://multitransformer-discharge-guard.hf.space/callback",
|
79 |
-
client_secret=None
|
80 |
)
|
81 |
|
82 |
-
|
83 |
-
|
84 |
with gr.Blocks() as demo:
|
85 |
gr.Markdown("# Patient Discharge Form with MeldRx Integration")
|
86 |
with gr.Tab("Authenticate with MeldRx"):
|
|
|
1 |
+
|
2 |
import gradio as gr
|
3 |
from meldrx import MeldRxAPI
|
4 |
import json
|
|
|
8 |
def __init__(self, redirect_uri: str, client_secret: str = None):
|
9 |
client_id = os.getenv("APPID")
|
10 |
if not client_id:
|
11 |
+
raise ValueError("APPID environment variable not set.")
|
12 |
workspace_id = os.getenv("WORKSPACE_URL")
|
13 |
if not workspace_id:
|
14 |
+
raise ValueError("WORKSPACE_URL environment variable not set.")
|
15 |
self.api = MeldRxAPI(client_id, client_secret, workspace_id, redirect_uri)
|
16 |
self.auth_code = None
|
17 |
self.access_token = None
|
|
|
43 |
):
|
44 |
form = f"""
|
45 |
**Patient Discharge Form**
|
|
|
|
|
46 |
- Name: {first_name} {middle_initial} {last_name}
|
47 |
- Date of Birth: {dob}, Age: {age}, Sex: {sex}
|
48 |
- Address: {address}, {city}, {state}, {zip_code}
|
49 |
+
- Doctor: {doctor_first_name} {doctor_middle_initial} {doctor_last_name}
|
|
|
|
|
50 |
- Hospital/Clinic: {hospital_name}
|
51 |
+
- Doctor Address: {doctor_address}, {doctor_city}, {doctor_state}, {doctor_zip}
|
52 |
+
- Admission Date: {admission_date}, Source: {referral_source}, Method: {admission_method}
|
53 |
+
- Discharge Date: {discharge_date}, Reason: {discharge_reason}
|
54 |
+
- Date of Death: {date_of_death}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
- Diagnosis: {diagnosis}
|
56 |
- Procedures: {procedures}
|
57 |
+
- Medications: {medications}
|
58 |
+
- Prepared By: {preparer_name}, {preparer_job_title}
|
|
|
|
|
|
|
|
|
59 |
"""
|
60 |
return form
|
61 |
|
|
|
62 |
CALLBACK_MANAGER = CallbackManager(
|
63 |
redirect_uri="https://multitransformer-discharge-guard.hf.space/callback",
|
64 |
+
client_secret=None
|
65 |
)
|
66 |
|
|
|
|
|
67 |
with gr.Blocks() as demo:
|
68 |
gr.Markdown("# Patient Discharge Form with MeldRx Integration")
|
69 |
with gr.Tab("Authenticate with MeldRx"):
|
meldrx.py
CHANGED
@@ -18,16 +18,14 @@ class MeldRxAPI:
|
|
18 |
self.workspace_id = workspace_id
|
19 |
self.redirect_uri = redirect_uri
|
20 |
self.access_token = None
|
21 |
-
self.code_verifier = None
|
22 |
self.session = requests.Session()
|
23 |
|
24 |
def _generate_code_verifier(self) -> str:
|
25 |
-
|
26 |
-
self.code_verifier = secrets.token_urlsafe(32) # Generates a 43-character safe string
|
27 |
return self.code_verifier
|
28 |
|
29 |
def _generate_code_challenge(self, code_verifier: str) -> str:
|
30 |
-
"""Generate a code_challenge from the code_verifier using SHA-256."""
|
31 |
sha256_hash = hashlib.sha256(code_verifier.encode('utf-8')).digest()
|
32 |
code_challenge = base64.urlsafe_b64encode(sha256_hash).decode('utf-8').rstrip('=')
|
33 |
return code_challenge
|
@@ -74,7 +72,6 @@ class MeldRxAPI:
|
|
74 |
return None
|
75 |
|
76 |
def get_authorization_url(self, scope: str = "patient/*.read openid profile", state: str = "random_state") -> str:
|
77 |
-
"""Generate the SMART on FHIR authorization URL with PKCE."""
|
78 |
code_verifier = self._generate_code_verifier()
|
79 |
code_challenge = self._generate_code_challenge(code_verifier)
|
80 |
params = {
|
@@ -85,13 +82,12 @@ class MeldRxAPI:
|
|
85 |
"state": state,
|
86 |
"aud": self.fhir_base_url,
|
87 |
"code_challenge": code_challenge,
|
88 |
-
"code_challenge_method": "S256"
|
89 |
}
|
90 |
query_string = "&".join(f"{k}={v}" for k, v in params.items())
|
91 |
return f"{self.authorize_url}?{query_string}"
|
92 |
|
93 |
def authenticate_with_code(self, auth_code: str) -> bool:
|
94 |
-
"""Exchange an authorization code for an access token, including the code_verifier."""
|
95 |
if not self.code_verifier:
|
96 |
print("Code verifier not set. Generate an authorization URL first.")
|
97 |
return False
|
@@ -100,7 +96,7 @@ class MeldRxAPI:
|
|
100 |
"code": auth_code,
|
101 |
"redirect_uri": self.redirect_uri,
|
102 |
"client_id": self.client_id,
|
103 |
-
"code_verifier": self.code_verifier
|
104 |
}
|
105 |
if self.client_secret:
|
106 |
payload["client_secret"] = self.client_secret
|
|
|
18 |
self.workspace_id = workspace_id
|
19 |
self.redirect_uri = redirect_uri
|
20 |
self.access_token = None
|
21 |
+
self.code_verifier = None
|
22 |
self.session = requests.Session()
|
23 |
|
24 |
def _generate_code_verifier(self) -> str:
|
25 |
+
self.code_verifier = secrets.token_urlsafe(32) # 43 characters, per PKCE spec
|
|
|
26 |
return self.code_verifier
|
27 |
|
28 |
def _generate_code_challenge(self, code_verifier: str) -> str:
|
|
|
29 |
sha256_hash = hashlib.sha256(code_verifier.encode('utf-8')).digest()
|
30 |
code_challenge = base64.urlsafe_b64encode(sha256_hash).decode('utf-8').rstrip('=')
|
31 |
return code_challenge
|
|
|
72 |
return None
|
73 |
|
74 |
def get_authorization_url(self, scope: str = "patient/*.read openid profile", state: str = "random_state") -> str:
|
|
|
75 |
code_verifier = self._generate_code_verifier()
|
76 |
code_challenge = self._generate_code_challenge(code_verifier)
|
77 |
params = {
|
|
|
82 |
"state": state,
|
83 |
"aud": self.fhir_base_url,
|
84 |
"code_challenge": code_challenge,
|
85 |
+
"code_challenge_method": "S256"
|
86 |
}
|
87 |
query_string = "&".join(f"{k}={v}" for k, v in params.items())
|
88 |
return f"{self.authorize_url}?{query_string}"
|
89 |
|
90 |
def authenticate_with_code(self, auth_code: str) -> bool:
|
|
|
91 |
if not self.code_verifier:
|
92 |
print("Code verifier not set. Generate an authorization URL first.")
|
93 |
return False
|
|
|
96 |
"code": auth_code,
|
97 |
"redirect_uri": self.redirect_uri,
|
98 |
"client_id": self.client_id,
|
99 |
+
"code_verifier": self.code_verifier
|
100 |
}
|
101 |
if self.client_secret:
|
102 |
payload["client_secret"] = self.client_secret
|