Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import tensorflow as tf
|
3 |
+
import gradio as gr
|
4 |
+
from keras.models import load_model
|
5 |
+
# Load the model
|
6 |
+
model = load_model('/content/drive/MyDrive/brain-tumor-mri/model/brain_tumor.keras')
|
7 |
+
labels = ['glioma', 'meningioma', 'notumor', 'pituitary']
|
8 |
+
def load_and_prepare_image(image_path, target_size=(224, 224)):
|
9 |
+
"""Load and prepare the image for prediction."""
|
10 |
+
img = load_img(image_path, target_size=target_size)
|
11 |
+
img_array = img_to_array(img)
|
12 |
+
img_array = np.expand_dims(img_array, axis=0)
|
13 |
+
img_array /= 255.0 # Normalize the image
|
14 |
+
return img_array
|
15 |
+
|
16 |
+
def predict(image_path):
|
17 |
+
if image_path is None:
|
18 |
+
return "Please upload an image.", "/content/drive/MyDrive/brain-tumor-mri/images/ii.PNG"
|
19 |
+
|
20 |
+
# Prepare the image
|
21 |
+
img = load_and_prepare_image(image_path)
|
22 |
+
|
23 |
+
# Make the prediction
|
24 |
+
prediction = model.predict(img)
|
25 |
+
predicted_class = np.argmax(prediction, axis=1)[0]
|
26 |
+
predicted_label = labels[predicted_class]
|
27 |
+
|
28 |
+
# Creative prediction message
|
29 |
+
prediction_message = f'This MRI scan suggests the presence of a {predicted_label}.'
|
30 |
+
|
31 |
+
return prediction_message, '/content/drive/MyDrive/brain-tumor-mri/images/ii.PNG' # Returning the image path for display
|
32 |
+
|
33 |
+
# Gradio interface setup with enhancements
|
34 |
+
iface = gr.Interface(
|
35 |
+
theme=gr.themes.Soft(),
|
36 |
+
fn=predict,
|
37 |
+
inputs=gr.Image(type="filepath", label="Upload MRI Image"),
|
38 |
+
outputs=[
|
39 |
+
gr.Textbox(label="Prediction", interactive=False, lines=2),
|
40 |
+
gr.Image(value="/content/drive/MyDrive/brain-tumor-mri/images/ii.PNG", label="Always ready to assist!")
|
41 |
+
],
|
42 |
+
title="🧠 Brain Tumor Classification",
|
43 |
+
description="Upload a brain MRI image for analysis.",
|
44 |
+
css="""
|
45 |
+
.gradio-container {
|
46 |
+
font-family: 'Arial', sans-serif;
|
47 |
+
background-color: #ddebf7;
|
48 |
+
border-radius: 10px;
|
49 |
+
padding: 20px;
|
50 |
+
}
|
51 |
+
.gr-button {
|
52 |
+
background-color: #4CAF50;
|
53 |
+
color: white;
|
54 |
+
}
|
55 |
+
.gr-image {
|
56 |
+
border: 2px solid #ddd;
|
57 |
+
border-radius: 10px;
|
58 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
59 |
+
}
|
60 |
+
.output-textbox {
|
61 |
+
font-size: 1.2em;
|
62 |
+
text-align: center;
|
63 |
+
color: #333;
|
64 |
+
}
|
65 |
+
""",
|
66 |
+
examples=[["/content/drive/MyDrive/brain-tumor-mri/images/Te-noTr_0002.jpg"], ["/content/drive/MyDrive/brain-tumor-mri/images/Te-meTr_0009.jpg"]],
|
67 |
+
)
|
68 |
+
|
69 |
+
# Launch the interface
|
70 |
+
iface.launch(share=True)
|