Spaces:
Sleeping
Sleeping
Basak Tamer commited on
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +52 -39
src/streamlit_app.py
CHANGED
|
@@ -1,55 +1,68 @@
|
|
| 1 |
-
import os
|
| 2 |
-
# CRITICAL: This flag must be set BEFORE importing tensorflow
|
| 3 |
-
os.environ["TF_USE_LEGACY_KERAS"] = "1"
|
| 4 |
-
|
| 5 |
import streamlit as st
|
| 6 |
import tensorflow as tf
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
from tensorflow import keras
|
| 15 |
|
| 16 |
@st.cache_resource
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# --- UI ---
|
| 35 |
-
st.title("🩸 Malaria
|
| 36 |
|
| 37 |
try:
|
| 38 |
-
model =
|
| 39 |
-
st.sidebar.success("
|
| 40 |
except Exception as e:
|
| 41 |
-
st.error(f"
|
| 42 |
-
st.info("Try checking if 'tf-keras' is in your requirements.txt")
|
| 43 |
st.stop()
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
-
if
|
| 48 |
-
|
| 49 |
-
st.image(
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# --- CONFIG ---
|
| 7 |
+
# Your error config was [None, 64, 64, 3], so we use 64 here
|
| 8 |
+
IMG_SIZE = (64, 64)
|
| 9 |
+
MODEL_PATH = 'src/malaria_cnnmodel.keras'
|
|
|
|
| 10 |
|
| 11 |
@st.cache_resource
|
| 12 |
+
def load_model_by_weights():
|
| 13 |
+
"""
|
| 14 |
+
Manual rebuild to bypass the 'batch_shape' deserialization error.
|
| 15 |
+
"""
|
| 16 |
+
# 1. Define the Input exactly as the error showed
|
| 17 |
+
inputs = tf.keras.Input(shape=(IMG_SIZE[0], IMG_SIZE[1], 3))
|
| 18 |
+
|
| 19 |
+
# 2. Build the Base (Assuming VGG16 based on previous errors)
|
| 20 |
+
base_model = tf.keras.applications.VGG16(
|
| 21 |
+
weights=None,
|
| 22 |
+
include_top=False,
|
| 23 |
+
input_tensor=inputs
|
| 24 |
+
)
|
| 25 |
|
| 26 |
+
# 3. Add the classification head
|
| 27 |
+
x = tf.keras.layers.Flatten()(base_model.output)
|
| 28 |
+
x = tf.keras.layers.Dense(256, activation='relu')(x)
|
| 29 |
+
outputs = tf.keras.layers.Dense(1, activation='sigmoid')(x)
|
| 30 |
+
|
| 31 |
+
model = tf.keras.Model(inputs=inputs, outputs=outputs)
|
| 32 |
+
|
| 33 |
+
# 4. LOAD WEIGHTS ONLY
|
| 34 |
+
# This bypasses the broken 'InputLayer' config in the file
|
| 35 |
+
try:
|
| 36 |
+
model.load_weights(MODEL_PATH)
|
| 37 |
+
except Exception:
|
| 38 |
+
# If 'dense_2' name mismatch occurs, load by topology
|
| 39 |
+
model.load_weights(MODEL_PATH, by_name=False)
|
| 40 |
+
|
| 41 |
+
return model
|
| 42 |
|
| 43 |
# --- UI ---
|
| 44 |
+
st.title("🩸 Malaria Detection (Version Bridge)")
|
| 45 |
|
| 46 |
try:
|
| 47 |
+
model = load_model_by_weights()
|
| 48 |
+
st.sidebar.success("Model Shell Rebuilt & Weights Loaded")
|
| 49 |
except Exception as e:
|
| 50 |
+
st.error(f"Final Attempt Failed: {e}")
|
|
|
|
| 51 |
st.stop()
|
| 52 |
|
| 53 |
+
uploaded_file = st.file_uploader("Upload Cell Image", type=["png", "jpg", "jpeg"])
|
| 54 |
|
| 55 |
+
if uploaded_file:
|
| 56 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 57 |
+
st.image(image, width=250)
|
| 58 |
+
|
| 59 |
+
# Preprocess
|
| 60 |
+
img = image.resize(IMG_SIZE)
|
| 61 |
+
img_array = np.array(img) / 255.0
|
| 62 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 63 |
+
|
| 64 |
+
if st.button("Run Prediction"):
|
| 65 |
+
pred = model.predict(img_array)[0][0]
|
| 66 |
+
label = "Parasitized" if pred > 0.5 else "Uninfected"
|
| 67 |
+
color = "red" if pred > 0.5 else "green"
|
| 68 |
+
st.markdown(f"Result: :{color}[**{label}**] ({pred:.2%})")
|