kkhushisaid commited on
Commit
1d9d109
·
verified ·
1 Parent(s): a169f01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -38
app.py CHANGED
@@ -1,46 +1,39 @@
1
- import tensorflow as tf
2
  import gradio as gr
3
- from tensorflow.keras.models import load_model
4
  import numpy as np
5
- from tensorflow.keras.preprocessing import image
 
6
 
7
- # ---- Define custom metrics ----
8
- def recall(y_true, y_pred):
9
- true_positives = tf.keras.backend.sum(tf.keras.backend.round(tf.keras.backend.clip(y_true * y_pred, 0, 1)))
10
- possible_positives = tf.keras.backend.sum(tf.keras.backend.round(tf.keras.backend.clip(y_true, 0, 1)))
11
- recall = true_positives / (possible_positives + tf.keras.backend.epsilon())
12
- return recall
13
 
14
- def precision(y_true, y_pred):
15
- true_positives = tf.keras.backend.sum(tf.keras.backend.round(tf.keras.backend.clip(y_true * y_pred, 0, 1)))
16
- predicted_positives = tf.keras.backend.sum(tf.keras.backend.round(tf.keras.backend.clip(y_pred, 0, 1)))
17
- precision = true_positives / (predicted_positives + tf.keras.backend.epsilon())
18
- return precision
 
 
 
19
 
20
- def f1(y_true, y_pred):
21
- p = precision(y_true, y_pred)
22
- r = recall(y_true, y_pred)
23
- return 2 * ((p * r) / (p + r + tf.keras.backend.epsilon()))
24
-
25
- # ---- Load the model ----
26
- model = load_model("pneumonia_cnn_model.h5", custom_objects={'f1': f1, 'precision': precision, 'recall': recall})
27
-
28
- # ---- Define prediction function ----
29
- def predict_image(img):
30
- img = img.resize((150, 150)) # Make sure this matches your training image size
31
- img = image.img_to_array(img)
32
- img = np.expand_dims(img, axis=0)
33
- img = img / 255.0
34
- prediction = model.predict(img)[0][0]
35
  label = "Pneumonia" if prediction > 0.5 else "Normal"
36
- return f"Prediction: {label} ({prediction:.2f})"
 
37
 
38
- # ---- Create Gradio Interface ----
39
- interface = gr.Interface(fn=predict_image,
40
- inputs=gr.Image(type="pil"),
41
- outputs="text",
42
- title="Pneumonia Detection",
43
- description="Upload a chest X-ray image to predict if it shows signs of pneumonia.")
 
 
 
 
44
 
45
- # ---- Launch ----
46
- interface.launch()
 
 
 
1
  import gradio as gr
 
2
  import numpy as np
3
+ from keras.models import load_model
4
+ from PIL import Image
5
 
6
+ # Load the trained model (no custom_objects needed if using built-in metrics only)
7
+ model = load_model("pneumonia_cnn_model.h5")
 
 
 
 
8
 
9
+ # Preprocessing function for uploaded X-ray images
10
+ def preprocess_image(image):
11
+ image = image.convert("L") # Convert to grayscale
12
+ image = image.resize((150, 150)) # Resize to model's expected input
13
+ image_array = np.array(image) / 255.0 # Normalize pixel values
14
+ image_array = np.expand_dims(image_array, axis=-1) # Add channel dimension
15
+ image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
16
+ return image_array
17
 
18
+ # Prediction function
19
+ def predict(image):
20
+ image_array = preprocess_image(image)
21
+ prediction = model.predict(image_array)[0][0]
 
 
 
 
 
 
 
 
 
 
 
22
  label = "Pneumonia" if prediction > 0.5 else "Normal"
23
+ confidence = prediction if prediction > 0.5 else 1 - prediction
24
+ return f"{label} ({confidence * 100:.2f}% confidence)"
25
 
26
+ # Gradio interface
27
+ interface = gr.Interface(
28
+ fn=predict,
29
+ inputs=gr.Image(type="pil"),
30
+ outputs="text",
31
+ title="Pneumonia Detection from Chest X-rays",
32
+ description="Upload a chest X-ray image to detect if the person has pneumonia.",
33
+ theme="huggingface",
34
+ allow_flagging="never"
35
+ )
36
 
37
+ # Launch the app
38
+ if __name__ == "__main__":
39
+ interface.launch()