Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- Dockerfile +9 -13
- app.py +132 -0
- requirements.txt +3 -3
Dockerfile
CHANGED
|
@@ -1,20 +1,16 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
curl \
|
| 8 |
-
git \
|
| 9 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
-
|
| 11 |
-
COPY requirements.txt ./
|
| 12 |
-
COPY src/ ./src/
|
| 13 |
|
|
|
|
| 14 |
RUN pip3 install -r requirements.txt
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
|
| 19 |
|
| 20 |
-
|
|
|
|
| 1 |
+
# Use a minimal base image with Python 3.9 installed
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
|
| 4 |
+
# Set the working directory inside the container to /app
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
+
# Copy all files from the current directory on the host to the container's /app directory
|
| 8 |
+
COPY . .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Install Python dependencies listed in requirements.txt
|
| 11 |
RUN pip3 install -r requirements.txt
|
| 12 |
|
| 13 |
+
# Define the command to run the Streamlit app on port 8501 and make it accessible externally
|
| 14 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
|
|
|
|
| 15 |
|
| 16 |
+
# NOTE: Disable XSRF protection for easier external access in order to make batch predictions
|
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import numpy as np
|
| 4 |
+
import requests
|
| 5 |
+
import pydicom
|
| 6 |
+
import cv2
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
# -----------------------------
|
| 10 |
+
# Backend API URL
|
| 11 |
+
# -----------------------------
|
| 12 |
+
API_URL = "https://viveksardey-pneumoniadetectionpredictionbackend.hf.space/v1/predict"
|
| 13 |
+
|
| 14 |
+
IMG_SIZE = 224
|
| 15 |
+
|
| 16 |
+
st.title("🩺 Pneumonia Detection from Chest X-ray (DICOM)")
|
| 17 |
+
|
| 18 |
+
st.write("""
|
| 19 |
+
Upload a **Chest X-ray DICOM (.dcm)** file.
|
| 20 |
+
The image will be converted to **NumPy (.npy)** format and sent to the backend model.
|
| 21 |
+
""")
|
| 22 |
+
|
| 23 |
+
# -----------------------------
|
| 24 |
+
# Upload file
|
| 25 |
+
# -----------------------------
|
| 26 |
+
uploaded_file = st.file_uploader(
|
| 27 |
+
"Upload DICOM Chest X-ray",
|
| 28 |
+
type=["dcm"]
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# -----------------------------
|
| 32 |
+
# Convert DICOM → Image
|
| 33 |
+
# -----------------------------
|
| 34 |
+
def dicom_to_image(dicom_file):
|
| 35 |
+
|
| 36 |
+
dicom = pydicom.dcmread(dicom_file)
|
| 37 |
+
img = dicom.pixel_array
|
| 38 |
+
|
| 39 |
+
return img
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# -----------------------------
|
| 43 |
+
# Preprocess image
|
| 44 |
+
# -----------------------------
|
| 45 |
+
def preprocess_image(img):
|
| 46 |
+
|
| 47 |
+
# Resize image
|
| 48 |
+
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
|
| 49 |
+
|
| 50 |
+
# Normalize
|
| 51 |
+
img = img.astype("float32")
|
| 52 |
+
img = img / img.max()
|
| 53 |
+
|
| 54 |
+
return img
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# -----------------------------
|
| 58 |
+
# If file uploaded
|
| 59 |
+
# -----------------------------
|
| 60 |
+
if uploaded_file is not None:
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
|
| 64 |
+
img = dicom_to_image(uploaded_file)
|
| 65 |
+
|
| 66 |
+
st.subheader("X-ray Preview")
|
| 67 |
+
st.image(img, caption="Uploaded DICOM Image", use_column_width=True)
|
| 68 |
+
|
| 69 |
+
processed_img = preprocess_image(img)
|
| 70 |
+
|
| 71 |
+
npy_array = np.array(processed_img)
|
| 72 |
+
|
| 73 |
+
st.success("DICOM successfully converted to NumPy format")
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
|
| 77 |
+
st.error(f"Error processing DICOM file: {e}")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# -----------------------------
|
| 81 |
+
# Predict button
|
| 82 |
+
# -----------------------------
|
| 83 |
+
if st.button("Predict Pneumonia"):
|
| 84 |
+
|
| 85 |
+
if uploaded_file is None:
|
| 86 |
+
|
| 87 |
+
st.warning("Please upload a DICOM image first")
|
| 88 |
+
|
| 89 |
+
else:
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
|
| 93 |
+
with st.spinner("Running Pneumonia Detection..."):
|
| 94 |
+
|
| 95 |
+
# Save temporary npy file
|
| 96 |
+
with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as tmp:
|
| 97 |
+
|
| 98 |
+
np.save(tmp.name, npy_array)
|
| 99 |
+
tmp_path = tmp.name
|
| 100 |
+
|
| 101 |
+
# Send request to backend API
|
| 102 |
+
with open(tmp_path, "rb") as f:
|
| 103 |
+
|
| 104 |
+
response = requests.post(
|
| 105 |
+
API_URL,
|
| 106 |
+
files={"file": f}
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
if response.status_code == 200:
|
| 110 |
+
|
| 111 |
+
result = response.json()
|
| 112 |
+
|
| 113 |
+
predicted_class = result["Predicted_Class"]
|
| 114 |
+
probability = result["Probability_Pneumonia"]
|
| 115 |
+
|
| 116 |
+
st.subheader("Prediction Result")
|
| 117 |
+
|
| 118 |
+
if predicted_class == "Pneumonia":
|
| 119 |
+
st.error("⚠️ Pneumonia Detected")
|
| 120 |
+
|
| 121 |
+
else:
|
| 122 |
+
st.success("✅ Normal Lungs")
|
| 123 |
+
|
| 124 |
+
st.write(f"Probability of Pneumonia: **{probability:.4f}**")
|
| 125 |
+
|
| 126 |
+
else:
|
| 127 |
+
|
| 128 |
+
st.error(f"Prediction failed. Status Code: {response.status_code}")
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
|
| 132 |
+
st.error(f"Error during prediction: {e}")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
streamlit
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
requests==2.28.1
|
| 3 |
+
streamlit==1.43.2
|