File size: 9,533 Bytes
bf961d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
"""
User Profile module for LifeUnity AI Cognitive Twin System.
Manages user data, preferences, and behavior tracking.
"""
import json
import os
from datetime import datetime
from typing import Dict, List, Optional
from pathlib import Path
from app.utils.logger import get_logger
logger = get_logger("UserProfile")
class UserProfile:
"""User profile manager for the Cognitive Twin system."""
def __init__(self, user_id: str = "default_user", data_dir: str = "data"):
"""
Initialize user profile.
Args:
user_id: Unique user identifier
data_dir: Directory to store user data
"""
self.user_id = user_id
self.data_dir = Path(data_dir)
self.data_dir.mkdir(exist_ok=True)
self.profile_file = self.data_dir / f"{user_id}_profile.json"
self.profile = self._load_profile()
logger.info(f"UserProfile initialized for user: {user_id}")
def _load_profile(self) -> Dict:
"""Load user profile from file."""
try:
if self.profile_file.exists():
with open(self.profile_file, 'r') as f:
profile = json.load(f)
logger.info(f"Loaded profile for user: {self.user_id}")
return profile
else:
# Create default profile
default_profile = self._create_default_profile()
self._save_profile(default_profile)
return default_profile
except Exception as e:
logger.error(f"Error loading profile: {str(e)}", exc_info=True)
return self._create_default_profile()
def _create_default_profile(self) -> Dict:
"""Create a default user profile."""
return {
'user_id': self.user_id,
'created_at': datetime.now().isoformat(),
'last_updated': datetime.now().isoformat(),
'baseline_data': {
'average_mood': 'neutral',
'stress_baseline': 50.0,
'productivity_baseline': 50.0,
'sleep_hours': 7.0
},
'emotion_history': [],
'behavior_patterns': {
'peak_productivity_hours': [],
'common_stress_triggers': [],
'mood_trends': {}
},
'notes': [],
'preferences': {
'notification_enabled': True,
'data_retention_days': 90
}
}
def _save_profile(self, profile: Optional[Dict] = None):
"""Save user profile to file."""
try:
if profile is None:
profile = self.profile
profile['last_updated'] = datetime.now().isoformat()
with open(self.profile_file, 'w') as f:
json.dump(profile, f, indent=2)
logger.debug(f"Profile saved for user: {self.user_id}")
except Exception as e:
logger.error(f"Error saving profile: {str(e)}", exc_info=True)
def update_baseline(self, baseline_data: Dict):
"""
Update user baseline data.
Args:
baseline_data: Dictionary with baseline metrics
"""
self.profile['baseline_data'].update(baseline_data)
self._save_profile()
logger.info(f"Updated baseline data for user: {self.user_id}")
def add_emotion_record(
self,
emotion: str,
confidence: float,
timestamp: Optional[str] = None
):
"""
Add emotion record to history.
Args:
emotion: Detected emotion
confidence: Confidence score
timestamp: Optional timestamp (ISO format)
"""
if timestamp is None:
timestamp = datetime.now().isoformat()
record = {
'emotion': emotion,
'confidence': confidence,
'timestamp': timestamp
}
self.profile['emotion_history'].append(record)
# Keep only recent records (last 1000)
if len(self.profile['emotion_history']) > 1000:
self.profile['emotion_history'] = self.profile['emotion_history'][-1000:]
self._save_profile()
logger.debug(f"Added emotion record: {emotion} ({confidence:.2f})")
def get_emotion_history(self, limit: Optional[int] = None) -> List[Dict]:
"""
Get emotion history.
Args:
limit: Maximum number of records to return
Returns:
List of emotion records
"""
history = self.profile.get('emotion_history', [])
if limit:
return history[-limit:]
return history
def add_note(self, content: str, tags: Optional[List[str]] = None):
"""
Add a note to user profile.
Args:
content: Note content
tags: Optional tags for the note
"""
note = {
'id': len(self.profile['notes']) + 1,
'content': content,
'timestamp': datetime.now().isoformat(),
'tags': tags or []
}
self.profile['notes'].append(note)
self._save_profile()
logger.info(f"Added note for user: {self.user_id}")
def get_notes(self, limit: Optional[int] = None) -> List[Dict]:
"""
Get user notes.
Args:
limit: Maximum number of notes to return
Returns:
List of notes
"""
notes = self.profile.get('notes', [])
if limit:
return notes[-limit:]
return notes
def update_behavior_pattern(self, pattern_type: str, data: any):
"""
Update behavior pattern.
Args:
pattern_type: Type of pattern (e.g., 'peak_productivity_hours')
data: Pattern data
"""
self.profile['behavior_patterns'][pattern_type] = data
self._save_profile()
logger.info(f"Updated behavior pattern: {pattern_type}")
def get_behavior_patterns(self) -> Dict:
"""Get all behavior patterns."""
return self.profile.get('behavior_patterns', {})
def calculate_stress_level(self) -> float:
"""
Calculate current stress level based on recent emotions.
Returns:
Stress level (0-100)
"""
recent_emotions = self.get_emotion_history(limit=10)
if not recent_emotions:
return 50.0 # Default neutral stress level
# Stress weights for different emotions
stress_weights = {
'angry': 90,
'fear': 85,
'disgust': 70,
'sad': 75,
'surprise': 40,
'happy': 20,
'neutral': 50
}
total_stress = 0.0
for record in recent_emotions:
emotion = record.get('emotion', 'neutral')
confidence = record.get('confidence', 0.5)
weight = stress_weights.get(emotion, 50)
total_stress += weight * confidence
avg_stress = total_stress / len(recent_emotions)
return round(avg_stress, 2)
def calculate_productivity_score(self) -> float:
"""
Calculate productivity score based on mood and patterns.
Returns:
Productivity score (0-100)
"""
recent_emotions = self.get_emotion_history(limit=10)
if not recent_emotions:
return 50.0 # Default neutral productivity
# Productivity weights for different emotions
productivity_weights = {
'happy': 90,
'neutral': 70,
'surprise': 60,
'sad': 40,
'angry': 30,
'fear': 35,
'disgust': 45
}
total_productivity = 0.0
for record in recent_emotions:
emotion = record.get('emotion', 'neutral')
confidence = record.get('confidence', 0.5)
weight = productivity_weights.get(emotion, 50)
total_productivity += weight * confidence
avg_productivity = total_productivity / len(recent_emotions)
return round(avg_productivity, 2)
def get_summary(self) -> Dict:
"""
Get user profile summary.
Returns:
Summary dictionary
"""
return {
'user_id': self.user_id,
'created_at': self.profile['created_at'],
'last_updated': self.profile['last_updated'],
'total_emotions_tracked': len(self.profile['emotion_history']),
'total_notes': len(self.profile['notes']),
'current_stress_level': self.calculate_stress_level(),
'current_productivity': self.calculate_productivity_score(),
'baseline_data': self.profile['baseline_data']
}
# Global profile instance
_profile = None
def get_user_profile(user_id: str = "default_user") -> UserProfile:
"""
Get or create a user profile instance.
Args:
user_id: User identifier
Returns:
UserProfile instance
"""
global _profile
if _profile is None or _profile.user_id != user_id:
_profile = UserProfile(user_id)
return _profile
|