Update src/streamlit_app.py
Browse files- src/streamlit_app.py +16 -26
src/streamlit_app.py
CHANGED
|
@@ -2,24 +2,13 @@ import os
|
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
from PIL import Image
|
| 5 |
-
import logging
|
| 6 |
from pathlib import Path
|
| 7 |
-
# Note: ConvNeXtLarge weights='imagenet' will be downloaded by TensorFlow,
|
| 8 |
-
# which also has its own caching mechanism. This is separate from the
|
| 9 |
-
# huggingface_hub download for the .h5 model weights.
|
| 10 |
from tensorflow.keras.applications import ConvNeXtLarge
|
| 11 |
import streamlit as st
|
| 12 |
import io
|
| 13 |
from huggingface_hub import hf_hub_download, try_to_load_from_cache
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
setup_logging()
|
| 17 |
-
|
| 18 |
-
def setup_logging():
|
| 19 |
-
logging.basicConfig(
|
| 20 |
-
level=logging.INFO,
|
| 21 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 22 |
-
)
|
| 23 |
|
| 24 |
# We will now rely on hf_hub_download's default cache behavior
|
| 25 |
def download_model_from_hub():
|
|
@@ -82,16 +71,16 @@ def load_model(model_path):
|
|
| 82 |
Loads the Keras model weights from a specified path.
|
| 83 |
"""
|
| 84 |
try:
|
| 85 |
-
logging.info(f"Attempting to load model from {model_path}")
|
| 86 |
# First create the model architecture
|
| 87 |
model = create_convnext_model()
|
| 88 |
|
| 89 |
# Then load the weights from the downloaded .h5 file
|
| 90 |
model.load_weights(model_path)
|
| 91 |
-
logging.info("Model weights loaded successfully.")
|
| 92 |
return model
|
| 93 |
except Exception as e:
|
| 94 |
-
logging.error(f"Error loading model weights: {str(e)}")
|
| 95 |
st.error(f"Error loading model weights: {str(e)}")
|
| 96 |
st.info("Ensure the downloaded file is a valid Keras .h5 weights file compatible with the ConvNeXtLarge architecture.")
|
| 97 |
# Re-raise the exception so Streamlit knows to stop if model loading fails
|
|
@@ -109,7 +98,7 @@ def preprocess_image(image, target_size=(512, 512)):
|
|
| 109 |
# Resize image using BICUBIC for potentially better quality than BILINEAR for downsampling
|
| 110 |
img = image.resize(target_size, Image.Resampling.BICUBIC)
|
| 111 |
|
| 112 |
-
# Convert to numpy array
|
| 113 |
img_array = np.array(img, dtype=np.float32) # Use float32 for normalization
|
| 114 |
|
| 115 |
# Normalize to [0, 1]
|
|
@@ -129,13 +118,14 @@ def preprocess_image(image, target_size=(512, 512)):
|
|
| 129 |
# Add batch dimension
|
| 130 |
img_array = np.expand_dims(img_array, axis=0)
|
| 131 |
|
| 132 |
-
logging.info("Image preprocessed successfully.")
|
| 133 |
return img_array
|
| 134 |
except Exception as e:
|
| 135 |
-
logging.error(f"Error preprocessing image: {str(e)}")
|
| 136 |
st.error(f"Error preprocessing image: {str(e)}")
|
| 137 |
raise
|
| 138 |
|
|
|
|
| 139 |
def predict_volcano(model, image):
|
| 140 |
"""
|
| 141 |
Makes a prediction using the loaded model.
|
|
@@ -159,7 +149,7 @@ def predict_volcano(model, image):
|
|
| 159 |
# Confidence is the probability for the predicted class
|
| 160 |
confidence = probability if probability > 0.5 else 1 - probability
|
| 161 |
|
| 162 |
-
logging.info(f"Prediction made: Result={result}, Probability={probability:.4f}")
|
| 163 |
|
| 164 |
return {
|
| 165 |
"result": result,
|
|
@@ -167,7 +157,7 @@ def predict_volcano(model, image):
|
|
| 167 |
"probability": float(probability) # Convert to standard Python float
|
| 168 |
}
|
| 169 |
except Exception as e:
|
| 170 |
-
logging.error(f"Error making prediction: {str(e)}")
|
| 171 |
st.error(f"Error making prediction: {str(e)}")
|
| 172 |
raise
|
| 173 |
|
|
@@ -205,7 +195,6 @@ def get_sample_images():
|
|
| 205 |
|
| 206 |
|
| 207 |
def main():
|
| 208 |
-
setup_logging()
|
| 209 |
|
| 210 |
st.set_page_config(
|
| 211 |
page_title="Volcano Detection",
|
|
@@ -249,22 +238,23 @@ def main():
|
|
| 249 |
if uploaded_file is not None:
|
| 250 |
try:
|
| 251 |
image = Image.open(uploaded_file)
|
| 252 |
-
logging.info("Image uploaded by user.")
|
| 253 |
except Exception as e:
|
| 254 |
st.error(f"Error opening uploaded image: {str(e)}")
|
| 255 |
-
logging.error(f"Error opening uploaded image: {str(e)}")
|
| 256 |
elif selected_option != "Select an image" and sample_images[selected_option] is not None:
|
| 257 |
try:
|
| 258 |
sample_image_path = sample_images[selected_option]
|
| 259 |
if Path(sample_image_path).exists():
|
| 260 |
image = Image.open(sample_image_path)
|
| 261 |
-
logging.info(f"Sample image '{selected_option}' loaded.")
|
| 262 |
else:
|
| 263 |
st.warning(f"Sample image file not found: {sample_image_path}")
|
| 264 |
-
logging.warning(f"Sample image file not found: {sample_image_path}")
|
| 265 |
except Exception as e:
|
| 266 |
st.error(f"Error loading sample image '{selected_option}': {str(e)}")
|
| 267 |
-
logging.error(f"Error loading sample image '{selected_option}': {str(e)}")
|
|
|
|
| 268 |
|
| 269 |
if image is not None:
|
| 270 |
# Display the image
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
| 6 |
from tensorflow.keras.applications import ConvNeXtLarge
|
| 7 |
import streamlit as st
|
| 8 |
import io
|
| 9 |
from huggingface_hub import hf_hub_download, try_to_load_from_cache
|
| 10 |
|
| 11 |
+
# Removed: setup_logging function and its call
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# We will now rely on hf_hub_download's default cache behavior
|
| 14 |
def download_model_from_hub():
|
|
|
|
| 71 |
Loads the Keras model weights from a specified path.
|
| 72 |
"""
|
| 73 |
try:
|
| 74 |
+
# Removed: logging.info(f"Attempting to load model from {model_path}")
|
| 75 |
# First create the model architecture
|
| 76 |
model = create_convnext_model()
|
| 77 |
|
| 78 |
# Then load the weights from the downloaded .h5 file
|
| 79 |
model.load_weights(model_path)
|
| 80 |
+
# Removed: logging.info("Model weights loaded successfully.")
|
| 81 |
return model
|
| 82 |
except Exception as e:
|
| 83 |
+
# Removed: logging.error(f"Error loading model weights: {str(e)}")
|
| 84 |
st.error(f"Error loading model weights: {str(e)}")
|
| 85 |
st.info("Ensure the downloaded file is a valid Keras .h5 weights file compatible with the ConvNeXtLarge architecture.")
|
| 86 |
# Re-raise the exception so Streamlit knows to stop if model loading fails
|
|
|
|
| 98 |
# Resize image using BICUBIC for potentially better quality than BILINEAR for downsampling
|
| 99 |
img = image.resize(target_size, Image.Resampling.BICUBIC)
|
| 100 |
|
| 101 |
+
# Convert to numpy array and normalize
|
| 102 |
img_array = np.array(img, dtype=np.float32) # Use float32 for normalization
|
| 103 |
|
| 104 |
# Normalize to [0, 1]
|
|
|
|
| 118 |
# Add batch dimension
|
| 119 |
img_array = np.expand_dims(img_array, axis=0)
|
| 120 |
|
| 121 |
+
# Removed: logging.info("Image preprocessed successfully.")
|
| 122 |
return img_array
|
| 123 |
except Exception as e:
|
| 124 |
+
# Removed: logging.error(f"Error preprocessing image: {str(e)}")
|
| 125 |
st.error(f"Error preprocessing image: {str(e)}")
|
| 126 |
raise
|
| 127 |
|
| 128 |
+
|
| 129 |
def predict_volcano(model, image):
|
| 130 |
"""
|
| 131 |
Makes a prediction using the loaded model.
|
|
|
|
| 149 |
# Confidence is the probability for the predicted class
|
| 150 |
confidence = probability if probability > 0.5 else 1 - probability
|
| 151 |
|
| 152 |
+
# Removed: logging.info(f"Prediction made: Result={result}, Probability={probability:.4f}")
|
| 153 |
|
| 154 |
return {
|
| 155 |
"result": result,
|
|
|
|
| 157 |
"probability": float(probability) # Convert to standard Python float
|
| 158 |
}
|
| 159 |
except Exception as e:
|
| 160 |
+
# Removed: logging.error(f"Error making prediction: {str(e)}")
|
| 161 |
st.error(f"Error making prediction: {str(e)}")
|
| 162 |
raise
|
| 163 |
|
|
|
|
| 195 |
|
| 196 |
|
| 197 |
def main():
|
|
|
|
| 198 |
|
| 199 |
st.set_page_config(
|
| 200 |
page_title="Volcano Detection",
|
|
|
|
| 238 |
if uploaded_file is not None:
|
| 239 |
try:
|
| 240 |
image = Image.open(uploaded_file)
|
| 241 |
+
# Removed: logging.info("Image uploaded by user.")
|
| 242 |
except Exception as e:
|
| 243 |
st.error(f"Error opening uploaded image: {str(e)}")
|
| 244 |
+
# Removed: logging.error(f"Error opening uploaded image: {str(e)}")
|
| 245 |
elif selected_option != "Select an image" and sample_images[selected_option] is not None:
|
| 246 |
try:
|
| 247 |
sample_image_path = sample_images[selected_option]
|
| 248 |
if Path(sample_image_path).exists():
|
| 249 |
image = Image.open(sample_image_path)
|
| 250 |
+
# Removed: logging.info(f"Sample image '{selected_option}' loaded.")
|
| 251 |
else:
|
| 252 |
st.warning(f"Sample image file not found: {sample_image_path}")
|
| 253 |
+
# Removed: logging.warning(f"Sample image file not found: {sample_image_path}")
|
| 254 |
except Exception as e:
|
| 255 |
st.error(f"Error loading sample image '{selected_option}': {str(e)}")
|
| 256 |
+
# Removed: logging.error(f"Error loading sample image '{selected_option}': {str(e)}")
|
| 257 |
+
|
| 258 |
|
| 259 |
if image is not None:
|
| 260 |
# Display the image
|