Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Load the saved model
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_model():
|
| 10 |
+
model = tf.keras.models.load_model("pneumonia_cnn_model.keras") # Use relative path for deployment
|
| 11 |
+
return model
|
| 12 |
+
|
| 13 |
+
model = load_model()
|
| 14 |
+
|
| 15 |
+
st.title("๐ซ Pneumonia Detection from Chest X-ray Images")
|
| 16 |
+
|
| 17 |
+
st.markdown("Upload your own X-ray or try one of the sample images below.")
|
| 18 |
+
|
| 19 |
+
# === Sample Image Section ===
|
| 20 |
+
sample_images = {
|
| 21 |
+
"Choose a sample image": None,
|
| 22 |
+
"๐ง Normal Sample": "normal.jpg",
|
| 23 |
+
"๐ค Pneumonia Sample": "pneumonia.jpg"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
selected_sample = st.selectbox("๐ Select a sample image", list(sample_images.keys()))
|
| 27 |
+
uploaded_file = st.file_uploader("๐ Or upload a chest X-ray image...", type=["jpg", "jpeg", "png"])
|
| 28 |
+
|
| 29 |
+
# Determine which image to use
|
| 30 |
+
if selected_sample != "Choose a sample image":
|
| 31 |
+
image_path = sample_images[selected_sample]
|
| 32 |
+
image = Image.open(image_path).convert("RGB")
|
| 33 |
+
st.image(image, caption=f'๐ผ๏ธ {selected_sample}', use_column_width=True)
|
| 34 |
+
elif uploaded_file is not None:
|
| 35 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 36 |
+
st.image(image, caption='๐ผ๏ธ Uploaded Image', use_column_width=True)
|
| 37 |
+
else:
|
| 38 |
+
image = None
|
| 39 |
+
|
| 40 |
+
# === Predict Button ===
|
| 41 |
+
if image and st.button('๐ Predict'):
|
| 42 |
+
img = image.resize((150, 150))
|
| 43 |
+
img_array = np.array(img) / 255.0
|
| 44 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 45 |
+
|
| 46 |
+
prediction = model.predict(img_array)
|
| 47 |
+
|
| 48 |
+
if prediction[0][0] > 0.5:
|
| 49 |
+
st.error("๐ฉบ **Prediction: Pneumonia Detected**")
|
| 50 |
+
else:
|
| 51 |
+
st.success("โ
**Prediction: Normal**")
|