Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
|
| 8 |
+
# 🔥 Download models from your Hugging Face repos
|
| 9 |
+
classification_model_path = hf_hub_download(
|
| 10 |
+
repo_id="MohammedAH/Brrain-MRI-Classification",
|
| 11 |
+
filename="brain_mri.h5"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
segmentation_model_path = hf_hub_download(
|
| 15 |
+
repo_id="MohammedAH/Unet-Brain-Segmentation",
|
| 16 |
+
filename="Unet_model.h5"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Load models once
|
| 20 |
+
classification_model = load_model(classification_model_path, compile=False)
|
| 21 |
+
segmentation_model = load_model(segmentation_model_path, compile=False)
|
| 22 |
+
|
| 23 |
+
class_names = ['glioma', 'meningioma', 'no_tumor', 'pituitary']
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def predict(image):
|
| 27 |
+
# Classification preprocessing
|
| 28 |
+
img = cv2.resize(image, (224, 224)) / 255.0
|
| 29 |
+
cls_input = np.expand_dims(img, axis=0)
|
| 30 |
+
|
| 31 |
+
preds = classification_model.predict(cls_input)
|
| 32 |
+
idx = int(np.argmax(preds[0]))
|
| 33 |
+
|
| 34 |
+
# Segmentation preprocessing
|
| 35 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 36 |
+
seg = cv2.resize(gray, (128, 128)) / 255.0
|
| 37 |
+
seg_input = np.expand_dims(seg, axis=(0, -1))
|
| 38 |
+
|
| 39 |
+
mask = segmentation_model.predict(seg_input)
|
| 40 |
+
mask = (mask > 0.5).astype(np.uint8)[0, :, :, 0]
|
| 41 |
+
|
| 42 |
+
return class_names[idx], float(preds[0][idx]), mask
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
interface = gr.Interface(
|
| 46 |
+
fn=predict,
|
| 47 |
+
inputs=gr.Image(type="numpy"),
|
| 48 |
+
outputs=[
|
| 49 |
+
gr.Text(label="Prediction"),
|
| 50 |
+
gr.Number(label="Confidence"),
|
| 51 |
+
gr.Image(label="Segmentation Mask")
|
| 52 |
+
]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
interface.launch()
|