File size: 13,298 Bytes
2c0541f
b458509
 
2c0541f
b458509
 
f2a6f7e
0756f3b
f2a6f7e
 
 
 
b458509
0756f3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b458509
 
 
2c0541f
f2a6f7e
 
2c0541f
f2a6f7e
b458509
2c0541f
 
f2a6f7e
 
0756f3b
 
 
 
 
 
2c0541f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2a6f7e
2c0541f
e111427
2c0541f
e111427
 
 
2c0541f
e111427
2c0541f
f2a6f7e
 
2c0541f
 
f2a6f7e
e111427
f2a6f7e
0756f3b
f2a6f7e
 
 
0756f3b
e111427
 
f2a6f7e
b458509
f2a6f7e
 
b458509
 
0756f3b
2c0541f
b458509
f2a6f7e
2c0541f
e111427
0756f3b
2c0541f
0756f3b
2c0541f
 
 
0756f3b
2c0541f
b458509
f2a6f7e
 
b458509
f2a6f7e
 
b458509
 
2c0541f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b458509
2c0541f
 
 
 
 
 
 
 
 
 
 
 
 
b458509
2c0541f
 
 
 
f2a6f7e
 
e111427
f2a6f7e
0756f3b
f2a6f7e
 
 
 
 
 
 
 
 
b458509
e111427
b458509
 
 
e111427
 
 
2c0541f
b458509
 
 
f2a6f7e
2c0541f
f2a6f7e
 
2c0541f
 
 
 
f2a6f7e
 
 
 
b458509
 
 
f2a6f7e
b458509
e111427
 
b458509
 
e111427
 
2c0541f
 
e111427
2c0541f
 
e111427
 
 
f2a6f7e
2c0541f
 
 
f2a6f7e
 
 
2c0541f
0756f3b
 
 
 
e111427
0756f3b
 
 
f2a6f7e
2c0541f
b458509
2c0541f
f2a6f7e
b458509
 
 
2c0541f
b458509
 
 
f2a6f7e
2c0541f
e111427
b458509
 
 
 
2c0541f
e111427
b458509
 
f2a6f7e
e111427
2c0541f
0756f3b
2c0541f
f2a6f7e
2c0541f
 
 
 
f2a6f7e
 
2c0541f
b458509
2c0541f
f2a6f7e
b458509
2c0541f
e111427
f2a6f7e
2c0541f
b458509
 
2c0541f
 
 
 
 
 
 
 
 
 
 
 
 
b458509
 
 
 
 
f2a6f7e
 
 
 
 
2c0541f
f2a6f7e
b458509
 
e111427
b458509
 
0756f3b
f2a6f7e
2c0541f
f2a6f7e
e111427
2c0541f
 
 
b458509
e111427
b458509
 
 
 
 
 
 
 
 
 
0756f3b
b458509
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
# app.py - Working MedGemma with Correct Implementation
import gradio as gr
import torch
from transformers import AutoProcessor, AutoModelForImageTextToText, pipeline
from PIL import Image
import os
import logging
from huggingface_hub import login

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

# Authenticate with Hugging Face
def authenticate_hf():
    """Authenticate with Hugging Face using token"""
    try:
        hf_token = os.getenv('HF_TOKEN')
        if hf_token:
            login(token=hf_token)
            logger.info("βœ… Authenticated with Hugging Face")
            return True
        else:
            logger.warning("⚠️ No HF_TOKEN found in environment")
            return False
    except Exception as e:
        logger.error(f"❌ Authentication failed: {e}")
        return False

# Model configuration
MODEL_ID = "google/medgemma-4b-it"

# Global variables
model = None
processor = None
pipeline_model = None

def load_model():
    """Load MedGemma model using the recommended approach"""
    global model, processor, pipeline_model
    
    try:
        # First authenticate
        auth_success = authenticate_hf()
        if not auth_success:
            logger.error("❌ Authentication required for MedGemma")
            return False
            
        logger.info(f"Loading MedGemma: {MODEL_ID}")
        
        # Method 1: Try using pipeline (recommended by HuggingFace)
        try:
            logger.info("Attempting to load using pipeline...")
            pipeline_model = pipeline(
                "image-text-to-text", 
                model=MODEL_ID,
                torch_dtype=torch.float32,
                device_map="auto" if torch.cuda.is_available() else None,
                trust_remote_code=True
            )
            logger.info("βœ… Pipeline model loaded successfully!")
            return True
        except Exception as e:
            logger.warning(f"Pipeline loading failed: {e}")
            
        # Method 2: Try direct model loading
        logger.info("Attempting direct model loading...")
        
        # Load processor
        processor = AutoProcessor.from_pretrained(
            MODEL_ID,
            trust_remote_code=True,
            token=True
        )
        logger.info("βœ… Processor loaded")
        
        # Load model
        model = AutoModelForImageTextToText.from_pretrained(
            MODEL_ID,
            torch_dtype=torch.float32,
            device_map="auto" if torch.cuda.is_available() else None,
            trust_remote_code=True,
            token=True
        )
        logger.info("βœ… Model loaded successfully!")
        return True
        
    except Exception as e:
        logger.error(f"❌ Error loading model: {str(e)}")
        import traceback
        logger.error(f"Full traceback: {traceback.format_exc()}")
        return False

# Initialize model at startup
model_loaded = load_model()

def analyze_medical_image(image, clinical_question, patient_history=""):
    """Analyze medical image with clinical context"""
    global model, processor, pipeline_model
    
    # Check if model is loaded
    if not model_loaded:
        return """❌ **Model Loading Issue**

MedGemma failed to load. This is likely due to:

1. **Transformers version**: Make sure you're using transformers >= 4.52.0
2. **Authentication**: Ensure HF_TOKEN is properly set
3. **Model compatibility**: MedGemma requires the latest transformers library

**Status**: Model loading failed. Please try refreshing the page or contact support."""
    
    if image is None:
        return "⚠️ Please upload a medical image first."
    
    if not clinical_question.strip():
        return "⚠️ Please provide a clinical question."
    
    try:
        # Method 1: Use pipeline if available
        if pipeline_model is not None:
            logger.info("Using pipeline for analysis...")
            
            # Prepare message in the format expected by pipeline
            messages = [
                {
                    "role": "user",
                    "content": [
                        {"type": "image", "image": image},
                        {"type": "text", "text": f"Patient History: {patient_history}\n\nClinical Question: {clinical_question}\n\nAs MedGemma, provide a detailed medical analysis of this image for educational purposes only."}
                    ]
                }
            ]
            
            # Generate response using pipeline
            result = pipeline_model(messages, max_new_tokens=1000)
            
            # Extract response text
            response = result[0]['generated_text'] if isinstance(result, list) else result['generated_text']
            
        # Method 2: Use direct model if pipeline failed
        elif model is not None and processor is not None:
            logger.info("Using direct model for analysis...")
            
            # Prepare messages for direct model
            messages = [
                {
                    "role": "system",
                    "content": [{"type": "text", "text": "You are MedGemma, an expert medical AI assistant. Provide detailed medical analysis for educational purposes only."}]
                },
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": f"Patient History: {patient_history}\n\nClinical Question: {clinical_question}"},
                        {"type": "image", "image": image}
                    ]
                }
            ]
            
            # Process inputs
            inputs = processor.apply_chat_template(
                messages,
                add_generation_prompt=True,
                tokenize=True,
                return_dict=True,
                return_tensors="pt"
            )
            
            # Generate response
            with torch.inference_mode():
                outputs = model.generate(
                    **inputs,
                    max_new_tokens=1000,
                    do_sample=True,
                    temperature=0.3,
                    top_p=0.9
                )
            
            # Decode response
            response = processor.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
        
        else:
            return "❌ No model available for analysis. Please try refreshing the page."
        
        # Clean up response
        response = response.strip()
        
        # Add medical disclaimer
        disclaimer = """

---
### ⚠️ MEDICAL DISCLAIMER
**This analysis is for educational and research purposes only.**
- This AI assistant is not a substitute for professional medical advice
- Always consult qualified healthcare professionals for diagnosis and treatment
- Do not make medical decisions based solely on this analysis
- In case of medical emergency, contact emergency services immediately
---
        """
        
        logger.info("βœ… Analysis completed successfully")
        return response + disclaimer
        
    except Exception as e:
        logger.error(f"❌ Error in analysis: {str(e)}")
        import traceback
        logger.error(f"Full traceback: {traceback.format_exc()}")
        return f"❌ Analysis failed: {str(e)}\n\nPlease try again with a different image or question."

# Create Gradio interface
def create_interface():
    with gr.Blocks(
        title="MedGemma Medical Analysis", 
        theme=gr.themes.Soft(),
        css="""
        .gradio-container { max-width: 1200px !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; margin: 16px 0; }
        .warning { background-color: #fffbeb; border: 1px solid #fed7aa; border-radius: 8px; padding: 16px; margin: 16px 0; }
        """
    ) as demo:
        
        # Header
        gr.Markdown("""
        # πŸ₯ MedGemma Medical Image Analysis
        
        **Advanced Medical AI Assistant powered by Google's MedGemma-4B**
        
        Specialized in medical imaging across multiple modalities:
        🫁 **Radiology** β€’ πŸ”¬ **Histopathology** β€’ πŸ‘οΈ **Ophthalmology** β€’ 🩺 **Dermatology**
        """)
        
        # Status display
        if model_loaded:
            method = "Pipeline" if pipeline_model else "Direct Model"
            gr.Markdown(f"""
            <div class="success">
            βœ… <strong>MEDGEMMA READY</strong><br>
            Model loaded successfully using {method} method. Ready for medical image analysis.
            </div>
            """)
        else:
            gr.Markdown("""
            <div class="warning">
            ⚠️ <strong>MODEL LOADING FAILED</strong><br>
            MedGemma failed to load. Please ensure you have the latest transformers library and proper authentication.
            </div>
            """)
        
        # Medical disclaimer
        gr.Markdown("""
        <div class="disclaimer">
        ⚠️ <strong>IMPORTANT MEDICAL DISCLAIMER</strong><br>
        This tool is for <strong>educational and research purposes only</strong>. 
        Do not upload real patient data. Always consult qualified healthcare professionals.
        </div>
        """)
        
        with gr.Row():
            # Left column
            with gr.Column(scale=1):
                gr.Markdown("## πŸ“€ Medical Image Upload")
                
                image_input = gr.Image(
                    label="Medical Image", 
                    type="pil",
                    height=300
                )
                
                clinical_question = gr.Textbox(
                    label="Clinical Question *",
                    placeholder="Examples:\nβ€’ Describe findings in this chest X-ray\nβ€’ What pathological changes are visible?\nβ€’ Provide differential diagnosis\nβ€’ Identify abnormalities",
                    lines=4
                )
                
                patient_history = gr.Textbox(
                    label="Patient History (Optional)",
                    placeholder="e.g., 65-year-old male with chronic cough",
                    lines=2
                )
                
                with gr.Row():
                    clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
                    analyze_btn = gr.Button("πŸ” Analyze", variant="primary", size="lg")
                
                # System info
                gr.Markdown(f"""
                **Status:** {'βœ… Ready' if model_loaded else '❌ Failed'}  
                **Method:** {'Pipeline' if pipeline_model else 'Direct' if model else 'None'}  
                **Device:** {'CUDA' if torch.cuda.is_available() else 'CPU'}  
                **Transformers:** {getattr(__import__('transformers'), '__version__', 'Unknown')}
                """)
                
            # Right column
            with gr.Column(scale=1):
                gr.Markdown("## πŸ“‹ Medical Analysis Results")
                
                output = gr.Textbox(
                    label="AI Medical Analysis",
                    lines=20,
                    show_copy_button=True,
                    placeholder="Upload a medical image and ask a clinical question..." if model_loaded else "Model unavailable - please check system status"
                )
        
        # Examples
        if model_loaded:
            with gr.Accordion("πŸ“š Example Cases", open=False):
                examples = gr.Examples(
                    examples=[
                        [
                            "https://upload.wikimedia.org/wikipedia/commons/c/c8/Chest_Xray_PA_3-8-2010.png",
                            "Analyze this chest X-ray systematically. Comment on heart size, lung fields, and any abnormalities.",
                            "Adult patient with respiratory symptoms"
                        ]
                    ],
                    inputs=[image_input, clinical_question, patient_history]
                )
        
        # 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=lambda: (None, "", "", ""),
            outputs=[image_input, clinical_question, patient_history, output]
        )
        
        # Footer
        gr.Markdown("""
        ---
        ### πŸ”¬ About MedGemma
        
        MedGemma-4B is Google's specialized medical AI model requiring transformers >= 4.52.0.
        
        ### πŸ”’ Privacy & Ethics
        - Real-time processing, no data storage
        - Educational and research purposes only
        - No patient data should be uploaded
        
        **Model:** Google MedGemma-4B | **License:** Apache 2.0
        """)
    
    return demo

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