Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,41 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import numpy as np
|
3 |
-
from tensorflow.keras.models import load_model
|
4 |
-
from PIL import Image
|
5 |
-
import traceback
|
6 |
|
7 |
-
# Load the
|
8 |
-
|
9 |
-
model = load_model("pneumonia_cnn_model.h5", compile=False)
|
10 |
-
print("β
Model loaded successfully!")
|
11 |
-
except Exception as e:
|
12 |
-
print("β Failed to load model:", e)
|
13 |
-
raise
|
14 |
-
|
15 |
-
# Preprocessing function for uploaded X-ray images
|
16 |
-
def preprocess_image(image):
|
17 |
-
image = image.convert("L") # Convert to grayscale
|
18 |
-
image = image.resize((150, 150)) # Resize to model's expected input
|
19 |
-
image_array = np.array(image) / 255.0 # Normalize pixel values
|
20 |
-
image_array = np.expand_dims(image_array, axis=-1) # Add channel dimension
|
21 |
-
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
22 |
-
print("π· Preprocessed image shape:", image_array.shape)
|
23 |
-
return image_array
|
24 |
|
25 |
# Prediction function
|
26 |
def predict(image):
|
27 |
try:
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
except Exception as e:
|
34 |
-
traceback_str = traceback.format_exc()
|
35 |
-
print("β Prediction error:\n", traceback_str)
|
36 |
return f"Error during prediction: {str(e)}"
|
37 |
|
38 |
-
# Gradio interface
|
39 |
-
|
40 |
-
fn=predict,
|
41 |
-
inputs=gr.Image(type="pil", label="Upload Chest X-ray"),
|
42 |
-
outputs=
|
43 |
-
|
44 |
-
description="Upload a chest X-ray image to detect if the person has pneumonia using a deep learning model.",
|
45 |
-
theme="default",
|
46 |
-
allow_flagging="never"
|
47 |
)
|
48 |
|
49 |
-
# Launch
|
50 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from PIL import Image, ImageOps
|
4 |
import numpy as np
|
|
|
|
|
|
|
5 |
|
6 |
+
# Load the model
|
7 |
+
model = tf.keras.models.load_model("pneumonia_cnn_model.h5")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Prediction function
|
10 |
def predict(image):
|
11 |
try:
|
12 |
+
# Convert to grayscale
|
13 |
+
img = ImageOps.grayscale(image)
|
14 |
+
|
15 |
+
# Resize image to match the model's expected input size (280x280)
|
16 |
+
img = img.resize((280, 280)) # Adjusted the size to 280x280
|
17 |
+
|
18 |
+
# Convert to numpy array and normalize the pixel values
|
19 |
+
img_array = np.array(img).reshape(1, 280, 280, 1) / 255.0
|
20 |
+
|
21 |
+
# Make prediction
|
22 |
+
prediction = model.predict(img_array)
|
23 |
+
|
24 |
+
# Interpret prediction
|
25 |
+
if prediction >= 0.5:
|
26 |
+
return "Pneumonia detected"
|
27 |
+
else:
|
28 |
+
return "No pneumonia detected"
|
29 |
except Exception as e:
|
|
|
|
|
30 |
return f"Error during prediction: {str(e)}"
|
31 |
|
32 |
+
# Create the Gradio interface
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=predict, # Function to call on image input
|
35 |
+
inputs=gr.Image(type="pil", label="Upload Chest X-ray Image"),
|
36 |
+
outputs="text", # Output is the prediction result (text)
|
37 |
+
live=True # Optional: set to False if you don't want to update results live
|
|
|
|
|
|
|
38 |
)
|
39 |
|
40 |
+
# Launch the app
|
41 |
+
iface.launch()
|