File size: 15,695 Bytes
bdb09c8
 
 
b296b05
 
b9dbc6c
bdb09c8
9059159
 
 
 
 
 
 
b9dbc6c
bdb09c8
b296b05
 
b9dbc6c
 
b296b05
b9dbc6c
bdb09c8
b9dbc6c
 
 
 
9059159
 
97685a7
b296b05
b9dbc6c
 
b296b05
b9dbc6c
97685a7
 
 
b9dbc6c
 
 
97685a7
b296b05
9059159
 
b9dbc6c
97685a7
b9dbc6c
bdb09c8
9059159
 
b296b05
bdb09c8
 
b9dbc6c
b296b05
b9dbc6c
97685a7
 
 
c6163b1
97685a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6163b1
97685a7
 
 
 
 
 
 
 
b296b05
 
 
b9dbc6c
97685a7
b296b05
b9dbc6c
97685a7
b296b05
 
b9dbc6c
b296b05
 
97685a7
 
 
 
 
c6163b1
b296b05
b9dbc6c
b296b05
b9dbc6c
 
b296b05
b9dbc6c
 
97685a7
b9dbc6c
97685a7
b9dbc6c
 
97685a7
b9dbc6c
 
97685a7
b9dbc6c
97685a7
b9dbc6c
bdb09c8
97685a7
 
 
 
b296b05
 
 
97685a7
b296b05
97685a7
 
 
 
 
b296b05
 
9059159
 
 
 
b296b05
 
 
 
 
 
97685a7
 
b296b05
 
97685a7
b296b05
 
 
 
 
 
 
 
 
 
 
 
 
97685a7
b296b05
b9dbc6c
 
 
 
97685a7
b9dbc6c
97685a7
 
 
 
 
b9dbc6c
bdb09c8
9059159
 
 
 
b9dbc6c
 
 
bdb09c8
 
9059159
b9dbc6c
97685a7
 
 
b9dbc6c
 
 
 
97685a7
 
 
 
 
 
 
 
 
b9dbc6c
97685a7
 
b9dbc6c
97685a7
b296b05
97685a7
 
 
 
b9dbc6c
b296b05
 
 
 
 
97685a7
 
 
 
 
 
 
 
b296b05
97685a7
 
 
 
b296b05
97685a7
b296b05
97685a7
 
 
 
 
 
 
b296b05
97685a7
 
 
b296b05
97685a7
b296b05
 
 
 
97685a7
 
b296b05
 
97685a7
 
 
 
9059159
97685a7
 
 
 
 
 
 
 
 
 
9059159
 
 
 
 
 
 
 
97685a7
 
 
 
9059159
 
 
 
 
 
97685a7
b296b05
 
97685a7
 
 
 
 
 
 
 
 
 
b296b05
97685a7
 
 
bdb09c8
9059159
bdb09c8
9059159
 
 
bdb09c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import streamlit as st
import google.generativeai as genai
import os
from PIL import Image
import io # Needed for handling image bytes
from typing import Optional, Tuple, Any # For type hinting

# --- Page Configuration (MUST BE THE FIRST STREAMLIT COMMAND) ---
st.set_page_config(
    page_title="AI Clinical Support Demonstrator",
    layout="wide",
    initial_sidebar_state="expanded"
)

# --- Configuration and Initialization ---

# Securely load API key
# Prioritize Streamlit secrets, fall back to environment variable for flexibility
GEMINI_API_KEY = st.secrets.get("GEMINI_API_KEY", os.environ.get("GEMINI_API_KEY"))

# Configure Gemini Client (only if key is found)
genai_client_configured = False
if GEMINI_API_KEY:
    try:
        genai.configure(api_key=GEMINI_API_KEY)
        genai_client_configured = True
    except Exception as e:
        # Use st.exception() for a more detailed error in the app if needed during debugging
        # st.exception(e)
        st.error(f"Fatal Error: Failed to configure Google Generative AI. Check API Key. Details: {e}")
        st.stop() # Stop execution if configuration fails
else:
    st.error("โš ๏ธ Gemini API Key not found. Please configure `GEMINI_API_KEY` in Streamlit secrets or environment variables.")
    st.stop() # Stop execution if no API key

# Initialize models (Consider more powerful model for agentic reasoning if needed)
TEXT_MODEL_NAME = 'gemini-1.5-pro-latest' # Model for agentic text reasoning
VISION_MODEL_NAME = 'gemini-1.5-flash' # Model for image analysis

if genai_client_configured:
    try:
        text_model = genai.GenerativeModel(TEXT_MODEL_NAME)
        vision_model = genai.GenerativeModel(VISION_MODEL_NAME)
        # Optional: Display success message (won't violate the rule as set_page_config was first)
        # st.sidebar.success(f"Models Initialized ({TEXT_MODEL_NAME}, {VISION_MODEL_NAME})", icon="โœ…")
    except Exception as e:
        st.error(f"Fatal Error: Failed to initialize Gemini models. Text: {TEXT_MODEL_NAME}, Vision: {VISION_MODEL_NAME}. Details: {e}")
        st.stop()
else:
    # This state should not be reached due to earlier st.stop() calls,
    # but included for completeness.
    st.error("AI Models could not be initialized due to configuration issues.")
    st.stop()


# --- Core AI Interaction Functions ---

# AGENTIC prompt for Text Analysis
AGENTIC_TEXT_ANALYSIS_PROMPT_TEMPLATE = """
**Simulated Clinical Reasoning Agent Task:**

**Role:** AI assistant simulating an agentic clinical reasoning process to support a healthcare professional by structuring information, generating possibilities, and suggesting investigation pathways based *strictly* on the provided text. **This is NOT a diagnosis.**

**Input Data:** Unstructured clinical information (e.g., symptoms, history, basic findings).

**Simulated Agentic Steps (Perform sequentially):**

1.  **Information Extraction & Structuring:**
    *   Key Demographics (Age, Sex if provided).
    *   Primary Symptoms/Signs.
    *   Relevant Medical History.
    *   Pertinent Negatives (if mentioned).

2.  **Differential Considerations Generation:**
    *   Based *only* on Step 1, list **potential differential considerations** (possible conditions).
    *   **Use cautious language:** "could be consistent with," "warrants consideration," "less likely but possible." **AVOID definitive statements.**
    *   Briefly justify each consideration based on findings.

3.  **Information Gap Analysis:**
    *   Identify critical missing information (e.g., lab results, imaging, exam specifics, duration/onset).

4.  **Suggested Next Steps for Investigation (for Clinician):**
    *   Propose logical next steps a **healthcare professional might consider**.
    *   Categorize (e.g., Further History, Exam Points, Labs, Imaging).
    *   Frame as *suggestions* (e.g., "Consider ordering...", "Assessment of X may be informative").

5.  **Mandatory Disclaimer:** Conclude with: "This AI-generated analysis is for informational support only. It is **NOT** a diagnosis and cannot replace the judgment of a qualified healthcare professional."

**Input Clinical Information:**
---
{text_input}
---

**Agentic Analysis:**
"""

# Standard prompt for Image Analysis
IMAGE_ANALYSIS_PROMPT_TEMPLATE = """
**Medical Image Analysis Request:**

**Context:** Analyze the provided medical image. User may provide additional context or questions.
**Task:**
1.  **Describe Visible Structures:** Briefly describe main anatomical structures.
2.  **Identify Potential Anomalies:** Point out areas that *appear* abnormal or deviate from typical presentation (e.g., "potential opacity," "altered signal intensity," "possible asymmetry"). Use cautious, descriptive language.
3.  **Correlate with User Prompt (if provided):** Address specific user questions based *only* on visual information.
4.  **Limitations:** State that image quality, view, and lack of clinical context limit analysis.
5.  **Disclaimer:** Explicitly state this is AI visual analysis, not radiological interpretation or diagnosis, requiring review by a qualified professional with clinical context.

**User's Additional Context/Question (if any):**
---
{user_prompt}
---

**Image Analysis:**
"""

def run_agentic_text_analysis(text_input: str) -> Tuple[Optional[str], Optional[str]]:
    """
    Sends clinical text to the Gemini text model for simulated agentic analysis.

    Args:
        text_input: The clinical text provided by the user.

    Returns:
        Tuple: (analysis_text, error_message)
    """
    if not text_input or not text_input.strip():
        return None, "Input text cannot be empty."
    try:
        prompt = AGENTIC_TEXT_ANALYSIS_PROMPT_TEMPLATE.format(text_input=text_input)
        # Using the designated text model
        response = text_model.generate_content(prompt)

        if response.parts:
            return response.text, None
        elif response.prompt_feedback.block_reason:
             return None, f"Analysis blocked by safety filters: {response.prompt_feedback.block_reason.name}. Review input."
        else:
             candidate = response.candidates[0] if response.candidates else None
             if candidate and candidate.finish_reason != "STOP":
                 return None, f"Analysis stopped prematurely. Reason: {candidate.finish_reason.name}."
             else:
                 return None, "Received an empty or unexpected response from the AI model."

    except Exception as e:
        # Log error for debugging purposes server-side if possible
        print(f"ERROR in run_agentic_text_analysis: {e}") # Simple print for demo
        st.error(f"Error during agentic text analysis. Check logs if possible.", icon="๐Ÿšจ") # Keep UI error generic
        return None, f"An internal error occurred during text analysis." # Generic message to user

def analyze_medical_image(image_file: Any, user_prompt: str = "") -> Tuple[Optional[str], Optional[str]]:
    """
    Sends a medical image (and optional prompt) to the Gemini Vision model for analysis.

    Args:
        image_file: Uploaded image file object from Streamlit.
        user_prompt: Optional text context/questions from the user.

    Returns:
        Tuple: (analysis_text, error_message)
    """
    if not image_file:
        return None, "Image file cannot be empty."
    try:
        try:
            image = Image.open(image_file)
            if image.mode != 'RGB':
                 image = image.convert('RGB')
        except Exception as img_e:
             return None, f"Error opening or processing image file: {img_e}"

        prompt_text = IMAGE_ANALYSIS_PROMPT_TEMPLATE.format(user_prompt=user_prompt if user_prompt else "N/A")
        model_input = [prompt_text, image]
        # Using the designated vision model
        response = vision_model.generate_content(model_input)

        if response.parts:
            return response.text, None
        elif response.prompt_feedback.block_reason:
            return None, f"Image analysis blocked by safety filters: {response.prompt_feedback.block_reason.name}. May relate to sensitive content policies."
        else:
             candidate = response.candidates[0] if response.candidates else None
             if candidate and candidate.finish_reason != "STOP":
                 return None, f"Image analysis stopped prematurely. Reason: {candidate.finish_reason.name}."
             else:
                 return None, "Received an empty or unexpected response from the AI model for image analysis."

    except Exception as e:
        # Log error for debugging purposes server-side if possible
        print(f"ERROR in analyze_medical_image: {e}") # Simple print for demo
        st.error(f"Error during image analysis. Check logs if possible.", icon="๐Ÿ–ผ๏ธ") # Keep UI error generic
        return None, f"An internal error occurred during image analysis." # Generic message to user


# --- Streamlit User Interface ---

def main():
    # REMOVED st.set_page_config() FROM HERE

    st.title("๐Ÿค– AI Clinical Support Demonstrator")
    st.caption(f"Agentic Text Analysis ({TEXT_MODEL_NAME}) & Image Analysis ({VISION_MODEL_NAME})")
    st.markdown("---")

    # --- CRITICAL DISCLAIMER ---
    st.warning(
        """
        **๐Ÿ”ด IMPORTANT SAFETY & USE DISCLAIMER ๐Ÿ”ด**
        *   This tool **DEMONSTRATES** AI capabilities. It **DOES NOT** provide medical advice or diagnosis.
        *   **Agentic Text Analysis:** Simulates reasoning on text input. Output is illustrative, not diagnostic.
        *   **Image Analysis:** Provides observations on images. Output is **NOT** a radiological interpretation.
        *   AI analysis lacks full clinical context, may be inaccurate, and **CANNOT** replace professional judgment.
        *   **ALWAYS consult qualified healthcare professionals** for diagnosis and treatment.
        *   **PRIVACY:** Do **NOT** upload identifiable patient information (PHI) without explicit consent and adherence to all privacy laws (e.g., HIPAA).
        """,
        icon="โš ๏ธ"
    )
    st.markdown("---")


    st.sidebar.header("Analysis Options")
    input_method = st.sidebar.radio(
        "Select Analysis Type:",
        ("Agentic Text Analysis", "Medical Image Analysis"),
        key="input_method_radio",
        help="Choose 'Agentic Text Analysis' for reasoning simulation on clinical text, or 'Medical Image Analysis' for visual observations on images."
    )
    st.sidebar.markdown("---") # Visual separator

    col1, col2 = st.columns(2)

    with col1:
        st.header("Input Data")
        analysis_result = None # Initialize results variables
        error_message = None
        output_header = "Analysis Results" # Default header for the output column

        # --- Agentic Text Analysis Input ---
        if input_method == "Agentic Text Analysis":
            st.subheader("Clinical Text for Agentic Analysis")
            text_input = st.text_area(
                "Paste de-identified clinical information (symptoms, history, findings):",
                height=350, # Slightly larger text area
                placeholder="Example: 68yo male, sudden SOB & pleuritic chest pain post-flight. HR 110, SpO2 92% RA. No known cardiac hx...",
                key="text_input_area"
            )
            analyze_button = st.button("โ–ถ๏ธ Run Agentic Text Analysis", key="analyze_text_button", type="primary")

            if analyze_button:
                if text_input:
                    with st.spinner("๐Ÿง  Simulating agentic reasoning..."):
                        analysis_result, error_message = run_agentic_text_analysis(text_input)
                    output_header = "Simulated Agentic Analysis Output"
                else:
                    st.warning("Please enter clinical text to analyze.", icon="โ˜๏ธ")

        # --- Medical Image Analysis Input ---
        elif input_method == "Medical Image Analysis":
            st.subheader("Medical Image for Analysis")
            image_file = st.file_uploader(
                "Upload a de-identified medical image (e.g., X-ray, CT slice). Supported: PNG, JPG, JPEG.",
                type=["png", "jpg", "jpeg"],
                key="image_uploader"
            )
            user_image_prompt = st.text_input(
                "Optional: Add context or specific question for image analysis:",
                placeholder="Example: 'Describe findings in the lung fields' or 'Any visible fractures?'",
                key="image_prompt_input"
            )
            analyze_button = st.button("๐Ÿ–ผ๏ธ Analyze Medical Image", key="analyze_image_button", type="primary")

            if analyze_button:
                if image_file:
                    # Using st.image within the column context
                    st.image(image_file, caption="Uploaded Image Preview", use_column_width=True)
                    with st.spinner("๐Ÿ‘๏ธ Analyzing image..."):
                        analysis_result, error_message = analyze_medical_image(image_file, user_image_prompt)
                    output_header = "Medical Image Analysis Output"
                else:
                    st.warning("Please upload an image file to analyze.", icon="โ˜๏ธ")

    # --- Output Column ---
    with col2:
        st.header(output_header)
        # Determine if analysis was attempted in this run
        # Use session state keys which are set when buttons are clicked
        button_pressed = st.session_state.get('analyze_text_button', False) or \
                         st.session_state.get('analyze_image_button', False)

        if button_pressed:
            # Check if the button was clicked AND we have results or an error from that action
            # analysis_result and error_message are set within the button's if block in col1
            if error_message:
                 st.error(f"Analysis Failed: {error_message}", icon="โŒ")
            elif analysis_result:
                 st.markdown(analysis_result) # Display the successful result
            # If button was pressed but neither error nor result is set, maybe input was invalid
            # The warnings for empty input are handled in col1, so this case might not be needed
            # else:
            #    st.info("Provide valid input and click Analyze.") # Fallback?

        else: # No button pressed in this run
            st.info("Analysis results will appear here after providing input and clicking the corresponding analysis button.")


    # --- Sidebar Explanations ---
    st.sidebar.markdown("---")
    st.sidebar.header("About The Prompts")
    with st.sidebar.expander("View Agentic Text Prompt Structure"):
        st.markdown(f"```plaintext\n{AGENTIC_TEXT_ANALYSIS_PROMPT_TEMPLATE.split('---')[0]} ... [Input Text] ...\n```")
        st.caption("Guides the AI through structured reasoning steps for text.")
    with st.sidebar.expander("View Image Analysis Prompt Structure"):
        st.markdown(f"```plaintext\n{IMAGE_ANALYSIS_PROMPT_TEMPLATE.split('---')[0]} ... [User Prompt] ...\n```")
        st.caption("Guides the AI to describe visual features and potential anomalies in images.")

    st.sidebar.markdown("---")
    st.sidebar.error(
        "**Ethical Use Reminder:** AI in medicine requires extreme caution. This tool is for demonstration and education, not clinical practice. Verify all information and rely on professional expertise."
        )

# --- Main Execution Guard ---
if __name__ == "__main__":
    # Ensure configuration happens before main UI logic runs
    # NOTE: The config/init code now runs automatically when the script is imported/run,
    # BEFORE the main() function is called here. This is correct.
    main()