yaraa11 commited on
Commit
b49dba5
·
verified ·
1 Parent(s): 5bfa39f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -29
app.py CHANGED
@@ -1,11 +1,10 @@
1
  import gradio as gr
2
  import numpy as np
3
- from PIL import Image
4
- import tensorflow as tf
5
  from tensorflow.keras.models import load_model
6
  from tensorflow.keras import backend as K
 
7
 
8
- # ---- Custom metrics/loss required to load your model ---- #
9
  def dice_coef(y_true, y_pred):
10
  y_true = K.cast(y_true, 'float32')
11
  y_pred = K.cast(y_pred, 'float32')
@@ -14,38 +13,31 @@ def dice_coef(y_true, y_pred):
14
  intersect = K.sum(y_true_f * y_pred_f)
15
  return (2. * intersect + K.epsilon()) / (K.sum(y_true_f) + K.sum(y_pred_f) + K.epsilon())
16
 
17
- # ---- Load model from Hugging Face model repo ---- #
18
- model_path = "CNNSegmentation_model.keras" # <-- Replace with your HF model ID
19
- model = load_model(model_path, custom_objects={'dice_coef': dice_coef}, compile=False)
20
 
21
- IMG_SIZE = (224, 224)
 
 
 
 
 
22
 
23
- # ---- Prediction function for Gradio ---- #
24
  def predict(img):
25
- # Convert to grayscale if not already
26
- img = img.convert("L")
27
-
28
- # Resize
29
- img_resized = img.resize(IMG_SIZE)
30
- x = np.array(img_resized) / 255.0
31
- x = np.expand_dims(x, axis=(0, -1)) # shape: (1, 224, 224, 1)
32
-
33
- # Predict mask
34
  pred = model.predict(x)[0]
35
- mask = (pred > 0.5).astype(np.uint8) * 255 # Convert to binary mask
36
-
37
- # Convert numpy mask to image
38
- mask_img = Image.fromarray(mask.squeeze(), mode="L")
39
- return mask_img
40
 
41
- # ---- Gradio Interface ---- #
42
  interface = gr.Interface(
43
  fn=predict,
44
- inputs=gr.Image(type="pil", label="Upload Brain MRI Image"),
45
- outputs=gr.Image(type="pil", label="Predicted Tumor Mask"),
46
- title="Brain Tumor Segmentation",
47
- description="Upload a brain MRI image to generate a tumor segmentation mask."
48
  )
49
 
50
- # interface.launch()
51
- interface.launch(server_preprocess=False, ssr_mode=False)
 
 
1
  import gradio as gr
2
  import numpy as np
 
 
3
  from tensorflow.keras.models import load_model
4
  from tensorflow.keras import backend as K
5
+ from PIL import Image
6
 
7
+ # Custom metric
8
  def dice_coef(y_true, y_pred):
9
  y_true = K.cast(y_true, 'float32')
10
  y_pred = K.cast(y_pred, 'float32')
 
13
  intersect = K.sum(y_true_f * y_pred_f)
14
  return (2. * intersect + K.epsilon()) / (K.sum(y_true_f) + K.sum(y_pred_f) + K.epsilon())
15
 
16
+ # Global variable for model
17
+ model = None
 
18
 
19
+ def get_model():
20
+ global model
21
+ if model is None:
22
+ # Load the .keras model only once when first needed
23
+ model = load_model("CNNSegmentation_model.keras", custom_objects={'dice_coef': dice_coef}, compile=False)
24
+ return model
25
 
 
26
  def predict(img):
27
+ model = get_model()
28
+ img = img.convert("L").resize((224, 224))
29
+ x = np.expand_dims(np.expand_dims(np.array(img)/255.0, -1), 0)
 
 
 
 
 
 
30
  pred = model.predict(x)[0]
31
+ mask = (pred > 0.5).astype(np.uint8) * 255
32
+ return Image.fromarray(mask.squeeze(), "L")
 
 
 
33
 
 
34
  interface = gr.Interface(
35
  fn=predict,
36
+ inputs=gr.Image(type="pil"),
37
+ outputs=gr.Image(type="pil"),
38
+ title="Brain Tumor Segmentation"
 
39
  )
40
 
41
+ # Plain launch
42
+ interface.launch()
43
+