Skip to content

Commit d1fbc15

Browse files
authored
Use streamlit-session-memo for session-specific caching (#1243)
1 parent de38d41 commit d1fbc15

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

Diff for: pages/1_object_detection.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import cv2
1313
import numpy as np
1414
import streamlit as st
15+
from streamlit_session_memo import st_session_memo
1516
from streamlit_webrtc import WebRtcMode, webrtc_streamer
1617

1718
from sample_utils.download import download_file
@@ -70,13 +71,12 @@ def generate_label_colors():
7071
download_file(PROTOTXT_URL, PROTOTXT_LOCAL_PATH, expected_size=29353)
7172

7273

73-
# Session-specific caching
74-
cache_key = "object_detection_dnn"
75-
if cache_key in st.session_state:
76-
net = st.session_state[cache_key]
77-
else:
78-
net = cv2.dnn.readNetFromCaffe(str(PROTOTXT_LOCAL_PATH), str(MODEL_LOCAL_PATH))
79-
st.session_state[cache_key] = net
74+
@st_session_memo
75+
def get_model():
76+
return cv2.dnn.readNetFromCaffe(str(PROTOTXT_LOCAL_PATH), str(MODEL_LOCAL_PATH))
77+
78+
79+
net = get_model()
8080

8181
score_threshold = st.slider("Score threshold", 0.0, 1.0, 0.5, 0.05)
8282

Diff for: pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dev = [
2929
"streamlit-server-state>=0.17.1",
3030
"twilio>=8.1",
3131
"opencv-python-headless>=4.5.4.58",
32+
"streamlit-session-memo>=0.3.2",
3233
# For testing older versions of Streamlit: https://discuss.streamlit.io/t/modulenotfounderror-no-module-named-altair-vegalite-v4/42921
3334
"altair<5",
3435
# For testing older versions of Streamlit: https://discuss.streamlit.io/t/streamlit-run-with-protobuf-error/25632/3

Diff for: uv.lock

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)