Update src/streamlit_app.py
Browse files- src/streamlit_app.py +18 -24
src/streamlit_app.py
CHANGED
|
@@ -14,32 +14,26 @@ st.set_page_config(
|
|
| 14 |
initial_sidebar_state="expanded"
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# Absolute path handling for Docker/HF
|
| 18 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 19 |
|
| 20 |
-
# 2. Ultra-Stable CSS:
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&display=swap');
|
| 24 |
|
| 25 |
-
/* Global Stability Fixes */
|
| 26 |
.main {
|
| 27 |
background-color: #020617;
|
| 28 |
color: #f8fafc;
|
| 29 |
font-family: 'Plus Jakarta Sans', sans-serif;
|
| 30 |
}
|
| 31 |
|
| 32 |
-
/*
|
| 33 |
[data-testid="stSidebar"] {
|
| 34 |
background-color: #0f172a !important;
|
| 35 |
border-right: 1px solid #1e293b;
|
| 36 |
-
min-width:
|
| 37 |
-
max-width:
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
/* Gallery Stability: Fixed height container to prevent shifting */
|
| 41 |
-
.stHorizontalBlock {
|
| 42 |
-
align-items: flex-start !important;
|
| 43 |
}
|
| 44 |
|
| 45 |
.metric-card {
|
|
@@ -93,7 +87,7 @@ st.markdown("""
|
|
| 93 |
</style>
|
| 94 |
""", unsafe_allow_html=True)
|
| 95 |
|
| 96 |
-
# 3.
|
| 97 |
@st.cache_resource
|
| 98 |
def load_assets():
|
| 99 |
model_path = os.path.join(BASE_DIR, "plant_disease_pred.h5")
|
|
@@ -113,7 +107,7 @@ def load_assets():
|
|
| 113 |
model, class_indices = load_assets()
|
| 114 |
|
| 115 |
def predict_disease(image):
|
| 116 |
-
# RGB conversion to prevent channel
|
| 117 |
img = image.convert("RGB")
|
| 118 |
img = img.resize((224, 224))
|
| 119 |
img_array = np.array(img).astype('float32') / 255.
|
|
@@ -121,6 +115,7 @@ def predict_disease(image):
|
|
| 121 |
idx = np.argmax(predictions[0])
|
| 122 |
confidence = float(np.max(predictions[0]) * 100)
|
| 123 |
|
|
|
|
| 124 |
if confidence < 75:
|
| 125 |
return "Unidentified Specimen", confidence
|
| 126 |
return class_indices.get(str(idx), f"Unknown {idx}"), confidence
|
|
@@ -130,19 +125,19 @@ st.markdown("""
|
|
| 130 |
<div style="padding-top: 1rem; margin-bottom: 2rem;">
|
| 131 |
<div class="emerald-badge">AI DIAGNOSTICS</div>
|
| 132 |
<h1 style="font-size: 3rem; font-weight: 800; color: #f8fafc; margin: 0; letter-spacing: -0.04em;">Precision Plant <span style="color: #10b981;">Pathology</span></h1>
|
| 133 |
-
<p style="color: #94a3b8; font-size: 1rem; margin-top: 0.5rem;">
|
| 134 |
</div>
|
| 135 |
""", unsafe_allow_html=True)
|
| 136 |
|
| 137 |
-
#
|
| 138 |
m1, m2 = st.columns(2)
|
| 139 |
with m1: st.markdown('<div class="metric-card"><h3>Library</h3><h2>38</h2></div>', unsafe_allow_html=True)
|
| 140 |
-
with m2: st.markdown('<div class="metric-card"><h3>
|
| 141 |
|
| 142 |
st.markdown("<br>", unsafe_allow_html=True)
|
| 143 |
|
| 144 |
-
# 5.
|
| 145 |
-
st.markdown("### Specimen Gallery")
|
| 146 |
samples = {
|
| 147 |
"Apple Rot": "apple_black rot.jpeg",
|
| 148 |
"Healthy Apple": "apple_healthy.jpeg",
|
|
@@ -157,9 +152,9 @@ for i, (name, filename) in enumerate(samples.items()):
|
|
| 157 |
img_path = os.path.join(BASE_DIR, filename)
|
| 158 |
with sample_cols[i]:
|
| 159 |
if os.path.exists(img_path):
|
|
|
|
| 160 |
img_disp = Image.open(img_path)
|
| 161 |
-
|
| 162 |
-
st.image(img_disp, width=150, caption=name)
|
| 163 |
if st.button(f"Load {i+1}", key=f"btn_{i}"):
|
| 164 |
st.session_state.active_img = Image.open(img_path)
|
| 165 |
|
|
@@ -173,14 +168,13 @@ with col_up:
|
|
| 173 |
st.session_state.active_img = Image.open(uploaded_file)
|
| 174 |
|
| 175 |
if st.session_state.active_img:
|
| 176 |
-
#
|
| 177 |
-
st.image(st.session_state.active_img, width=
|
| 178 |
else:
|
| 179 |
-
st.markdown('<div style="border: 1px solid #1e293b; border-radius: 1rem; height:
|
| 180 |
|
| 181 |
with col_res:
|
| 182 |
if st.session_state.active_img and model:
|
| 183 |
-
# Action Primary Recognition
|
| 184 |
if st.button("RUN INTELLIGENT ANALYSIS"):
|
| 185 |
with st.spinner("Processing..."):
|
| 186 |
prediction, confidence = predict_disease(st.session_state.active_img)
|
|
|
|
| 14 |
initial_sidebar_state="expanded"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# Absolute path handling for Docker/HF environment
|
| 18 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 19 |
|
| 20 |
+
# 2. Ultra-Stable CSS: Rigid Dimensions & High Contrast
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&display=swap');
|
| 24 |
|
|
|
|
| 25 |
.main {
|
| 26 |
background-color: #020617;
|
| 27 |
color: #f8fafc;
|
| 28 |
font-family: 'Plus Jakarta Sans', sans-serif;
|
| 29 |
}
|
| 30 |
|
| 31 |
+
/* LOCK SIDEBAR: Prevents horizontal shifts */
|
| 32 |
[data-testid="stSidebar"] {
|
| 33 |
background-color: #0f172a !important;
|
| 34 |
border-right: 1px solid #1e293b;
|
| 35 |
+
min-width: 260px !important;
|
| 36 |
+
max-width: 260px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
.metric-card {
|
|
|
|
| 87 |
</style>
|
| 88 |
""", unsafe_allow_html=True)
|
| 89 |
|
| 90 |
+
# 3. Asset Management
|
| 91 |
@st.cache_resource
|
| 92 |
def load_assets():
|
| 93 |
model_path = os.path.join(BASE_DIR, "plant_disease_pred.h5")
|
|
|
|
| 107 |
model, class_indices = load_assets()
|
| 108 |
|
| 109 |
def predict_disease(image):
|
| 110 |
+
# RGB conversion to prevent channel mismatch (4 vs 3)
|
| 111 |
img = image.convert("RGB")
|
| 112 |
img = img.resize((224, 224))
|
| 113 |
img_array = np.array(img).astype('float32') / 255.
|
|
|
|
| 115 |
idx = np.argmax(predictions[0])
|
| 116 |
confidence = float(np.max(predictions[0]) * 100)
|
| 117 |
|
| 118 |
+
# 75% Confidence Threshold for reliability
|
| 119 |
if confidence < 75:
|
| 120 |
return "Unidentified Specimen", confidence
|
| 121 |
return class_indices.get(str(idx), f"Unknown {idx}"), confidence
|
|
|
|
| 125 |
<div style="padding-top: 1rem; margin-bottom: 2rem;">
|
| 126 |
<div class="emerald-badge">AI DIAGNOSTICS</div>
|
| 127 |
<h1 style="font-size: 3rem; font-weight: 800; color: #f8fafc; margin: 0; letter-spacing: -0.04em;">Precision Plant <span style="color: #10b981;">Pathology</span></h1>
|
| 128 |
+
<p style="color: #94a3b8; font-size: 1rem; margin-top: 0.5rem;">Clinical identification system for botanical pathogens.</p>
|
| 129 |
</div>
|
| 130 |
""", unsafe_allow_html=True)
|
| 131 |
|
| 132 |
+
# Metrics Grid
|
| 133 |
m1, m2 = st.columns(2)
|
| 134 |
with m1: st.markdown('<div class="metric-card"><h3>Library</h3><h2>38</h2></div>', unsafe_allow_html=True)
|
| 135 |
+
with m2: st.markdown('<div class="metric-card"><h3>Accuracy</h3><h2>96.5%</h2></div>', unsafe_allow_html=True)
|
| 136 |
|
| 137 |
st.markdown("<br>", unsafe_allow_html=True)
|
| 138 |
|
| 139 |
+
# 5. Fixed-Size Specimen Gallery
|
| 140 |
+
st.markdown("### Clinical Specimen Gallery")
|
| 141 |
samples = {
|
| 142 |
"Apple Rot": "apple_black rot.jpeg",
|
| 143 |
"Healthy Apple": "apple_healthy.jpeg",
|
|
|
|
| 152 |
img_path = os.path.join(BASE_DIR, filename)
|
| 153 |
with sample_cols[i]:
|
| 154 |
if os.path.exists(img_path):
|
| 155 |
+
# FIXED SMALLER SIZE: Crucial for stopping jitter
|
| 156 |
img_disp = Image.open(img_path)
|
| 157 |
+
st.image(img_disp, width=120, caption=name)
|
|
|
|
| 158 |
if st.button(f"Load {i+1}", key=f"btn_{i}"):
|
| 159 |
st.session_state.active_img = Image.open(img_path)
|
| 160 |
|
|
|
|
| 168 |
st.session_state.active_img = Image.open(uploaded_file)
|
| 169 |
|
| 170 |
if st.session_state.active_img:
|
| 171 |
+
# STRICT WIDTH CONSTRAINT for active view
|
| 172 |
+
st.image(st.session_state.active_img, width=400, caption="Active Specimen")
|
| 173 |
else:
|
| 174 |
+
st.markdown('<div style="border: 1px solid #1e293b; border-radius: 1rem; height: 260px; display: flex; align-items: center; justify-content: center; background: rgba(15, 23, 42, 0.4); color: #64748b;">Select or upload specimen</div>', unsafe_allow_html=True)
|
| 175 |
|
| 176 |
with col_res:
|
| 177 |
if st.session_state.active_img and model:
|
|
|
|
| 178 |
if st.button("RUN INTELLIGENT ANALYSIS"):
|
| 179 |
with st.spinner("Processing..."):
|
| 180 |
prediction, confidence = predict_disease(st.session_state.active_img)
|