Spaces:
Sleeping
Sleeping
| """MrrrMe Backend - Emotion Fusion Logic""" | |
| import numpy as np | |
| from ..config import EMOTION_MAP, FUSION_WEIGHTS | |
| def adjust_fusion_weights(face_quality: float, voice_active: bool, text_length: int) -> dict: | |
| """ | |
| Adjust fusion weights based on quality metrics | |
| Returns: | |
| Dict with adjusted weights and adjustment log | |
| """ | |
| adjusted_weights = FUSION_WEIGHTS.copy() | |
| adjustments = [] | |
| # Reduce face weight if quality is poor | |
| if face_quality < 0.5: | |
| adjusted_weights['face'] *= 0.7 | |
| adjustments.append(f"Face weight reduced (low quality: {face_quality:.3f})") | |
| # Reduce voice weight if not active | |
| if not voice_active: | |
| adjusted_weights['voice'] *= 0.5 | |
| adjustments.append("Voice weight reduced (no recent speech)") | |
| # Reduce text weight if very short | |
| if text_length < 10: | |
| adjusted_weights['text'] *= 0.7 | |
| adjustments.append(f"Text weight reduced (short input: {text_length} chars)") | |
| # Normalize to sum to 1.0 | |
| total = sum(adjusted_weights.values()) | |
| final_weights = {k: v/total for k, v in adjusted_weights.items()} | |
| return { | |
| 'weights': final_weights, | |
| 'adjustments': adjustments | |
| } | |
| def calculate_fusion(face_probs, voice_probs, text_probs, weights: dict): | |
| """Calculate weighted fusion of emotion probabilities""" | |
| fused_probs = ( | |
| weights['face'] * face_probs + | |
| weights['voice'] * voice_probs + | |
| weights['text'] * text_probs | |
| ) | |
| fused_probs = fused_probs / (np.sum(fused_probs) + 1e-8) | |
| fused_idx = int(np.argmax(fused_probs)) | |
| emotions = ['Neutral', 'Happy', 'Sad', 'Angry'] | |
| fused_emotion = emotions[fused_idx] | |
| intensity = float(np.max(fused_probs)) | |
| return fused_emotion, intensity, fused_probs | |