Spaces:
Sleeping
Sleeping
Kevin King
commited on
Commit
Β·
650fd5d
1
Parent(s):
0a27525
FEAT: Preload deepface model from repository
Browse files
src/streamlit_app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
# Point the cache directory to the guaranteed writable /tmp folder
|
| 4 |
-
os.environ['DEEPFACE_HOME'] = '/tmp/.deepface'
|
| 5 |
import numpy as np
|
| 6 |
import torch
|
| 7 |
import whisper
|
|
@@ -13,19 +11,43 @@ import tempfile
|
|
| 13 |
import cv2
|
| 14 |
from moviepy.editor import VideoFileClip
|
| 15 |
import time
|
|
|
|
| 16 |
|
| 17 |
-
# Create a cross-platform, writable cache directory for all libraries
|
| 18 |
CACHE_DIR = os.path.join(tempfile.gettempdir(), "affectlink_cache")
|
| 19 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 20 |
os.environ['DEEPFACE_HOME'] = CACHE_DIR
|
| 21 |
os.environ['HF_HOME'] = CACHE_DIR
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# --- Page Configuration ---
|
| 24 |
st.set_page_config(page_title="AffectLink Demo", page_icon="π", layout="wide")
|
| 25 |
st.title("AffectLink: Post-Hoc Emotion Analysis")
|
| 26 |
st.write("Upload a short video clip (under 30 seconds) to analyze facial expressions, speech-to-text, and the emotional tone of the audio.")
|
| 27 |
|
| 28 |
# --- Logger Configuration ---
|
|
|
|
|
|
|
|
|
|
| 29 |
logging.basicConfig(level=logging.INFO)
|
| 30 |
logging.getLogger('deepface').setLevel(logging.ERROR)
|
| 31 |
logging.getLogger('huggingface_hub').setLevel(logging.WARNING)
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
import whisper
|
|
|
|
| 11 |
import cv2
|
| 12 |
from moviepy.editor import VideoFileClip
|
| 13 |
import time
|
| 14 |
+
import shutil # Import the shutil library for file copying
|
| 15 |
|
| 16 |
+
# --- Create a cross-platform, writable cache directory for all libraries ---
|
| 17 |
CACHE_DIR = os.path.join(tempfile.gettempdir(), "affectlink_cache")
|
| 18 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 19 |
os.environ['DEEPFACE_HOME'] = CACHE_DIR
|
| 20 |
os.environ['HF_HOME'] = CACHE_DIR
|
| 21 |
|
| 22 |
+
# === THIS IS THE NEW CODE TO PRELOAD THE DEEPFACE MODEL ===
|
| 23 |
+
# Define paths for the pre-included model weights
|
| 24 |
+
MODEL_NAME = "facial_expression_model_weights.h5"
|
| 25 |
+
SOURCE_PATH = os.path.join("src", "weights", MODEL_NAME)
|
| 26 |
+
DEST_DIR = os.path.join(CACHE_DIR, ".deepface", "weights")
|
| 27 |
+
DEST_PATH = os.path.join(DEST_DIR, MODEL_NAME)
|
| 28 |
+
|
| 29 |
+
# Create the destination directory if it doesn't exist and copy the model
|
| 30 |
+
if not os.path.exists(DEST_PATH):
|
| 31 |
+
print(f"Model not found in cache. Copying from {SOURCE_PATH} to {DEST_PATH}...")
|
| 32 |
+
os.makedirs(DEST_DIR, exist_ok=True)
|
| 33 |
+
try:
|
| 34 |
+
shutil.copy(SOURCE_PATH, DEST_PATH)
|
| 35 |
+
print("Model copied successfully.")
|
| 36 |
+
except FileNotFoundError:
|
| 37 |
+
print(f"Warning: Local model file not found at {SOURCE_PATH}. App will attempt to download it.")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Error copying model file: {e}")
|
| 40 |
+
# =========================================================
|
| 41 |
+
|
| 42 |
# --- Page Configuration ---
|
| 43 |
st.set_page_config(page_title="AffectLink Demo", page_icon="π", layout="wide")
|
| 44 |
st.title("AffectLink: Post-Hoc Emotion Analysis")
|
| 45 |
st.write("Upload a short video clip (under 30 seconds) to analyze facial expressions, speech-to-text, and the emotional tone of the audio.")
|
| 46 |
|
| 47 |
# --- Logger Configuration ---
|
| 48 |
+
# [The rest of your code remains the same]
|
| 49 |
+
# [I have included the full script below for clarity]
|
| 50 |
+
|
| 51 |
logging.basicConfig(level=logging.INFO)
|
| 52 |
logging.getLogger('deepface').setLevel(logging.ERROR)
|
| 53 |
logging.getLogger('huggingface_hub').setLevel(logging.WARNING)
|
src/weights/facial_expression_model_weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e8e8851d3fa05c001b1c27fd8841dfe08d7f82bb786a53ad8776725b7a1e824c
|
| 3 |
+
size 5977392
|