Mrhuman1's picture
Update app.py
f807de7 verified
raw
history blame
1.16 kB
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
# Load the model from local path (available in Space's files)
model = tf.keras.models.load_model("brain_tumor_model.h5")
# Class labels
class_names = ['Glioma Tumor', 'Meningioma Tumor', 'No Tumor', 'Pituitary Tumor']
# Image preprocessing
def preprocess_image(image):
image = image.resize((224, 224))
image = np.array(image) / 255.0
if image.shape[-1] == 4:
image = image[..., :3] # Remove alpha if present
image = np.expand_dims(image, axis=0)
return image
# Prediction logic
def predict(image):
img = preprocess_image(image)
prediction = model.predict(img)
predicted_class = class_names[np.argmax(prediction)]
confidence = np.max(prediction)
return f"Predicted Tumor Type: {predicted_class} (Confidence: {confidence:.2f})"
# Gradio interface
interface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs="text",
title="🧠 Brain Tumor MRI Classifier",
description="Upload a Brain MRI image to predict if it is Glioma, Meningioma, Pituitary Tumor, or No Tumor."
)
interface.launch()