ATllll's picture
Update app.py
f361624 verified
import streamlit as st
import tensorflow as tf
from PIL import Image
import numpy as np
import os
# Load the saved model
@st.cache_resource
def load_model():
model = tf.keras.models.load_model("pneumonia_cnn_model.keras")
return model
model = load_model()
st.title("🫁 Pneumonia Detection from Chest X-ray Images")
st.markdown("Upload your own X-ray or try one of the sample images below.")
# === Sample Image Section ===
sample_images = {
"Choose a sample image": None,
"🧍 Normal Sample": "normal.jpeg",
"πŸ€’ Pneumonia Sample": "pneumonia.jpeg"
}
selected_sample = st.selectbox("πŸ“‚ Select a sample image", list(sample_images.keys()))
uploaded_file = st.file_uploader("πŸ“ Or upload a chest X-ray image...", type=["jpg", "jpeg", "png"])
# Determine which image to use
if selected_sample != "Choose a sample image":
image_path = sample_images[selected_sample]
image = Image.open(image_path).convert("RGB")
st.image(image, caption=f'πŸ–ΌοΈ {selected_sample}', use_column_width=True)
elif uploaded_file is not None:
image = Image.open(uploaded_file).convert("RGB")
st.image(image, caption='πŸ–ΌοΈ Uploaded Image', use_column_width=True)
else:
image = None
# === Predict Button ===
if image and st.button('πŸ” Predict'):
img = image.resize((150, 150))
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
prediction = model.predict(img_array)[0][0]
# Set thresholds to determine confidence
if prediction < 0.3:
st.success("βœ… **Prediction: Normal**")
elif prediction > 0.7:
st.error("🩺 **Prediction: Pneumonia Detected**")
else:
st.warning("⚠️ Unable to confidently detect pneumonia. Please upload a clear chest X-ray image.")