File size: 2,189 Bytes
f87715d
 
 
 
45c4d09
f87715d
36b1996
f87715d
 
 
 
 
 
 
 
 
 
 
36b1996
f87715d
 
 
 
 
 
 
 
 
 
 
 
36b1996
f87715d
 
 
 
 
 
 
 
36b1996
f87715d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36b1996
f87715d
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
import tensorflow as tf
import gradio as gr
from keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
# Load the model
model = load_model('brain_tumor.keras')
labels = ['glioma', 'meningioma', 'notumor', 'pituitary']
def load_and_prepare_image(image_path, target_size=(224, 224)):
    """Load and prepare the image for prediction."""
    img = load_img(image_path, target_size=target_size)
    img_array = img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array /= 255.0  # Normalize the image
    return img_array

def predict(image_path):
    if image_path is None:
        return "Please upload an image.", "ii.PNG"

    # Prepare the image
    img = load_and_prepare_image(image_path)

    # Make the prediction
    prediction = model.predict(img)
    predicted_class = np.argmax(prediction, axis=1)[0]
    predicted_label = labels[predicted_class]

    # Creative prediction message
    prediction_message = f'This MRI scan suggests the presence of a {predicted_label}.'

    return prediction_message, 'ii.PNG'  # Returning the image path for display

# Gradio interface setup with enhancements
iface = gr.Interface(
    theme=gr.themes.Soft(),
    fn=predict,
    inputs=gr.Image(type="filepath", label="Upload MRI Image"),
    outputs=[
        gr.Textbox(label="Prediction", interactive=False, lines=2),
        gr.Image(value="ii.PNG", label="Always ready to assist!")
    ],
    title="🧠 Brain Tumor Classification",
    description="Upload a brain MRI image for analysis.",
    css="""
    .gradio-container {
        font-family: 'Arial', sans-serif;
        background-color: #ddebf7;
        border-radius: 10px;
        padding: 20px;
    }
    .gr-button {
        background-color: #4CAF50;
        color: white;
    }
    .gr-image {
        border: 2px solid #ddd;
        border-radius: 10px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    }
    .output-textbox {
        font-size: 1.2em;
        text-align: center;
        color: #333;
    }
    """,
    examples=[["Te-noTr_0002.jpg"], ["Te-meTr_0009.jpg"]],
)

# Launch the interface
iface.launch(share=True)