File size: 21,349 Bytes
814a594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c20836
814a594
 
 
 
 
 
b816c7b
814a594
 
 
 
 
 
 
 
 
 
b816c7b
814a594
e7656ba
 
 
 
 
814a594
0856b31
 
 
 
e4f9e09
0856b31
 
 
 
 
 
91dda23
c13f20c
9dfb8d0
 
 
 
0856b31
91dda23
814a594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ed08c7
 
 
 
 
 
 
814a594
9ed08c7
 
 
 
 
 
 
814a594
 
 
 
 
 
 
 
 
 
 
 
f0eb205
 
 
 
 
 
 
 
 
 
 
 
 
e13af0d
 
f0eb205
7d2f14f
 
 
 
 
 
 
 
 
 
77c4859
 
7d2f14f
5963fb2
7d2f14f
 
 
 
 
 
67ab029
 
51fb737
7d2f14f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
814a594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c20836
d28e001
ca50e59
ed8aeee
 
 
 
 
d28e001
 
 
 
 
ed8aeee
 
d28e001
ca50e59
d28e001
 
 
 
 
ca50e59
d28e001
 
 
 
814a594
 
0c20836
814a594
f0eb205
 
 
 
 
814a594
 
 
 
b816c7b
596cc4a
 
523cd54
b816c7b
596cc4a
b816c7b
 
4d2300f
 
 
 
51fb737
2ffc244
596cc4a
b816c7b
e13af0d
523cd54
 
 
 
 
 
 
 
 
 
 
 
 
e13af0d
 
 
7d2f14f
2ffc244
523cd54
7d2f14f
b816c7b
bdb6d79
 
 
0c20836
bdb6d79
 
 
 
 
 
 
ccd6eeb
bdb6d79
 
 
 
 
 
523cd54
bdb6d79
 
 
 
 
523cd54
bdb6d79
 
 
 
 
 
 
 
 
0c20836
bdb6d79
 
523cd54
7d2f14f
 
 
ccd6eeb
bdb6d79
ccd6eeb
67ab029
ccd6eeb
 
7d2f14f
 
 
 
 
 
 
 
 
7978786
 
bdb6d79
 
 
 
 
 
4fa08fd
 
274d219
bdb6d79
8409fbf
274d219
 
60372e5
4fa08fd
274d219
bdb6d79
4fa08fd
 
bdb6d79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fa08fd
60372e5
7978786
 
 
 
 
7d2f14f
 
 
7978786
 
7d2f14f
 
7978786
 
b816c7b
7d2f14f
814a594
596cc4a
0c20836
 
523cd54
814a594
1c6e0dc
469d56f
89a066a
814a594
 
b816c7b
cf1a596
b816c7b
 
596cc4a
b816c7b
 
 
 
814a594
b816c7b
 
814a594
b816c7b
 
 
 
 
 
 
 
596cc4a
b816c7b
 
596cc4a
b816c7b
4b7750b
 
 
 
 
 
814a594
596cc4a
b816c7b
 
596cc4a
814a594
596cc4a
814a594
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import gradio as gr
import os
from typing import Tuple, Optional
import os
import shutil
import sys
from pathlib import Path
import cv2
import gradio as gr
import numpy as np
import spaces
import torch
from PIL import Image
from tqdm import tqdm
from pathlib import Path
from huggingface_hub import login
from transformers import AutoModel, AutoTokenizer, BitsAndBytesConfig

token = os.getenv("HF_TOKEN")
if token:
    login(token=token)
current_dir = Path(__file__).parent
sys.path.append(str(current_dir))

from modeling.BaseModel import BaseModel
from modeling import build_model
from utilities.arguments import load_opt_from_config_files
from utilities.constants import BIOMED_CLASSES
from inference_utils.inference import interactive_infer_image
from inference_utils.output_processing import check_mask_stats
from inference_utils.processing_utils import read_rgb

import spaces


MARKDOWN = """
<div align="center" style="padding: 20px 0;">
    <h1 style="font-size: 3em; margin: 0;">
        ሀ<span style="color: #32CD32;">A</span>ኪ<span style="color: #FFD700;">i</span>ም
        <sup style="font-size: 0.5em;">AI</sup>
    </h1>

    <div style="display: flex; justify-content: center; align-items: center; gap: 15px; margin: 15px 0;">
        <a href="https://cyberbrainai.com/">
            <img src="https://cyberbrainai.com/assets/logo.svg" alt="CyberBrain AI" style="width:40px; height:40px; vertical-align: middle;">
        </a>
        <a href="https://colab.research.google.com/drive/1p3Yf_6xdZPMz5RUtt_NyxrDjrbSgvTDy#scrollTo=t30NqIrCKdAI">
            <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="ድinቅneሽ" style="vertical-align: middle;">
        </a>
        <a href="https://www.youtube.com/watch?v=Dv003fTyO-Y">
            <img src="https://badges.aleen42.com/src/youtube.svg" alt="YouTube" style="vertical-align: middle;">
        </a>
    </div>
</div>
    <div>
        <p style="font-size: 1.4em; line-height: 1.5; margin: 15px 0; text-align: left;">
            This demo integrates BiomedParse, a foundation model for joint segmentation, detection, and recognition across 9 biomedical imaging modalities. 
            The model supports <span style="color: #FF4500;">CT</span>, <span style="color: #4169E1;">MRI</span>, <span style="color: #32CD32;">X-Ray</span>, <span style="color: #9370DB;">Pathology</span>, <span style="color: #FFD700;">Ultrasound</span>, <span style="color: #FF69B4;">Endoscope</span>, <span style="color: #20B2AA;">Fundus</span>, <span style="color: #FF8C00;">Dermoscopy</span>, and <span style="color: #8B008B;">OCT</span>.
        </p>
    </div>

"""

IMAGE_PROCESSING_EXAMPLES = [
    ["BiomedParse Segmentation", 
     "https://raw.githubusercontent.com/microsoft/BiomedParse/main/examples/T0011.jpg",
     "Optic disc in retinal Fundus"],
    ["BiomedParse Segmentation",
     "https://raw.githubusercontent.com/microsoft/BiomedParse/main/examples/Part_3_226_pathology_breast.png",
     "optic disc, optic cup"],
    ["BiomedParse Segmentation",
     "https://raw.githubusercontent.com/microsoft/BiomedParse/main/examples/covid_1585.png",
     "COVID-19 infection in chest X-Ray"],
    ["BiomedParse Segmentation",
     "https://raw.githubusercontent.com/microsoft/BiomedParse/main/examples/TCGA_HT_7856_19950831_8_MRI-FLAIR_brain.png",
     "Lower-grade glioma in brain MRI"],
    ["BiomedParse Segmentation",
     "https://raw.githubusercontent.com/microsoft/BiomedParse/main/examples/LIDC-IDRI-0140_143_280_CT_lung.png",
     "COVID-19 infection in chest CT"],
    ["BiomedParse Segmentation",
     "https://raw.githubusercontent.com/microsoft/BiomedParse/main/examples/144DME_as_F.jpeg",
     "Cystoid macular edema in retinal OCT"],
    ["BiomedParse Segmentation",
     "https://raw.githubusercontent.com/microsoft/BiomedParse/main/examples/Part_1_516_pathology_breast.png",
     "Glandular structure in colon Pathology"],
    ["BiomedParse Segmentation",
     "https://raw.githubusercontent.com/microsoft/BiomedParse/main/examples/ISIC_0015551.jpg",
     "Melanoma in skin Dermoscopy"],
    ["BiomedParse Segmentation",
     "https://raw.githubusercontent.com/microsoft/BiomedParse/main/examples/C3_EndoCV2021_00462.jpg",
     "Neoplastic polyp in colon Endoscope"]
]

BIOMEDPARSE_MODES = {
    "CT-Abdomen": ["abdomen", "liver"],
    "CT-Chest": ["lung"],
    "CT-Liver": ["liver"],
    "MRI-Abdomen": ["abdomen"],
    "MRI-Cardiac": ["heart"],
    "MRI-FLAIR-Brain": ["brain"],
    "MRI-T1-Gd-Brain": ["brain"],
    "Pathology": ["bladder", "breast", "cervix", "colon", "esophagus", "kidney", 
                  "liver", "ovarian", "prostate", "stomach", "testis", "thyroid", "uterus"],
    "X-Ray-Chest": ["chest"],
    "Ultrasound-Cardiac": ["heart"],
    "Endoscopy": ["colon"],
    "Fundus": ["retinal"],
    "Dermoscopy": ["skin"],
    "OCT": ["retinal"]
}

IMAGE_INFERENCE_MODES = [
    "BIOMED SEGMENTATION",
    "BIOMED DETECTION", 
    "BIOMED RECOGNITION",
    "BIOMED SEGMENTATION + DETECTION",
    "BIOMED SEGMENTATION + RECOGNITION",
    "BIOMED DETECTION + RECOGNITION",
    "BIOMED SEGMENTATION + DETECTION + RECOGNITION"
]

MODALITY_PROMPTS = {
   "CT-Abdomen": ["postcava", "aorta", "right kidney", "kidney", "left kidney", "duodenum", "pancreas", "liver", "spleen", "stomach", "gallbladder", "left adrenal gland", "adrenal gland", "right adrenal gland", "esophagus"],
   "CT-Chest": ["nodule", "COVID-19 infection", "tumor"],
   "MRI-Abdomen": ["aorta", "postcava", "right kidney", "duodenum", "kidney", "left kidney", "liver", "pancreas", "gallbladder", "stomach", "spleen", "left adrenal gland", "adrenal gland", "right adrenal gland", "esophagus"],
   "MRI-Cardiac": ["left heart ventricle", "myocardium", "right heart ventricle"],
   "MRI-FLAIR-Brain": ["edema", "tumor core", "whole tumor"],
   "MRI-T1-Gd-Brain": ["enhancing tumor", "non-enhancing tumor", "tumor core"],
   "Pathology": ["connective tissue cells", "inflammatory cells", "neoplastic cells", "epithelial cells"],
   "X-Ray-Chest": ["left lung", "lung", "right lung"],
   "Ultrasound-Cardiac": ["left heart atrium", "left heart ventricle"],
   "Endoscopy": ["neoplastic polyp", "polyp", "non-neoplastic polyp"],
   "Fundus": ["optic cup", "optic disc"],
   "Dermoscopy": ["lesion", "melanoma"],
   "OCT": ["edema"]
}

def extract_modality_and_prompts(llm_output):
    """
    Extract modality and relevant prompts from LLM output
    Returns: (modality_type, list_of_prompts)
    """
    llm_output = llm_output.lower()
    
    # Dictionary mapping keywords to modalities
    modality_indicators = {
        'dermatoscop': 'Dermoscopy',
        'dermatoscope': 'Dermoscopy',
        'dermal': 'Dermoscopy',
        'skin lesion': 'Dermoscopy',
        'dermatological': 'Dermoscopy',
        'oct': 'OCT',
        'optical coherence': 'OCT',
        'fundus': 'Fundus',
        'retina': 'Fundus',
        'endoscop': 'Endoscopy',
        'colon': 'Endoscopy',
        'pathological': 'Pathology',
        # 'tissue': 'Pathology',
        'histolog': 'Pathology',
        'x-ray': 'X-Ray-Chest',
        'xray': 'X-Ray-Chest',
        'chest radiograph': 'X-Ray-Chest',
        'mri': None,  # Will be refined below
        'magnetic resonance': None,  # Will be refined below
        'ct': None,  # Will be refined below
        'computed tomography': None,  # Will be refined below
        'ultrasound': 'Ultrasound-Cardiac',
        'sonograph': 'Ultrasound-Cardiac'
    }
    
    # First pass: Detect base modality
    detected_modality = None
    for keyword, modality in modality_indicators.items():
        if keyword in llm_output:
            detected_modality = modality
            break
    
    # Second pass: Refine MRI and CT if detected
    if detected_modality is None and ('mri' in llm_output or 'magnetic resonance' in llm_output):
        if 'brain' in llm_output or 'flair' in llm_output:
            detected_modality = 'MRI-FLAIR-Brain'
        elif 'cardiac' in llm_output or 'heart' in llm_output:
            detected_modality = 'MRI-Cardiac'
        elif 'abdomen' in llm_output:
            detected_modality = 'MRI-Abdomen'
        elif 't1' in llm_output or 'contrast' in llm_output:
            detected_modality = 'MRI-T1-Gd-Brain'
        else:
            detected_modality = 'MRI'
    
    if detected_modality is None and ('ct' in llm_output or 'computed tomography' in llm_output):
        if 'chest' in llm_output or 'lung' in llm_output:
            detected_modality = 'CT-Chest'
        elif 'liver' in llm_output:
            detected_modality = 'CT-Liver'
        elif 'abdomen' in llm_output:
            detected_modality = 'CT-Abdomen'
        else:
            detected_modality = 'CT'
    
    # If still no modality detected, return None
    if not detected_modality:
        return "", []
    
    # Get relevant prompts for the detected modality
    if detected_modality in MODALITY_PROMPTS:
        relevant_prompts = MODALITY_PROMPTS[detected_modality]
    else:
        relevant_prompts = []
    
    return detected_modality, relevant_prompts

def on_mode_dropdown_change(selected_mode):
    if selected_mode in IMAGE_INFERENCE_MODES:
        return [
            gr.Dropdown(visible=True, choices=list(BIOMEDPARSE_MODES.keys()), label="Modality"),
            gr.Dropdown(visible=True, label="Anatomical Site"),
            gr.Textbox(visible=False),
            gr.Textbox(visible=False)
        ]
    else:
        return [
            gr.Dropdown(visible=False),
            gr.Dropdown(visible=False),
            gr.Textbox(visible=True),
            gr.Textbox(visible=(selected_mode == None))
        ]

def on_modality_change(modality):
    if modality:
        return gr.Dropdown(choices=BIOMEDPARSE_MODES[modality], visible=True)
    return gr.Dropdown(visible=False)

def initialize_model():
    opt = load_opt_from_config_files(["configs/biomedparse_inference.yaml"])
    pretrained_pth = 'hf_hub:microsoft/BiomedParse'
    opt['device'] = 'cuda' if torch.cuda.is_available() else 'cpu'
    model = BaseModel(opt, build_model(opt)).from_pretrained(pretrained_pth).eval()
    with torch.no_grad():
        model.model.sem_seg_head.predictor.lang_encoder.get_text_embeddings(
            BIOMED_CLASSES + ["background"], is_eval=True
        )
    return model

def initialize_llm():
    try:
        print("Starting LLM initialization...")
        quantization_config = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_compute_dtype=torch.float16
        )
        
        model = AutoModel.from_pretrained(
            "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
            device_map="auto",
            torch_dtype=torch.float16,
            trust_remote_code=True,
            low_cpu_mem_usage=True,
            quantization_config=quantization_config
        )
        print("Model loaded successfully")
        
        tokenizer = AutoTokenizer.from_pretrained(
            "ContactDoctor/Bio-Medical-MultiModal-Llama-3-8B-V1",
            trust_remote_code=True
        )
        print("Tokenizer loaded successfully")
        return model, tokenizer
    except Exception as e:
        print(f"Failed to initialize LLM: {str(e)}")
        return None, None

model = initialize_model()
llm_model, llm_tokenizer = initialize_llm()

def update_example_prompts(modality):
    if modality in MODALITY_PROMPTS:
        examples = MODALITY_PROMPTS[modality]
        return f"Example prompts for {modality}:\n" + ", ".join(examples)
    return ""

@spaces.GPU
@torch.inference_mode()
@torch.autocast(device_type="cuda", dtype=torch.bfloat16)
def process_image(image_path, user_prompt, modality=None):
    try:
        if not image_path:
            return [], "Please upload an image", "No modality detected"
        
        image = read_rgb(image_path)
        pil_image = Image.fromarray(image)
        
        # question = (
        #     f"Analyze this medical image considering the following context: {user_prompt}. "
        #     "Include modality, anatomical structures, and any abnormalities."
        # )
        question = 'What type of medical imaging modality is this?  which organ? Be specific.'
        msgs = [{'role': 'user', 'content': [pil_image, question]}]
        
        llm_response = ""
        if llm_model and llm_tokenizer:
            try:
                for new_text in llm_model.chat(
                    image=pil_image,
                    msgs=msgs,
                    tokenizer=llm_tokenizer,
                    sampling=True,
                    temperature=0.95,
                    stream=True
                ):
                    llm_response += new_text
            except Exception as e:
                print(f"LLM chat error: {str(e)}")
                llm_response = "LLM analysis failed. Proceeding with basic analysis."
        else:
            llm_response = "LLM not available. Please check LLM initialization logs."
        
        detected_modality, relevant_prompts = extract_modality_and_prompts(llm_response)
        if not detected_modality:
            detected_modality = "X-Ray-Chest"  # Fallback modality
            relevant_prompts = MODALITY_PROMPTS["X-Ray-Chest"]
        
        # results = []
        # analysis_results = []
        # colors = [(255,0,0), (0,255,0), (0,0,255), (255,255,0), (255,0,255)]
        
        # # Add color mapping to analysis with more natural language
        # color_descriptions = []
        # for idx, prompt in enumerate(relevant_prompts):
        #     color = colors[idx % len(colors)]
        #     color_name = {(255,0,0): "red", (0,255,0): "green", (0,0,255): "blue", 
        #                  (255,255,0): "yellow", (255,0,255): "magenta"}[color]
        #     color_descriptions.append(f"The {prompt} is highlighted in {color_name} color")
        
        # for idx, prompt in enumerate(relevant_prompts):
        #     try:
        #         mask_list = interactive_infer_image(model, pil_image, [prompt])
        #         if mask_list is None or len(mask_list) == 0:
        #             analysis_results.append(f"No mask generated for '{prompt}'")
        #             continue
                
        #         pred_mask = mask_list[0]
        #         # Check if mask is valid using numpy's any() function
        #         if pred_mask is None or not np.any(pred_mask):
        #             analysis_results.append(f"Empty mask generated for '{prompt}'")
        #             continue
                
        #         overlay_image = image.copy()
        #         color = colors[idx % len(colors)]
        #         mask_indices = pred_mask > 0.5
        #         if np.any(mask_indices):  # Use np.any() for boolean array check
        #             overlay_image[mask_indices] = color
        #             results.append(overlay_image)
        #     except Exception as e:
        #         print(f"Error processing finding {prompt}: {str(e)}")
        #         analysis_results.append(f"Failed to process '{prompt}': {str(e)}")
        
        # if not results:
        #     results = [image]  # Return original image if no overlays were created
            
        detailed_analysis = ""
        if llm_model and llm_tokenizer:
            try:
                # Add color legend with more natural language
                # detailed_analysis += "\n\n As shown in the images outputs details:\n \n" + "\n".join(color_descriptions)
                
                analysis_prompt = f"Focus more on the user question. which is: {user_prompt}. Give the modality, organ, analysis, abnormalities (if any), treatment (if abnormalities are present) for this image. "
                msgs = [{'role': 'user', 'content': [pil_image, analysis_prompt]}]
                
                for new_text in llm_model.chat(
                    image=pil_image,
                    msgs=msgs,
                    tokenizer=llm_tokenizer,
                    sampling=True,
                    temperature=0.95,
                    stream=True
                ):
                    detailed_analysis += new_text
                
                # Add segmentation details in a more natural way
                results = []
                analysis_results = []
                colors = [(255,0,0), (0,255,0), (0,0,255), (255,255,0), (255,0,255)]
                
                # Add color mapping to analysis with more natural language
                color_descriptions = []
                # First loop: collect prompts found in the analysis
                found_prompts = []
                count = 0
                for idx, prompt in enumerate(relevant_prompts):
                    if prompt in detailed_analysis.lower():
                        color = colors[count % len(colors)]
                        color_name = {(255,0,0): "red", (0,255,0): "green", (0,0,255): "blue", (255,255,0): "yellow", (255,0,255): "magenta"}[color]                        
                        color_descriptions.append(f"The {prompt} is highlighted in {color_name} color for reference")
                        found_prompts.append(prompt)
                        count += 1
                
                # Second loop: only process prompts found in analysis
                for idx, prompt in enumerate(found_prompts):
                    try:
                        mask_list = interactive_infer_image(model, pil_image, [prompt])
                        if mask_list is None or len(mask_list) == 0:
                            analysis_results.append(f"No mask generated for '{prompt}'")
                            continue
                        
                        pred_mask = mask_list[0]
                        # Check if mask is valid using numpy's any() function
                        if pred_mask is None or not np.any(pred_mask):
                            analysis_results.append(f"Empty mask generated for '{prompt}'")
                            continue
                        
                        overlay_image = image.copy()
                        color = colors[idx % len(colors)]
                        mask_indices = pred_mask > 0.5
                        if np.any(mask_indices):  # Use np.any() for boolean array check
                            overlay_image[mask_indices] = color
                            results.append(overlay_image)
                    except Exception as e:
                        print(f"Error processing finding {prompt}: {str(e)}")
                        analysis_results.append(f"Failed to process '{prompt}': {str(e)}")
                
                if not results:
                    results = [image]  # Return original image if no overlays were created
                detailed_analysis += ""
                if color_descriptions:
                    detailed_analysis += " " + " ".join(color_descriptions) + "."
                else:
                    detailed_analysis += " No significant segments were detected."
                
            except Exception as e:
                print(f"LLM chat error: {str(e)}")
                detailed_analysis = "LLM analysis failed. Proceeding with basic analysis."
                if color_descriptions:
                    detailed_analysis += "\n\nHowever, in the segmentation analysis: " + " ".join(color_descriptions) + "."
        else:
            detailed_analysis = "LLM not available. Please check LLM initialization logs."
            if color_descriptions:
                detailed_analysis += "\n\nIn the segmentation analysis: " + " ".join(color_descriptions) + "."
        
        return results, detailed_analysis, detected_modality

    except Exception as e:
        error_msg = f"⚠️ An error occurred: {str(e)}"
        print(f"Error details: {str(e)}", flush=True)
        return [image] if 'image' in locals() else [], error_msg, "Error detecting modality"

with gr.Blocks() as demo:
    gr.HTML(MARKDOWN)    
    with gr.Row():        
        with gr.Column():
            image_input = gr.Image(type="filepath", label="Input Image")
            prompt_input = gr.Textbox(
                lines=2, 
                placeholder="Ask any question about the medical image...", 
                label="Question/Prompt"
            )
            detected_modality = gr.Textbox(
                label="Detected Modality", 
                interactive=False,
                visible=True
            )
            submit_btn = gr.Button("Analyze")
            
        with gr.Column():
            output_gallery = gr.Gallery(
                label="Segmentation Results", 
                show_label=True,
                columns=[2], 
                height="auto"
            )
            analysis_output = gr.Textbox(
                label="Analysis", 
                interactive=False,
                show_label=True,
                lines=10
            )
    
    # Add this to clear outputs when input image is cleared
    image_input.clear(
        lambda: ([], "", ""),
        outputs=[output_gallery, analysis_output, detected_modality]
    )
    
    submit_btn.click(
        fn=process_image,
        inputs=[image_input, prompt_input],
        outputs=[output_gallery, analysis_output, detected_modality],
        api_name="process"
    )

demo.launch()