Spaces:
Runtime error

MRM5 commited on
Commit
0bbfe64
·
verified ·
1 Parent(s): 7ad1455

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +25 -22
src/streamlit_app.py CHANGED
@@ -14,26 +14,31 @@ st.set_page_config(
14
  initial_sidebar_state="expanded"
15
  )
16
 
17
- # Absolute path handling for Hugging Face
18
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
19
 
20
- # High-Visibility Professional Theme
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
- section[data-testid="stSidebar"] {
 
32
  background-color: #0f172a !important;
33
  border-right: 1px solid #1e293b;
 
 
 
34
  }
35
 
36
- /* Professional Metric Cards */
37
  .metric-card {
38
  background: rgba(15, 23, 42, 0.6);
39
  backdrop-filter: blur(16px);
@@ -45,7 +50,7 @@ st.markdown("""
45
  }
46
  .metric-card h2 { color: #10b981; font-size: 2.5rem; font-weight: 800; margin: 0; }
47
 
48
- /* HIGH-VISIBILITY COMMAND BUTTON */
49
  div[class="stButton"] > button {
50
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
51
  color: #ffffff !important;
@@ -58,13 +63,10 @@ st.markdown("""
58
  letter-spacing: 0.15em;
59
  font-size: 1.1rem;
60
  box-shadow: 0 0 30px rgba(16, 185, 129, 0.4);
61
- transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
62
  }
63
 
64
  div[class="stButton"] > button:hover {
65
- transform: scale(1.03);
66
- background: linear-gradient(90deg, #059669 0%, #10b981 100%);
67
- border-color: #ffffff;
68
  box-shadow: 0 0 50px rgba(16, 185, 129, 0.6);
69
  }
70
 
@@ -95,6 +97,9 @@ def load_assets():
95
  model_path = os.path.join(BASE_DIR, "plant_disease_pred.h5")
96
  class_indices_path = os.path.join(BASE_DIR, "class_indices.json")
97
  try:
 
 
 
98
  model = keras.models.load_model(model_path)
99
  if os.path.exists(class_indices_path):
100
  with open(class_indices_path, 'r') as f:
@@ -110,7 +115,6 @@ def load_assets():
110
  model, class_indices = load_assets()
111
 
112
  def predict_disease(image):
113
- # Standardize to RGB
114
  img = image.convert("RGB")
115
  img = img.resize((224, 224))
116
  img_array = np.array(img).astype('float32') / 255.
@@ -118,28 +122,27 @@ def predict_disease(image):
118
  idx = np.argmax(predictions[0])
119
  confidence = float(np.max(predictions[0]) * 100)
120
 
121
- # Reliability Guardrail
122
  if confidence < 75:
123
- return "Non-Botanical / Unidentified", confidence
124
  return class_indices.get(str(idx), f"Unknown {idx}"), confidence
125
 
126
  # 3. Main Interface
127
  st.markdown("""
128
  <div style="padding-top: 1rem; margin-bottom: 3rem;">
129
- <div class="emerald-badge">AI-POWERED DIAGNOSTICS</div>
130
  <h1 style="font-size: 3.5rem; font-weight: 800; color: #f8fafc; margin: 0; letter-spacing: -0.05em;">Precision Plant <span style="color: #10b981;">Pathology</span></h1>
131
- <p style="color: #94a3b8; font-size: 1.1rem; margin-top: 0.5rem;">Diagnostic identification of botanical pathogens.</p>
132
  </div>
133
  """, unsafe_allow_html=True)
134
 
135
- # Metrics Section
136
  m1, m2 = st.columns(2)
137
  with m1: st.markdown('<div class="metric-card"><h3>Species Library</h3><h2>38</h2></div>', unsafe_allow_html=True)
138
  with m2: st.markdown('<div class="metric-card"><h3>CNN Accuracy</h3><h2>96.5%</h2></div>', unsafe_allow_html=True)
139
 
140
  st.markdown("<br>", unsafe_allow_html=True)
141
 
142
- # 4. Gallery
143
  st.markdown("### Clinical Specimen Gallery")
144
  samples = {
145
  "Apple Rot": "apple_black rot.jpeg",
@@ -155,10 +158,10 @@ for i, (name, filename) in enumerate(samples.items()):
155
  img_path = os.path.join(BASE_DIR, filename)
156
  with sample_cols[i]:
157
  if os.path.exists(img_path):
158
- img_disp = Image.open(img_path)
159
  st.image(img_disp, use_container_width=True, caption=name)
160
  if st.button(f"Load {name}", key=f"btn_{i}"):
161
- st.session_state.active_img = img_disp
162
 
163
  st.markdown("---")
164
 
@@ -171,17 +174,16 @@ with col_up:
171
  if st.session_state.active_img:
172
  st.image(st.session_state.active_img, use_container_width=True, caption="Active Specimen")
173
  else:
174
- st.markdown('<div style="border: 2px dashed #1e293b; border-radius: 2rem; padding: 6rem; text-align: center; background: rgba(15, 23, 42, 0.4); color: #64748b;">Load a sample or upload a specimen</div>', unsafe_allow_html=True)
175
 
176
  with col_res:
177
  if st.session_state.active_img and model:
178
- # High-Impact identifiable button
179
  if st.button("RUN INTELLIGENT ANALYSIS"):
180
  with st.spinner("Processing feature maps..."):
181
  prediction, confidence = predict_disease(st.session_state.active_img)
182
  st.markdown(f"""
183
  <div class="result-panel">
184
- <h4 style="margin: 0 0 2rem 0; color: #10b981; font-weight: 700;">Diagnostic Assessment</h4>
185
  <div style="display: flex; justify-content: center; margin-bottom: 2.5rem;">
186
  <div style="position: relative; width: 150px; height: 150px;">
187
  <svg viewBox="0 0 100 100" style="transform: rotate(-90deg); width: 100%; height: 100%;">
@@ -190,7 +192,8 @@ with col_res:
190
  stroke-dasharray="{2.83 * confidence} 283" stroke-linecap="round" />
191
  </svg>
192
  <div style="position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; transform: rotate(0deg);">
193
- <span style="font-size: 2.25rem; font-weight: 800; color: #ffffff;">{confidence:.0f}%</span>
 
194
  </div>
195
  </div>
196
  </div>
 
14
  initial_sidebar_state="expanded"
15
  )
16
 
17
+ # Absolute path handling
18
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
19
 
20
+ # High-Visibility Professional Theme with Jitter Fixes
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
+ /* Sidebar Jitter Fix: Lock width and prevent horizontal overflow */
33
+ [data-testid="stSidebar"] {
34
  background-color: #0f172a !important;
35
  border-right: 1px solid #1e293b;
36
+ min-width: 300px !important;
37
+ max-width: 300px !important;
38
+ overflow-x: hidden !important;
39
  }
40
 
41
+ /* Metric Card Optimization */
42
  .metric-card {
43
  background: rgba(15, 23, 42, 0.6);
44
  backdrop-filter: blur(16px);
 
50
  }
51
  .metric-card h2 { color: #10b981; font-size: 2.5rem; font-weight: 800; margin: 0; }
52
 
53
+ /* Action Button Visibility */
54
  div[class="stButton"] > button {
55
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
56
  color: #ffffff !important;
 
63
  letter-spacing: 0.15em;
64
  font-size: 1.1rem;
65
  box-shadow: 0 0 30px rgba(16, 185, 129, 0.4);
 
66
  }
67
 
68
  div[class="stButton"] > button:hover {
69
+ transform: scale(1.02);
 
 
70
  box-shadow: 0 0 50px rgba(16, 185, 129, 0.6);
71
  }
72
 
 
97
  model_path = os.path.join(BASE_DIR, "plant_disease_pred.h5")
98
  class_indices_path = os.path.join(BASE_DIR, "class_indices.json")
99
  try:
100
+ if not os.path.exists(model_path):
101
+ st.error("Model file not found. Ensure plant_disease_pred.h5 is in the root directory.")
102
+ return None, None
103
  model = keras.models.load_model(model_path)
104
  if os.path.exists(class_indices_path):
105
  with open(class_indices_path, 'r') as f:
 
115
  model, class_indices = load_assets()
116
 
117
  def predict_disease(image):
 
118
  img = image.convert("RGB")
119
  img = img.resize((224, 224))
120
  img_array = np.array(img).astype('float32') / 255.
 
122
  idx = np.argmax(predictions[0])
123
  confidence = float(np.max(predictions[0]) * 100)
124
 
 
125
  if confidence < 75:
126
+ return "Unidentified / Low Confidence", confidence
127
  return class_indices.get(str(idx), f"Unknown {idx}"), confidence
128
 
129
  # 3. Main Interface
130
  st.markdown("""
131
  <div style="padding-top: 1rem; margin-bottom: 3rem;">
132
+ <div class="emerald-badge">⚡ AI DIAGNOSTIC ENGINE</div>
133
  <h1 style="font-size: 3.5rem; font-weight: 800; color: #f8fafc; margin: 0; letter-spacing: -0.05em;">Precision Plant <span style="color: #10b981;">Pathology</span></h1>
134
+ <p style="color: #94a3b8; font-size: 1.1rem; margin-top: 0.5rem;">Diagnostic identification system for botanical pathogens.</p>
135
  </div>
136
  """, unsafe_allow_html=True)
137
 
138
+ # Metrics Grid
139
  m1, m2 = st.columns(2)
140
  with m1: st.markdown('<div class="metric-card"><h3>Species Library</h3><h2>38</h2></div>', unsafe_allow_html=True)
141
  with m2: st.markdown('<div class="metric-card"><h3>CNN Accuracy</h3><h2>96.5%</h2></div>', unsafe_allow_html=True)
142
 
143
  st.markdown("<br>", unsafe_allow_html=True)
144
 
145
+ # 4. Clinical Specimen Gallery
146
  st.markdown("### Clinical Specimen Gallery")
147
  samples = {
148
  "Apple Rot": "apple_black rot.jpeg",
 
158
  img_path = os.path.join(BASE_DIR, filename)
159
  with sample_cols[i]:
160
  if os.path.exists(img_path):
161
+ img_disp = Image.open(img_path).resize((400, 300))
162
  st.image(img_disp, use_container_width=True, caption=name)
163
  if st.button(f"Load {name}", key=f"btn_{i}"):
164
+ st.session_state.active_img = Image.open(img_path)
165
 
166
  st.markdown("---")
167
 
 
174
  if st.session_state.active_img:
175
  st.image(st.session_state.active_img, use_container_width=True, caption="Active Specimen")
176
  else:
177
+ st.markdown('<div style="border: 2px dashed #1e293b; border-radius: 2rem; padding: 6rem; text-align: center; background: rgba(15, 23, 42, 0.4); color: #64748b;">Upload or select a specimen to begin analysis</div>', unsafe_allow_html=True)
178
 
179
  with col_res:
180
  if st.session_state.active_img and model:
 
181
  if st.button("RUN INTELLIGENT ANALYSIS"):
182
  with st.spinner("Processing feature maps..."):
183
  prediction, confidence = predict_disease(st.session_state.active_img)
184
  st.markdown(f"""
185
  <div class="result-panel">
186
+ <h4 style="margin: 0 0 2rem 0; color: #10b981; font-weight: 700;">Diagnostic Result</h4>
187
  <div style="display: flex; justify-content: center; margin-bottom: 2.5rem;">
188
  <div style="position: relative; width: 150px; height: 150px;">
189
  <svg viewBox="0 0 100 100" style="transform: rotate(-90deg); width: 100%; height: 100%;">
 
192
  stroke-dasharray="{2.83 * confidence} 283" stroke-linecap="round" />
193
  </svg>
194
  <div style="position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; transform: rotate(0deg);">
195
+ <span style="font-size: 2rem; font-weight: 800; color: #ffffff;">{confidence:.0f}%</span>
196
+ <span style="font-size: 0.6rem; color: #94a3b8; font-weight: 700; text-transform: uppercase;">Confidence</span>
197
  </div>
198
  </div>
199
  </div>