Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -2,40 +2,77 @@ import streamlit as st
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
from tensorflow.keras.models import load_model
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
@st.cache_resource
|
| 9 |
def load_vgg_model():
|
| 10 |
return load_model("brain_tumor_vgg16_model.keras")
|
| 11 |
|
| 12 |
-
model = load_vgg_model()
|
| 13 |
-
img_size = 150
|
| 14 |
-
|
| 15 |
# Streamlit UI
|
| 16 |
st.title("Brain Tumor MRI Classification App")
|
| 17 |
st.write("Upload a brain MRI scan to check if it contains a tumor.")
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
from tensorflow.keras.models import load_model
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
import platform
|
| 8 |
+
import subprocess
|
| 9 |
|
| 10 |
+
# Add a function to load the model
|
| 11 |
@st.cache_resource
|
| 12 |
def load_vgg_model():
|
| 13 |
return load_model("brain_tumor_vgg16_model.keras")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# Streamlit UI
|
| 16 |
st.title("Brain Tumor MRI Classification App")
|
| 17 |
st.write("Upload a brain MRI scan to check if it contains a tumor.")
|
| 18 |
|
| 19 |
+
# --- DEBUGGING INFORMATION ---
|
| 20 |
+
st.header("Debugging Information")
|
| 21 |
+
|
| 22 |
+
# Check current working directory
|
| 23 |
+
st.write(f"Current working directory: {os.getcwd()}")
|
| 24 |
+
|
| 25 |
+
# List files in the directory
|
| 26 |
+
st.write("Files in /app:")
|
| 27 |
+
for file in os.listdir("."):
|
| 28 |
+
st.write(f"- {file}")
|
| 29 |
+
|
| 30 |
+
# Display Python and library versions
|
| 31 |
+
st.write("Python version:", sys.version)
|
| 32 |
+
st.write("Numpy version:", np.__version__)
|
| 33 |
+
st.write("TensorFlow version:", tf.__version__)
|
| 34 |
+
st.write("Pillow version:", Image.__version__)
|
| 35 |
+
|
| 36 |
+
# Display environment variables
|
| 37 |
+
st.write("Environment Variables:")
|
| 38 |
+
for key, value in os.environ.items():
|
| 39 |
+
st.write(f"- {key}: {value}")
|
| 40 |
+
|
| 41 |
+
# --- END DEBUGGING INFORMATION ---
|
| 42 |
+
|
| 43 |
+
# Try to load the model and display success/failure
|
| 44 |
+
try:
|
| 45 |
+
with st.spinner('Loading model...'):
|
| 46 |
+
model = load_vgg_model()
|
| 47 |
+
st.success("Model loaded successfully.")
|
| 48 |
+
|
| 49 |
+
# Define image size (must match the model's input size)
|
| 50 |
+
img_size = 150
|
| 51 |
+
|
| 52 |
+
# UI for image upload
|
| 53 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 54 |
+
|
| 55 |
+
if uploaded_file is not None:
|
| 56 |
+
# Display the uploaded image
|
| 57 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 58 |
+
st.image(image, caption='Uploaded MRI Scan', use_column_width=True)
|
| 59 |
+
st.write("")
|
| 60 |
+
st.write("Classifying...")
|
| 61 |
+
|
| 62 |
+
# Preprocess the image for the model
|
| 63 |
+
img_array = np.array(image.resize((img_size, img_size)))
|
| 64 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 65 |
+
img_array = img_array / 255.0
|
| 66 |
+
|
| 67 |
+
# Make prediction
|
| 68 |
+
prediction = model.predict(img_array)
|
| 69 |
+
class_predicted = (prediction > 0.5).astype("int32")[0][0]
|
| 70 |
+
|
| 71 |
+
# Display the result
|
| 72 |
+
if class_predicted == 1:
|
| 73 |
+
st.error("Prediction: Tumor Detected")
|
| 74 |
+
else:
|
| 75 |
+
st.success("Prediction: No Tumor Detected")
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
st.error(f"Failed to load or use the model. Error: {e}")
|