MiChaelinzo commited on
Commit
53876d7
·
verified ·
1 Parent(s): 9bde543

Update callbackmanager.py

Browse files

It will use AI LLMA 3.1 need pk of it

Files changed (1) hide show
  1. callbackmanager.py +345 -36
callbackmanager.py CHANGED
@@ -6,6 +6,7 @@ import tempfile
6
  from datetime import datetime
7
  import traceback
8
  import logging
 
9
 
10
  # Set up logging
11
  logging.basicConfig(level=logging.INFO)
@@ -20,24 +21,331 @@ import hl7 # For HL7
20
  from xml.etree import ElementTree # For XML and CCDA
21
  from pypdf import PdfReader # For PDF
22
  import csv # For CSV
23
- # Assuming your AI analysis functions are in the same script or imported
24
- # For now, let's define placeholder AI analysis functions for Gradio context
25
- def analyze_dicom_file_with_ai(dicom_file):
26
- return "DICOM Analysis Report (Placeholder - Real AI integration needed)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- def analyze_hl7_file_with_ai(hl7_file):
29
- return "HL7 Analysis Report (Placeholder - Real AI integration needed)"
30
 
31
- def analyze_cda_xml_file_with_ai(cda_xml_file):
32
- return "CCDA/XML Analysis Report (Placeholder - Real AI integration needed)"
33
 
34
- def analyze_pdf_file_with_ai(pdf_file):
35
- return "PDF Analysis Report (Placeholder - Real AI integration needed)"
 
 
 
36
 
37
- def analyze_csv_file_with_ai(csv_file):
38
- return "CSV Analysis Report (Placeholder - Real AI integration needed)"
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
 
 
 
 
 
 
 
 
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  class CallbackManager:
42
  def __init__(self, redirect_uri: str, client_secret: str = None):
43
  client_id = os.getenv("APPID")
@@ -323,6 +631,7 @@ def generate_discharge_paper_one_click():
323
  return None, f"Error during discharge paper generation: {str(e)}"
324
 
325
 
 
326
  # Create a simplified interface to avoid complex component interactions
327
  CALLBACK_MANAGER = CallbackManager(
328
  redirect_uri="https://multitransformer-discharge-guard.hf.space/callback",
@@ -331,7 +640,7 @@ CALLBACK_MANAGER = CallbackManager(
331
 
332
  # Create the UI
333
  with gr.Blocks() as demo:
334
- gr.Markdown("# Patient Discharge Form with MeldRx & Medical File Analysis")
335
 
336
  with gr.Tab("Authenticate with MeldRx"):
337
  gr.Markdown("## SMART on FHIR Authentication")
@@ -345,9 +654,9 @@ with gr.Blocks() as demo:
345
  patient_data_output = gr.Textbox(label="Patient Data", lines=10)
346
 
347
  # Add button to generate PDF from MeldRx data
348
- meldrx_pdf_button = gr.Button("Generate PDF from MeldRx Data")
349
- meldrx_pdf_status = gr.Textbox(label="PDF Generation Status")
350
- meldrx_pdf_download = gr.File(label="Download Generated PDF")
351
 
352
  auth_submit.click(fn=CALLBACK_MANAGER.set_auth_code, inputs=auth_code_input, outputs=auth_result)
353
 
@@ -375,7 +684,7 @@ with gr.Blocks() as demo:
375
  if resource.get("resourceType") == "Patient":
376
  patients.append(resource)
377
 
378
- # Generate HTML for each patient
379
  html = "<h3>Patients</h3>"
380
  for patient in patients:
381
  # Extract name
@@ -451,11 +760,11 @@ with gr.Blocks() as demo:
451
  # Add buttons for both display form and generate PDF
452
  with gr.Row():
453
  submit_display = gr.Button("Display Form")
454
- submit_pdf = gr.Button("Generate PDF")
455
 
456
  # Output areas
457
  form_output = gr.Markdown()
458
- pdf_output = gr.File(label="Download PDF")
459
 
460
  # Connect the display form button
461
  submit_display.click(
@@ -470,7 +779,7 @@ with gr.Blocks() as demo:
470
  outputs=form_output
471
  )
472
 
473
- # Connect the generate PDF button
474
  submit_pdf.click(
475
  generate_pdf_from_form,
476
  inputs=[
@@ -507,38 +816,38 @@ with gr.Blocks() as demo:
507
  analyze_ccd_button = gr.Button("Analyze CCD with AI") # Redundant
508
 
509
 
510
- # Connect AI Analysis Buttons (using placeholder functions for now)
511
  analyze_dicom_button.click(
512
- lambda file: analyze_dicom_file_with_ai(file.name) if file else "No DICOM file uploaded",
513
  inputs=dicom_file, outputs=dicom_ai_output
514
  )
515
  analyze_hl7_button.click(
516
- lambda file: analyze_hl7_file_with_ai(file.name) if file else "No HL7 file uploaded",
517
  inputs=hl7_file, outputs=hl7_ai_output
518
  )
519
  analyze_xml_button.click(
520
- lambda file: analyze_cda_xml_file_with_ai(file.name) if file else "No XML file uploaded", # Using CCDA/XML analyzer for generic XML for now
521
  inputs=xml_file, outputs=xml_ai_output
522
  )
523
  analyze_ccda_button.click(
524
- lambda file: analyze_cda_xml_file_with_ai(file.name) if file else "No CCDA file uploaded", # Using CCDA/XML analyzer
525
  inputs=ccda_file, outputs=ccda_ai_output
526
  )
527
  analyze_ccd_button.click( # Redundant button, but kept for UI if needed
528
- lambda file: analyze_cda_xml_file_with_ai(file.name) if file else "No CCD file uploaded", # Using CCDA/XML analyzer
529
  inputs=ccd_file, outputs=ccd_ai_output
530
  )
531
 
532
- with gr.Tab("One-Click Discharge Paper"): # New Tab for One-Click Discharge Paper
533
- gr.Markdown("## One-Click Medical Discharge Paper Generation")
534
- one_click_pdf_button = gr.Button("Generate Discharge Paper (One-Click)")
535
- one_click_pdf_status = gr.Textbox(label="Discharge Paper Generation Status")
536
- one_click_pdf_download = gr.File(label="Download Discharge Paper")
537
 
538
- one_click_pdf_button.click(
539
- generate_discharge_paper_one_click,
540
  inputs=[],
541
- outputs=[one_click_pdf_download, one_click_pdf_status]
542
  )
543
 
544
 
@@ -556,9 +865,9 @@ with gr.Blocks() as demo:
556
  outputs=dashboard_output
557
  )
558
 
559
- # Add functionality for PDF generation from MeldRx data
560
  meldrx_pdf_button.click(
561
- fn=generate_pdf_from_meldrx,
562
  inputs=patient_data_output,
563
  outputs=[meldrx_pdf_download, meldrx_pdf_status]
564
  )
 
6
  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)
 
21
  from xml.etree import ElementTree # For XML and CCDA
22
  from pypdf import PdfReader # For PDF
23
  import csv # For CSV
24
+ import io # For IO operations
25
+ from PIL import Image # For image handling
26
+ import google.generativeai as genai # For Gemini AI
27
+
28
+ # Access Google API key from Streamlit secrets (replace with your method if not using streamlit secrets in Gradio)
29
+ GOOGLE_API_KEY = st.secrets["gemini_api"]["api_key"] # Assuming you have streamlit secrets configured in Gradio or replace with other method
30
+ genai.configure(api_key=GOOGLE_API_KEY)
31
+
32
+ # Generation Configuration (adjust as needed) - copy from your streamlit code
33
+ generation_config = {
34
+ "temperature": 0.7,
35
+ "top_p": 0.95,
36
+ "top_k": 64,
37
+ "max_output_tokens": 65536,
38
+ "response_mime_type": "text/plain",
39
+ }
40
+
41
+ # System Instruction - copy from your streamlit code
42
+ system_instructions = """
43
+ **Identity & Core Purpose**
44
+ ... (rest of your system instructions string from streamlit code) ...
45
+ Very Important: Use the API Directories fhir endpoints FROM THIS LINK: https://app.meldrx.com/api/directories/fhir/endpoints
46
+ """
47
+
48
+ # Define AI models - copy from your streamlit code
49
+ text_model = genai.GenerativeModel(
50
+ model_name="gemini-2.0-flash-thinking-exp-01-21",
51
+ generation_config=generation_config,
52
+ system_instruction=system_instructions, # Apply system instruction here
53
+ )
54
+ vision_model = genai.GenerativeModel( # Use gemini-pro-vision for image analysis
55
+ model_name="gemini-2.0-flash-thinking-exp-01-21", generation_config=generation_config
56
+ )
57
+
58
+
59
+ # Copy AI analysis functions from your streamlit code (analyze_dicom_file_with_ai, analyze_hl7_file_with_ai, etc.)
60
+ # Paste all your AI analysis functions here, for example:
61
+ def analyze_dicom_file_with_ai(dicom_file_path): # Modified to accept file path
62
+ """Analyzes DICOM file metadata using DocuNexus AI."""
63
+ try:
64
+ dicom_file = pydicom.dcmread(dicom_file_path) # Read dicom using path
65
+ dicom_metadata_json = dicom_file.to_json_dict()
66
+ prediction_response, trace_data_dicom_ai = analyze_dicom_content_ai(dicom_metadata_json) # Call content analysis
67
+ if prediction_response:
68
+ report = f"DocuNexus AI Analysis of DICOM Metadata:\n\nDICOM Metadata Analysis Report:\n{prediction_response}\n\nDisclaimer: The DocuNexus AGI-generated analysis is for conceptual informational purposes only and may or **NOT medical advice.** Analysis is based on DICOM *metadata* and not image interpretation."
69
+ return report
70
+ else:
71
+ error_message = f"AI Analysis from DICOM Metadata: No predictions generated or analysis encountered an issue."
72
+ if trace_data_dicom_ai and "error" in trace_data_dicom_ai:
73
+ error_message += f"\nAI Analysis Failed: {trace_data_dicom_ai['error']}"
74
+ return error_message
75
+
76
+ except Exception as e:
77
+ return f"Error during DICOM file processing in analyze_dicom_file_with_ai: {e}"
78
+
79
+ def analyze_dicom_content_ai(dicom_metadata_json): # Copied from your code
80
+ """Analyzes DICOM metadata JSON content using DocuNexus AI."""
81
+ prompt_text = f"""Perform a **deep and comprehensive analysis** of the following DICOM metadata in JSON format to provide a **structured summary and identify potential clinically relevant information with deep insights**. Focus not just on summarizing fields, but on **interpreting their clinical significance, identifying subtle patterns, and drawing inferences about the study's implications**. Think like an experienced radiologist reviewing this metadata for crucial diagnostic clues. Remember this is metadata, not the image itself, so focus on what can be gleaned from the data itself. Provide a "**Deep DICOM Metadata Analysis Report**". Important: Use the API Directories fhir endpoints FROM THIS LINK: https://app.meldrx.com/api/directories/fhir/endpoints.
82
+
83
+ **DICOM Metadata (JSON):**
84
+ ```json
85
+ {json.dumps(dicom_metadata_json, indent=2)}
86
+ ```
87
+
88
+ ... (rest of your prompt_text for DICOM metadata analysis) ...
89
+ * Remember, this deep analysis is for conceptual informational purposes only and **NOT medical advice.** Focus on deep summarization and structuring the extracted metadata in a highly clinically relevant way.
90
+ """
91
+
92
+ trace_data_detail_dicom_analysis = {
93
+ "prompt": "DICOM Metadata Analysis Request",
94
+ "language": "English",
95
+ "response_length": "Comprehensive",
96
+ "model_name": "DocuNexus v1.0",
97
+ "generated_text": "N/A",
98
+ "input_file_types": ["DICOM Metadata JSON"],
99
+ "mode": "DICOM Metadata Analysis",
100
+ "candidates": [],
101
+ "usage_metadata": {},
102
+ "prompt_feedback": "N/A",
103
+ }
104
+
105
+ try:
106
+ ai_response = text_model.generate_content(prompt_text) # Use text_model
107
+ the_response = ai_response.text
108
+ return the_response, trace_data_detail_dicom_analysis
109
+
110
+ except Exception as e:
111
+ error_message = f"AI Analysis Error in analyze_dicom_content_ai (DICOM Metadata): {e}"
112
+ trace_data_detail_dicom_analysis["error"] = f"AI Analysis Error: {e}"
113
+ return error_message, trace_data_detail_dicom_analysis
114
+
115
+ # ... (Paste other AI analysis functions: analyze_hl7_file_with_ai, analyze_cda_xml_file_with_ai, analyze_pdf_file_with_ai, analyze_csv_file_with_ai here - ensure to adapt file reading for Gradio file paths if necessary) ...
116
+ def analyze_hl7_file_with_ai(hl7_file_path):
117
+ """Analyzes HL7 file content using DocuNexus AI."""
118
+ try:
119
+ with open(hl7_file_path, 'r') as f: # Open file using path
120
+ hl7_message_raw = f.read()
121
+ prediction_response, trace_data_hl7_ai = analyze_hl7_content_ai(hl7_message_raw)
122
+
123
+ if prediction_response:
124
+ report = f"DocuNexus AI Analysis of HL7 Message:\n\nHL7 Message Analysis Report:\n{prediction_response}\n\n**Disclaimer:** The DocuNexus AGI-generated analysis is for conceptual informational purposes only and may or **NOT medical advice.** Analysis is based on HL7 message content."
125
+ return report
126
+ else:
127
+ error_message = f"AI Analysis from HL7 Message: No predictions generated or analysis encountered an issue."
128
+ if trace_data_hl7_ai and "error" in trace_data_hl7_ai:
129
+ error_message += f"AI Analysis Failed: {trace_data_hl7_ai['error']}"
130
+ return error_message
131
+
132
+ except Exception as e:
133
+ return f"Error during HL7 file processing in analyze_hl7_file_with_ai: {e}"
134
+
135
+ def analyze_hl7_content_ai(hl7_message_string): # Copied from your code
136
+ """Analyzes HL7 message content using DocuNexus AI."""
137
+ prompt_text = f"""Conduct a **deep and thorough analysis** of the following HL7 message content to provide a **structured summary and identify key clinical information with deep understanding**. Go beyond basic parsing; aim to **interpret the clinical narrative** embedded within the HL7 message. **Engage in deep search to contextualize medical codes and terminology**. Provide a "**Comprehensive HL7 Message Analysis Report**".
138
+
139
+ **HL7 Message Content:**
140
+ ```hl7
141
+ {hl7_message_string}
142
+ ```
143
+
144
+ ... (rest of your prompt_text for HL7 analysis) ...
145
+ * Remember, this deep analysis is for conceptual informational purposes only and **NOT medical advice.** Focus on deep summarization and structuring the extracted data in a highly clinically relevant way based on the HL7 content.
146
+ """
147
+ # ... (rest of the function code) ...
148
+ trace_data_detail_hl7_analysis = {
149
+ "prompt": "HL7 Message Analysis Request",
150
+ "language": "English",
151
+ "response_length": "Comprehensive",
152
+ "model_name": "DocuNexus v1.0",
153
+ "generated_text": "N/A",
154
+ "input_file_types": ["HL7 Message"],
155
+ "mode": "HL7 Message Analysis",
156
+ "candidates": [],
157
+ "usage_metadata": {},
158
+ "prompt_feedback": "N/A",
159
+ }
160
+
161
+ try:
162
+ ai_response = text_model.generate_content(prompt_text) # Use text model
163
+ the_response = ai_response.text
164
+ return the_response, trace_data_detail_hl7_analysis
165
+
166
+ except Exception as e:
167
+ error_message = f"AI Analysis Error in analyze_hl7_content_ai (HL7 Message): {e}"
168
+ trace_data_detail_hl7_analysis["error"] = f"AI Analysis Error: {e}"
169
+ return error_message, trace_data_detail_hl7_analysis
170
+
171
+
172
+ def analyze_cda_xml_file_with_ai(cda_xml_file_path): # Modified to accept file path
173
+ """Analyzes generic CDA or XML file content using DocuNexus AI (more generalized version) Important: Use the API Directories fhir endpoints FROM THIS LINK: https://app.meldrx.com/api/directories/fhir/endpoints."""
174
+ try:
175
+ with open(cda_xml_file_path, 'r') as f: # Open file using path
176
+ cda_xml_content = f.read()
177
+ prediction_response, trace_data_cda_xml_ai = analyze_cda_xml_content_ai(
178
+ cda_xml_content
179
+ )
180
+ if prediction_response:
181
+ report = f"DocuNexus AI Analysis of Medical XML/CDA Data:\n\nMedical Document Analysis Report:\n{prediction_response}\n\n**Disclaimer:** The DocuNexus AGI-generated analysis is for conceptual informational purposes only and may or **NOT medical advice.** Analysis is based on XML/CDA content."
182
+ return report
183
+ else:
184
+ error_message = f"AI Analysis from XML/CDA Data: No predictions generated or analysis encountered an issue."
185
+ if trace_data_cda_xml_ai and "error" in trace_data_cda_xml_ai:
186
+ error_message += f"AI Analysis Failed: {trace_data_cda_xml_ai['error']}"
187
+ return error_message
188
+
189
+ except Exception as e:
190
+ return f"Error during XML/CDA file processing in analyze_cda_xml_file_with_ai: {e}"
191
+
192
+ def analyze_cda_xml_content_ai(cda_xml_content): # Copied from your code
193
+ """Analyzes generic CDA or XML content using DocuNexus AI (more generalized version)."""
194
 
195
+ prompt_text = f"""Analyze the following medical XML/CDA content to provide a **structured and comprehensive patient data analysis**, similar to how a medical professional would review a patient's chart or a clinical document. You need to parse the XML structure yourself to extract the relevant information. Use bullet points, tables, or numbered steps for complex tasks. Provide a "Medical Document Analysis" report.
 
196
 
197
+ **Instructions for DocuNexus AI:**
 
198
 
199
+ 1. **Parse the XML content above.** Understand the XML structure to identify sections that are relevant to clinical information. For CDA specifically, look for sections like Problems, Medications, Allergies, Encounters, Results, and Vital Signs. For generic medical XML, adapt based on the tags present.
200
+ 2. **Extract and Summarize Key Medical Information:** Focus on extracting the following information if present in the XML:
201
+ * **Patient Demographics Summary:** (If available, summarize demographic details)
202
+ ... (rest of your prompt_text for CDA/XML analysis) ...
203
+ * Remember, this analysis is for conceptual informational purposes only and **NOT medical advice.** Focus on summarizing and structuring the extracted data in a clinically relevant way based on the XML/CDA content.
204
 
205
+ """
206
+
207
+ trace_data_detail_cda_xml_analysis = {
208
+ "prompt": "Generic CDA/XML Analysis Request",
209
+ "language": "English",
210
+ "response_length": "Comprehensive",
211
+ "model_name": "DocuNexus v1.0",
212
+ "generated_text": "N/A",
213
+ "input_file_types": ["CDA/XML"],
214
+ "mode": "Generic XML/CDA Analysis",
215
+ "candidates": [],
216
+ "usage_metadata": {},
217
+ "prompt_feedback": "N/A",
218
+ }
219
 
220
+ try:
221
+ ai_response = text_model.generate_content(prompt_text) # Use text model
222
+ the_response = ai_response.text
223
+ return the_response, trace_data_detail_cda_xml_analysis
224
+
225
+ except Exception as e:
226
+ error_message = f"AI Analysis Error in analyze_cda_xml_content_ai (Generic XML/CDA): {e}"
227
+ trace_data_detail_cda_xml_analysis["error"] = f"AI Analysis Error: {e}"
228
+ return error_message, trace_data_detail_cda_xml_analysis
229
 
230
+
231
+ def analyze_pdf_file_with_ai(pdf_file_path): # Modified to accept file path
232
+ """Analyzes PDF file content using DocuNexus AI."""
233
+ try:
234
+ with open(pdf_file_path, 'rb') as f: # Open file in binary mode for PdfReader
235
+ pdf_file = f # Pass file object to PdfReader
236
+ pdf_reader = PdfReader(pdf_file)
237
+ text_content = ""
238
+ for page in pdf_reader.pages:
239
+ text_content += page.extract_text()
240
+
241
+ prediction_response, trace_data_pdf_ai = analyze_pdf_content_ai(text_content)
242
+
243
+ if prediction_response:
244
+ report = f"DocuNexus AI Analysis of PDF Content:\n\nMedical Report Analysis Report:\n{prediction_response}\n\n**Disclaimer:** The DocuNexus AGI-generated analysis is for conceptual informational purposes only and may or **NOT medical advice.** Analysis is based on PDF text content."
245
+ return report
246
+ else:
247
+ error_message = f"AI Analysis from PDF Content: No predictions generated or analysis encountered an issue."
248
+ if trace_data_pdf_ai and "error" in trace_data_pdf_ai:
249
+ error_message += f"AI Analysis Failed: {trace_data_pdf_ai['error']}"
250
+ return error_message
251
+
252
+ except Exception as e:
253
+ return f"Error during PDF file processing in analyze_pdf_file_with_ai: {e}"
254
+
255
+ def analyze_pdf_content_ai(pdf_text_content): # Copied from your code
256
+ """Analyzes PDF text content using DocuNexus AI."""
257
+ prompt_text = f"""Analyze the following medical PDF text content to provide a **structured summary and identify key clinical information**. Focus on patient demographics, medical history, findings, diagnoses, medications, recommendations, and any important clinical details conveyed in the document. Provide a "Medical Report Analysis" report.
258
+
259
+ **Medical PDF Text Content:**
260
+ ```text
261
+ {pdf_text_content}
262
+ ```
263
+
264
+ ... (rest of your prompt_text for PDF analysis) ...
265
+ * Remember, this analysis is for conceptual informational purposes only and **NOT medical advice.** Focus on summarizing and structuring the extracted data in a clinically relevant way based on the PDF content.
266
+ """
267
+
268
+ trace_data_detail_pdf_analysis = {
269
+ "prompt": "PDF Text Analysis Request",
270
+ "language": "English",
271
+ "response_length": "Comprehensive",
272
+ "model_name": "DocuNexus v1.0",
273
+ "generated_text": "N/A",
274
+ "input_file_types": ["PDF Text"],
275
+ "mode": "PDF Text Analysis",
276
+ "candidates": [],
277
+ "usage_metadata": {},
278
+ "prompt_feedback": "N/A",
279
+ }
280
+
281
+ try:
282
+ ai_response = text_model.generate_content(prompt_text) # Use text model
283
+ the_response = ai_response.text
284
+ return the_response, trace_data_detail_pdf_analysis
285
+
286
+ except Exception as e:
287
+ error_message = f"AI Analysis Error in analyze_pdf_content_ai (PDF Text): {e}"
288
+ trace_data_detail_pdf_analysis["error"] = f"AI Analysis Error: {e}"
289
+ return error_message, trace_data_detail_pdf_analysis
290
+
291
+
292
+ def analyze_csv_file_with_ai(csv_file_path): # Modified to accept file path
293
+ """Analyzes CSV file content using DocuNexus AI."""
294
+ try:
295
+ with open(csv_file_path, 'r') as f: # Open file using path
296
+ csv_content = f.read()
297
+ prediction_response, trace_data_csv_ai = analyze_csv_content_ai(csv_content)
298
+
299
+ if prediction_response:
300
+ report = f"DocuNexus AI Analysis of CSV Data:\n\nData Analysis Report:\n{prediction_response}\n\n**Disclaimer:** The DocuNexus AGI-generated analysis is for conceptual informational purposes only and may or **NOT medical advice.** Analysis is based on CSV data content."
301
+ return report
302
+ else:
303
+ error_message = f"AI Analysis from CSV Data: No predictions generated or analysis encountered an issue."
304
+ if trace_data_csv_ai and "error" in trace_data_csv_ai:
305
+ error_message += f"AI Analysis Failed: {trace_data_csv_ai['error']}"
306
+ return error_message
307
+
308
+ except Exception as e:
309
+ return f"Error during CSV file processing in analyze_csv_file_with_ai: {e}"
310
+
311
+ def analyze_csv_content_ai(csv_content_string): # Copied from your code
312
+ """Analyzes CSV content (string) using DocuNexus AI."""
313
+ prompt_text = f"""Analyze the following medical CSV data to provide a **structured summary and identify potential clinical insights**. Assume the CSV represents patient-related medical data. Focus on understanding the columns, summarizing key data points, identifying trends or patterns, and noting any potential clinical significance of the data. Provide a "Data Analysis" report.
314
+
315
+ **Medical CSV Data:**
316
+ ```csv
317
+ {csv_content_string}
318
+ ```
319
+
320
+ ... (rest of your prompt_text for CSV analysis) ...
321
+ * Remember, this analysis is for conceptual informational purposes only and **NOT medical advice.** Focus on summarizing and structuring the data in a clinically relevant way based on the CSV content.
322
+ """
323
+
324
+ trace_data_detail_csv_analysis = {
325
+ "prompt": "CSV Data Analysis Request",
326
+ "language": "English",
327
+ "response_length": "Comprehensive",
328
+ "model_name": "DocuNexus v1.0",
329
+ "generated_text": "N/A",
330
+ "input_file_types": ["CSV Data"],
331
+ "mode": "CSV Data Analysis",
332
+ "candidates": [],
333
+ "usage_metadata": {},
334
+ "prompt_feedback": "N/A",
335
+ }
336
+
337
+ try:
338
+ ai_response = text_model.generate_content(prompt_text) # Use text model
339
+ the_response = ai_response.text
340
+ return the_response, trace_data_detail_csv_analysis
341
+
342
+ except Exception as e:
343
+ error_message = f"AI Analysis Error in analyze_csv_content_ai (CSV Data): {e}"
344
+ trace_data_detail_csv_analysis["error"] = f"AI Analysis Error: {e}"
345
+ return error_message, trace_data_detail_csv_analysis
346
+
347
+
348
+ # ... (CallbackManager, display_form, generate_pdf_from_form, generate_pdf_from_meldrx, generate_discharge_paper_one_click, client initialization remain the same) ...
349
  class CallbackManager:
350
  def __init__(self, redirect_uri: str, client_secret: str = None):
351
  client_id = os.getenv("APPID")
 
631
  return None, f"Error during discharge paper generation: {str(e)}"
632
 
633
 
634
+
635
  # Create a simplified interface to avoid complex component interactions
636
  CALLBACK_MANAGER = CallbackManager(
637
  redirect_uri="https://multitransformer-discharge-guard.hf.space/callback",
 
640
 
641
  # Create the UI
642
  with gr.Blocks() as demo:
643
+ gr.Markdown("# Patient Discharge Form with MeldRx & Medical File Analysis + AI") # Updated title
644
 
645
  with gr.Tab("Authenticate with MeldRx"):
646
  gr.Markdown("## SMART on FHIR Authentication")
 
654
  patient_data_output = gr.Textbox(label="Patient Data", lines=10)
655
 
656
  # Add button to generate PDF from MeldRx data
657
+ meldrx_pdf_button = gr.Button("Generate PDF from MeldRx Data (No AI)") # Renamed button
658
+ meldrx_pdf_status = gr.Textbox(label="PDF Generation Status (No AI)") # Renamed status
659
+ meldrx_pdf_download = gr.File(label="Download Generated PDF (No AI)") # Renamed download
660
 
661
  auth_submit.click(fn=CALLBACK_MANAGER.set_auth_code, inputs=auth_code_input, outputs=auth_result)
662
 
 
684
  if resource.get("resourceType") == "Patient":
685
  patients.append(resource)
686
 
687
+ # Generate HTML card
688
  html = "<h3>Patients</h3>"
689
  for patient in patients:
690
  # Extract name
 
760
  # Add buttons for both display form and generate PDF
761
  with gr.Row():
762
  submit_display = gr.Button("Display Form")
763
+ submit_pdf = gr.Button("Generate PDF (No AI)") # Renamed button to clarify no AI
764
 
765
  # Output areas
766
  form_output = gr.Markdown()
767
+ pdf_output = gr.File(label="Download PDF (No AI)") # Renamed output to clarify no AI
768
 
769
  # Connect the display form button
770
  submit_display.click(
 
779
  outputs=form_output
780
  )
781
 
782
+ # Connect the generate PDF button (No AI version)
783
  submit_pdf.click(
784
  generate_pdf_from_form,
785
  inputs=[
 
816
  analyze_ccd_button = gr.Button("Analyze CCD with AI") # Redundant
817
 
818
 
819
+ # Connect AI Analysis Buttons - using REAL AI functions now
820
  analyze_dicom_button.click(
821
+ analyze_dicom_file_with_ai, # Call REAL AI function
822
  inputs=dicom_file, outputs=dicom_ai_output
823
  )
824
  analyze_hl7_button.click(
825
+ analyze_hl7_file_with_ai, # Call REAL AI function
826
  inputs=hl7_file, outputs=hl7_ai_output
827
  )
828
  analyze_xml_button.click(
829
+ analyze_cda_xml_file_with_ai, # Call REAL AI function
830
  inputs=xml_file, outputs=xml_ai_output
831
  )
832
  analyze_ccda_button.click(
833
+ analyze_cda_xml_file_with_ai, # Call REAL AI function
834
  inputs=ccda_file, outputs=ccda_ai_output
835
  )
836
  analyze_ccd_button.click( # Redundant button, but kept for UI if needed
837
+ analyze_cda_xml_file_with_ai, # Call REAL AI function
838
  inputs=ccd_file, outputs=ccd_ai_output
839
  )
840
 
841
+ with gr.Tab("One-Click Discharge Paper (AI)"): # New Tab for One-Click Discharge Paper with AI
842
+ gr.Markdown("## One-Click Medical Discharge Paper Generation with AI Content") # Updated title
843
+ one_click_ai_pdf_button = gr.Button("Generate Discharge Paper with AI (One-Click)") # Updated button label
844
+ one_click_ai_pdf_status = gr.Textbox(label="Discharge Paper Generation Status (AI)") # Updated status label
845
+ one_click_ai_pdf_download = gr.File(label="Download Discharge Paper (AI)") # Updated download label
846
 
847
+ one_click_ai_pdf_button.click(
848
+ generate_discharge_paper_one_click, # Use the same one-click function, which now calls AI
849
  inputs=[],
850
+ outputs=[one_click_ai_pdf_download, one_click_ai_pdf_status]
851
  )
852
 
853
 
 
865
  outputs=dashboard_output
866
  )
867
 
868
+ # Connect meldrx_pdf_button (No AI PDF)
869
  meldrx_pdf_button.click(
870
+ fn=generate_pdf_from_meldrx_no_ai, # Assuming you have a no-ai version of generate_pdf_from_meldrx or adjust as needed
871
  inputs=patient_data_output,
872
  outputs=[meldrx_pdf_download, meldrx_pdf_status]
873
  )