import gradio as gr import numpy as np from PIL import Image import tensorflow as tf import os # Load only brain tumor model for now def load_brain_model(): try: brain_model = tf.keras.models.load_model('models/brain_best.keras') print("✅ Brain tumor model loaded successfully!") return brain_model except Exception as e: print(f"❌ Error loading brain tumor model: {e}") return None brain_model = load_brain_model() def preprocess_image(image): if image is None: return None # Convert to RGB if needed if image.mode != 'RGB': image = image.convert('RGB') # Resize to model input size image = image.resize((224, 224)) img_array = np.array(image) / 255.0 img_array = np.expand_dims(img_array, axis=0) return img_array def predict_cancer(cancer_type, image): # This function will only be called for brain tumor detection # since the button is hidden for other cancer types if brain_model is None: return "❌ Brain tumor model not loaded properly. Please check model files." if image is None: return "⚠️ Please upload an MRI scan first." if cancer_type == "Brain Tumor Detection": try: processed_image = preprocess_image(image) if processed_image is None: return "❌ Error processing image." prediction = brain_model.predict(processed_image, verbose=0)[0][0] confidence = prediction * 100 if prediction > 0.5: result = f"🔴 **Brain Tumor Detected**\nConfidence: {confidence:.2f}%" else: result = f"🟢 **No Brain Tumor Detected**\nConfidence: {100 - confidence:.2f}%" return result except Exception as e: return f"❌ Error during prediction: {str(e)}" else: return "❌ This function should only be called for brain tumor detection." def update_info(cancer_type): info_text = { "Brain Tumor Detection": """🧠 **Brain Tumor Detection** **What to upload:** MRI scan images of the brain **Supported formats:** JPG, PNG, JPEG **Model type:** Binary classification (Tumor/No Tumor) **Status:** ✅ Active and ready to use **Note:** This model detects the presence of brain tumors in MRI scans. Early detection is crucial for effective treatment.""", "Breast Cancer Detection": """🎗️ **Breast Cancer Detection** **Status:** 🚧 Under Development **What it will do:** Analyze mammography or histopathology images **Future features:** Binary classification (Malignant/Benign) **Note:** This feature is currently being developed. We're working hard to bring you accurate breast cancer detection soon! For now, please use the Brain Tumor Detection feature.""", "Skin Cancer Detection": """🔍 **Skin Cancer Detection** **Status:** 🚧 Under Development **What it will do:** Analyze dermoscopy or clinical images of skin lesions **Future features:** Multi-class classification for various skin conditions **Note:** This feature is currently being developed. We're working on accurate skin cancer detection capabilities! For now, please use the Brain Tumor Detection feature.""" } return info_text.get(cancer_type, "Please select a cancer type.") def update_interface_visibility(cancer_type): """Update visibility of upload components based on cancer type""" if cancer_type == "Brain Tumor Detection": return [ gr.update(visible=True), # image_input gr.update(visible=True), # predict_btn gr.update(value="Upload an MRI scan and click 'Analyze Image' to detect brain tumors.") # result_output ] else: development_message = f"🚧 **{cancer_type}**\n\n🔧 This feature is currently under development.\nWe're working hard to bring you this functionality soon!\n\nFor now, please use the Brain Tumor Detection feature." return [ gr.update(visible=False), # image_input gr.update(visible=False), # predict_btn gr.update(value=development_message) # result_output ] def create_interface(): with gr.Blocks( theme=gr.themes.Soft(), title="🩺 Cancer Detection System" ) as app: gr.HTML("""
"Early Detection Saves Lives"
🚨 Remember: This is an AI assistant for educational purposes. For actual medical concerns, please consult healthcare professionals.
Built with ❤️ using Gradio and TensorFlow