MohammedAH commited on
Commit
b08c4c2
·
verified ·
1 Parent(s): 93bffb4

Create app.py

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