File size: 25,662 Bytes
576ba03
b458509
 
576ba03
 
 
 
 
10a0b3c
d91b6af
 
 
576ba03
 
d91b6af
 
 
 
 
 
 
2ebe272
d91b6af
 
 
 
 
 
2ebe272
d91b6af
 
 
 
 
 
 
2ebe272
d91b6af
 
2ebe272
d91b6af
 
 
 
 
10a0b3c
d91b6af
 
b458509
d91b6af
 
 
 
 
 
 
 
 
 
 
 
 
576ba03
 
 
 
 
 
d91b6af
 
 
 
5273a4f
576ba03
d91b6af
576ba03
 
 
b458509
576ba03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d91b6af
576ba03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d91b6af
576ba03
 
2ebe272
576ba03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ebe272
10a0b3c
576ba03
d91b6af
2ebe272
d91b6af
 
 
 
2ebe272
10a0b3c
d91b6af
10a0b3c
2ebe272
d91b6af
 
 
 
 
 
 
576ba03
5273a4f
576ba03
 
5273a4f
576ba03
 
d91b6af
5273a4f
576ba03
d91b6af
 
 
 
 
 
576ba03
10a0b3c
576ba03
5273a4f
59f1e1c
5273a4f
576ba03
d91b6af
 
 
576ba03
 
d91b6af
576ba03
10a0b3c
576ba03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d91b6af
 
5273a4f
d91b6af
 
10a0b3c
 
 
d91b6af
576ba03
10a0b3c
576ba03
 
 
 
d91b6af
576ba03
d91b6af
 
576ba03
10a0b3c
d91b6af
 
 
 
 
 
576ba03
d91b6af
 
 
 
 
10a0b3c
576ba03
d91b6af
 
 
 
576ba03
d91b6af
0c3e999
d91b6af
0c3e999
d91b6af
 
 
 
 
0c3e999
d91b6af
 
 
2ebe272
d91b6af
 
576ba03
0c3e999
10a0b3c
 
 
 
0c3e999
10a0b3c
 
0c3e999
576ba03
 
5273a4f
d91b6af
 
5273a4f
 
 
 
 
 
576ba03
5273a4f
 
 
576ba03
5273a4f
 
 
576ba03
5273a4f
576ba03
d91b6af
 
576ba03
d91b6af
 
576ba03
d91b6af
5273a4f
576ba03
d91b6af
 
 
 
 
576ba03
d91b6af
576ba03
d91b6af
576ba03
d91b6af
 
 
576ba03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d91b6af
 
 
 
0c3e999
576ba03
 
d91b6af
 
 
 
5273a4f
d91b6af
5273a4f
 
 
576ba03
5273a4f
 
 
 
 
 
576ba03
 
 
 
 
 
 
 
 
 
 
d91b6af
5273a4f
d91b6af
10a0b3c
576ba03
d91b6af
5273a4f
576ba03
d91b6af
576ba03
 
d91b6af
576ba03
d91b6af
644aa62
5273a4f
d91b6af
576ba03
d91b6af
5273a4f
576ba03
 
5273a4f
576ba03
 
5273a4f
 
d91b6af
5273a4f
10a0b3c
5273a4f
 
d91b6af
5273a4f
10a0b3c
576ba03
 
 
 
59f1e1c
576ba03
 
 
 
 
 
 
59f1e1c
0c3e999
d91b6af
 
10a0b3c
d91b6af
 
 
 
 
 
5273a4f
d91b6af
 
 
 
 
 
 
 
0c3e999
10a0b3c
 
5273a4f
d91b6af
 
 
10a0b3c
5273a4f
d91b6af
 
 
10a0b3c
5273a4f
d91b6af
 
b458509
0c3e999
576ba03
d91b6af
576ba03
d91b6af
576ba03
 
 
 
 
d91b6af
576ba03
 
 
 
 
d91b6af
576ba03
d91b6af
b458509
d91b6af
b458509
d91b6af
b458509
d91b6af
 
 
 
5273a4f
 
d91b6af
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# app.py - Medical AI with Proper Vision Analysis
import gradio as gr
import torch
from transformers import (
    BlipProcessor, BlipForConditionalGeneration,
    AutoProcessor, AutoModelForCausalLM,
    pipeline
)
from PIL import Image
import logging
from collections import defaultdict, Counter
import time
import requests
from io import BytesIO

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Usage tracking
class UsageTracker:
    def __init__(self):
        self.stats = {
            'total_analyses': 0,
            'successful_analyses': 0,
            'failed_analyses': 0,
            'average_processing_time': 0.0,
            'question_types': Counter()
        }
    
    def log_analysis(self, success, duration, question_type=None):
        self.stats['total_analyses'] += 1
        if success:
            self.stats['successful_analyses'] += 1
        else:
            self.stats['failed_analyses'] += 1
        
        total_time = self.stats['average_processing_time'] * (self.stats['total_analyses'] - 1)
        self.stats['average_processing_time'] = (total_time + duration) / self.stats['total_analyses']
        
        if question_type:
            self.stats['question_types'][question_type] += 1

# Rate limiting
class RateLimiter:
    def __init__(self, max_requests_per_hour=60):
        self.max_requests_per_hour = max_requests_per_hour
        self.requests = defaultdict(list)
    
    def is_allowed(self, user_id="default"):
        current_time = time.time()
        hour_ago = current_time - 3600
        self.requests[user_id] = [req_time for req_time in self.requests[user_id] if req_time > hour_ago]
        if len(self.requests[user_id]) < self.max_requests_per_hour:
            self.requests[user_id].append(current_time)
            return True
        return False

# Initialize components
usage_tracker = UsageTracker()
rate_limiter = RateLimiter()

# Try multiple models for better medical analysis
MODELS_TO_TRY = [
    "microsoft/git-base-coco",  # Better for detailed descriptions
    "Salesforce/blip2-opt-2.7b",  # More capable BLIP2 model
    "Salesforce/blip-image-captioning-large"  # Fallback
]

# Global variables
model = None
processor = None
device = "cuda" if torch.cuda.is_available() else "cpu"
current_model_name = None

def load_best_model():
    """Try to load the best available model for medical image analysis"""
    global model, processor, current_model_name
    
    for model_name in MODELS_TO_TRY:
        try:
            logger.info(f"Trying to load: {model_name}")
            
            if "git-base" in model_name:
                # Use transformers pipeline for GIT model
                model = pipeline("image-to-text", model=model_name, device=0 if torch.cuda.is_available() else -1)
                processor = None
                current_model_name = model_name
                logger.info(f"βœ… Successfully loaded GIT model: {model_name}")
                return True
                
            elif "blip2" in model_name:
                # Try BLIP2 model
                processor = AutoProcessor.from_pretrained(model_name)
                model = AutoModelForCausalLM.from_pretrained(
                    model_name,
                    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
                    device_map="auto" if torch.cuda.is_available() else None,
                )
                current_model_name = model_name
                logger.info(f"βœ… Successfully loaded BLIP2 model: {model_name}")
                return True
                
            else:
                # Standard BLIP model
                processor = BlipProcessor.from_pretrained(model_name)
                model = BlipForConditionalGeneration.from_pretrained(
                    model_name,
                    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
                    device_map="auto" if torch.cuda.is_available() else None,
                )
                if torch.cuda.is_available() and hasattr(model, 'to'):
                    model = model.to(device)
                current_model_name = model_name
                logger.info(f"βœ… Successfully loaded BLIP model: {model_name}")
                return True
                
        except Exception as e:
            logger.warning(f"Failed to load {model_name}: {e}")
            continue
    
    logger.error("❌ Failed to load any model")
    return False

# Load model at startup
model_ready = load_best_model()

def get_detailed_medical_analysis(image, question):
    """Get detailed medical analysis using the best available model"""
    try:
        if "git-base" in current_model_name:
            # Use GIT model (usually gives more detailed descriptions)
            results = model(image, max_new_tokens=200)
            description = results[0]['generated_text'] if results else "Unable to analyze image"
            
            # For medical questions, try to expand the analysis
            if any(word in question.lower() for word in ['abnormal', 'diagnosis', 'condition', 'pathology']):
                # Add medical context to the basic description
                medical_prompt = f"Medical analysis: {description}"
                return description, medical_prompt
            
            return description, description
            
        elif "blip2" in current_model_name:
            # Use BLIP2 model
            inputs = processor(image, question, return_tensors="pt")
            if torch.cuda.is_available():
                inputs = {k: v.to(device) for k, v in inputs.items()}
            
            with torch.no_grad():
                generated_ids = model.generate(**inputs, max_new_tokens=150, do_sample=False)
            
            generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
            
            # Also get unconditional description
            basic_inputs = processor(image, return_tensors="pt")
            if torch.cuda.is_available():
                basic_inputs = {k: v.to(device) for k, v in basic_inputs.items()}
            
            with torch.no_grad():
                basic_ids = model.generate(**basic_inputs, max_new_tokens=100, do_sample=False)
            
            basic_text = processor.batch_decode(basic_ids, skip_special_tokens=True)[0]
            
            return basic_text, generated_text
            
        else:
            # Standard BLIP model - improved approach
            # Get unconditional caption first
            inputs = processor(image, return_tensors="pt")
            if torch.cuda.is_available():
                inputs = {k: v.to(device) for k, v in inputs.items()}
            
            with torch.no_grad():
                output_ids = model.generate(**inputs, max_length=100, num_beams=3, do_sample=False)
            
            basic_description = processor.decode(output_ids[0], skip_special_tokens=True)
            
            # Try conditional generation with better prompting
            medical_prompts = [
                f"Question: {question} Answer:",
                f"Medical analysis: {question}",
                f"Describe the medical findings: {question}"
            ]
            
            best_response = basic_description
            
            for prompt in medical_prompts:
                try:
                    inputs_qa = processor(image, prompt, return_tensors="pt")
                    if torch.cuda.is_available():
                        inputs_qa = {k: v.to(device) for k, v in inputs_qa.items()}
                    
                    with torch.no_grad():
                        qa_output_ids = model.generate(
                            **inputs_qa,
                            max_length=200,
                            num_beams=3,
                            do_sample=False,
                            early_stopping=True
                        )
                    
                    # Decode only generated part
                    input_length = inputs_qa['input_ids'].shape[1]
                    qa_response = processor.decode(qa_output_ids[0][input_length:], skip_special_tokens=True).strip()
                    
                    if qa_response and len(qa_response) > 20 and not qa_response.lower().startswith('question'):
                        best_response = qa_response
                        break
                        
                except Exception as e:
                    continue
            
            return basic_description, best_response
            
    except Exception as e:
        logger.error(f"Analysis failed: {e}")
        return "Unable to analyze image", "Analysis failed"

def enhance_medical_description(basic_desc, clinical_question, patient_history):
    """Enhance basic description with medical context and educational content"""
    
    # Common medical image analysis patterns
    chest_xray_analysis = """
**Systematic Chest X-ray Analysis:**

**Technical Quality:**
- Image appears to be a standard PA chest radiograph
- Adequate penetration and positioning for diagnostic evaluation

**Anatomical Review:**
- **Heart**: Cardiac silhouette evaluation for size and contour
- **Lungs**: Assessment of lung fields for opacity, consolidation, or air trapping
- **Pleura**: Examination for pleural effusion or pneumothorax
- **Bones**: Rib cage and spine alignment assessment
- **Soft Tissues**: Evaluation of surrounding structures

**Clinical Correlation Needed:**
Given the patient's presentation with cough and fever, key considerations include:
- **Pneumonia**: Look for consolidation, air bronchograms, or infiltrates
- **Viral vs Bacterial**: Pattern recognition for different infectious etiologies  
- **Atelectasis**: Collapsed lung segments that might appear as increased opacity
- **Pleural Changes**: Fluid collection that could indicate infection complications

**Educational Points:**
- Chest X-rays are the first-line imaging for respiratory symptoms
- Clinical correlation is essential - symptoms guide interpretation
- Follow-up imaging may be needed based on treatment response
    """
    
    # Determine if this is likely a chest X-ray
    if any(term in basic_desc.lower() for term in ['chest', 'lung', 'rib', 'heart', 'x-ray', 'radiograph']) or \
       any(term in clinical_question.lower() for term in ['chest', 'lung', 'respiratory', 'cough']):
        enhanced_analysis = chest_xray_analysis
    else:
        # Generic medical image analysis
        enhanced_analysis = f"""
**Medical Image Analysis Framework:**

**Image Description:**
{basic_desc}

**Clinical Context Integration:**
- Patient presentation: {patient_history if patient_history else 'Clinical history provided'}
- Imaging indication: {clinical_question}

**Systematic Approach:**
1. **Technical Assessment**: Image quality and acquisition parameters
2. **Anatomical Review**: Systematic evaluation of visible structures  
3. **Pathological Assessment**: Identification of any abnormal findings
4. **Clinical Correlation**: Integration with patient symptoms and history

**Educational Considerations:**
- Medical imaging interpretation requires systematic approach
- Clinical context significantly influences interpretation priorities
- Multiple imaging modalities may be complementary for diagnosis
- Professional radiological review is essential for clinical decisions
        """
    
    return enhanced_analysis

def analyze_medical_image(image, clinical_question, patient_history=""):
    """Enhanced medical image analysis with better AI models"""
    start_time = time.time()
    
    # Rate limiting
    if not rate_limiter.is_allowed():
        usage_tracker.log_analysis(False, time.time() - start_time)
        return "⚠️ Rate limit exceeded. Please wait before trying again."
    
    if not model_ready or model is None:
        usage_tracker.log_analysis(False, time.time() - start_time)
        return "❌ Medical AI model not loaded. Please refresh the page."
    
    if image is None:
        return "⚠️ Please upload a medical image first."
    
    if not clinical_question.strip():
        return "⚠️ Please provide a clinical question."
    
    try:
        logger.info("Starting enhanced medical image analysis...")
        
        # Get detailed analysis from AI model
        basic_description, detailed_response = get_detailed_medical_analysis(image, clinical_question)
        
        # Enhance with medical knowledge
        enhanced_analysis = enhance_medical_description(basic_description, clinical_question, patient_history)
        
        # Create comprehensive medical report
        formatted_response = f"""# πŸ₯ **Enhanced Medical AI Analysis**

## **Clinical Question:** {clinical_question}
{f"## **Patient History:** {patient_history}" if patient_history.strip() else ""}

---

## πŸ” **AI Vision Analysis**

### **Image Description:**
{basic_description}

### **Question-Specific Analysis:**
{detailed_response}

---

## πŸ“‹ **Medical Assessment Framework**
{enhanced_analysis}

---

## πŸŽ“ **Educational Summary**

**Learning Objectives:**
- Demonstrate systematic approach to medical image interpretation
- Integrate clinical history with imaging findings
- Understand the importance of professional validation in medical diagnosis

**Key Teaching Points:**
- Medical imaging is one component of comprehensive patient assessment
- Clinical correlation enhances diagnostic accuracy
- Multiple imaging modalities may provide complementary information
- Professional interpretation is essential for patient care decisions

**Clinical Decision Making:**
Based on the combination of:
- Patient symptoms: {patient_history if patient_history else 'As provided'}
- Imaging findings: As described above
- Clinical context: {clinical_question}

**Next Steps in Clinical Practice:**
- Professional radiological review
- Correlation with laboratory findings
- Consider additional imaging if clinically indicated
- Follow-up based on treatment response
"""
        
        # Add medical disclaimer
        disclaimer = """
---
## ⚠️ **IMPORTANT MEDICAL DISCLAIMER**

**FOR EDUCATIONAL AND RESEARCH PURPOSES ONLY**

- **🚫 AI Limitations**: AI analysis has significant limitations for medical diagnosis
- **πŸ‘¨β€βš•οΈ Professional Review Required**: All findings must be validated by qualified healthcare professionals
- **🚨 Emergency Care**: For urgent medical concerns, seek immediate medical attention
- **πŸ₯ Clinical Integration**: AI findings are educational tools, not diagnostic conclusions
- **πŸ“‹ Learning Tool**: Designed for medical education and training purposes
- **πŸ”’ Privacy**: Do not upload real patient data or identifiable information

**This analysis demonstrates AI-assisted medical image interpretation concepts for educational purposes only.**

---
**Model**: {current_model_name} | **Device**: {device.upper()} | **Purpose**: Medical Education
        """
        
        # Log successful analysis
        duration = time.time() - start_time
        question_type = classify_question(clinical_question)
        usage_tracker.log_analysis(True, duration, question_type)
        
        logger.info(f"βœ… Enhanced medical analysis completed in {duration:.2f}s")
        return formatted_response + disclaimer
        
    except Exception as e:
        duration = time.time() - start_time
        usage_tracker.log_analysis(False, duration)
        logger.error(f"❌ Analysis error: {str(e)}")
        return f"❌ Enhanced analysis failed: {str(e)}\n\nPlease try again with a different image."

def classify_question(question):
    """Classify clinical question type"""
    question_lower = question.lower()
    if any(word in question_lower for word in ['describe', 'findings', 'observe', 'see', 'show']):
        return 'descriptive'
    elif any(word in question_lower for word in ['diagnosis', 'differential', 'condition']):
        return 'diagnostic'
    elif any(word in question_lower for word in ['abnormal', 'pathology', 'disease']):
        return 'pathological'
    else:
        return 'general'

def get_usage_stats():
    """Get usage statistics"""
    stats = usage_tracker.stats
    if stats['total_analyses'] == 0:
        return "πŸ“Š **Usage Statistics**\n\nNo analyses performed yet."
    
    success_rate = (stats['successful_analyses'] / stats['total_analyses']) * 100
    
    return f"""πŸ“Š **Enhanced Medical AI Statistics**

**Performance Metrics:**
- **Total Analyses**: {stats['total_analyses']}
- **Success Rate**: {success_rate:.1f}%
- **Average Processing Time**: {stats['average_processing_time']:.2f} seconds

**Question Types:**
{chr(10).join([f"- **{qtype.title()}**: {count}" for qtype, count in stats['question_types'].most_common(3)])}

**System Status**: {'🟒 Enhanced Model Active' if model_ready else 'πŸ”΄ Offline'}
**Current Model**: {current_model_name if current_model_name else 'None'}
**Device**: {device.upper()}
"""

def clear_all():
    """Clear all inputs and outputs"""
    return None, "", "", ""

def set_chest_example():
    """Set chest X-ray example"""
    return "Describe this chest X-ray systematically and identify any abnormalities", "30-year-old patient with productive cough, fever, and shortness of breath"

def set_pathology_example():
    """Set pathology example"""
    return "What pathological findings are visible? Describe the tissue characteristics.", "Biopsy specimen for histopathological evaluation"

def set_general_example():
    """Set general analysis example"""
    return "Provide a systematic analysis of this medical image", "Patient requiring comprehensive imaging evaluation"

# Create enhanced Gradio interface
def create_interface():
    with gr.Blocks(
        title="Enhanced Medical AI Analysis", 
        theme=gr.themes.Soft(),
        css="""
        .gradio-container { max-width: 1400px !important; }
        .disclaimer { background-color: #fef2f2; border: 1px solid #fecaca; border-radius: 8px; padding: 16px; margin: 16px 0; }
        .success { background-color: #f0f9ff; border: 1px solid #bae6fd; border-radius: 8px; padding: 16px 0; }
        .enhanced { background-color: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 8px; padding: 16px 0; }
        """
    ) as demo:
        
        # Header
        gr.Markdown("""
        # πŸ₯ Enhanced Medical AI Image Analysis
        
        **Advanced Medical AI with Better Vision Models - Educational Analysis**
        
        **Enhanced Features:** 🧠 Multiple AI Models β€’ πŸ”¬ Systematic Analysis β€’ πŸ“‹ Educational Framework β€’ πŸŽ“ Clinical Integration
        """)
        
        # Status display
        if model_ready:
            gr.Markdown(f"""
            <div class="enhanced">
            βœ… <strong>ENHANCED MEDICAL AI READY</strong><br>
            Advanced model loaded: <strong>{current_model_name}</strong><br>
            Now provides detailed medical image analysis with systematic framework and educational content.
            </div>
            """)
        else:
            gr.Markdown("""
            <div class="disclaimer">
            ⚠️ <strong>MODEL LOADING</strong><br>
            Enhanced Medical AI is loading. Please wait and refresh if needed.
            </div>
            """)
        
        # Medical disclaimer
        gr.Markdown("""
        <div class="disclaimer">
        ⚠️ <strong>MEDICAL DISCLAIMER</strong><br>
        This enhanced tool provides AI-assisted medical analysis for <strong>educational purposes only</strong>. 
        Uses advanced vision models for better image understanding. Do not upload real patient data.
        </div>
        """)
        
        with gr.Row():
            # Left column - Main interface
            with gr.Column(scale=2):
                # Image upload
                gr.Markdown("## πŸ“€ Medical Image Upload")
                image_input = gr.Image(
                    label="Upload Medical Image (Enhanced Analysis)", 
                    type="pil",
                    height=300
                )
                
                # Clinical inputs
                gr.Markdown("## πŸ’¬ Clinical Information")
                clinical_question = gr.Textbox(
                    label="Clinical Question *",
                    placeholder="Enhanced examples:\nβ€’ Systematically describe this chest X-ray and identify abnormalities\nβ€’ What pathological findings are visible in this image?\nβ€’ Provide detailed analysis of anatomical structures\nβ€’ Analyze this medical scan for educational purposes",
                    lines=4
                )
                
                patient_history = gr.Textbox(
                    label="Patient History & Clinical Context",
                    placeholder="Detailed example: 35-year-old female with 3-day history of productive cough, fever (38.5Β°C), shortness of breath, and left-sided chest pain",
                    lines=3
                )
                
                # Action buttons
                with gr.Row():
                    clear_btn = gr.Button("πŸ—‘οΈ Clear All", variant="secondary")
                    analyze_btn = gr.Button("πŸ” Enhanced Medical Analysis", variant="primary", size="lg")
                
                # Results
                gr.Markdown("## πŸ“‹ Enhanced Medical Analysis Results")
                output = gr.Textbox(
                    label="Advanced AI Medical Analysis (Multiple Models)",
                    lines=25,
                    show_copy_button=True,
                    placeholder="Upload a medical image and provide detailed clinical question for comprehensive AI analysis..."
                )
            
            # Right column - Status and controls
            with gr.Column(scale=1):
                gr.Markdown("## ℹ️ Enhanced System Status")
                
                system_info = f"""
**Status**: {'βœ… Advanced Models Active' if model_ready else 'πŸ”„ Loading'}  
**Primary Model**: {current_model_name if current_model_name else 'Loading...'}  
**Device**: {device.upper()}  
**Enhancement**: 🧠 Multiple AI Models  
**Analysis**: πŸ“‹ Systematic Framework
"""
                gr.Markdown(system_info)
                
                # Statistics
                gr.Markdown("## πŸ“Š Usage Analytics")
                stats_display = gr.Markdown(get_usage_stats())
                refresh_stats_btn = gr.Button("πŸ”„ Refresh Stats", size="sm")
                
                # Quick examples
                if model_ready:
                    gr.Markdown("## 🎯 Enhanced Examples")
                    chest_btn = gr.Button("🫁 Chest X-ray Analysis", size="sm")
                    pathology_btn = gr.Button("πŸ”¬ Pathology Study", size="sm") 
                    general_btn = gr.Button("πŸ“‹ Systematic Analysis", size="sm")
                    
                    gr.Markdown("## πŸš€ Enhancements")
                    gr.Markdown(f"""
                    βœ… **Advanced Vision Models**  
                    βœ… **Systematic Medical Framework**  
                    βœ… **Educational Integration**  
                    βœ… **Clinical Context Analysis**  
                    βœ… **Model**: {current_model_name.split('/')[-1] if current_model_name else 'Enhanced'}
                    """)
        
        # Event handlers
        analyze_btn.click(
            fn=analyze_medical_image,
            inputs=[image_input, clinical_question, patient_history],
            outputs=output,
            show_progress=True
        )
        
        clear_btn.click(
            fn=clear_all,
            outputs=[image_input, clinical_question, patient_history, output]
        )
        
        refresh_stats_btn.click(
            fn=get_usage_stats,
            outputs=stats_display
        )
        
        # Quick example handlers
        if model_ready:
            chest_btn.click(
                fn=set_chest_example,
                outputs=[clinical_question, patient_history]
            )
            
            pathology_btn.click(
                fn=set_pathology_example,
                outputs=[clinical_question, patient_history]
            )
            
            general_btn.click(
                fn=set_general_example,
                outputs=[clinical_question, patient_history]
            )
        
        # Footer
        gr.Markdown(f"""
        ---
        ## πŸš€ **Enhanced Medical AI Features**
        
        ### **Advanced Vision Models:**
        - **Microsoft GIT**: Enhanced image-to-text capabilities
        - **BLIP2**: Advanced vision-language understanding  
        - **Multi-Model Fallback**: Automatic best model selection
        - **Better Descriptions**: More detailed and accurate analysis
        
        ### **Medical Framework Integration:**
        - **Systematic Analysis**: Structured medical image interpretation
        - **Clinical Correlation**: Integration of symptoms with imaging
        - **Educational Content**: Teaching points and learning objectives
        - **Professional Guidelines**: Follows medical education standards
        
        **Current Model**: {current_model_name if current_model_name else 'Loading...'} | **Purpose**: Enhanced Medical Education
        """)
    
    return demo

# Launch the application
if __name__ == "__main__":
    demo = create_interface()
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True,
        share=False
    )