big changes to the application flow
Browse files- utils/oneclick.py +23 -6
utils/oneclick.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import logging
|
|
|
3 |
from typing import Optional, Dict, Any, Tuple
|
4 |
from huggingface_hub import InferenceClient
|
5 |
from utils.meldrx import MeldRxAPI
|
@@ -7,7 +8,11 @@ from utils.pdfutils import PDFGenerator
|
|
7 |
from utils.responseparser import PatientDataExtractor
|
8 |
from datetime import datetime
|
9 |
|
10 |
-
logging
|
|
|
|
|
|
|
|
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
@@ -17,6 +22,7 @@ client = InferenceClient(api_key=HF_TOKEN)
|
|
17 |
MODEL_NAME = "meta-llama/Llama-3.3-70B-Instruct"
|
18 |
|
19 |
def generate_ai_discharge_summary(patient_dict: Dict[str, str]) -> Optional[str]:
|
|
|
20 |
try:
|
21 |
patient_info = (
|
22 |
f"Patient Name: {patient_dict['first_name']} {patient_dict['last_name']}\n"
|
@@ -30,6 +36,8 @@ def generate_ai_discharge_summary(patient_dict: Dict[str, str]) -> Optional[str]
|
|
30 |
f"Discharge Instructions:\n[Generated based on available data]"
|
31 |
)
|
32 |
|
|
|
|
|
33 |
messages = [
|
34 |
{
|
35 |
"role": "assistant",
|
@@ -56,10 +64,11 @@ def generate_ai_discharge_summary(patient_dict: Dict[str, str]) -> Optional[str]
|
|
56 |
if content:
|
57 |
discharge_summary += content
|
58 |
|
|
|
59 |
return discharge_summary.strip()
|
60 |
|
61 |
except Exception as e:
|
62 |
-
logger.error(
|
63 |
return None
|
64 |
|
65 |
def generate_discharge_paper_one_click(
|
@@ -68,15 +77,21 @@ def generate_discharge_paper_one_click(
|
|
68 |
first_name: str = None,
|
69 |
last_name: str = None
|
70 |
) -> Tuple[Optional[str], str, Optional[str]]:
|
|
|
71 |
try:
|
72 |
if not meldrx_api.access_token:
|
73 |
if not meldrx_api.authenticate():
|
74 |
return None, "Error: Authentication failed. Please authenticate first.", None
|
75 |
|
|
|
76 |
patient_data = meldrx_api.get_patients()
|
77 |
-
if not patient_data
|
78 |
-
return None, "Error:
|
|
|
|
|
|
|
79 |
|
|
|
80 |
extractor = PatientDataExtractor(patient_data, format_type="json")
|
81 |
patients = extractor.get_all_patients()
|
82 |
|
@@ -104,6 +119,8 @@ def generate_discharge_paper_one_click(
|
|
104 |
else:
|
105 |
patient_dict = patients[0]
|
106 |
|
|
|
|
|
107 |
ai_content = generate_ai_discharge_summary(patient_dict)
|
108 |
if not ai_content:
|
109 |
return None, "Error: Failed to generate AI discharge summary.", None
|
@@ -132,5 +149,5 @@ def generate_discharge_paper_one_click(
|
|
132 |
return None, "Error: Failed to generate PDF.", display_summary
|
133 |
|
134 |
except Exception as e:
|
135 |
-
logger.error(
|
136 |
-
return None, f"Error: {str(e)}", None
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
+
import traceback
|
4 |
from typing import Optional, Dict, Any, Tuple
|
5 |
from huggingface_hub import InferenceClient
|
6 |
from utils.meldrx import MeldRxAPI
|
|
|
8 |
from utils.responseparser import PatientDataExtractor
|
9 |
from datetime import datetime
|
10 |
|
11 |
+
# Set up logging with detailed output
|
12 |
+
logging.basicConfig(
|
13 |
+
level=logging.INFO,
|
14 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
15 |
+
)
|
16 |
logger = logging.getLogger(__name__)
|
17 |
|
18 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
22 |
MODEL_NAME = "meta-llama/Llama-3.3-70B-Instruct"
|
23 |
|
24 |
def generate_ai_discharge_summary(patient_dict: Dict[str, str]) -> Optional[str]:
|
25 |
+
"""Generate a discharge summary using AI based on extracted patient data."""
|
26 |
try:
|
27 |
patient_info = (
|
28 |
f"Patient Name: {patient_dict['first_name']} {patient_dict['last_name']}\n"
|
|
|
36 |
f"Discharge Instructions:\n[Generated based on available data]"
|
37 |
)
|
38 |
|
39 |
+
logger.info("Generating AI discharge summary with patient info: %s", patient_info)
|
40 |
+
|
41 |
messages = [
|
42 |
{
|
43 |
"role": "assistant",
|
|
|
64 |
if content:
|
65 |
discharge_summary += content
|
66 |
|
67 |
+
logger.info("AI discharge summary generated successfully")
|
68 |
return discharge_summary.strip()
|
69 |
|
70 |
except Exception as e:
|
71 |
+
logger.error("Error generating AI discharge summary: %s\n%s", str(e), traceback.format_exc())
|
72 |
return None
|
73 |
|
74 |
def generate_discharge_paper_one_click(
|
|
|
77 |
first_name: str = None,
|
78 |
last_name: str = None
|
79 |
) -> Tuple[Optional[str], str, Optional[str]]:
|
80 |
+
"""Generate a discharge paper with AI content in one click."""
|
81 |
try:
|
82 |
if not meldrx_api.access_token:
|
83 |
if not meldrx_api.authenticate():
|
84 |
return None, "Error: Authentication failed. Please authenticate first.", None
|
85 |
|
86 |
+
logger.info("Fetching patient data from MeldRx API")
|
87 |
patient_data = meldrx_api.get_patients()
|
88 |
+
if not patient_data:
|
89 |
+
return None, "Error: No patient data returned from MeldRx API.", None
|
90 |
+
if "entry" not in patient_data:
|
91 |
+
logger.error("Invalid patient data format: %s", patient_data)
|
92 |
+
return None, "Error: Patient data is not in expected FHIR Bundle format.", None
|
93 |
|
94 |
+
logger.info("Extracting patient data")
|
95 |
extractor = PatientDataExtractor(patient_data, format_type="json")
|
96 |
patients = extractor.get_all_patients()
|
97 |
|
|
|
119 |
else:
|
120 |
patient_dict = patients[0]
|
121 |
|
122 |
+
logger.info("Selected patient: %s %s", patient_dict['first_name'], patient_dict['last_name'])
|
123 |
+
|
124 |
ai_content = generate_ai_discharge_summary(patient_dict)
|
125 |
if not ai_content:
|
126 |
return None, "Error: Failed to generate AI discharge summary.", None
|
|
|
149 |
return None, "Error: Failed to generate PDF.", display_summary
|
150 |
|
151 |
except Exception as e:
|
152 |
+
logger.error("Error in one-click discharge generation: %s\n%s", str(e), traceback.format_exc())
|
153 |
+
return None, f"Error: {str(e)}", None
|