Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +6 -6
- app.py +31 -29
- requirements.txt +0 -1
Dockerfile
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
FROM python:3.9
|
| 2 |
|
| 3 |
# Set the working directory inside the container to /app
|
|
@@ -7,14 +8,12 @@ WORKDIR /app
|
|
| 7 |
COPY requirements.txt .
|
| 8 |
|
| 9 |
# Install Python dependencies listed in requirements.txt
|
| 10 |
-
# Using --no-cache-dir to save space
|
| 11 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
|
| 13 |
-
# --- CRITICAL FIX: Explicitly set Keras backend
|
| 14 |
-
# This forces Keras to use the installed TensorFlow library.
|
| 15 |
ENV KERAS_BACKEND=tensorflow
|
| 16 |
|
| 17 |
-
# Copy application files and the model file
|
| 18 |
COPY app.py .
|
| 19 |
COPY tuned_ai_model_best_lat.keras .
|
| 20 |
|
|
@@ -26,5 +25,6 @@ ENV HOME=/home/user \
|
|
| 26 |
WORKDIR $HOME/app
|
| 27 |
COPY --chown=user . $HOME/app
|
| 28 |
|
| 29 |
-
# Define the command to run the Streamlit app
|
| 30 |
-
|
|
|
|
|
|
| 1 |
+
# Use a minimal base image with Python 3.9 installed
|
| 2 |
FROM python:3.9
|
| 3 |
|
| 4 |
# Set the working directory inside the container to /app
|
|
|
|
| 8 |
COPY requirements.txt .
|
| 9 |
|
| 10 |
# Install Python dependencies listed in requirements.txt
|
|
|
|
| 11 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
|
| 13 |
+
# --- CRITICAL FIX: Explicitly set Keras backend ---
|
|
|
|
| 14 |
ENV KERAS_BACKEND=tensorflow
|
| 15 |
|
| 16 |
+
# Copy application files and the model file (must be named tuned_ai_model_best_lat.keras)
|
| 17 |
COPY app.py .
|
| 18 |
COPY tuned_ai_model_best_lat.keras .
|
| 19 |
|
|
|
|
| 25 |
WORKDIR $HOME/app
|
| 26 |
COPY --chown=user . $HOME/app
|
| 27 |
|
| 28 |
+
# Define the command to run the Streamlit app, re-including the CRITICAL security flag
|
| 29 |
+
# This flag should prevent the 403 Forbidden error caused by XSRF protection in proxy environments.
|
| 30 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
|
app.py
CHANGED
|
@@ -5,26 +5,25 @@ import tensorflow as tf
|
|
| 5 |
from tensorflow.keras.models import load_model
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
-
import requests
|
| 9 |
from io import BytesIO
|
| 10 |
-
import cv2 # Import cv2 for image resizing
|
| 11 |
|
| 12 |
# Define the correct class names for output
|
| 13 |
CLASS_NAMES = {0: 'Normal', 1: 'Viral Pneumonia', 2: 'Covid'}
|
| 14 |
-
IMG_SIZE = 224
|
| 15 |
|
| 16 |
# Function to load the model (cached for efficiency)
|
| 17 |
@st.cache_resource
|
| 18 |
def load_tuned_model():
|
| 19 |
-
#
|
| 20 |
-
#
|
| 21 |
return tf.keras.models.load_model(
|
| 22 |
"tuned_ai_model_best_lat.keras",
|
| 23 |
-
|
| 24 |
)
|
| 25 |
|
| 26 |
# Function to run the prediction and show progress
|
| 27 |
def run_prediction(image_file, model, img_size):
|
|
|
|
| 28 |
progress_bar = st.progress(0)
|
| 29 |
status_text = st.empty()
|
| 30 |
for i in range(100):
|
|
@@ -33,34 +32,46 @@ def run_prediction(image_file, model, img_size):
|
|
| 33 |
time.sleep(0.01)
|
| 34 |
|
| 35 |
try:
|
| 36 |
-
# Read the image
|
| 37 |
image = Image.open(image_file).convert("RGB")
|
| 38 |
-
|
| 39 |
-
# Correct image resizing to match model's trained input shape (224x224)
|
| 40 |
img_array = np.array(image.resize((img_size, img_size)))
|
| 41 |
-
|
| 42 |
-
# Expand dimensions to create a batch of size 1
|
| 43 |
img_array = np.expand_dims(img_array, axis=0)
|
| 44 |
-
|
| 45 |
-
# Normalize the image data
|
| 46 |
img_array = img_array / 255.0
|
| 47 |
|
| 48 |
# Make the prediction
|
| 49 |
prediction = model.predict(img_array).flatten()
|
| 50 |
|
| 51 |
-
#
|
| 52 |
class_predicted_idx = np.argmax(prediction)
|
| 53 |
predicted_class_name = CLASS_NAMES[class_predicted_idx]
|
| 54 |
|
| 55 |
status_text.success("Prediction complete!")
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
status_text.error(f"An error occurred during prediction: {e}")
|
| 60 |
-
|
| 61 |
|
| 62 |
-
# Load the model
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
st.title("COVID Detection from Chest X-ray")
|
| 66 |
st.write("Upload a chest X-ray image to predict if it shows signs of Normal, Viral Pneumonia, or COVID.")
|
|
@@ -68,17 +79,8 @@ st.write("Upload a chest X-ray image to predict if it shows signs of Normal, Vir
|
|
| 68 |
uploaded_file = st.file_uploader("Choose an X-ray image...", type=["jpg", "jpeg", "png"])
|
| 69 |
|
| 70 |
if uploaded_file is not None:
|
| 71 |
-
# Display the uploaded image
|
| 72 |
image = Image.open(uploaded_file)
|
| 73 |
st.image(image, caption="Uploaded X-ray Image", use_container_width=True)
|
| 74 |
|
| 75 |
-
# Make prediction when button is clicked
|
| 76 |
if st.button("Predict"):
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
if predicted_class:
|
| 80 |
-
st.write(f"Prediction: **{predicted_class}**")
|
| 81 |
-
st.write("Prediction Probabilities:")
|
| 82 |
-
# Display probabilities for all classes
|
| 83 |
-
for i, prob in enumerate(prediction_probabilities):
|
| 84 |
-
st.write(f"- {CLASS_NAMES[i]}: {prob:.4f}")
|
|
|
|
| 5 |
from tensorflow.keras.models import load_model
|
| 6 |
import os
|
| 7 |
import time
|
|
|
|
| 8 |
from io import BytesIO
|
|
|
|
| 9 |
|
| 10 |
# Define the correct class names for output
|
| 11 |
CLASS_NAMES = {0: 'Normal', 1: 'Viral Pneumonia', 2: 'Covid'}
|
| 12 |
+
IMG_SIZE = 224
|
| 13 |
|
| 14 |
# Function to load the model (cached for efficiency)
|
| 15 |
@st.cache_resource
|
| 16 |
def load_tuned_model():
|
| 17 |
+
# Use custom_objects for maximum robustness when loading models with
|
| 18 |
+
# pre-trained components like VGG16 in specific environments.
|
| 19 |
return tf.keras.models.load_model(
|
| 20 |
"tuned_ai_model_best_lat.keras",
|
| 21 |
+
custom_objects={'VGG16': tf.keras.applications.VGG16}
|
| 22 |
)
|
| 23 |
|
| 24 |
# Function to run the prediction and show progress
|
| 25 |
def run_prediction(image_file, model, img_size):
|
| 26 |
+
# Progress visualization (for user experience)
|
| 27 |
progress_bar = st.progress(0)
|
| 28 |
status_text = st.empty()
|
| 29 |
for i in range(100):
|
|
|
|
| 32 |
time.sleep(0.01)
|
| 33 |
|
| 34 |
try:
|
| 35 |
+
# Read and process the image using PIL (Image)
|
| 36 |
image = Image.open(image_file).convert("RGB")
|
|
|
|
|
|
|
| 37 |
img_array = np.array(image.resize((img_size, img_size)))
|
|
|
|
|
|
|
| 38 |
img_array = np.expand_dims(img_array, axis=0)
|
|
|
|
|
|
|
| 39 |
img_array = img_array / 255.0
|
| 40 |
|
| 41 |
# Make the prediction
|
| 42 |
prediction = model.predict(img_array).flatten()
|
| 43 |
|
| 44 |
+
# Find the predicted class index and name
|
| 45 |
class_predicted_idx = np.argmax(prediction)
|
| 46 |
predicted_class_name = CLASS_NAMES[class_predicted_idx]
|
| 47 |
|
| 48 |
status_text.success("Prediction complete!")
|
| 49 |
+
|
| 50 |
+
# Display results
|
| 51 |
+
st.subheader("Prediction Probabilities:")
|
| 52 |
+
for i, prob in enumerate(prediction):
|
| 53 |
+
class_name = CLASS_NAMES[i]
|
| 54 |
+
if i == class_predicted_idx:
|
| 55 |
+
st.markdown(f"**{class_name}: {prob*100:.2f}%** (Predicted)", unsafe_allow_html=True)
|
| 56 |
+
else:
|
| 57 |
+
st.markdown(f"{class_name}: {prob*100:.2f}%")
|
| 58 |
+
|
| 59 |
+
if class_predicted_idx == 2:
|
| 60 |
+
st.error(f"**Result: ❗ HIGH LIKELIHOOD OF COVID DETECTED ❗**")
|
| 61 |
+
else:
|
| 62 |
+
st.success(f"**Result: ✅ {predicted_class_name} Detected**")
|
| 63 |
+
|
| 64 |
except Exception as e:
|
| 65 |
status_text.error(f"An error occurred during prediction: {e}")
|
| 66 |
+
st.exception(e) # Show full traceback for debugging
|
| 67 |
|
| 68 |
+
# Load the model and ensure the app stops if loading fails
|
| 69 |
+
try:
|
| 70 |
+
model = load_tuned_model()
|
| 71 |
+
except Exception as e:
|
| 72 |
+
st.error("Model Loading Failed. Check TF/Keras versions and model file name (tuned_ai_model_best_lat.keras).")
|
| 73 |
+
st.exception(e)
|
| 74 |
+
st.stop()
|
| 75 |
|
| 76 |
st.title("COVID Detection from Chest X-ray")
|
| 77 |
st.write("Upload a chest X-ray image to predict if it shows signs of Normal, Viral Pneumonia, or COVID.")
|
|
|
|
| 79 |
uploaded_file = st.file_uploader("Choose an X-ray image...", type=["jpg", "jpeg", "png"])
|
| 80 |
|
| 81 |
if uploaded_file is not None:
|
|
|
|
| 82 |
image = Image.open(uploaded_file)
|
| 83 |
st.image(image, caption="Uploaded X-ray Image", use_container_width=True)
|
| 84 |
|
|
|
|
| 85 |
if st.button("Predict"):
|
| 86 |
+
run_prediction(uploaded_file, model, IMG_SIZE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -4,4 +4,3 @@ numpy==2.0.2
|
|
| 4 |
pandas==2.2.2
|
| 5 |
keras==3.10.0
|
| 6 |
Pillow==11.3.0
|
| 7 |
-
opencv-python==4.12.0.88
|
|
|
|
| 4 |
pandas==2.2.2
|
| 5 |
keras==3.10.0
|
| 6 |
Pillow==11.3.0
|
|
|