whitphx HF staff commited on
Commit
bc98518
1 Parent(s): ae6b0ae

Fix to use session_state as a session_specific cache

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -20,7 +20,6 @@ from streamlit_webrtc import (
20
  WebRtcStreamerContext,
21
  webrtc_streamer,
22
  )
23
- from streamlit_webrtc.session_info import get_session_id
24
 
25
  HERE = Path(__file__).parent
26
 
@@ -294,13 +293,13 @@ def app_object_detection():
294
  name: str
295
  prob: float
296
 
297
- @st.cache
298
- def get_model(
299
- session_id,
300
- ): # HACK: Pass session_id as an arg to make the cache session-specific
301
- return cv2.dnn.readNetFromCaffe(str(PROTOTXT_LOCAL_PATH), str(MODEL_LOCAL_PATH))
302
-
303
- net = get_model(get_session_id())
304
 
305
  confidence_threshold = st.slider(
306
  "Confidence threshold", 0.0, 1.0, DEFAULT_CONFIDENCE_THRESHOLD, 0.05
 
20
  WebRtcStreamerContext,
21
  webrtc_streamer,
22
  )
 
23
 
24
  HERE = Path(__file__).parent
25
 
 
293
  name: str
294
  prob: float
295
 
296
+ # Session-specific caching
297
+ cache_key = "object_detection_dnn"
298
+ if cache_key in st.session_state:
299
+ net = st.session_state[cache_key]
300
+ else:
301
+ net = cv2.dnn.readNetFromCaffe(str(PROTOTXT_LOCAL_PATH), str(MODEL_LOCAL_PATH))
302
+ st.session_state[cache_key] = net
303
 
304
  confidence_threshold = st.slider(
305
  "Confidence threshold", 0.0, 1.0, DEFAULT_CONFIDENCE_THRESHOLD, 0.05