Spaces:
Runtime error

MRM5 commited on
Commit
c0e6f50
·
verified ·
1 Parent(s): 8248837

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +45 -44
src/streamlit_app.py CHANGED
@@ -14,60 +14,63 @@ st.set_page_config(
14
  initial_sidebar_state="expanded"
15
  )
16
 
17
- # Absolute path handling for deployment
18
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
19
 
20
- # 2. Professional High-Stability Theme (Strict 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
  .main {
26
  background-color: #020617;
27
  color: #f8fafc;
28
  font-family: 'Plus Jakarta Sans', sans-serif;
29
  }
30
-
31
- /* STABILITY FIX: Lock sidebar and prevent horizontal shifts */
32
  [data-testid="stSidebar"] {
33
  background-color: #0f172a !important;
34
  border-right: 1px solid #1e293b;
35
- min-width: 300px !important;
36
- max-width: 300px !important;
37
- overflow-x: hidden !important;
 
 
 
 
38
  }
39
 
40
  .metric-card {
41
  background: rgba(15, 23, 42, 0.6);
42
  backdrop-filter: blur(16px);
43
  border: 1px solid rgba(16, 185, 129, 0.2);
44
- border-radius: 1.2rem;
45
  padding: 1.5rem;
46
  text-align: center;
47
  box-shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.5);
48
  }
49
- .metric-card h2 { color: #10b981; font-size: 2.2rem; font-weight: 800; margin: 0; }
50
 
51
- /* HIGH-VISIBILITY PRIMARY ACTION BUTTON */
52
  div[class="stButton"] > button {
53
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
54
  color: #ffffff !important;
55
  border-radius: 0.8rem;
56
- border: 2px solid rgba(255, 255, 255, 0.3);
57
- padding: 1.2rem 2rem;
58
  font-weight: 800;
59
  width: 100%;
60
  text-transform: uppercase;
61
  letter-spacing: 0.15em;
62
- font-size: 1rem;
63
- box-shadow: 0 0 25px rgba(16, 185, 129, 0.5);
64
- transition: all 0.3s ease;
65
  }
66
 
67
  div[class="stButton"] > button:hover {
68
- transform: translateY(-2px);
69
- box-shadow: 0 0 40px rgba(16, 185, 129, 0.7);
70
- border-color: #ffffff;
71
  }
72
 
73
  .result-panel {
@@ -85,13 +88,12 @@ st.markdown("""
85
  font-size: 0.7rem;
86
  font-weight: 700;
87
  border: 1px solid rgba(16, 185, 129, 0.3);
88
- display: inline-block;
89
  margin-bottom: 1rem;
90
  }
91
  </style>
92
  """, unsafe_allow_html=True)
93
 
94
- # 3. Backend Logic & Asset Loading
95
  @st.cache_resource
96
  def load_assets():
97
  model_path = os.path.join(BASE_DIR, "plant_disease_pred.h5")
@@ -105,13 +107,13 @@ def load_assets():
105
  else:
106
  indices = {str(i): f"Pathogen {i}" for i in range(38)}
107
  return model, indices
108
- except Exception as e:
109
  return None, None
110
 
111
  model, class_indices = load_assets()
112
 
113
  def predict_disease(image):
114
- # Convert to 3-channel RGB to prevent depth errors
115
  img = image.convert("RGB")
116
  img = img.resize((224, 224))
117
  img_array = np.array(img).astype('float32') / 255.
@@ -123,24 +125,24 @@ def predict_disease(image):
123
  return "Unidentified Specimen", confidence
124
  return class_indices.get(str(idx), f"Unknown {idx}"), confidence
125
 
126
- # 4. Main Diagnostic Interface
127
  st.markdown("""
128
  <div style="padding-top: 1rem; margin-bottom: 2rem;">
129
- <div class="emerald-badge">AI-POWERED DIAGNOSTICS</div>
130
  <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>
131
  <p style="color: #94a3b8; font-size: 1rem; margin-top: 0.5rem;">Diagnostic identification system for botanical pathogens.</p>
132
  </div>
133
  """, unsafe_allow_html=True)
134
 
135
- # Static Metrics Grid
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
- # 5. Stabilized Specimen Gallery
143
- st.markdown("### Clinical Specimen Gallery")
144
  samples = {
145
  "Apple Rot": "apple_black rot.jpeg",
146
  "Healthy Apple": "apple_healthy.jpeg",
@@ -155,51 +157,50 @@ 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
- # Reduced size to ensure stability and focus
159
  img_disp = Image.open(img_path)
160
- st.image(img_disp, width=160, caption=name)
161
- if st.button(f"Load {name}", key=f"btn_{i}"):
 
162
  st.session_state.active_img = Image.open(img_path)
163
 
164
- st.markdown("---")
165
 
166
  # Main Diagnostic Hub
167
- col_up, col_res = st.columns([1.4, 1])
168
  with col_up:
169
  uploaded_file = st.file_uploader("Upload Specimen", type=["jpg", "jpeg", "png"], label_visibility="collapsed")
170
  if uploaded_file:
171
  st.session_state.active_img = Image.open(uploaded_file)
172
 
173
  if st.session_state.active_img:
174
- # Fixed width for the main display to prevent "shaking"
175
- st.image(st.session_state.active_img, width=480, caption="Active Specimen")
176
  else:
177
- st.markdown('<div style="border: 2px dashed #1e293b; border-radius: 1.5rem; height: 300px; display: flex; align-items: center; justify-content: 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
- # Recognized Action Button
182
  if st.button("RUN INTELLIGENT ANALYSIS"):
183
- with st.spinner("Processing feature maps..."):
184
  prediction, confidence = predict_disease(st.session_state.active_img)
185
  st.markdown(f"""
186
  <div class="result-panel">
187
  <h4 style="margin: 0 0 1.5rem 0; color: #10b981; font-weight: 700;">Diagnostic Result</h4>
188
  <div style="display: flex; justify-content: center; margin-bottom: 2rem;">
189
- <div style="position: relative; width: 140px; height: 140px;">
190
  <svg viewBox="0 0 100 100" style="transform: rotate(-90deg); width: 100%; height: 100%;">
191
  <circle cx="50" cy="50" r="45" fill="none" stroke="#1e293b" stroke-width="6" />
192
  <circle cx="50" cy="50" r="45" fill="none" stroke="#10b981" stroke-width="6"
193
  stroke-dasharray="{2.83 * confidence} 283" stroke-linecap="round" />
194
  </svg>
195
- <div style="position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; transform: rotate(0deg);">
196
- <span style="font-size: 2rem; font-weight: 800; color: #ffffff;">{confidence:.0f}%</span>
197
- <span style="font-size: 0.6rem; color: #94a3b8; font-weight: 700; text-transform: uppercase;">Confidence</span>
198
  </div>
199
  </div>
200
  </div>
201
- <div style="border: 1px solid #1e293b; padding: 1rem; border-radius: 1rem; text-align: center;">
202
- <p style="font-size: 1.3rem; font-weight: 800; color: #ffffff; margin: 0;">{prediction}</p>
203
  </div>
204
  </div>
205
  """, unsafe_allow_html=True)
 
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: No Emojis, No Shaking
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: Strict width and overflow control */
33
  [data-testid="stSidebar"] {
34
  background-color: #0f172a !important;
35
  border-right: 1px solid #1e293b;
36
+ min-width: 280px !important;
37
+ max-width: 280px !important;
38
+ }
39
+
40
+ /* Gallery Stability: Fixed height container to prevent shifting */
41
+ .stHorizontalBlock {
42
+ align-items: flex-start !important;
43
  }
44
 
45
  .metric-card {
46
  background: rgba(15, 23, 42, 0.6);
47
  backdrop-filter: blur(16px);
48
  border: 1px solid rgba(16, 185, 129, 0.2);
49
+ border-radius: 1rem;
50
  padding: 1.5rem;
51
  text-align: center;
52
  box-shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.5);
53
  }
54
+ .metric-card h2 { color: #10b981; font-size: 2rem; font-weight: 800; margin: 0; }
55
 
56
+ /* HIGH-VISIBILITY COMMAND BUTTON */
57
  div[class="stButton"] > button {
58
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
59
  color: #ffffff !important;
60
  border-radius: 0.8rem;
61
+ border: 2px solid rgba(255, 255, 255, 0.2);
62
+ padding: 1rem 2rem;
63
  font-weight: 800;
64
  width: 100%;
65
  text-transform: uppercase;
66
  letter-spacing: 0.15em;
67
+ box-shadow: 0 0 20px rgba(16, 185, 129, 0.4);
68
+ transition: transform 0.2s ease;
 
69
  }
70
 
71
  div[class="stButton"] > button:hover {
72
+ transform: scale(1.01);
73
+ box-shadow: 0 0 40px rgba(16, 185, 129, 0.6);
 
74
  }
75
 
76
  .result-panel {
 
88
  font-size: 0.7rem;
89
  font-weight: 700;
90
  border: 1px solid rgba(16, 185, 129, 0.3);
 
91
  margin-bottom: 1rem;
92
  }
93
  </style>
94
  """, unsafe_allow_html=True)
95
 
96
+ # 3. Backend Asset Loading
97
  @st.cache_resource
98
  def load_assets():
99
  model_path = os.path.join(BASE_DIR, "plant_disease_pred.h5")
 
107
  else:
108
  indices = {str(i): f"Pathogen {i}" for i in range(38)}
109
  return model, indices
110
+ except Exception:
111
  return None, None
112
 
113
  model, class_indices = load_assets()
114
 
115
  def predict_disease(image):
116
+ # RGB conversion to prevent channel errors
117
  img = image.convert("RGB")
118
  img = img.resize((224, 224))
119
  img_array = np.array(img).astype('float32') / 255.
 
125
  return "Unidentified Specimen", confidence
126
  return class_indices.get(str(idx), f"Unknown {idx}"), confidence
127
 
128
+ # 4. Header Section
129
  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;">Diagnostic identification system for botanical pathogens.</p>
134
  </div>
135
  """, unsafe_allow_html=True)
136
 
137
+ # Fixed-height Metrics
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>CNN Accuracy</h3><h2>96.5%</h2></div>', unsafe_allow_html=True)
141
 
142
  st.markdown("<br>", unsafe_allow_html=True)
143
 
144
+ # 5. Stability-Locked Gallery
145
+ st.markdown("### Specimen Gallery")
146
  samples = {
147
  "Apple Rot": "apple_black rot.jpeg",
148
  "Healthy Apple": "apple_healthy.jpeg",
 
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
+ # Use width to prevent jitter
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
 
166
+ st.divider()
167
 
168
  # Main Diagnostic Hub
169
+ col_up, col_res = st.columns([1.2, 1])
170
  with col_up:
171
  uploaded_file = st.file_uploader("Upload Specimen", type=["jpg", "jpeg", "png"], label_visibility="collapsed")
172
  if uploaded_file:
173
  st.session_state.active_img = Image.open(uploaded_file)
174
 
175
  if st.session_state.active_img:
176
+ # Strict width constraint for the main display
177
+ st.image(st.session_state.active_img, width=440, caption="Active Specimen")
178
  else:
179
+ st.markdown('<div style="border: 1px solid #1e293b; border-radius: 1rem; height: 280px; display: flex; align-items: center; justify-content: center; background: rgba(15, 23, 42, 0.4); color: #64748b;">Select or upload specimen to begin</div>', unsafe_allow_html=True)
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)
187
  st.markdown(f"""
188
  <div class="result-panel">
189
  <h4 style="margin: 0 0 1.5rem 0; color: #10b981; font-weight: 700;">Diagnostic Result</h4>
190
  <div style="display: flex; justify-content: center; margin-bottom: 2rem;">
191
+ <div style="position: relative; width: 120px; height: 120px;">
192
  <svg viewBox="0 0 100 100" style="transform: rotate(-90deg); width: 100%; height: 100%;">
193
  <circle cx="50" cy="50" r="45" fill="none" stroke="#1e293b" stroke-width="6" />
194
  <circle cx="50" cy="50" r="45" fill="none" stroke="#10b981" stroke-width="6"
195
  stroke-dasharray="{2.83 * confidence} 283" stroke-linecap="round" />
196
  </svg>
197
+ <div style="position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; transform: rotate(0deg);">
198
+ <span style="font-size: 1.8rem; font-weight: 800; color: #ffffff;">{confidence:.0f}%</span>
 
199
  </div>
200
  </div>
201
  </div>
202
+ <div style="text-align: center; border: 1px solid #1e293b; padding: 1rem; border-radius: 0.8rem;">
203
+ <p style="font-size: 1.2rem; font-weight: 800; color: #ffffff; margin: 0;">{prediction}</p>
204
  </div>
205
  </div>
206
  """, unsafe_allow_html=True)