Upload folder using huggingface_hub
Browse files- app.py +14 -22
- requirements.txt +0 -1
app.py
CHANGED
|
@@ -1,57 +1,47 @@
|
|
| 1 |
-
import requests
|
| 2 |
import streamlit as st
|
| 3 |
import pydicom
|
| 4 |
import numpy as np
|
| 5 |
-
import
|
| 6 |
|
| 7 |
st.title("Medical Image Prediction App")
|
| 8 |
st.write("Upload a DICOM (.dcm) image and get prediction.")
|
| 9 |
|
| 10 |
-
# Upload DICOM file
|
| 11 |
uploaded_file = st.file_uploader("Upload DICOM file", type=["dcm"])
|
| 12 |
|
| 13 |
-
#
|
| 14 |
def load_dicom_preview(file):
|
| 15 |
dicom = pydicom.dcmread(file, force=True)
|
| 16 |
img = dicom.pixel_array.astype(np.float32)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
img -= np.min(img)
|
| 20 |
if np.max(img) != 0:
|
| 21 |
img = img / np.max(img)
|
|
|
|
| 22 |
|
| 23 |
-
# Convert grayscale → RGB
|
| 24 |
if len(img.shape) == 2:
|
| 25 |
-
img = np.stack([img]
|
| 26 |
|
| 27 |
-
img = (img * 255).astype(np.uint8)
|
| 28 |
return img
|
| 29 |
|
| 30 |
-
|
| 31 |
-
# Display preview safely
|
| 32 |
if uploaded_file is not None:
|
| 33 |
try:
|
| 34 |
-
# Reset pointer BEFORE reading for preview
|
| 35 |
uploaded_file.seek(0)
|
| 36 |
preview_img = load_dicom_preview(uploaded_file)
|
| 37 |
-
|
| 38 |
st.image(preview_img, caption="DICOM Preview", use_container_width=True)
|
| 39 |
-
|
| 40 |
-
# Reset pointer AGAIN before sending to backend
|
| 41 |
uploaded_file.seek(0)
|
| 42 |
-
|
| 43 |
except Exception as e:
|
| 44 |
st.error(f"Error reading DICOM file: {e}")
|
| 45 |
|
| 46 |
-
|
| 47 |
-
# Prediction request
|
| 48 |
if st.button("Predict"):
|
| 49 |
-
|
| 50 |
if uploaded_file is None:
|
| 51 |
st.error("Please upload a DICOM file before predicting.")
|
| 52 |
else:
|
| 53 |
-
uploaded_file.seek(0)
|
| 54 |
-
|
| 55 |
files = {"file": uploaded_file}
|
| 56 |
|
| 57 |
response = requests.post(
|
|
@@ -61,7 +51,9 @@ if st.button("Predict"):
|
|
| 61 |
|
| 62 |
if response.status_code == 200:
|
| 63 |
result = response.json()
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
st.error(f"API Error: {response.text}")
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pydicom
|
| 3 |
import numpy as np
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
st.title("Medical Image Prediction App")
|
| 7 |
st.write("Upload a DICOM (.dcm) image and get prediction.")
|
| 8 |
|
| 9 |
+
# --- Upload DICOM file ---
|
| 10 |
uploaded_file = st.file_uploader("Upload DICOM file", type=["dcm"])
|
| 11 |
|
| 12 |
+
# --- Preview function (visual only) ---
|
| 13 |
def load_dicom_preview(file):
|
| 14 |
dicom = pydicom.dcmread(file, force=True)
|
| 15 |
img = dicom.pixel_array.astype(np.float32)
|
| 16 |
|
| 17 |
+
# Scale to 0–255 for display only
|
| 18 |
img -= np.min(img)
|
| 19 |
if np.max(img) != 0:
|
| 20 |
img = img / np.max(img)
|
| 21 |
+
img = (img * 255).astype(np.uint8)
|
| 22 |
|
| 23 |
+
# Convert grayscale → RGB
|
| 24 |
if len(img.shape) == 2:
|
| 25 |
+
img = np.stack([img]*3, axis=-1)
|
| 26 |
|
|
|
|
| 27 |
return img
|
| 28 |
|
| 29 |
+
# --- Display preview ---
|
|
|
|
| 30 |
if uploaded_file is not None:
|
| 31 |
try:
|
|
|
|
| 32 |
uploaded_file.seek(0)
|
| 33 |
preview_img = load_dicom_preview(uploaded_file)
|
|
|
|
| 34 |
st.image(preview_img, caption="DICOM Preview", use_container_width=True)
|
|
|
|
|
|
|
| 35 |
uploaded_file.seek(0)
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
st.error(f"Error reading DICOM file: {e}")
|
| 38 |
|
| 39 |
+
# --- Predict button ---
|
|
|
|
| 40 |
if st.button("Predict"):
|
|
|
|
| 41 |
if uploaded_file is None:
|
| 42 |
st.error("Please upload a DICOM file before predicting.")
|
| 43 |
else:
|
| 44 |
+
uploaded_file.seek(0)
|
|
|
|
| 45 |
files = {"file": uploaded_file}
|
| 46 |
|
| 47 |
response = requests.post(
|
|
|
|
| 51 |
|
| 52 |
if response.status_code == 200:
|
| 53 |
result = response.json()
|
| 54 |
+
pred_class = result.get("Predicted_Class")
|
| 55 |
+
prob = result.get("Probability")
|
| 56 |
+
st.success(f"Predicted Class: {pred_class}")
|
| 57 |
+
st.info(f"Probability: {prob:.4f}")
|
| 58 |
else:
|
| 59 |
st.error(f"API Error: {response.text}")
|
requirements.txt
CHANGED
|
@@ -4,4 +4,3 @@ pydicom==2.4.4
|
|
| 4 |
opencv-python-headless==4.10.0.84
|
| 5 |
pandas==2.3.1
|
| 6 |
numpy==1.26.0
|
| 7 |
-
#tensorflow==2.18.0
|
|
|
|
| 4 |
opencv-python-headless==4.10.0.84
|
| 5 |
pandas==2.3.1
|
| 6 |
numpy==1.26.0
|
|
|