tmafantiri's picture
Update app.py
51638a9 verified
import gradio as gr
import numpy as np
from PIL import Image
from keras.models import load_model
# Load your pre-trained model (make sure the model file is in the same directory)
model = load_model('brain_tumor_model.h5')
# Function to process image and make predictions
def predict_image(image):
# Resize the image
img = image.resize((128, 128))
# Convert the image to a NumPy array
img = np.array(img)
# Check if the image has 3 color channels
if img.shape == (128, 128): # If grayscale, convert to RGB
img = np.stack((img,) * 3, axis=-1)
# Add a batch dimension
img = np.expand_dims(img, axis=0)
# Make the prediction
prediction = model.predict(img)
# Get the predicted class and confidence level
predicted_class = np.argmax(prediction)
confidence = np.max(prediction)
# Return the results
if predicted_class == 0:
return f'No tumor detected. Confidence: {confidence:.2f}'
else:
return f'Tumor detected. Confidence: {confidence:.2f}'
# Create custom CSS for background color
css = """
body {
background-color: #f0f4f7;
}
"""
# Create the Gradio interface
iface = gr.Interface(
fn=predict_image,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(),
title="Brain Tumor Detection AI App",
description="Upload a Magnetic Resonance Imaging (MRI) scan image to detect brain tumors.",
css=css, # Apply the custom background color
#theme="dark", # Apply a dark theme to the interface
flagging_options=["Incorrect Diagnosis", "Image Not Clear", "Model Error"], # Add flagging options
)
# Launch the interface
iface.launch()