File size: 1,422 Bytes
b08c4c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import gradio as gr
import numpy as np
import cv2
from tensorflow.keras.models import load_model
from huggingface_hub import hf_hub_download

# Load models from your repos
classification_model_path = hf_hub_download(
    repo_id="MohammedAH/Brrain-MRI-Classification",
    filename="brain_mri.h5"
)

segmentation_model_path = hf_hub_download(
    repo_id="MohammedAH/Unet-Brain-Segmentation",
    filename="Unet_model.h5"
)

classification_model = load_model(classification_model_path, compile=False)
segmentation_model = load_model(segmentation_model_path, compile=False)

class_names = ['glioma', 'meningioma', 'no_tumor', 'pituitary']


def predict(image):
    # Classification
    img = cv2.resize(image, (224, 224)) / 255.0
    cls_input = np.expand_dims(img, axis=0)

    preds = classification_model.predict(cls_input)
    idx = int(np.argmax(preds[0]))

    # Segmentation
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    seg = cv2.resize(gray, (128, 128)) / 255.0
    seg_input = np.expand_dims(seg, axis=(0, -1))

    mask = segmentation_model.predict(seg_input)
    mask = (mask > 0.5).astype(np.uint8)[0, :, :, 0]

    return {
        "prediction": class_names[idx],
        "confidence": float(preds[0][idx]),
        "mask": mask.tolist()
    }


interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="numpy"),
    outputs=gr.JSON()   # 🔥 important (API-friendly)
)

interface.launch()