kkhushisaid commited on
Commit
f8b5711
·
verified ·
1 Parent(s): 804a44e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -27
app.py CHANGED
@@ -1,36 +1,50 @@
1
  import gradio as gr
2
- import numpy as np
3
  import tensorflow as tf
4
- from tensorflow.keras.models import load_model
5
- from tensorflow.keras.preprocessing import image
 
 
 
 
 
 
 
 
6
 
7
- # Load the trained model
8
- model = load_model("pneumonia_cnn_model.h5", custom_objects={
9
- 'f1': lambda y_true, y_pred: 2*((precision(y_true, y_pred)*recall(y_true, y_pred)) / (precision(y_true, y_pred)+recall(y_true, y_pred)+tf.keras.backend.epsilon())),
10
- 'precision': precision,
11
- 'recall': recall
12
- })
13
 
14
- # Preprocessing function
 
 
 
 
 
 
 
 
 
 
 
15
  def predict_pneumonia(img):
16
- img = img.convert('L') # convert to grayscale
17
  img = img.resize((299, 299))
18
- img_array = image.img_to_array(img)
19
- img_array = img_array / 255.0
20
- img_array = np.expand_dims(img_array, axis=0) # batch dimension
21
  prediction = model.predict(img_array)[0][0]
 
 
 
22
 
23
- if prediction >= 0.5:
24
- return "PNEUMONIA 🫁 (Confidence: {:.2f}%)".format(prediction * 100)
25
- else:
26
- return "NORMAL ✅ (Confidence: {:.2f}%)".format((1 - prediction) * 100)
27
-
28
- # Gradio interface
29
- interface = gr.Interface(fn=predict_pneumonia,
30
- inputs=gr.Image(type="pil"),
31
- outputs="text",
32
- title="Pneumonia Detection from Chest X-rays",
33
- description="Upload a chest X-ray image to detect Pneumonia using a CNN model.")
34
 
35
- if __name__ == "__main__":
36
- interface.launch()
 
1
  import gradio as gr
 
2
  import tensorflow as tf
3
+ import numpy as np
4
+ from keras import backend as K
5
+ from tensorflow.keras.preprocessing.image import img_to_array
6
+ from PIL import Image
7
+
8
+ # --- Custom Metrics ---
9
+ def recall(y_true, y_pred):
10
+ true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
11
+ possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
12
+ return true_positives / (possible_positives + K.epsilon())
13
 
14
+ def precision(y_true, y_pred):
15
+ true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
16
+ predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
17
+ return true_positives / (predicted_positives + K.epsilon())
 
 
18
 
19
+ def f1(y_true, y_pred):
20
+ prec = precision(y_true, y_pred)
21
+ rec = recall(y_true, y_pred)
22
+ return 2 * ((prec * rec) / (prec + rec + K.epsilon()))
23
+
24
+ # --- Load the Model ---
25
+ model = tf.keras.models.load_model(
26
+ "pneumonia_cnn_model.h5",
27
+ custom_objects={'precision': precision, 'recall': recall, 'f1': f1}
28
+ )
29
+
30
+ # --- Prediction Function ---
31
  def predict_pneumonia(img):
32
+ img = img.convert('L') # Convert to grayscale
33
  img = img.resize((299, 299))
34
+ img_array = img_to_array(img) / 255.0
35
+ img_array = np.expand_dims(img_array, axis=0) # Shape: (1, 299, 299, 1)
 
36
  prediction = model.predict(img_array)[0][0]
37
+ label = "PNEUMONIA" if prediction >= 0.5 else "NORMAL"
38
+ confidence = prediction if prediction >= 0.5 else 1 - prediction
39
+ return f"{label} ({confidence * 100:.2f}% confidence)"
40
 
41
+ # --- Gradio Interface ---
42
+ interface = gr.Interface(
43
+ fn=predict_pneumonia,
44
+ inputs=gr.Image(type="pil"),
45
+ outputs=gr.Text(),
46
+ title="Pneumonia Detection from Chest X-ray",
47
+ description="Upload a chest X-ray image to detect Pneumonia using a CNN model trained on grayscale 299x299 images."
48
+ )
 
 
 
49
 
50
+ interface.launch()