Skip to content

Commit ebd985c

Browse files
committed
feat: extend rate limit to 10 seconds with live countdown
Changes: - Increased cooldown from 2s to 10s for better server protection - Added live countdown timer that updates every second - Shows info banner: "Next prediction available in: X seconds" - Button label updates: "⏳ Wait X seconds..." - Auto-refresh using st.rerun() to tick down the timer User experience: - Clear visual feedback on when they can click again - Prevents accidental rapid clicks that could crash the app - More conservative rate limiting for Streamlit Cloud stability
1 parent bbb0cae commit ebd985c

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

src/tabs/disease_detector.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ def render_disease_detector_tab(df, disease_colors=None):
5959
# Show rate limit info if user is being throttled
6060
current_time = time.time()
6161
time_since_last = current_time - st.session_state.last_prediction_time
62-
cooldown_seconds = 2.0
62+
cooldown_seconds = 10.0
6363

6464
if time_since_last < cooldown_seconds and st.session_state.prediction_count > 0:
6565
remaining = cooldown_seconds - time_since_last
66-
st.info(f"⏳ Rate limit: Please wait {remaining:.1f}s before next prediction (prevents server overload)")
66+
st.info(f"⏳ Rate limit: Please wait {remaining:.0f} seconds before next prediction (prevents server overload)")
6767

6868

6969
# Load DenseNet121 model
@@ -139,13 +139,25 @@ def render_disease_detector_tab(df, disease_colors=None):
139139
# Add rate limiting to prevent rapid clicking
140140
current_time = time.time()
141141
time_since_last = current_time - st.session_state.last_prediction_time
142-
cooldown_seconds = 2.0 # Minimum 2 seconds between predictions
142+
cooldown_seconds = 10.0 # Minimum 10 seconds between predictions
143143

144144
button_disabled = time_since_last < cooldown_seconds
145-
button_label = "🎲 Pick Random X-Ray"
145+
146+
# Show countdown or ready state
146147
if button_disabled:
147-
remaining = cooldown_seconds - time_since_last
148-
button_label = f"⏳ Wait {remaining:.1f}s..."
148+
remaining = int(cooldown_seconds - time_since_last) + 1 # Round up for countdown
149+
button_label = f"⏳ Wait {remaining} second{'s' if remaining != 1 else ''}..."
150+
151+
# Add a placeholder that auto-refreshes to show countdown
152+
countdown_placeholder = st.empty()
153+
countdown_placeholder.info(f"🕐 Next prediction available in: **{remaining} seconds**")
154+
155+
# Auto-refresh every second to update countdown
156+
if remaining > 0:
157+
time.sleep(0.1) # Small delay to trigger rerun
158+
st.rerun()
159+
else:
160+
button_label = "🎲 Pick Random X-Ray"
149161

150162
if st.button(button_label, type="primary", width='stretch', disabled=button_disabled):
151163
st.session_state.last_prediction_time = current_time
@@ -269,9 +281,11 @@ def render_disease_detector_tab(df, disease_colors=None):
269281
# Rate limiting check before running prediction
270282
current_time = time.time()
271283
time_since_last = current_time - st.session_state.last_prediction_time
284+
cooldown_seconds = 10.0
272285

273-
if time_since_last < 2.0 and st.session_state.prediction_count > 0:
274-
st.warning(f"⏳ Please wait {2.0 - time_since_last:.1f} seconds before next prediction to prevent server overload.")
286+
if time_since_last < cooldown_seconds and st.session_state.prediction_count > 0:
287+
remaining = int(cooldown_seconds - time_since_last) + 1
288+
st.warning(f"⏳ Please wait {remaining} seconds before next prediction to prevent server overload.")
275289
st.stop()
276290

277291
# Update prediction tracking

0 commit comments

Comments
 (0)