File size: 1,249 Bytes
be50dc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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()