Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
-
from
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
# Load the trained model
|
| 8 |
-
model = tf.keras.models.load_model("BRAIINTUMORMODEL.h5")
|
| 9 |
|
| 10 |
# Define class labels
|
| 11 |
-
class_labels = ["
|
| 12 |
|
| 13 |
-
# Function to make predictions
|
| 14 |
def predict_brain_tumor(img):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
|
|
|
| 19 |
prediction = model.predict(img_array)[0]
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
for label, confidence in zip(class_labels, prediction):
|
| 24 |
-
output_str += f"{label}\n{'‾'*50}\n{confidence*100:.0f}%\n\n\n"
|
| 25 |
-
|
| 26 |
-
return output_str
|
| 27 |
|
| 28 |
-
# Example images
|
| 29 |
example_images = [
|
| 30 |
["example_glioma.jpg"],
|
| 31 |
["example_meningioma.jpeg"],
|
|
@@ -37,11 +39,10 @@ example_images = [
|
|
| 37 |
iface = gr.Interface(
|
| 38 |
fn=predict_brain_tumor,
|
| 39 |
inputs=gr.Image(type="pil"),
|
| 40 |
-
outputs=gr.Textbox(label="
|
| 41 |
title="🧠 Brain Tumor Classification",
|
| 42 |
-
description="Upload an MRI image to classify
|
| 43 |
examples=example_images
|
| 44 |
)
|
| 45 |
|
| 46 |
-
# Launch Gradio App
|
| 47 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
+
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
# Load the trained model
|
| 7 |
+
model = tf.keras.models.load_model("BRAIINTUMORMODEL.h5")
|
| 8 |
|
| 9 |
# Define class labels
|
| 10 |
+
class_labels = ["Glioma", "Meningioma", "No Tumor", "Pituitary"]
|
| 11 |
|
|
|
|
| 12 |
def predict_brain_tumor(img):
|
| 13 |
+
# Convert to RGB if needed
|
| 14 |
+
if img.mode != 'RGB':
|
| 15 |
+
img = img.convert('RGB')
|
| 16 |
+
|
| 17 |
+
# Preprocess image
|
| 18 |
+
img = img.resize((224, 224))
|
| 19 |
+
img_array = np.array(img) / 255.0
|
| 20 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 21 |
|
| 22 |
+
# Make prediction
|
| 23 |
prediction = model.predict(img_array)[0]
|
| 24 |
+
predicted_class = np.argmax(prediction)
|
| 25 |
+
confidence = np.max(prediction) * 100
|
| 26 |
|
| 27 |
+
# Format output
|
| 28 |
+
return f"Predicted Tumor Type: {class_labels[predicted_class]} (Confidence: {confidence:.2f}%)"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Example images
|
| 31 |
example_images = [
|
| 32 |
["example_glioma.jpg"],
|
| 33 |
["example_meningioma.jpeg"],
|
|
|
|
| 39 |
iface = gr.Interface(
|
| 40 |
fn=predict_brain_tumor,
|
| 41 |
inputs=gr.Image(type="pil"),
|
| 42 |
+
outputs=gr.Textbox(label="Prediction Result"),
|
| 43 |
title="🧠 Brain Tumor Classification",
|
| 44 |
+
description="Upload an MRI image to classify the tumor type. You can also click on the example images below.",
|
| 45 |
examples=example_images
|
| 46 |
)
|
| 47 |
|
|
|
|
| 48 |
iface.launch()
|