File size: 1,159 Bytes
a5a0035
 
 
 
 
f807de7
 
a5a0035
 
 
 
f807de7
a5a0035
 
 
 
f807de7
a5a0035
 
 
f807de7
a5a0035
 
f807de7
 
 
a5a0035
 
 
 
 
 
 
f807de7
 
a5a0035
 
 
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
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()