|
|
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
|
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model("best_model.h5")
|
|
|
|
|
|
|
|
|
class_names = ["no", "yes"]
|
|
|
|
|
|
|
|
|
label_mapping = {
|
|
|
"no": "No Tumor",
|
|
|
"yes": "Yes, that's a Brain Tumor"
|
|
|
}
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
|
|
|
|
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()
|
|
|
|