Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,30 +2,28 @@ import gradio as gr
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
-
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
model = tf.keras.models.load_model(model_path)
|
| 10 |
|
| 11 |
# Class labels
|
| 12 |
class_names = ['Glioma Tumor', 'Meningioma Tumor', 'No Tumor', 'Pituitary Tumor']
|
| 13 |
|
| 14 |
-
#
|
| 15 |
def preprocess_image(image):
|
| 16 |
image = image.resize((224, 224))
|
| 17 |
image = np.array(image) / 255.0
|
| 18 |
if image.shape[-1] == 4:
|
| 19 |
-
image = image[..., :3]
|
| 20 |
image = np.expand_dims(image, axis=0)
|
| 21 |
return image
|
| 22 |
|
| 23 |
-
# Prediction
|
| 24 |
def predict(image):
|
| 25 |
img = preprocess_image(image)
|
| 26 |
-
|
| 27 |
-
predicted_class = class_names[np.argmax(
|
| 28 |
-
confidence = np.max(
|
| 29 |
return f"Predicted Tumor Type: {predicted_class} (Confidence: {confidence:.2f})"
|
| 30 |
|
| 31 |
# Gradio interface
|
|
@@ -33,8 +31,8 @@ interface = gr.Interface(
|
|
| 33 |
fn=predict,
|
| 34 |
inputs=gr.Image(type="pil"),
|
| 35 |
outputs="text",
|
| 36 |
-
title="Brain Tumor Classifier",
|
| 37 |
-
description="Upload a Brain MRI image
|
| 38 |
)
|
| 39 |
|
| 40 |
interface.launch()
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
+
# Load the model from local path (available in Space's files)
|
| 7 |
+
model = tf.keras.models.load_model("brain_tumor_model.h5")
|
|
|
|
| 8 |
|
| 9 |
# Class labels
|
| 10 |
class_names = ['Glioma Tumor', 'Meningioma Tumor', 'No Tumor', 'Pituitary Tumor']
|
| 11 |
|
| 12 |
+
# Image preprocessing
|
| 13 |
def preprocess_image(image):
|
| 14 |
image = image.resize((224, 224))
|
| 15 |
image = np.array(image) / 255.0
|
| 16 |
if image.shape[-1] == 4:
|
| 17 |
+
image = image[..., :3] # Remove alpha if present
|
| 18 |
image = np.expand_dims(image, axis=0)
|
| 19 |
return image
|
| 20 |
|
| 21 |
+
# Prediction logic
|
| 22 |
def predict(image):
|
| 23 |
img = preprocess_image(image)
|
| 24 |
+
prediction = model.predict(img)
|
| 25 |
+
predicted_class = class_names[np.argmax(prediction)]
|
| 26 |
+
confidence = np.max(prediction)
|
| 27 |
return f"Predicted Tumor Type: {predicted_class} (Confidence: {confidence:.2f})"
|
| 28 |
|
| 29 |
# Gradio interface
|
|
|
|
| 31 |
fn=predict,
|
| 32 |
inputs=gr.Image(type="pil"),
|
| 33 |
outputs="text",
|
| 34 |
+
title="🧠 Brain Tumor MRI Classifier",
|
| 35 |
+
description="Upload a Brain MRI image to predict if it is Glioma, Meningioma, Pituitary Tumor, or No Tumor."
|
| 36 |
)
|
| 37 |
|
| 38 |
interface.launch()
|