Spaces:
Sleeping
Sleeping
import numpy as np | |
import tensorflow as tf | |
import gradio as gr | |
from keras.models import load_model | |
from tensorflow.keras.preprocessing.image import load_img, img_to_array | |
# Load the model | |
model = load_model('brain_tumor.keras') | |
labels = ['glioma', 'meningioma', 'notumor', 'pituitary'] | |
def load_and_prepare_image(image_path, target_size=(224, 224)): | |
"""Load and prepare the image for prediction.""" | |
img = load_img(image_path, target_size=target_size) | |
img_array = img_to_array(img) | |
img_array = np.expand_dims(img_array, axis=0) | |
img_array /= 255.0 # Normalize the image | |
return img_array | |
def predict(image_path): | |
if image_path is None: | |
return "Please upload an image.", "ii.PNG" | |
# Prepare the image | |
img = load_and_prepare_image(image_path) | |
# Make the prediction | |
prediction = model.predict(img) | |
predicted_class = np.argmax(prediction, axis=1)[0] | |
predicted_label = labels[predicted_class] | |
# Creative prediction message | |
prediction_message = f'This MRI scan suggests the presence of a {predicted_label}.' | |
return prediction_message, 'ii.PNG' # Returning the image path for display | |
# Gradio interface setup with enhancements | |
iface = gr.Interface( | |
theme=gr.themes.Soft(), | |
fn=predict, | |
inputs=gr.Image(type="filepath", label="Upload MRI Image"), | |
outputs=[ | |
gr.Textbox(label="Prediction", interactive=False, lines=2), | |
gr.Image(value="ii.PNG", label="Always ready to assist!") | |
], | |
title="🧠 Brain Tumor Classification", | |
description="Upload a brain MRI image for analysis.", | |
css=""" | |
.gradio-container { | |
font-family: 'Arial', sans-serif; | |
background-color: #ddebf7; | |
border-radius: 10px; | |
padding: 20px; | |
} | |
.gr-button { | |
background-color: #4CAF50; | |
color: white; | |
} | |
.gr-image { | |
border: 2px solid #ddd; | |
border-radius: 10px; | |
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); | |
} | |
.output-textbox { | |
font-size: 1.2em; | |
text-align: center; | |
color: #333; | |
} | |
""", | |
examples=[["Te-noTr_0002.jpg"], ["Te-meTr_0009.jpg"]], | |
) | |
# Launch the interface | |
iface.launch(share=True) |