Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,33 @@ from keras.layers import BatchNormalization, DepthwiseConv2D, TFSMLayer
|
|
| 9 |
import os
|
| 10 |
from io import BytesIO
|
| 11 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# --- Fix deserialization issues for BatchNorm and DepthwiseConv2D ---
|
| 14 |
original_bn = BatchNormalization.from_config
|
|
@@ -197,36 +224,39 @@ def show_lime(img, model, pred_idx, pred_label, all_probs):
|
|
| 197 |
)
|
| 198 |
lime_img = mark_boundaries(temp, mask)
|
| 199 |
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
# Save image to BytesIO buffer for both display and download
|
| 206 |
-
buf = BytesIO()
|
| 207 |
-
plt.imsave(buf, lime_img, format="png")
|
| 208 |
-
buf.seek(0)
|
| 209 |
-
|
| 210 |
-
# Display image with full column width
|
| 211 |
-
st.image(buf.getvalue(), use_column_width=True)
|
| 212 |
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
| 214 |
st.download_button(
|
| 215 |
"📥 Download LIME Image",
|
| 216 |
-
|
| 217 |
file_name=f"{pred_label}_LIME.png",
|
| 218 |
mime="image/png"
|
| 219 |
)
|
| 220 |
|
| 221 |
-
with
|
|
|
|
| 222 |
st.markdown(
|
| 223 |
-
"<div class='overlay'>"
|
| 224 |
f"<h3>🧠 Model's Reasoning</h3>"
|
| 225 |
f"{explanation_text.get(pred_label, 'No explanation available.')}"
|
| 226 |
"</div>",
|
| 227 |
-
unsafe_allow_html=True
|
| 228 |
)
|
| 229 |
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
|
| 232 |
|
|
|
|
| 9 |
import os
|
| 10 |
from io import BytesIO
|
| 11 |
import base64
|
| 12 |
+
st.markdown("""
|
| 13 |
+
<style>
|
| 14 |
+
.equal-cols {
|
| 15 |
+
display: flex;
|
| 16 |
+
gap: 1.5rem;
|
| 17 |
+
}
|
| 18 |
+
.equal-cols > div {
|
| 19 |
+
flex: 1;
|
| 20 |
+
display: flex;
|
| 21 |
+
flex-direction: column;
|
| 22 |
+
}
|
| 23 |
+
.lime-image {
|
| 24 |
+
width: 100%;
|
| 25 |
+
border-radius: 10px;
|
| 26 |
+
}
|
| 27 |
+
.overlay-box {
|
| 28 |
+
background-color: rgba(255, 255, 255, 0.85);
|
| 29 |
+
padding: 1rem;
|
| 30 |
+
border-radius: 10px;
|
| 31 |
+
overflow-y: auto;
|
| 32 |
+
color: #333;
|
| 33 |
+
font-size: 16px;
|
| 34 |
+
line-height: 1.5;
|
| 35 |
+
height: 100%;
|
| 36 |
+
}
|
| 37 |
+
</style>
|
| 38 |
+
""", unsafe_allow_html=True)
|
| 39 |
|
| 40 |
# --- Fix deserialization issues for BatchNorm and DepthwiseConv2D ---
|
| 41 |
original_bn = BatchNormalization.from_config
|
|
|
|
| 224 |
)
|
| 225 |
lime_img = mark_boundaries(temp, mask)
|
| 226 |
|
| 227 |
+
# Save to buffer once
|
| 228 |
+
buf = BytesIO()
|
| 229 |
+
plt.imsave(buf, lime_img, format="png")
|
| 230 |
+
buf.seek(0)
|
| 231 |
+
lime_data = buf.getvalue()
|
| 232 |
|
| 233 |
+
# Custom equal-height container
|
| 234 |
+
st.markdown('<div class="equal-cols">', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
# Left column with image
|
| 237 |
+
with st.container():
|
| 238 |
+
st.markdown("### 📍 LIME Explanation")
|
| 239 |
+
st.image(lime_data, use_container_width=True, output_format="PNG")
|
| 240 |
st.download_button(
|
| 241 |
"📥 Download LIME Image",
|
| 242 |
+
lime_data,
|
| 243 |
file_name=f"{pred_label}_LIME.png",
|
| 244 |
mime="image/png"
|
| 245 |
)
|
| 246 |
|
| 247 |
+
# Right column with explanation
|
| 248 |
+
with st.container():
|
| 249 |
st.markdown(
|
| 250 |
+
f"<div class='overlay-box'>"
|
| 251 |
f"<h3>🧠 Model's Reasoning</h3>"
|
| 252 |
f"{explanation_text.get(pred_label, 'No explanation available.')}"
|
| 253 |
"</div>",
|
| 254 |
+
unsafe_allow_html=True
|
| 255 |
)
|
| 256 |
|
| 257 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 258 |
+
|
| 259 |
+
|
| 260 |
|
| 261 |
|
| 262 |
|