Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,16 @@
|
|
| 1 |
-
from huggingface_hub import hf_hub_download
|
| 2 |
-
from tensorflow.keras.models import load_model
|
| 3 |
-
from tensorflow.keras.utils import img_to_array
|
| 4 |
import gradio as gr
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
filename="clean_model.keras",
|
| 11 |
-
repo_type="model"
|
| 12 |
-
)
|
| 13 |
-
model = load_model(model_path)
|
| 14 |
-
|
| 15 |
|
|
|
|
| 16 |
class_labels = ['pituitary', 'glioma', 'notumor', 'meningioma']
|
| 17 |
|
|
|
|
| 18 |
def predict_image(img):
|
| 19 |
img = img.convert("RGB")
|
| 20 |
img = img.resize((128, 128))
|
|
@@ -23,17 +19,23 @@ def predict_image(img):
|
|
| 23 |
|
| 24 |
predictions = model.predict(img_array)
|
| 25 |
idx = np.argmax(predictions)
|
| 26 |
-
|
| 27 |
|
| 28 |
label = class_labels[idx]
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
interface = gr.Interface(
|
| 34 |
fn=predict_image,
|
| 35 |
-
inputs=gr.Image(type="pil", label="Upload MRI"),
|
| 36 |
-
outputs="
|
| 37 |
-
title="Brain Tumor Detection"
|
|
|
|
| 38 |
)
|
|
|
|
| 39 |
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
from tensorflow.keras.utils import img_to_array
|
| 6 |
|
| 7 |
+
# ✅ Direct load since model file is in same directory
|
| 8 |
+
model = load_model("clean_model.keras")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# ✅ Class labels as per your training
|
| 11 |
class_labels = ['pituitary', 'glioma', 'notumor', 'meningioma']
|
| 12 |
|
| 13 |
+
# ✅ Prediction function
|
| 14 |
def predict_image(img):
|
| 15 |
img = img.convert("RGB")
|
| 16 |
img = img.resize((128, 128))
|
|
|
|
| 19 |
|
| 20 |
predictions = model.predict(img_array)
|
| 21 |
idx = np.argmax(predictions)
|
| 22 |
+
confidence = np.max(predictions)
|
| 23 |
|
| 24 |
label = class_labels[idx]
|
| 25 |
+
if label == 'notumor':
|
| 26 |
+
result = "🟢 No Tumor Detected"
|
| 27 |
+
else:
|
| 28 |
+
result = f"🔴 Tumor Type: {label.capitalize()}"
|
| 29 |
|
| 30 |
+
return f"{result}\nConfidence: {confidence * 100:.2f}%"
|
| 31 |
+
|
| 32 |
+
# ✅ Gradio Interface
|
| 33 |
interface = gr.Interface(
|
| 34 |
fn=predict_image,
|
| 35 |
+
inputs=gr.Image(type="pil", label="Upload MRI Image"),
|
| 36 |
+
outputs=gr.Textbox(label="Prediction Result"),
|
| 37 |
+
title="🧠 Brain Tumor Detection",
|
| 38 |
+
description="Upload an MRI scan to detect brain tumor type using a trained CNN model."
|
| 39 |
)
|
| 40 |
+
|
| 41 |
interface.launch()
|