# app.py - Medical AI using LLaVA (Large Language and Vision Assistant) import gradio as gr import torch from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration from PIL import Image import logging from collections import defaultdict, Counter import time # 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=30): 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() # Model configuration - Using LLaVA-Next (latest version) MODEL_ID = "llava-hf/llava-v1.6-mistral-7b-hf" # Global variables model = None processor = None def load_llava(): """Load LLaVA model for medical analysis""" global model, processor try: logger.info(f"Loading LLaVA model: {MODEL_ID}") # Load processor processor = LlavaNextProcessor.from_pretrained(MODEL_ID) # Load model with appropriate settings model = LlavaNextForConditionalGeneration.from_pretrained( MODEL_ID, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, device_map="auto" if torch.cuda.is_available() else None, low_cpu_mem_usage=True ) logger.info("✅ LLaVA model loaded successfully!") return True except Exception as e: logger.error(f"❌ Error loading LLaVA: {str(e)}") return False # Load model at startup llava_ready = load_llava() def analyze_medical_image_llava(image, clinical_question, patient_history=""): """Analyze medical image using LLaVA""" 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 llava_ready or model is None: usage_tracker.log_analysis(False, time.time() - start_time) return "❌ LLaVA model not loaded. Please refresh the page and wait for model loading." 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 LLaVA medical analysis...") # Prepare comprehensive medical prompt medical_prompt = f"""You are a highly skilled medical AI assistant with expertise in medical image analysis. You have extensive knowledge in: - **Radiology**: X-rays, CT scans, MRI, ultrasound interpretation - **Pathology**: Histological analysis, tissue examination, cellular patterns - **Dermatology**: Skin lesions, rashes, dermatological conditions - **Ophthalmology**: Retinal imaging, eye examinations, ocular pathology - **General Medical Imaging**: Cross-sectional anatomy, normal variants, pathological findings **Patient Information:** {f"Patient History: {patient_history}" if patient_history.strip() else "No specific patient history provided"} **Clinical Question:** {clinical_question} **Instructions:** Please provide a comprehensive medical analysis of this image following this structure: 1. **IMAGE QUALITY ASSESSMENT** - Technical adequacy of the image - Any artifacts or limitations - Overall diagnostic quality 2. **SYSTEMATIC OBSERVATION** - Describe what you see in detail - Identify anatomical structures visible - Note any normal findings 3. **ABNORMAL FINDINGS** - Identify any pathological changes - Describe abnormalities in detail - Note their location and characteristics 4. **CLINICAL SIGNIFICANCE** - Explain the importance of findings - Relate to potential diagnoses - Discuss clinical implications 5. **DIFFERENTIAL DIAGNOSIS** - List possible conditions - Explain reasoning for each - Prioritize based on imaging findings 6. **RECOMMENDATIONS** - Suggest additional imaging if needed - Recommend clinical correlation - Advise on follow-up or further evaluation Please be thorough, educational, and professional in your analysis. Always emphasize that this is for educational purposes and requires professional medical validation.""" # Prepare conversation for LLaVA conversation = [ { "role": "user", "content": [ {"type": "text", "text": medical_prompt}, {"type": "image", "image": image} ] } ] # Apply chat template prompt = processor.apply_chat_template(conversation, add_generation_prompt=True) # Process inputs inputs = processor(prompt, image, return_tensors='pt') # Move to appropriate device if torch.cuda.is_available() and hasattr(model, 'device'): inputs = {k: v.to(model.device) for k, v in inputs.items()} # Generate response logger.info("Generating comprehensive medical analysis...") with torch.inference_mode(): output = model.generate( **inputs, max_new_tokens=2000, do_sample=True, temperature=0.2, # Lower temperature for more focused medical analysis top_p=0.9, repetition_penalty=1.1, pad_token_id=processor.tokenizer.eos_token_id ) # Decode response generated_text = processor.decode(output[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True) # Clean up response response = generated_text.strip() # Format the response with medical structure formatted_response = f"""# 🏥 **LLaVA Medical Image Analysis** ## **Clinical Question:** {clinical_question} {f"## **Patient History:** {patient_history}" if patient_history.strip() else ""} --- ## 🔍 **Comprehensive Medical Analysis** {response} --- ## 📋 **Summary and Clinical Correlation** **Key Points:** - This analysis provides a systematic approach to medical image interpretation - All findings should be correlated with clinical presentation and patient history - The AI assessment serves as an educational tool and decision support aid **Clinical Workflow:** 1. **Review** the systematic analysis above 2. **Correlate** findings with patient symptoms and history 3. **Consult** with appropriate medical specialists as needed 4. **Document** findings in the patient's medical record 5. **Follow up** with recommended additional studies if indicated **Educational Value:** This analysis demonstrates structured medical image interpretation methodology and clinical reasoning processes used in healthcare settings. """ # Add comprehensive medical disclaimer disclaimer = """ --- ## ⚠️ **IMPORTANT MEDICAL DISCLAIMER** **FOR EDUCATIONAL AND RESEARCH PURPOSES ONLY** - **Not a Medical Diagnosis**: This AI analysis does not constitute a medical diagnosis, treatment recommendation, or professional medical advice - **Professional Review Required**: All findings must be validated by qualified healthcare professionals - **Emergency Situations**: For urgent medical concerns, contact emergency services immediately (911 in US) - **Clinical Correlation**: AI findings must be correlated with clinical examination and patient history - **Liability**: This system is not intended for clinical decision-making and users assume all responsibility - **Educational Tool**: Designed for medical education, training, and research applications only - **Data Privacy**: Do not upload images containing patient identifiable information **Always consult qualified healthcare professionals for medical diagnosis and treatment decisions.** --- **Powered by**: LLaVA (Large Language and Vision Assistant) | **Model**: {MODEL_ID} """ # Log successful analysis duration = time.time() - start_time question_type = classify_question(clinical_question) usage_tracker.log_analysis(True, duration, question_type) logger.info("✅ LLaVA medical analysis completed successfully") return formatted_response + disclaimer except Exception as e: duration = time.time() - start_time usage_tracker.log_analysis(False, duration) logger.error(f"❌ LLaVA analysis error: {str(e)}") if "memory" in str(e).lower() or "cuda" in str(e).lower(): return "❌ **Memory Error**: The model requires more memory. Try using a smaller image or upgrading to GPU hardware." else: return f"❌ **Analysis Failed**: {str(e)}\n\nPlease try again with a different image or contact support if the issue persists." 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']): return 'descriptive' elif any(word in question_lower for word in ['diagnosis', 'differential', 'condition', 'disease']): return 'diagnostic' elif any(word in question_lower for word in ['abnormal', 'pathology', 'lesion', 'mass']): return 'pathological' elif any(word in question_lower for word in ['analyze', 'assess', 'evaluate', 'review']): return 'analytical' else: return 'general' def get_usage_stats(): """Get comprehensive 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"""📊 **LLaVA Medical AI Usage Statistics** **Performance Metrics:** - **Total Analyses**: {stats['total_analyses']} - **Success Rate**: {success_rate:.1f}% - **Average Processing Time**: {stats['average_processing_time']:.2f} seconds - **Failed Analyses**: {stats['failed_analyses']} **Question Type Distribution:** {chr(10).join([f"- **{qtype.title()}**: {count} ({count/stats['total_analyses']*100:.1f}%)" for qtype, count in stats['question_types'].most_common()])} **System Information:** - **Model**: LLaVA-v1.6-Mistral-7B - **Capabilities**: Medical image analysis and clinical reasoning - **Device**: {'GPU' if torch.cuda.is_available() else 'CPU'} - **Status**: {'🟢 Operational' if llava_ready else '🔴 Offline'} """ # Create comprehensive Gradio interface def create_interface(): with gr.Blocks( title="LLaVA Medical Image 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; margin: 16px 0; } .warning { background-color: #fffbeb; border: 1px solid #fed7aa; border-radius: 8px; padding: 16px; margin: 16px 0; } """ ) as demo: # Header gr.Markdown(""" # 🏥 LLaVA Medical Image Analysis **Advanced Medical AI powered by LLaVA (Large Language and Vision Assistant)** **Specialized Medical Capabilities:** 🫁 **Radiology** • 🔬 **Pathology** • 🩺 **Dermatology** • 👁️ **Ophthalmology** • 🧠 **Clinical Reasoning** """) # Status display if llava_ready: gr.Markdown("""