Spaces:
Build error
Build error
Kevin King commited on
Commit ·
e83cd54
1
Parent(s): 764dc1d
REFAC: Update requirements and enhance Streamlit app for multimodal emotion analysis
Browse filesFiles changed (2)
- requirements.txt+3-2
- src/streamlit_app.py+85-67
16 unmodified lines1718192021222324252616 unmodified linestorch==2.7.0torchaudio==2.7.0# Pin data/audio libraries for stabilitypandas==2.2.2numpy==1.26.4soundfile==0.12.1librosa==0.10.1scipy==1.13.0Pillow==10.3.016 unmodified lines171819202122232425262716 unmodified linestorch==2.7.0torchaudio==2.7.0# Pin data/audio libraries for stability and new featurespandas==2.2.2numpy==1.26.4soundfile==0.12.1librosa==0.10.1scipy==1.13.0Pillow==10.3.0scikit-learn==1.4.2
10 unmodified lines1112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657589 unmodified lines6869707172737 unmodified lines8182838485868788897 unmodified lines9798991001011021031041051061071081091101111121 unmodified line11411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717810 unmodified linesimport cv2from moviepy.editor import VideoFileClipimport timeimport shutil# --- Create a cross-platform, writable cache directory for all libraries ---CACHE_DIR = os.path.join(tempfile.gettempdir(), "affectlink_cache")os.makedirs(CACHE_DIR, exist_ok=True)os.environ['DEEPFACE_HOME'] = CACHE_DIRos.environ['HF_HOME'] = CACHE_DIR# Define paths for the pre-included model weightsMODEL_NAME = "facial_expression_model_weights.h5"SOURCE_PATH = os.path.join("src", "weights", MODEL_NAME)DEST_DIR = os.path.join(CACHE_DIR, ".deepface", "weights")DEST_PATH = os.path.join(DEST_DIR, MODEL_NAME)# Create the destination directory if it doesn't exist and copy the modelif not os.path.exists(DEST_PATH):print(f"Model not found in cache. Copying from {SOURCE_PATH} to {DEST_PATH}...")os.makedirs(DEST_DIR, exist_ok=True)try:shutil.copy(SOURCE_PATH, DEST_PATH)print("Model copied successfully.")except FileNotFoundError:print(f"Warning: Local model file not found at {SOURCE_PATH}. App will attempt to download it.")except Exception as e:print(f"Error copying model file: {e}")# --- Page Configuration ---st.set_page_config(page_title="AffectLink Demo", page_icon="😊", layout="wide")st.title("AffectLink: Post-Hoc Emotion Analysis")st.write("Upload a short video clip (under 30 seconds) to analyze facial expressions, speech-to-text, and the emotional tone of the audio.")# --- Logger Configuration ---logging.basicConfig(level=logging.INFO)logging.getLogger('deepface').setLevel(logging.ERROR)logging.getLogger('huggingface_hub').setLevel(logging.WARNING)logging.getLogger('moviepy').setLevel(logging.ERROR)# --- Emotion Mappings ---UNIFIED_EMOTIONS = ['neutral', 'happy', 'sad', 'angry']TEXT_TO_UNIFIED = {'neutral': 'neutral', 'joy': 'happy', 'sadness': 'sad', 'anger': 'angry'}SER_TO_UNIFIED = {'neu': 'neutral', 'hap': 'happy', 'sad': 'sad', 'ang': 'angry'}AUDIO_SAMPLE_RATE = 16000# --- Model Loading ---9 unmodified lineswhisper_model, text_classifier, ser_model, ser_feature_extractor = load_models()# --- UI and Processing Logic ---uploaded_file = st.file_uploader("Choose a video file...", type=["mp4", "mov", "avi", "mkv"])7 unmodified linesst.video(temp_video_path)if st.button("Analyze Video"):facial_analysis_results = []audio_analysis_results = {}with st.spinner("Analyzing video for facial expressions... (1 frame per second)"):cap = Nonetry:7 unmodified linestimestamp = frame_count / fpsanalysis = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False, silent=True)if isinstance(analysis, list) and len(analysis) > 0:facial_analysis_results.append((timestamp, analysis[0]['dominant_emotion'].capitalize()))frame_count += 1except Exception as e:st.error(f"An error occurred during facial analysis: {e}")finally:if cap: cap.release()with st.spinner("Extracting and analyzing audio..."):video_clip = Nonetemp_audio_path = Nonetry:video_clip = VideoFileClip(temp_video_path)if video_clip.audio:1 unmodified linevideo_clip.audio.write_audiofile(taudio.name, fps=AUDIO_SAMPLE_RATE, logger=None)temp_audio_path = taudio.nameresult = whisper_model.transcribe(temp_audio_path, fp16=False)transcribed_text = result['text'] if result['text'].strip() else "No speech detected."audio_analysis_results['Transcription'] = transcribed_textif "No speech detected" not in transcribed_text:text_emotions = text_classifier(transcribed_text)[0]unified_text_scores = {e: 0.0 for e in UNIFIED_EMOTIONS}for emo in text_emotions:unified_emo = TEXT_TO_UNIFIED.get(emo['label'])if unified_emo: unified_text_scores[unified_emo] += emo['score']audio_analysis_results['Text Emotion'] = max(unified_text_scores, key=unified_text_scores.get).capitalize()audio_array, _ = sf.read(temp_audio_path, dtype='float32')if audio_array.ndim == 2:audio_array = audio_array.mean(axis=1)min_length = 1024if len(audio_array) < min_length:padding = np.zeros(min_length - len(audio_array), dtype=np.float32)audio_array = np.concatenate([audio_array, padding])inputs = ser_feature_extractor(audio_array, sampling_rate=AUDIO_SAMPLE_RATE, return_tensors="pt", padding=True)with torch.no_grad():logits = ser_model(**inputs).logitsscores = torch.nn.functional.softmax(logits, dim=1).squeeze()unified_ser_scores = {e: 0.0 for e in UNIFIED_EMOTIONS}for i, score in enumerate(scores):raw_emo = ser_model.config.id2label[i]unified_emo = SER_TO_UNIFIED.get(raw_emo)if unified_emo: unified_ser_scores[unified_emo] += score.item()audio_analysis_results['Speech Emotion'] = max(unified_ser_scores, key=unified_ser_scores.get).capitalize()else:audio_analysis_results['Transcription'] = "No audio track found in the video."except Exception as e:st.error(f"An error occurred during audio analysis: {e}")finally:if video_clip: video_clip.close()if temp_audio_path and os.path.exists(temp_audio_path): os.unlink(temp_audio_path)st.header("Analysis Results")col1, col2 = st.columns(2)with col1:st.subheader("Audio Analysis")st.write(f"**Transcription:** \"{audio_analysis_results.get('Transcription', 'N/A')}\"")st.metric("Emotion from Text", audio_analysis_results.get('Text Emotion', 'N/A'))st.metric("Emotion from Speech", audio_analysis_results.get('Speech Emotion', 'N/A'))with col2:st.subheader("Facial Expression Timeline")if facial_analysis_results:for timestamp, emotion in facial_analysis_results:st.write(f"**Time {int(timestamp // 60):02d}:{int(timestamp % 60):02d}:** {emotion}")else:st.write("No faces detected or video processing failed.")finally:if temp_video_path and os.path.exists(temp_video_path):time.sleep(1)try:os.unlink(temp_video_path)except Exception:10 unmodified lines1112131415161718192021222324252627282930313233343536373839404142439 unmodified lines53545556575859606162636465666768697071727374757677787980817 unmodified lines89909192939495969798997 unmodified lines1071081091101111121131141151161171181191201211 unmodified line12312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519610 unmodified linesimport cv2from moviepy.editor import VideoFileClipimport timeimport pandas as pdfrom sklearn.metrics.pairwise import cosine_similarity# --- Create a cross-platform, writable cache directory ---CACHE_DIR = os.path.join(tempfile.gettempdir(), "affectlink_cache")os.makedirs(CACHE_DIR, exist_ok=True)os.environ['DEEPFACE_HOME'] = CACHE_DIRos.environ['HF_HOME'] = CACHE_DIR# --- Page Configuration ---st.set_page_config(page_title="AffectLink Demo", page_icon="😊", layout="wide")st.title("AffectLink: Post-Hoc Emotion Analysis")st.write("Upload a short video clip (under 30 seconds) to see a multimodal emotion analysis.")# --- Logger Configuration ---# [Logger setup remains the same]logging.basicConfig(level=logging.INFO)logging.getLogger('deepface').setLevel(logging.ERROR)logging.getLogger('huggingface_hub').setLevel(logging.WARNING)logging.getLogger('moviepy').setLevel(logging.ERROR)# --- Emotion Mappings ---UNIFIED_EMOTIONS = ['angry', 'happy', 'sad', 'neutral'] # Defined order for vectorsTEXT_TO_UNIFIED = {'neutral': 'neutral', 'joy': 'happy', 'sadness': 'sad', 'anger': 'angry'}SER_TO_UNIFIED = {'neu': 'neutral', 'hap': 'happy', 'sad': 'sad', 'ang': 'angry'}FACIAL_TO_UNIFIED = {'neutral': 'neutral', 'happy': 'happy', 'sad': 'sad', 'angry': 'angry'}AUDIO_SAMPLE_RATE = 16000# --- Model Loading ---9 unmodified lineswhisper_model, text_classifier, ser_model, ser_feature_extractor = load_models()# --- Helper Functions for Analysis ---def create_unified_vector(scores_dict, mapping_dict):"""Creates a normalized vector from a dictionary of scores based on a mapping."""vector = np.zeros(len(UNIFIED_EMOTIONS))for label, score in scores_dict.items():unified_label = mapping_dict.get(label)if unified_label and unified_label in UNIFIED_EMOTIONS:idx = UNIFIED_EMOTIONS.index(unified_label)vector[idx] += score# Normalize the vectornorm = np.linalg.norm(vector)if norm > 0:vector /= normreturn vectordef get_consistency_level(cosine_sim):"""Convert cosine similarity to a qualitative label."""if cosine_sim >= 0.8: return "High"if cosine_sim >= 0.6: return "Medium"if cosine_sim >= 0.3: return "Low"return "Very Low"# --- UI and Processing Logic ---uploaded_file = st.file_uploader("Choose a video file...", type=["mp4", "mov", "avi", "mkv"])7 unmodified linesst.video(temp_video_path)if st.button("Analyze Video"):# Dictionaries to hold all resultsfer_timeline = {}audio_analysis_results = {}# --- Video Processing ---with st.spinner("Analyzing video for facial expressions... (1 frame per second)"):cap = Nonetry:7 unmodified linestimestamp = frame_count / fpsanalysis = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False, silent=True)if isinstance(analysis, list) and len(analysis) > 0:# Store the full emotion dictionary for the plotfer_timeline[timestamp] = analysis[0]['emotion']frame_count += 1finally:if cap: cap.release()# --- Audio Processing ---with st.spinner("Extracting and analyzing audio..."):video_clip = Nonetry:video_clip = VideoFileClip(temp_video_path)if video_clip.audio:1 unmodified linevideo_clip.audio.write_audiofile(taudio.name, fps=AUDIO_SAMPLE_RATE, logger=None)temp_audio_path = taudio.name# Run all audio modelsresult = whisper_model.transcribe(temp_audio_path, fp16=False)transcribed_text = result['text'].strip()audio_analysis_results['Transcription'] = transcribed_text if transcribed_text else "No speech detected."if transcribed_text:text_emotions = text_classifier(transcribed_text)[0]audio_analysis_results['Text Emotion Scores'] = {emo['label']: emo['score'] for emo in text_emotions}audio_array, _ = sf.read(temp_audio_path, dtype='float32')if audio_array.ndim == 2: audio_array = audio_array.mean(axis=1)if len(audio_array) < 1024: audio_array = np.pad(audio_array, (0, 1024 - len(audio_array)))inputs = ser_feature_extractor(audio_array, sampling_rate=AUDIO_SAMPLE_RATE, return_tensors="pt", padding=True)with torch.no_grad():logits = ser_model(**inputs).logitsscores = torch.nn.functional.softmax(logits, dim=1).squeeze()ser_scores = {ser_model.config.id2label[i]: score.item() for i, score in enumerate(scores)}audio_analysis_results['Speech Emotion Scores'] = ser_scoreselse:audio_analysis_results['Transcription'] = "No audio track found."finally:if video_clip: video_clip.close()if 'temp_audio_path' in locals() and os.path.exists(temp_audio_path): os.unlink(temp_audio_path)# --- Post-Analysis and Visualization ---st.header("Analysis Results")# Prepare data for displayfer_avg_scores = pd.DataFrame(fer_timeline).T.mean().to_dict() if fer_timeline else {}ser_scores = audio_analysis_results.get('Speech Emotion Scores', {})text_scores = audio_analysis_results.get('Text Emotion Scores', {})# Create vectors for cosine similarityfer_vector = create_unified_vector(fer_avg_scores, FACIAL_TO_UNIFIED)ser_vector = create_unified_vector(ser_scores, SER_TO_UNIFIED)text_vector = create_unified_vector(text_scores, TEXT_TO_UNIFIED)# Calculate similaritiessim_face_text = cosine_similarity([fer_vector], [text_vector])[0][0]sim_face_speech = cosine_similarity([fer_vector], [ser_vector])[0][0]sim_speech_text = cosine_similarity([ser_vector], [text_vector])[0][0]avg_similarity = np.mean([sim_face_text, sim_face_speech, sim_speech_text])# Display metricscol1, col2 = st.columns([1, 2])with col1:st.subheader("Multimodal Summary")st.write(f"**Transcription:** \"{audio_analysis_results.get('Transcription', 'N/A')}\"")st.metric("Dominant Facial Emotion", max(fer_avg_scores, key=fer_avg_scores.get).capitalize() if fer_avg_scores else "N/A")st.metric("Dominant Text Emotion", max(text_scores, key=lambda k: TEXT_TO_UNIFIED.get(k) is not None and text_scores.get(k) or -1).capitalize() if text_scores else "N/A")st.metric("Dominant Speech Emotion", max(ser_scores, key=lambda k: SER_TO_UNIFIED.get(k) is not None and ser_scores.get(k) or -1).capitalize() if ser_scores else "N/A")st.metric("Emotion Consistency", get_consistency_level(avg_similarity), f"{avg_similarity:.2f} Avg. Cosine Similarity")with col2:st.subheader("Facial Emotion Over Time")if fer_timeline:# Convert timeline to a DataFrame suitable for st.line_chartdf = pd.DataFrame(fer_timeline).T# Filter for only the unified emotions we care aboutdf_filtered = df[list(FACIAL_TO_UNIFIED.keys())].rename(columns=FACIAL_TO_UNIFIED)st.line_chart(df_filtered)else:st.write("No faces detected to plot.")finally:if temp_video_path and os.path.exists(temp_video_path):time.sleep(1)try:os.unlink(temp_video_path)except Exception:
requirements.txt+3-2
src/streamlit_app.py+85-67