Spaces:
Runtime error

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

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +18 -21
src/streamlit_app.py CHANGED
@@ -14,22 +14,21 @@ st.set_page_config(
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;
@@ -38,7 +37,6 @@ st.markdown("""
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,7 +48,7 @@ st.markdown("""
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,6 +61,7 @@ st.markdown("""
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 {
@@ -97,9 +96,6 @@ def load_assets():
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:
@@ -109,12 +105,12 @@ def load_assets():
109
  indices = {str(i): f"Pathogen {i}" for i in range(38)}
110
  return model, indices
111
  except Exception as e:
112
- st.error(f"System Load Error: {e}")
113
  return None, None
114
 
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.
@@ -123,13 +119,13 @@ def predict_disease(image):
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>
@@ -142,7 +138,7 @@ with m2: st.markdown('<div class="metric-card"><h3>CNN Accuracy</h3><h2>96.5%</h
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,8 +154,9 @@ for i, (name, filename) in enumerate(samples.items()):
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
 
@@ -172,9 +169,10 @@ with col_up:
172
  st.session_state.active_img = Image.open(uploaded_file)
173
 
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:
@@ -183,7 +181,7 @@ with col_res:
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,8 +190,7 @@ with col_res:
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>
 
14
  initial_sidebar_state="expanded"
15
  )
16
 
17
+ # Absolute path handling for Hugging Face environment
18
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
19
 
20
+ # High-Visibility Professional Theme with Layout Stability 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
  .main {
26
  background-color: #020617;
27
  color: #f8fafc;
28
  font-family: 'Plus Jakarta Sans', sans-serif;
29
  }
30
 
31
+ /* Sidebar Stability Fix */
32
  [data-testid="stSidebar"] {
33
  background-color: #0f172a !important;
34
  border-right: 1px solid #1e293b;
 
37
  overflow-x: hidden !important;
38
  }
39
 
 
40
  .metric-card {
41
  background: rgba(15, 23, 42, 0.6);
42
  backdrop-filter: blur(16px);
 
48
  }
49
  .metric-card h2 { color: #10b981; font-size: 2.5rem; font-weight: 800; margin: 0; }
50
 
51
+ /* ACTION BUTTON GLOW */
52
  div[class="stButton"] > button {
53
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
54
  color: #ffffff !important;
 
61
  letter-spacing: 0.15em;
62
  font-size: 1.1rem;
63
  box-shadow: 0 0 30px rgba(16, 185, 129, 0.4);
64
+ transition: all 0.4s ease;
65
  }
66
 
67
  div[class="stButton"] > button:hover {
 
96
  model_path = os.path.join(BASE_DIR, "plant_disease_pred.h5")
97
  class_indices_path = os.path.join(BASE_DIR, "class_indices.json")
98
  try:
 
 
 
99
  model = keras.models.load_model(model_path)
100
  if os.path.exists(class_indices_path):
101
  with open(class_indices_path, 'r') as f:
 
105
  indices = {str(i): f"Pathogen {i}" for i in range(38)}
106
  return model, indices
107
  except Exception as e:
 
108
  return None, None
109
 
110
  model, class_indices = load_assets()
111
 
112
  def predict_disease(image):
113
+ # Fixed 3-channel conversion
114
  img = image.convert("RGB")
115
  img = img.resize((224, 224))
116
  img_array = np.array(img).astype('float32') / 255.
 
119
  confidence = float(np.max(predictions[0]) * 100)
120
 
121
  if confidence < 75:
122
+ return "Unidentified Specimen", confidence
123
  return class_indices.get(str(idx), f"Unknown {idx}"), confidence
124
 
125
+ # 3. Main Interface Header
126
  st.markdown("""
127
  <div style="padding-top: 1rem; margin-bottom: 3rem;">
128
+ <div class="emerald-badge">AI-POWERED DIAGNOSTICS</div>
129
  <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>
130
  <p style="color: #94a3b8; font-size: 1.1rem; margin-top: 0.5rem;">Diagnostic identification system for botanical pathogens.</p>
131
  </div>
 
138
 
139
  st.markdown("<br>", unsafe_allow_html=True)
140
 
141
+ # 4. Specimen Gallery with Fixed Sizing to prevent jitter
142
  st.markdown("### Clinical Specimen Gallery")
143
  samples = {
144
  "Apple Rot": "apple_black rot.jpeg",
 
154
  img_path = os.path.join(BASE_DIR, filename)
155
  with sample_cols[i]:
156
  if os.path.exists(img_path):
157
+ img_disp = Image.open(img_path)
158
+ # Reduced size and fixed width to stop shaking
159
+ st.image(img_disp, width=220, caption=name)
160
  if st.button(f"Load {name}", key=f"btn_{i}"):
161
  st.session_state.active_img = Image.open(img_path)
162
 
 
169
  st.session_state.active_img = Image.open(uploaded_file)
170
 
171
  if st.session_state.active_img:
172
+ # Balanced sizing for high-resolution uploads
173
+ st.image(st.session_state.active_img, width=500, caption="Active Specimen")
174
  else:
175
+ 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)
176
 
177
  with col_res:
178
  if st.session_state.active_img and model:
 
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
  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>