Spaces:
Runtime error

MRM5 commited on
Commit
dd68430
·
verified ·
1 Parent(s): acef378

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +213 -46
src/streamlit_app.py CHANGED
@@ -16,7 +16,7 @@ st.set_page_config(
16
  # Absolute path handling for Docker/HF environment
17
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
18
 
19
- # 2. Ultra-Rigid CSS: Total Layout Locking
20
  st.markdown("""
21
  <style>
22
  @import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&display=swap');
@@ -27,7 +27,7 @@ st.markdown("""
27
  font-family: 'Plus Jakarta Sans', sans-serif;
28
  }
29
 
30
- /* SIDEBAR LOCK: Strict width prevents horizontal jitter */
31
  [data-testid="stSidebar"] {
32
  background-color: #0f172a !important;
33
  border-right: 1px solid #1e293b;
@@ -35,18 +35,37 @@ st.markdown("""
35
  max-width: 260px !important;
36
  }
37
 
38
- /* ACTIVE IMAGE CONTAINER: Rigid height to prevent vertical shaking */
39
  .stImage {
40
- min-height: 280px;
41
- max-height: 280px;
42
- display: flex;
43
- align-items: center;
44
- justify-content: center;
45
  background-color: rgba(15, 23, 42, 0.4);
46
  border-radius: 1rem;
47
  border: 1px solid #1e293b;
48
  overflow: hidden;
49
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  .metric-card {
52
  background: rgba(15, 23, 42, 0.6);
@@ -57,9 +76,35 @@ st.markdown("""
57
  text-align: center;
58
  min-height: 100px;
59
  }
60
- .metric-card h2 { color: #10b981; font-size: 1.8rem; font-weight: 800; margin: 0; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- /* HIGH-VISIBILITY PRIMARY ACTION BUTTON */
63
  div[class="stButton"] > button {
64
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
65
  color: #ffffff !important;
@@ -71,6 +116,21 @@ st.markdown("""
71
  text-transform: uppercase;
72
  letter-spacing: 0.1em;
73
  box-shadow: 0 0 20px rgba(16, 185, 129, 0.4);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  }
75
  </style>
76
  """, unsafe_allow_html=True)
@@ -89,16 +149,17 @@ def load_assets():
89
  else:
90
  indices = {str(i): f"Pathogen {i}" for i in range(38)}
91
  return model, indices
92
- except Exception:
 
93
  return None, None
94
 
95
  model, class_indices = load_assets()
96
 
97
  def predict_disease(image):
98
- img = image.convert("RGB") # RGB conversion prevents depth errors
99
  img = img.resize((224, 224))
100
  img_array = np.array(img).astype('float32') / 255.
101
- predictions = model.predict(np.expand_dims(img_array, axis=0))
102
  idx = np.argmax(predictions[0])
103
  confidence = float(np.max(predictions[0]) * 100)
104
 
@@ -111,18 +172,31 @@ st.markdown("""
111
  <div style="padding-top: 1rem; margin-bottom: 2rem;">
112
  <div style="color: #10b981; font-size: 0.7rem; font-weight: 700; letter-spacing: 0.1em; margin-bottom: 0.5rem;">AI DIAGNOSTICS</div>
113
  <h1 style="font-size: 2.5rem; font-weight: 800; color: #f8fafc; margin: 0; letter-spacing: -0.04em;">Precision Plant <span style="color: #10b981;">Pathology</span></h1>
114
- <p style="color: #94a3b8; font-size: 0.9rem;">Diagnostic identification system for botanical pathogens.</p>
115
  </div>
116
  """, unsafe_allow_html=True)
117
 
118
  m1, m2 = st.columns(2)
119
- with m1: st.markdown('<div class="metric-card"><h3>Species Library</h3><h2>38</h2></div>', unsafe_allow_html=True)
120
- with m2: st.markdown('<div class="metric-card"><h3>CNN Accuracy</h3><h2>96.5%</h2></div>', unsafe_allow_html=True)
 
 
121
 
122
  st.divider()
123
 
124
- # 5. Fixed 2x2 Specimen Hub
 
 
 
 
 
 
 
 
 
 
125
  st.markdown("### Clinical Specimen Selection")
 
126
  samples = [
127
  ("Apple Rot", "apple_black rot.jpeg"),
128
  ("Healthy Apple", "apple_healthy.jpeg"),
@@ -130,62 +204,155 @@ samples = [
130
  ("Tomato Spot", "tomato_bacterial_spot.jpeg")
131
  ]
132
 
133
- if 'active_img' not in st.session_state:
134
- st.session_state.active_img = None
135
-
136
- # Stable Grid Logic: 2 Rows, 2 Columns
137
  r1_c1, r1_c2 = st.columns(2)
138
  with r1_c1:
139
- if os.path.exists(os.path.join(BASE_DIR, samples[0][1])):
140
- st.image(Image.open(os.path.join(BASE_DIR, samples[0][1])), width=110)
 
141
  if st.button(f"Analyze {samples[0][0]}", key="btn_0"):
142
- st.session_state.active_img = Image.open(os.path.join(BASE_DIR, samples[0][1]))
 
 
 
143
  with r1_c2:
144
- if os.path.exists(os.path.join(BASE_DIR, samples[1][1])):
145
- st.image(Image.open(os.path.join(BASE_DIR, samples[1][1])), width=110)
 
146
  if st.button(f"Analyze {samples[1][0]}", key="btn_1"):
147
- st.session_state.active_img = Image.open(os.path.join(BASE_DIR, samples[1][1]))
 
 
148
 
149
  r2_c1, r2_c2 = st.columns(2)
150
  with r2_c1:
151
- if os.path.exists(os.path.join(BASE_DIR, samples[2][1])):
152
- st.image(Image.open(os.path.join(BASE_DIR, samples[2][1])), width=110)
 
153
  if st.button(f"Analyze {samples[2][0]}", key="btn_2"):
154
- st.session_state.active_img = Image.open(os.path.join(BASE_DIR, samples[2][1]))
 
 
 
155
  with r2_c2:
156
- if os.path.exists(os.path.join(BASE_DIR, samples[3][1])):
157
- st.image(Image.open(os.path.join(BASE_DIR, samples[3][1])), width=110)
 
158
  if st.button(f"Analyze {samples[3][0]}", key="btn_3"):
159
- st.session_state.active_img = Image.open(os.path.join(BASE_DIR, samples[3][1]))
 
 
160
 
161
  st.divider()
162
 
163
- # 6. Static Analysis Hub (Drag-and-Drop Removed)
164
  col_up, col_res = st.columns([1.2, 1])
 
165
  with col_up:
166
- # RIGID CONTAINER height is now the primary UI anchor
167
  if st.session_state.active_img:
168
- st.image(st.session_state.active_img, width=280)
169
  else:
170
- st.markdown('<div style="border: 2px dashed #1e293b; border-radius: 1rem; height: 280px; display: flex; align-items: center; justify-content: center; background: rgba(15, 23, 42, 0.4); color: #64748b; text-align: center; padding: 20px;">Select a clinical specimen above to begin automated pathology assessment</div>', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
  with col_res:
 
173
  if st.session_state.active_img and model:
174
- if st.button("RUN INTELLIGENT ANALYSIS"):
175
  with st.spinner("Analyzing feature maps..."):
176
  prediction, confidence = predict_disease(st.session_state.active_img)
177
- st.markdown(f"""
178
- <div style="background: #0f172a; border: 1px solid #1e293b; border-radius: 1rem; padding: 1.5rem; text-align: center;">
179
- <h4 style="color: #10b981; margin-bottom: 1rem;">Diagnostic Result</h4>
180
- <div style="font-size: 2.2rem; font-weight: 800; color: #ffffff;">{confidence:.0f}%</div>
181
- <div style="color: #94a3b8; font-size: 0.6rem; text-transform: uppercase; margin-bottom: 1rem;">Confidence Score</div>
182
- <div style="border: 1px solid #1e293b; padding: 0.8rem; border-radius: 0.5rem; font-weight: 700;">{prediction}</div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  </div>
184
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
 
186
  # Footer
187
  st.markdown("""
188
- <div style="margin-top: 3rem; padding: 2rem; border-top: 1px solid #1e293b; text-align: center; color: #475569; font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.1em;">
 
 
 
 
 
 
 
 
 
189
  SYSTEMS ENGINEER: RMUTUA | PRECISION AGRO-PATHOLOGY
190
  </div>
191
  """, unsafe_allow_html=True)
 
16
  # Absolute path handling for Docker/HF environment
17
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
18
 
19
+ # 2. Enhanced CSS: Eliminate All Layout Shifts
20
  st.markdown("""
21
  <style>
22
  @import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&display=swap');
 
27
  font-family: 'Plus Jakarta Sans', sans-serif;
28
  }
29
 
30
+ /* SIDEBAR LOCK */
31
  [data-testid="stSidebar"] {
32
  background-color: #0f172a !important;
33
  border-right: 1px solid #1e293b;
 
35
  max-width: 260px !important;
36
  }
37
 
38
+ /* CRITICAL: Lock all containers to prevent layout shifts */
39
  .stImage {
40
+ min-height: 280px !important;
41
+ max-height: 280px !important;
42
+ display: flex !important;
43
+ align-items: center !important;
44
+ justify-content: center !important;
45
  background-color: rgba(15, 23, 42, 0.4);
46
  border-radius: 1rem;
47
  border: 1px solid #1e293b;
48
  overflow: hidden;
49
  }
50
+
51
+ .stImage img {
52
+ max-height: 280px !important;
53
+ width: auto !important;
54
+ object-fit: contain;
55
+ }
56
+
57
+ /* Lock column heights to prevent vertical shifting */
58
+ [data-testid="column"] {
59
+ min-height: 350px;
60
+ }
61
+
62
+ /* Prevent spinner from causing layout shift */
63
+ .stSpinner {
64
+ position: absolute;
65
+ top: 50%;
66
+ left: 50%;
67
+ transform: translate(-50%, -50%);
68
+ }
69
 
70
  .metric-card {
71
  background: rgba(15, 23, 42, 0.6);
 
76
  text-align: center;
77
  min-height: 100px;
78
  }
79
+ .metric-card h2 {
80
+ color: #10b981;
81
+ font-size: 1.8rem;
82
+ font-weight: 800;
83
+ margin: 0;
84
+ }
85
+ .metric-card h3 {
86
+ color: #94a3b8;
87
+ font-size: 0.8rem;
88
+ margin-bottom: 0.5rem;
89
+ font-weight: 600;
90
+ }
91
+
92
+ /* Sample grid buttons - consistent sizing */
93
+ .sample-grid-btn {
94
+ background: rgba(15, 23, 42, 0.8);
95
+ border: 1px solid #1e293b;
96
+ border-radius: 0.5rem;
97
+ padding: 0.5rem;
98
+ text-align: center;
99
+ transition: all 0.2s;
100
+ min-height: 180px;
101
+ display: flex;
102
+ flex-direction: column;
103
+ align-items: center;
104
+ justify-content: center;
105
+ }
106
 
107
+ /* PRIMARY ACTION BUTTON */
108
  div[class="stButton"] > button {
109
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
110
  color: #ffffff !important;
 
116
  text-transform: uppercase;
117
  letter-spacing: 0.1em;
118
  box-shadow: 0 0 20px rgba(16, 185, 129, 0.4);
119
+ transition: all 0.3s ease;
120
+ }
121
+
122
+ div[class="stButton"] > button:hover {
123
+ background: linear-gradient(90deg, #059669 0%, #047857 100%);
124
+ box-shadow: 0 0 30px rgba(16, 185, 129, 0.6);
125
+ transform: translateY(-2px);
126
+ }
127
+
128
+ /* Result container - fixed height */
129
+ .result-container {
130
+ min-height: 280px;
131
+ display: flex;
132
+ align-items: center;
133
+ justify-content: center;
134
  }
135
  </style>
136
  """, unsafe_allow_html=True)
 
149
  else:
150
  indices = {str(i): f"Pathogen {i}" for i in range(38)}
151
  return model, indices
152
+ except Exception as e:
153
+ st.error(f"Model loading failed: {e}")
154
  return None, None
155
 
156
  model, class_indices = load_assets()
157
 
158
  def predict_disease(image):
159
+ img = image.convert("RGB")
160
  img = img.resize((224, 224))
161
  img_array = np.array(img).astype('float32') / 255.
162
+ predictions = model.predict(np.expand_dims(img_array, axis=0), verbose=0)
163
  idx = np.argmax(predictions[0])
164
  confidence = float(np.max(predictions[0]) * 100)
165
 
 
172
  <div style="padding-top: 1rem; margin-bottom: 2rem;">
173
  <div style="color: #10b981; font-size: 0.7rem; font-weight: 700; letter-spacing: 0.1em; margin-bottom: 0.5rem;">AI DIAGNOSTICS</div>
174
  <h1 style="font-size: 2.5rem; font-weight: 800; color: #f8fafc; margin: 0; letter-spacing: -0.04em;">Precision Plant <span style="color: #10b981;">Pathology</span></h1>
175
+ <p style="color: #94a3b8; font-size: 0.9rem; margin-top: 0.5rem;">Diagnostic identification system for botanical pathogens.</p>
176
  </div>
177
  """, unsafe_allow_html=True)
178
 
179
  m1, m2 = st.columns(2)
180
+ with m1:
181
+ st.markdown('<div class="metric-card"><h3>Species Library</h3><h2>38</h2></div>', unsafe_allow_html=True)
182
+ with m2:
183
+ st.markdown('<div class="metric-card"><h3>CNN Accuracy</h3><h2>96.5%</h2></div>', unsafe_allow_html=True)
184
 
185
  st.divider()
186
 
187
+ # 5. Initialize session state
188
+ if 'active_img' not in st.session_state:
189
+ st.session_state.active_img = None
190
+ if 'show_result' not in st.session_state:
191
+ st.session_state.show_result = False
192
+ if 'prediction' not in st.session_state:
193
+ st.session_state.prediction = None
194
+ if 'confidence' not in st.session_state:
195
+ st.session_state.confidence = None
196
+
197
+ # 6. Sample Selection Grid
198
  st.markdown("### Clinical Specimen Selection")
199
+
200
  samples = [
201
  ("Apple Rot", "apple_black rot.jpeg"),
202
  ("Healthy Apple", "apple_healthy.jpeg"),
 
204
  ("Tomato Spot", "tomato_bacterial_spot.jpeg")
205
  ]
206
 
207
+ # 2x2 Grid with consistent layout
 
 
 
208
  r1_c1, r1_c2 = st.columns(2)
209
  with r1_c1:
210
+ sample_path = os.path.join(BASE_DIR, samples[0][1])
211
+ if os.path.exists(sample_path):
212
+ st.image(Image.open(sample_path), width=110)
213
  if st.button(f"Analyze {samples[0][0]}", key="btn_0"):
214
+ st.session_state.active_img = Image.open(sample_path)
215
+ st.session_state.show_result = False
216
+ st.rerun()
217
+
218
  with r1_c2:
219
+ sample_path = os.path.join(BASE_DIR, samples[1][1])
220
+ if os.path.exists(sample_path):
221
+ st.image(Image.open(sample_path), width=110)
222
  if st.button(f"Analyze {samples[1][0]}", key="btn_1"):
223
+ st.session_state.active_img = Image.open(sample_path)
224
+ st.session_state.show_result = False
225
+ st.rerun()
226
 
227
  r2_c1, r2_c2 = st.columns(2)
228
  with r2_c1:
229
+ sample_path = os.path.join(BASE_DIR, samples[2][1])
230
+ if os.path.exists(sample_path):
231
+ st.image(Image.open(sample_path), width=110)
232
  if st.button(f"Analyze {samples[2][0]}", key="btn_2"):
233
+ st.session_state.active_img = Image.open(sample_path)
234
+ st.session_state.show_result = False
235
+ st.rerun()
236
+
237
  with r2_c2:
238
+ sample_path = os.path.join(BASE_DIR, samples[3][1])
239
+ if os.path.exists(sample_path):
240
+ st.image(Image.open(sample_path), width=110)
241
  if st.button(f"Analyze {samples[3][0]}", key="btn_3"):
242
+ st.session_state.active_img = Image.open(sample_path)
243
+ st.session_state.show_result = False
244
+ st.rerun()
245
 
246
  st.divider()
247
 
248
+ # 7. Analysis Hub - Fixed Height Layout
249
  col_up, col_res = st.columns([1.2, 1])
250
+
251
  with col_up:
 
252
  if st.session_state.active_img:
253
+ st.image(st.session_state.active_img, use_container_width=True)
254
  else:
255
+ st.markdown('''
256
+ <div style="
257
+ border: 2px dashed #1e293b;
258
+ border-radius: 1rem;
259
+ height: 280px;
260
+ display: flex;
261
+ align-items: center;
262
+ justify-content: center;
263
+ background: rgba(15, 23, 42, 0.4);
264
+ color: #64748b;
265
+ text-align: center;
266
+ padding: 20px;
267
+ ">
268
+ <div>
269
+ <p style="font-size: 1rem; font-weight: 600; margin-bottom: 0.5rem;">No Specimen Selected</p>
270
+ <p style="font-size: 0.8rem; opacity: 0.7;">Select a clinical specimen above to begin automated pathology assessment</p>
271
+ </div>
272
+ </div>
273
+ ''', unsafe_allow_html=True)
274
 
275
  with col_res:
276
+ # Always render container with fixed height
277
  if st.session_state.active_img and model:
278
+ if st.button("🔬 RUN INTELLIGENT ANALYSIS", key="analyze_btn"):
279
  with st.spinner("Analyzing feature maps..."):
280
  prediction, confidence = predict_disease(st.session_state.active_img)
281
+ st.session_state.prediction = prediction
282
+ st.session_state.confidence = confidence
283
+ st.session_state.show_result = True
284
+
285
+ # Display result if available
286
+ if st.session_state.show_result and st.session_state.prediction:
287
+ st.markdown(f"""
288
+ <div class="result-container">
289
+ <div style="
290
+ background: #0f172a;
291
+ border: 1px solid #1e293b;
292
+ border-radius: 1rem;
293
+ padding: 1.5rem;
294
+ text-align: center;
295
+ width: 100%;
296
+ ">
297
+ <h4 style="color: #10b981; margin-bottom: 1rem; font-size: 0.9rem; text-transform: uppercase; letter-spacing: 0.1em;">Diagnostic Result</h4>
298
+ <div style="font-size: 2.8rem; font-weight: 800; color: #ffffff; margin-bottom: 0.5rem;">{st.session_state.confidence:.1f}%</div>
299
+ <div style="color: #94a3b8; font-size: 0.65rem; text-transform: uppercase; margin-bottom: 1.5rem; letter-spacing: 0.05em;">Confidence Score</div>
300
+ <div style="
301
+ border: 1px solid #1e293b;
302
+ padding: 1rem;
303
+ border-radius: 0.5rem;
304
+ font-weight: 700;
305
+ background: rgba(16, 185, 129, 0.05);
306
+ font-size: 0.95rem;
307
+ ">{st.session_state.prediction}</div>
308
+ </div>
309
+ </div>
310
+ """, unsafe_allow_html=True)
311
+ else:
312
+ st.markdown('''
313
+ <div class="result-container">
314
+ <div style="
315
+ background: rgba(15, 23, 42, 0.4);
316
+ border: 1px dashed #1e293b;
317
+ border-radius: 1rem;
318
+ padding: 2rem;
319
+ text-align: center;
320
+ color: #64748b;
321
+ width: 100%;
322
+ ">
323
+ <p style="font-size: 0.9rem;">Click the button above to start analysis</p>
324
  </div>
325
+ </div>
326
+ ''', unsafe_allow_html=True)
327
+ else:
328
+ st.markdown('''
329
+ <div class="result-container">
330
+ <div style="
331
+ background: rgba(15, 23, 42, 0.4);
332
+ border: 1px dashed #1e293b;
333
+ border-radius: 1rem;
334
+ padding: 2rem;
335
+ text-align: center;
336
+ color: #64748b;
337
+ width: 100%;
338
+ ">
339
+ <p style="font-size: 0.9rem;">Results will appear here</p>
340
+ </div>
341
+ </div>
342
+ ''', unsafe_allow_html=True)
343
 
344
  # Footer
345
  st.markdown("""
346
+ <div style="
347
+ margin-top: 3rem;
348
+ padding: 2rem;
349
+ border-top: 1px solid #1e293b;
350
+ text-align: center;
351
+ color: #475569;
352
+ font-size: 0.65rem;
353
+ text-transform: uppercase;
354
+ letter-spacing: 0.1em;
355
+ ">
356
  SYSTEMS ENGINEER: RMUTUA | PRECISION AGRO-PATHOLOGY
357
  </div>
358
  """, unsafe_allow_html=True)