Spaces:
Runtime error

MRM5 commited on
Commit
7da92c9
·
verified ·
1 Parent(s): a0a8103

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +22 -13
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-Stable CSS: Container 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');
@@ -35,6 +35,7 @@ st.markdown("""
35
  max-width: 260px !important;
36
  }
37
 
 
38
  .metric-card {
39
  background: rgba(15, 23, 42, 0.6);
40
  backdrop-filter: blur(16px);
@@ -46,7 +47,7 @@ st.markdown("""
46
  }
47
  .metric-card h2 { color: #10b981; font-size: 1.8rem; font-weight: 800; margin: 0; }
48
 
49
- /* HIGH-VISIBILITY BUTTON */
50
  div[class="stButton"] > button {
51
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
52
  color: #ffffff !important;
@@ -59,6 +60,12 @@ st.markdown("""
59
  letter-spacing: 0.1em;
60
  box-shadow: 0 0 20px rgba(16, 185, 129, 0.4);
61
  }
 
 
 
 
 
 
62
  </style>
63
  """, unsafe_allow_html=True)
64
 
@@ -82,7 +89,7 @@ def load_assets():
82
  model, class_indices = load_assets()
83
 
84
  def predict_disease(image):
85
- img = image.convert("RGB") # RGB conversion prevents depth errors
86
  img = img.resize((224, 224))
87
  img_array = np.array(img).astype('float32') / 255.
88
  predictions = model.predict(np.expand_dims(img_array, axis=0))
@@ -93,7 +100,7 @@ def predict_disease(image):
93
  return "Unidentified Specimen", confidence
94
  return class_indices.get(str(idx), f"Unknown {idx}"), confidence
95
 
96
- # 4. Header
97
  st.markdown("""
98
  <div style="padding-top: 1rem; margin-bottom: 2rem;">
99
  <div style="color: #10b981; font-size: 0.7rem; font-weight: 700; letter-spacing: 0.1em; margin-bottom: 0.5rem;">AI DIAGNOSTICS</div>
@@ -102,10 +109,10 @@ st.markdown("""
102
  """, unsafe_allow_html=True)
103
 
104
  m1, m2 = st.columns(2)
105
- with m1: st.markdown('<div class="metric-card"><h3>Library</h3><h2>38</h2></div>', unsafe_allow_html=True)
106
- with m2: st.markdown('<div class="metric-card"><h3>Accuracy</h3><h2>96.5%</h2></div>', unsafe_allow_html=True)
107
 
108
- # 5. 2x2 Clinical Gallery Logic
109
  st.markdown("### Clinical Specimens")
110
  samples = [
111
  ("Apple Rot", "apple_black rot.jpeg"),
@@ -117,6 +124,7 @@ samples = [
117
  if 'active_img' not in st.session_state:
118
  st.session_state.active_img = None
119
 
 
120
  # Row 1 (Items 1 & 2)
121
  row1_cols = st.columns(2)
122
  for i in range(2):
@@ -124,7 +132,7 @@ for i in range(2):
124
  img_path = os.path.join(BASE_DIR, filename)
125
  with row1_cols[i]:
126
  if os.path.exists(img_path):
127
- st.image(Image.open(img_path), width=130) # Fixed size
128
  if st.button(f"Load {name}", key=f"btn_{i}"):
129
  st.session_state.active_img = Image.open(img_path)
130
 
@@ -135,13 +143,13 @@ for i in range(2, 4):
135
  img_path = os.path.join(BASE_DIR, filename)
136
  with row2_cols[i-2]:
137
  if os.path.exists(img_path):
138
- st.image(Image.open(img_path), width=130) # Fixed size
139
  if st.button(f"Load {name}", key=f"btn_{i}"):
140
  st.session_state.active_img = Image.open(img_path)
141
 
142
  st.divider()
143
 
144
- # Main Diagnostic Hub
145
  col_up, col_res = st.columns([1.2, 1])
146
  with col_up:
147
  uploaded_file = st.file_uploader("Upload", type=["jpg", "jpeg", "png"], label_visibility="collapsed")
@@ -149,14 +157,15 @@ with col_up:
149
  st.session_state.active_img = Image.open(uploaded_file)
150
 
151
  if st.session_state.active_img:
152
- st.image(st.session_state.active_img, width=320) # Stabilized width
 
153
  else:
154
  st.markdown('<div style="border: 1px solid #1e293b; border-radius: 1rem; height: 180px; background: rgba(15, 23, 42, 0.4);"></div>', unsafe_allow_html=True)
155
 
156
  with col_res:
157
  if st.session_state.active_img and model:
158
  if st.button("RUN INTELLIGENT ANALYSIS"):
159
- with st.spinner("Analyzing..."):
160
  prediction, confidence = predict_disease(st.session_state.active_img)
161
  st.markdown(f"""
162
  <div style="background: #0f172a; border: 1px solid #1e293b; border-radius: 1rem; padding: 1.5rem; text-align: center;">
@@ -169,7 +178,7 @@ with col_res:
169
 
170
  # Footer
171
  st.markdown("""
172
- <div style="margin-top: 3rem; padding: 2rem; border-top: 1px solid #1e293b; text-align: center; color: #475569; font-size: 0.65rem; text-transform: uppercase;">
173
  SYSTEMS ENGINEER: RMUTUA | PRECISION AGRO-PATHOLOGY
174
  </div>
175
  """, 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. Ultra-Stable CSS: Rigid Container 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');
 
35
  max-width: 260px !important;
36
  }
37
 
38
+ /* METRIC CARDS: Fixed height to prevent vertical shifting */
39
  .metric-card {
40
  background: rgba(15, 23, 42, 0.6);
41
  backdrop-filter: blur(16px);
 
47
  }
48
  .metric-card h2 { color: #10b981; font-size: 1.8rem; font-weight: 800; margin: 0; }
49
 
50
+ /* HIGH-VISIBILITY COMMAND BUTTON */
51
  div[class="stButton"] > button {
52
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
53
  color: #ffffff !important;
 
60
  letter-spacing: 0.1em;
61
  box-shadow: 0 0 20px rgba(16, 185, 129, 0.4);
62
  }
63
+
64
+ /* IMAGE CONTAINER STABILITY: Reserves space before load */
65
+ [data-testid="stImage"] {
66
+ background-color: rgba(15, 23, 42, 0.2);
67
+ border-radius: 0.5rem;
68
+ }
69
  </style>
70
  """, unsafe_allow_html=True)
71
 
 
89
  model, class_indices = load_assets()
90
 
91
  def predict_disease(image):
92
+ img = image.convert("RGB") # Fixes channel mismatch
93
  img = img.resize((224, 224))
94
  img_array = np.array(img).astype('float32') / 255.
95
  predictions = model.predict(np.expand_dims(img_array, axis=0))
 
100
  return "Unidentified Specimen", confidence
101
  return class_indices.get(str(idx), f"Unknown {idx}"), confidence
102
 
103
+ # 4. Header Section
104
  st.markdown("""
105
  <div style="padding-top: 1rem; margin-bottom: 2rem;">
106
  <div style="color: #10b981; font-size: 0.7rem; font-weight: 700; letter-spacing: 0.1em; margin-bottom: 0.5rem;">AI DIAGNOSTICS</div>
 
109
  """, unsafe_allow_html=True)
110
 
111
  m1, m2 = st.columns(2)
112
+ with m1: st.markdown('<div class="metric-card"><h3>Species Library</h3><h2>38</h2></div>', unsafe_allow_html=True)
113
+ with m2: st.markdown('<div class="metric-card"><h3>CNN Accuracy</h3><h2>96.5%</h2></div>', unsafe_allow_html=True)
114
 
115
+ # 5. 2x2 Clinical Gallery
116
  st.markdown("### Clinical Specimens")
117
  samples = [
118
  ("Apple Rot", "apple_black rot.jpeg"),
 
124
  if 'active_img' not in st.session_state:
125
  st.session_state.active_img = None
126
 
127
+ # Arrangement Logic for 2 rows and 2 columns
128
  # Row 1 (Items 1 & 2)
129
  row1_cols = st.columns(2)
130
  for i in range(2):
 
132
  img_path = os.path.join(BASE_DIR, filename)
133
  with row1_cols[i]:
134
  if os.path.exists(img_path):
135
+ st.image(Image.open(img_path), width=120) # Small fixed width to stop shaking
136
  if st.button(f"Load {name}", key=f"btn_{i}"):
137
  st.session_state.active_img = Image.open(img_path)
138
 
 
143
  img_path = os.path.join(BASE_DIR, filename)
144
  with row2_cols[i-2]:
145
  if os.path.exists(img_path):
146
+ st.image(Image.open(img_path), width=120) # Fixed size prevents jumping
147
  if st.button(f"Load {name}", key=f"btn_{i}"):
148
  st.session_state.active_img = Image.open(img_path)
149
 
150
  st.divider()
151
 
152
+ # 6. Diagnostic Hub
153
  col_up, col_res = st.columns([1.2, 1])
154
  with col_up:
155
  uploaded_file = st.file_uploader("Upload", type=["jpg", "jpeg", "png"], label_visibility="collapsed")
 
157
  st.session_state.active_img = Image.open(uploaded_file)
158
 
159
  if st.session_state.active_img:
160
+ # Stabilized width for active view
161
+ st.image(st.session_state.active_img, width=320)
162
  else:
163
  st.markdown('<div style="border: 1px solid #1e293b; border-radius: 1rem; height: 180px; background: rgba(15, 23, 42, 0.4);"></div>', unsafe_allow_html=True)
164
 
165
  with col_res:
166
  if st.session_state.active_img and model:
167
  if st.button("RUN INTELLIGENT ANALYSIS"):
168
+ with st.spinner("Analyzing feature maps..."):
169
  prediction, confidence = predict_disease(st.session_state.active_img)
170
  st.markdown(f"""
171
  <div style="background: #0f172a; border: 1px solid #1e293b; border-radius: 1rem; padding: 1.5rem; text-align: center;">
 
178
 
179
  # Footer
180
  st.markdown("""
181
+ <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;">
182
  SYSTEMS ENGINEER: RMUTUA | PRECISION AGRO-PATHOLOGY
183
  </div>
184
  """, unsafe_allow_html=True)