ma4389's picture
Upload 3 files
be50dc6 verified
import gradio as gr
import tensorflow as tf
import numpy as np
from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
# Load the trained model
model = tf.keras.models.load_model("best_model.h5")
# Original class names from directory
class_names = ["no", "yes"]
# Mapping to user-friendly labels
label_mapping = {
"no": "No Tumor",
"yes": "Yes, that's a Brain Tumor"
}
# Prediction function
def predict(image):
image = image.resize((224, 224))
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
image = preprocess_input(image)
preds = model.predict(image)[0]
label_idx = np.argmax(preds)
raw_label = class_names[label_idx]
readable_label = label_mapping[raw_label]
confidence = float(preds[label_idx])
return {readable_label: confidence}
# Gradio Interface
interface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=2),
title="🧠 Brain Tumor Detection",
description="Upload an MRI image to detect if it has a brain tumor using InceptionV3."
)
if __name__ == "__main__":
interface.launch()