Kevin King commited on
Commit
1878486
·
1 Parent(s): cf09d5c

REFAC: Finalize cache directory setup and improve resource management in Streamlit app

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +12 -6
src/streamlit_app.py CHANGED
@@ -15,11 +15,15 @@ import pandas as pd
15
  from sklearn.metrics.pairwise import cosine_similarity
16
  import matplotlib.pyplot as plt
17
 
18
- # --- Create a cross-platform, writable cache directory ---
 
19
  CACHE_DIR = os.path.join(tempfile.gettempdir(), "affectlink_cache")
20
- os.makedirs(CACHE_DIR, exist_ok=True)
 
 
21
  os.environ['DEEPFACE_HOME'] = CACHE_DIR
22
  os.environ['HF_HOME'] = CACHE_DIR
 
23
 
24
  # --- Page Configuration ---
25
  st.set_page_config(page_title="AffectLink Demo", page_icon="😊", layout="wide")
@@ -28,6 +32,10 @@ st.write("Upload a short video clip (under 30 seconds) to see a multimodal emoti
28
 
29
  # --- Logger Configuration ---
30
  # [Logger setup remains the same]
 
 
 
 
31
 
32
  # --- Emotion Mappings ---
33
  UNIFIED_EMOTIONS = ['angry', 'happy', 'sad', 'neutral']
@@ -71,9 +79,7 @@ uploaded_file = st.file_uploader("Choose a video file...", type=["mp4", "mov", "
71
 
72
  if uploaded_file is not None:
73
  temp_video_path = None
74
- # --- THIS IS THE FIX ---
75
  video_clip_for_duration = None
76
- # ========================
77
  try:
78
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tfile:
79
  tfile.write(uploaded_file.read())
@@ -227,8 +233,8 @@ if uploaded_file is not None:
227
  st.write("No emotion data available to plot.")
228
 
229
  finally:
230
- if video_clip_for_duration: video_clip_for_duration.close()
231
- if 'temp_audio_path' in locals() and os.path.exists(temp_audio_path): os.unlink(temp_audio_path)
232
  if temp_video_path and os.path.exists(temp_video_path):
233
  time.sleep(1)
234
  try:
 
15
  from sklearn.metrics.pairwise import cosine_similarity
16
  import matplotlib.pyplot as plt
17
 
18
+ # --- THIS IS THE FINAL FIX ---
19
+ # Create a cross-platform, writable cache directory for all libraries
20
  CACHE_DIR = os.path.join(tempfile.gettempdir(), "affectlink_cache")
21
+ DEEPFACE_CACHE_PATH = os.path.join(CACHE_DIR, ".deepface", "weights")
22
+ os.makedirs(DEEPFACE_CACHE_PATH, exist_ok=True) # Proactively create the full path
23
+
24
  os.environ['DEEPFACE_HOME'] = CACHE_DIR
25
  os.environ['HF_HOME'] = CACHE_DIR
26
+ # ===========================
27
 
28
  # --- Page Configuration ---
29
  st.set_page_config(page_title="AffectLink Demo", page_icon="😊", layout="wide")
 
32
 
33
  # --- Logger Configuration ---
34
  # [Logger setup remains the same]
35
+ logging.basicConfig(level=logging.INFO)
36
+ logging.getLogger('deepface').setLevel(logging.ERROR)
37
+ logging.getLogger('huggingface_hub').setLevel(logging.WARNING)
38
+ logging.getLogger('moviepy').setLevel(logging.ERROR)
39
 
40
  # --- Emotion Mappings ---
41
  UNIFIED_EMOTIONS = ['angry', 'happy', 'sad', 'neutral']
 
79
 
80
  if uploaded_file is not None:
81
  temp_video_path = None
 
82
  video_clip_for_duration = None
 
83
  try:
84
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tfile:
85
  tfile.write(uploaded_file.read())
 
233
  st.write("No emotion data available to plot.")
234
 
235
  finally:
236
+ if 'video_clip_for_duration' in locals() and video_clip_for_duration: video_clip_for_duration.close()
237
+ if 'temp_audio_path' in locals() and temp_audio_path and os.path.exists(temp_audio_path): os.unlink(temp_audio_path)
238
  if temp_video_path and os.path.exists(temp_video_path):
239
  time.sleep(1)
240
  try: