Update app.py
Browse files
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
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
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 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
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 |
-
|
|
|
37 |
|
38 |
-
#
|
39 |
-
interface = gr.Interface(
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
#
|
46 |
-
|
|
|
|
|
|
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()
|