File size: 1,661 Bytes
efdad1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd992bf
efdad1a
 
 
 
 
fd992bf
efdad1a
fd992bf
efdad1a
 
 
fd992bf
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image

# Load the trained model
model = tf.keras.models.load_model("Best_Model_On_Partial.keras")

# Class labels
class_labels = ['Glioma', 'Meningioma', 'No Tumor', 'Pituitary']

# Image preprocessing function
def preprocess_image(image):
    image = image.convert("RGB")
    image = image.resize((224, 224))
    image = np.array(image) / 255.0  # Normalize
    image = np.expand_dims(image, axis=0)  # Add batch dimension
    return image

# Prediction function
def predict(image):
    processed_image = preprocess_image(image)
    prediction = model.predict(processed_image)
    predicted_class = np.argmax(prediction)
    confidence = np.max(prediction) * 100
    
    return f"🧠 Prediction: {class_labels[predicted_class]} (Confidence: {confidence:.2f}%)"

# Customizing Gradio UI
custom_css = """
    body {background-color: #1A1F3B; color: #E0E0E0; font-family: Arial, sans-serif;}
    .gradio-container {max-width: 800px; margin: auto; text-align: center;}
    .gr-button {background-color: #007BFF !important; color: white !important; border-radius: 8px;}
    .gr-box {background-color: #2C3E50; padding: 10px; border-radius: 10px;}
"""

# Final Gradio interface
interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload Brain MRI"),
    outputs=gr.Textbox(label="Prediction"),
    title="Brain Tumor Detection 🧠",
    description="Upload an MRI scan to classify brain tumors into Glioma, Meningioma, Pituitary, or No Tumor.",
    theme="default",
    css=custom_css
)

# Launch the app
if __name__ == "__main__":
    interface.launch()